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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 unicode_cache_ = new UnicodeCache(); | 74 unicode_cache_ = new UnicodeCache(); |
75 scanner_ = new Scanner(unicode_cache_); | 75 scanner_ = new Scanner(unicode_cache_); |
76 stream_ = new Utf8ToUtf16CharacterStream(source_, length); | 76 stream_ = new Utf8ToUtf16CharacterStream(source_, length); |
77 scanner_->Initialize(stream_); | 77 scanner_->Initialize(stream_); |
78 } | 78 } |
79 | 79 |
80 ~BaselineScanner() { | 80 ~BaselineScanner() { |
81 delete scanner_; | 81 delete scanner_; |
82 delete stream_; | 82 delete stream_; |
83 delete unicode_cache_; | 83 delete unicode_cache_; |
84 delete source_; | 84 delete[] source_; |
85 } | 85 } |
86 | 86 |
87 Token::Value Next(int* beg_pos, int* end_pos) { | 87 Token::Value Next(int* beg_pos, int* end_pos) { |
88 Token::Value res = scanner_->Next(); | 88 Token::Value res = scanner_->Next(); |
89 *beg_pos = scanner_->location().beg_pos; | 89 *beg_pos = scanner_->location().beg_pos; |
90 *end_pos = scanner_->location().end_pos; | 90 *end_pos = scanner_->location().end_pos; |
91 return res; | 91 return res; |
92 } | 92 } |
93 | 93 |
94 private: | 94 private: |
95 UnicodeCache* unicode_cache_; | 95 UnicodeCache* unicode_cache_; |
96 Scanner* scanner_; | 96 Scanner* scanner_; |
97 const byte* source_; | 97 const byte* source_; |
98 Utf8ToUtf16CharacterStream* stream_; | 98 Utf8ToUtf16CharacterStream* stream_; |
99 }; | 99 }; |
100 | 100 |
| 101 ExperimentalScanner::ExperimentalScanner(const char* fname, |
| 102 bool read_all_at_once) |
| 103 : current_(0), |
| 104 fetched_(0), |
| 105 read_all_at_once_(read_all_at_once), |
| 106 source_(0), |
| 107 length_(0) { |
| 108 file_ = fopen(fname, "rb"); |
| 109 scanner_ = new PushScanner(this); |
| 110 if (read_all_at_once_) { |
| 111 source_ = ReadFile(fname, NULL, &length_); |
| 112 } |
| 113 } |
| 114 |
| 115 |
| 116 ExperimentalScanner::~ExperimentalScanner() { |
| 117 fclose(file_); |
| 118 delete[] source_; |
| 119 } |
| 120 |
| 121 |
| 122 void ExperimentalScanner::FillTokens() { |
| 123 current_ = 0; |
| 124 fetched_ = 0; |
| 125 if (read_all_at_once_) { |
| 126 scanner_->push(source_, length_ + 1); |
| 127 } else { |
| 128 uint8_t chars[BUFFER_SIZE]; |
| 129 int n = static_cast<int>(fread(&chars, 1, BUFFER_SIZE, file_)); |
| 130 for (int i = n; i < BUFFER_SIZE; i++) chars[i] = 0; |
| 131 scanner_->push(chars, BUFFER_SIZE); |
| 132 } |
| 133 } |
| 134 |
| 135 |
| 136 Token::Value ExperimentalScanner::Next(int* beg_pos, int* end_pos) { |
| 137 while (current_ == fetched_) { |
| 138 FillTokens(); |
| 139 } |
| 140 *beg_pos = beg_[current_]; |
| 141 *end_pos = end_[current_]; |
| 142 Token::Value res = token_[current_]; |
| 143 if (token_[current_] != Token::Token::EOS && |
| 144 token_[current_] != Token::ILLEGAL) { |
| 145 current_++; |
| 146 } |
| 147 return res; |
| 148 } |
| 149 |
| 150 |
| 151 void ExperimentalScanner::Record(Token::Value token, int beg, int end) { |
| 152 if (token == Token::EOS) end--; |
| 153 if (fetched_ >= token_.size()) { |
| 154 token_.push_back(token); |
| 155 beg_.push_back(beg); |
| 156 end_.push_back(end); |
| 157 } else { |
| 158 token_[fetched_] = token; |
| 159 beg_[fetched_] = beg; |
| 160 end_[fetched_] = end; |
| 161 } |
| 162 fetched_++; |
| 163 } |
| 164 |
101 | 165 |
102 int main(int argc, char* argv[]) { | 166 int main(int argc, char* argv[]) { |
103 v8::V8::InitializeICU(); | 167 v8::V8::InitializeICU(); |
104 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 168 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
105 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 169 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
106 { | 170 { |
107 v8::HandleScope handle_scope(isolate); | 171 v8::HandleScope handle_scope(isolate); |
108 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 172 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
109 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); | 173 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); |
110 ASSERT(!context.IsEmpty()); | 174 ASSERT(!context.IsEmpty()); |
111 { | 175 { |
112 v8::Context::Scope scope(context); | 176 v8::Context::Scope scope(context); |
113 Isolate* isolate = Isolate::Current(); | 177 Isolate* isolate = Isolate::Current(); |
114 HandleScope handle_scope(isolate); | 178 HandleScope handle_scope(isolate); |
115 BaselineScanner baseline(argv[1], isolate); | 179 BaselineScanner baseline(argv[1], isolate); |
116 ExperimentalScanner experimental(argv[1]); | 180 ExperimentalScanner experimental(argv[1], true); |
117 Token::Value expected_token, actual_token; | 181 |
118 int expected_beg, expected_end, actual_beg, actual_end; | 182 std::vector<Token::Value> baseline_tokens, experimental_tokens; |
119 do { | 183 std::vector<size_t> baseline_beg, baseline_end, experimental_beg, |
120 expected_token = baseline.Next(&expected_beg, &expected_end); | 184 experimental_end; |
121 actual_token = experimental.Next(&actual_beg, &actual_end); | 185 Token::Value token; |
| 186 int beg, end; |
| 187 |
| 188 TimeDelta baseline_time, experimental_time; |
| 189 ElapsedTimer timer; |
| 190 { |
| 191 timer.Start(); |
| 192 do { |
| 193 token = baseline.Next(&beg, &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 baseline_time = timer.Elapsed(); |
| 199 } |
| 200 |
| 201 { |
| 202 timer.Start(); |
| 203 do { |
| 204 token = experimental.Next(&beg, &end); |
| 205 experimental_tokens.push_back(token); |
| 206 experimental_beg.push_back(beg); |
| 207 experimental_end.push_back(end); |
| 208 } while (token != Token::EOS); |
| 209 experimental_time = timer.Elapsed(); |
| 210 } |
| 211 |
| 212 for (size_t i = 0; i < experimental_tokens.size(); ++i) { |
122 printf("=> %11s at (%d, %d)\n", | 213 printf("=> %11s at (%d, %d)\n", |
123 Token::Name(actual_token), | 214 Token::Name(experimental_tokens[i]), |
124 actual_beg, actual_end); | 215 experimental_beg[i], experimental_end[i]); |
125 if (expected_token != actual_token || | 216 if (experimental_tokens[i] != baseline_tokens[i] || |
126 expected_beg != actual_beg || | 217 experimental_beg[i] != baseline_beg[i] || |
127 expected_end != actual_end) { | 218 experimental_end[i] != baseline_end[i]) { |
128 printf("MISMATCH:\n"); | 219 printf("MISMATCH:\n"); |
129 printf("Expected: %s at (%d, %d)\n", | 220 printf("Expected: %s at (%d, %d)\n", |
130 Token::Name(expected_token), | 221 Token::Name(baseline_tokens[i]), |
131 expected_beg, expected_end); | 222 baseline_beg[i], baseline_end[i]); |
132 printf("Actual: %s at (%d, %d)\n", | 223 printf("Actual: %s at (%d, %d)\n", |
133 Token::Name(actual_token), | 224 Token::Name(experimental_tokens[i]), |
134 actual_beg, actual_end); | 225 experimental_beg[i], experimental_end[i]); |
135 return 1; | 226 return 1; |
136 } | 227 } |
137 } while (actual_token != Token::EOS); | 228 } |
| 229 printf("Baseline: %f ms\nExperimental %f ms\n", |
| 230 baseline_time.InMillisecondsF(), |
| 231 experimental_time.InMillisecondsF()); |
138 } | 232 } |
139 } | 233 } |
140 v8::V8::Dispose(); | 234 v8::V8::Dispose(); |
141 return 0; | 235 return 0; |
142 } | 236 } |
OLD | NEW |