Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include <assert.h> | 28 #include <assert.h> |
| 29 #include <fcntl.h> | 29 #include <fcntl.h> |
| 30 #include <string.h> | 30 #include <string.h> |
| 31 #include <stdio.h> | 31 #include <stdio.h> |
| 32 #include <stdlib.h> | 32 #include <stdlib.h> |
| 33 #include <sys/time.h> | |
| 33 #include "v8.h" | 34 #include "v8.h" |
| 34 | 35 |
| 35 #include "api.h" | 36 #include "api.h" |
| 36 #include "ast.h" | 37 #include "ast.h" |
| 37 #include "char-predicates-inl.h" | 38 #include "char-predicates-inl.h" |
| 38 #include "messages.h" | 39 #include "messages.h" |
| 39 #include "platform.h" | 40 #include "platform.h" |
| 40 #include "runtime.h" | 41 #include "runtime.h" |
| 41 #include "scanner-character-streams.h" | 42 #include "scanner-character-streams.h" |
| 42 #include "scopeinfo.h" | 43 #include "scopeinfo.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 unicode_cache_ = new UnicodeCache(); | 75 unicode_cache_ = new UnicodeCache(); |
| 75 scanner_ = new Scanner(unicode_cache_); | 76 scanner_ = new Scanner(unicode_cache_); |
| 76 stream_ = new Utf8ToUtf16CharacterStream(source_, length); | 77 stream_ = new Utf8ToUtf16CharacterStream(source_, length); |
| 77 scanner_->Initialize(stream_); | 78 scanner_->Initialize(stream_); |
| 78 } | 79 } |
| 79 | 80 |
| 80 ~BaselineScanner() { | 81 ~BaselineScanner() { |
| 81 delete scanner_; | 82 delete scanner_; |
| 82 delete stream_; | 83 delete stream_; |
| 83 delete unicode_cache_; | 84 delete unicode_cache_; |
| 84 delete source_; | 85 delete[] source_; |
| 85 } | 86 } |
| 86 | 87 |
| 87 Token::Value Next(int* beg_pos, int* end_pos) { | 88 Token::Value Next(int* beg_pos, int* end_pos) { |
| 88 Token::Value res = scanner_->Next(); | 89 Token::Value res = scanner_->Next(); |
| 89 *beg_pos = scanner_->location().beg_pos; | 90 *beg_pos = scanner_->location().beg_pos; |
| 90 *end_pos = scanner_->location().end_pos; | 91 *end_pos = scanner_->location().end_pos; |
| 91 return res; | 92 return res; |
| 92 } | 93 } |
| 93 | 94 |
| 94 private: | 95 private: |
| 95 UnicodeCache* unicode_cache_; | 96 UnicodeCache* unicode_cache_; |
| 96 Scanner* scanner_; | 97 Scanner* scanner_; |
| 97 const byte* source_; | 98 const byte* source_; |
| 98 Utf8ToUtf16CharacterStream* stream_; | 99 Utf8ToUtf16CharacterStream* stream_; |
| 99 }; | 100 }; |
| 100 | 101 |
| 102 ExperimentalScanner::ExperimentalScanner(const char* fname, | |
| 103 bool read_all_at_once) | |
| 104 : current_(0), | |
| 105 fetched_(0), | |
| 106 read_all_at_once_(read_all_at_once), | |
| 107 source_(0), | |
| 108 length_(0) { | |
| 109 file_ = fopen(fname, "rb"); | |
| 110 scanner_ = new PushScanner(this); | |
| 111 if (read_all_at_once_) { | |
| 112 source_ = ReadFile(fname, NULL, &length_); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 | |
| 117 ExperimentalScanner::~ExperimentalScanner() { | |
| 118 fclose(file_); | |
| 119 delete[] source_; | |
| 120 } | |
| 121 | |
| 122 | |
| 123 void ExperimentalScanner::FillTokens() { | |
| 124 current_ = 0; | |
| 125 fetched_ = 0; | |
| 126 if (read_all_at_once_) { | |
| 127 scanner_->push(source_, length_ + 1); | |
| 128 } else { | |
| 129 uint8_t chars[BUFFER_SIZE]; | |
| 130 int n = static_cast<int>(fread(&chars, 1, BUFFER_SIZE, file_)); | |
| 131 for (int i = n; i < BUFFER_SIZE; i++) chars[i] = 0; | |
| 132 scanner_->push(chars, BUFFER_SIZE); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 | |
| 137 Token::Value ExperimentalScanner::Next(int* beg_pos, int* end_pos) { | |
| 138 while (current_ == fetched_) { | |
| 139 FillTokens(); | |
| 140 } | |
| 141 *beg_pos = beg_[current_]; | |
| 142 *end_pos = end_[current_]; | |
| 143 Token::Value res = token_[current_]; | |
| 144 if (token_[current_] != Token::Token::EOS && | |
| 145 token_[current_] != Token::ILLEGAL) { | |
| 146 current_++; | |
| 147 } | |
| 148 return res; | |
| 149 } | |
| 150 | |
| 151 | |
| 152 void ExperimentalScanner::Record(Token::Value token, int beg, int end) { | |
| 153 if (token == Token::EOS) end--; | |
| 154 if (fetched_ >= token_.size()) { | |
| 155 token_.push_back(token); | |
| 156 beg_.push_back(beg); | |
| 157 end_.push_back(end); | |
| 158 } else { | |
| 159 token_[fetched_] = token; | |
| 160 beg_[fetched_] = beg; | |
| 161 end_[fetched_] = end; | |
| 162 } | |
| 163 fetched_++; | |
| 164 } | |
| 165 | |
| 101 | 166 |
| 102 int main(int argc, char* argv[]) { | 167 int main(int argc, char* argv[]) { |
| 103 v8::V8::InitializeICU(); | 168 v8::V8::InitializeICU(); |
| 104 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 169 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 105 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 170 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 106 { | 171 { |
| 107 v8::HandleScope handle_scope(isolate); | 172 v8::HandleScope handle_scope(isolate); |
| 108 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 173 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
| 109 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); | 174 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); |
| 110 ASSERT(!context.IsEmpty()); | 175 ASSERT(!context.IsEmpty()); |
| 111 { | 176 { |
| 112 v8::Context::Scope scope(context); | 177 v8::Context::Scope scope(context); |
| 113 Isolate* isolate = Isolate::Current(); | 178 Isolate* isolate = Isolate::Current(); |
| 114 HandleScope handle_scope(isolate); | 179 HandleScope handle_scope(isolate); |
| 115 BaselineScanner baseline(argv[1], isolate); | 180 BaselineScanner baseline(argv[1], isolate); |
| 116 ExperimentalScanner experimental(argv[1]); | 181 ExperimentalScanner experimental(argv[1], true); |
| 117 Token::Value expected_token, actual_token; | 182 |
| 118 int expected_beg, expected_end, actual_beg, actual_end; | 183 std::vector<Token::Value> baseline_tokens, experimental_tokens; |
| 184 std::vector<size_t> baseline_beg, baseline_end, experimental_beg, | |
| 185 experimental_end; | |
| 186 Token::Value token; | |
| 187 int beg, end; | |
| 188 | |
| 189 struct timeval baseline_tv_start, baseline_tv_stop, experimental_tv_start, | |
| 190 experimental_tv_stop; | |
| 191 gettimeofday(&baseline_tv_start, NULL); | |
|
ulan
2013/10/17 13:43:32
We can use ElapsedTimer of v8 here.
marja
2013/10/17 13:56:34
Done.
| |
| 119 do { | 192 do { |
| 120 expected_token = baseline.Next(&expected_beg, &expected_end); | 193 token = baseline.Next(&beg, &end); |
| 121 actual_token = experimental.Next(&actual_beg, &actual_end); | 194 baseline_tokens.push_back(token); |
| 195 baseline_beg.push_back(beg); | |
| 196 baseline_end.push_back(end); | |
| 197 } while (token != Token::EOS); | |
| 198 gettimeofday(&baseline_tv_stop, NULL); | |
| 199 | |
| 200 gettimeofday(&experimental_tv_start, NULL); | |
| 201 do { | |
| 202 token = experimental.Next(&beg, &end); | |
| 203 experimental_tokens.push_back(token); | |
| 204 experimental_beg.push_back(beg); | |
| 205 experimental_end.push_back(end); | |
| 206 } while (token != Token::EOS); | |
| 207 gettimeofday(&experimental_tv_stop, NULL); | |
| 208 | |
| 209 for (size_t i = 0; i < experimental_tokens.size(); ++i) { | |
| 122 printf("=> %11s at (%d, %d)\n", | 210 printf("=> %11s at (%d, %d)\n", |
| 123 Token::Name(actual_token), | 211 Token::Name(experimental_tokens[i]), |
| 124 actual_beg, actual_end); | 212 experimental_beg[i], experimental_end[i]); |
| 125 if (expected_token != actual_token || | 213 if (experimental_tokens[i] != baseline_tokens[i] || |
| 126 expected_beg != actual_beg || | 214 experimental_beg[i] != baseline_beg[i] || |
| 127 expected_end != actual_end) { | 215 experimental_end[i] != baseline_end[i]) { |
| 128 printf("MISMATCH:\n"); | 216 printf("MISMATCH:\n"); |
| 129 printf("Expected: %s at (%d, %d)\n", | 217 printf("Expected: %s at (%d, %d)\n", |
| 130 Token::Name(expected_token), | 218 Token::Name(baseline_tokens[i]), |
| 131 expected_beg, expected_end); | 219 baseline_beg[i], baseline_end[i]); |
| 132 printf("Actual: %s at (%d, %d)\n", | 220 printf("Actual: %s at (%d, %d)\n", |
| 133 Token::Name(actual_token), | 221 Token::Name(experimental_tokens[i]), |
| 134 actual_beg, actual_end); | 222 experimental_beg[i], experimental_end[i]); |
| 135 return 1; | 223 return 1; |
| 136 } | 224 } |
| 137 } while (actual_token != Token::EOS); | 225 } |
| 226 fprintf(stderr, | |
| 227 "Baseline: %ld\n", | |
| 228 1000000 * (baseline_tv_stop.tv_sec - baseline_tv_start.tv_sec) + | |
| 229 baseline_tv_stop.tv_usec - baseline_tv_start.tv_usec); | |
| 230 fprintf(stderr, | |
| 231 "Experimental: %ld\n", | |
| 232 1000000 * (experimental_tv_stop.tv_sec - | |
| 233 experimental_tv_start.tv_sec) + | |
| 234 experimental_tv_stop.tv_usec - experimental_tv_start.tv_usec); | |
| 138 } | 235 } |
| 139 } | 236 } |
| 140 v8::V8::Dispose(); | 237 v8::V8::Dispose(); |
| 141 return 0; | 238 return 0; |
| 142 } | 239 } |
| OLD | NEW |