Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: runtime/vm/scanner.cc

Issue 315303002: Improve scanner performance by remembering string's CharAt function as the string type does not cha… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/scanner.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/scanner.h" 5 #include "vm/scanner.h"
6 6
7 #include "platform/assert.h" 7 #include "platform/assert.h"
8 #include "vm/dart.h" 8 #include "vm/dart.h"
9 #include "vm/flags.h" 9 #include "vm/flags.h"
10 #include "vm/object.h" 10 #include "vm/object.h"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 c0_pos_.line = 1; 51 c0_pos_.line = 1;
52 c0_pos_.column = 0; 52 c0_pos_.column = 0;
53 ReadChar(); 53 ReadChar();
54 } 54 }
55 55
56 56
57 Scanner::Scanner(const String& src, const String& private_key) 57 Scanner::Scanner(const String& src, const String& private_key)
58 : source_(src), 58 : source_(src),
59 source_length_(src.Length()), 59 source_length_(src.Length()),
60 saved_context_(NULL), 60 saved_context_(NULL),
61 private_key_(String::ZoneHandle(private_key.raw())) { 61 private_key_(String::ZoneHandle(private_key.raw())),
62 char_at_func_(src.CharAtFunc()),
63 isolate_(Isolate::Current()) {
62 Reset(); 64 Reset();
63 } 65 }
64 66
67
65 Scanner::~Scanner() { 68 Scanner::~Scanner() {
66 while (saved_context_ != NULL) { 69 while (saved_context_ != NULL) {
67 ScanContext* ctx = saved_context_; 70 ScanContext* ctx = saved_context_;
68 saved_context_ = ctx->next; 71 saved_context_ = ctx->next;
69 delete ctx; 72 delete ctx;
70 } 73 }
71 } 74 }
72 75
73 76
74 void Scanner::ErrorMsg(const char* msg) { 77 void Scanner::ErrorMsg(const char* msg) {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 150
148 bool Scanner::IsIdentChar(int32_t c) { 151 bool Scanner::IsIdentChar(int32_t c) {
149 return IsLetter(c) || IsDecimalDigit(c) || (c == '_') || (c == '$'); 152 return IsLetter(c) || IsDecimalDigit(c) || (c == '_') || (c == '$');
150 } 153 }
151 154
152 155
153 bool Scanner::IsIdent(const String& str) { 156 bool Scanner::IsIdent(const String& str) {
154 if (!str.IsOneByteString()) { 157 if (!str.IsOneByteString()) {
155 return false; 158 return false;
156 } 159 }
157 if (str.Length() == 0 || !IsIdentStartChar(str.CharAt(0))) { 160 if (str.Length() == 0 || !IsIdentStartChar(CallCharAt()(str, 0))) {
158 return false; 161 return false;
159 } 162 }
160 for (int i = 1; i < str.Length(); i++) { 163 for (int i = 1; i < str.Length(); i++) {
161 if (!IsIdentChar(str.CharAt(i))) { 164 if (!IsIdentChar(CallCharAt()(str, i))) {
162 return false; 165 return false;
163 } 166 }
164 } 167 }
165 return true; 168 return true;
166 } 169 }
167 170
168 171
169 // This method is used when parsing integers and doubles in Dart code. We 172 // This method is used when parsing integers and doubles in Dart code. We
170 // are reusing the Scanner's handling of number literals in that situation. 173 // are reusing the Scanner's handling of number literals in that situation.
171 bool Scanner::IsValidLiteral(const Scanner::GrowableTokenStream& tokens, 174 bool Scanner::IsValidLiteral(const Scanner::GrowableTokenStream& tokens,
(...skipping 23 matching lines...) Expand all
195 return false; 198 return false;
196 } 199 }
197 200
198 201
199 void Scanner::ReadChar() { 202 void Scanner::ReadChar() {
200 if (lookahead_pos_ < source_length_) { 203 if (lookahead_pos_ < source_length_) {
201 if (c0_ == '\n') { 204 if (c0_ == '\n') {
202 newline_seen_ = true; 205 newline_seen_ = true;
203 c0_pos_.line++; 206 c0_pos_.line++;
204 c0_pos_.column = 0; 207 c0_pos_.column = 0;
205 if (source_.CharAt(lookahead_pos_) == '\r') { 208 if (CallCharAt()(source_, lookahead_pos_) == '\r') {
206 // Replace a sequence of '\r' '\n' with a single '\n'. 209 // Replace a sequence of '\r' '\n' with a single '\n'.
207 if (LookaheadChar(1) == '\n') { 210 if (LookaheadChar(1) == '\n') {
208 lookahead_pos_++; 211 lookahead_pos_++;
209 } 212 }
210 } 213 }
211 } 214 }
212 lookahead_pos_++; 215 lookahead_pos_++;
213 c0_pos_.column++; 216 c0_pos_.column++;
214 c0_ = LookaheadChar(0); 217 c0_ = LookaheadChar(0);
215 // Replace '\r' with '\n'. 218 // Replace '\r' with '\n'.
216 if (c0_ == '\r') { 219 if (c0_ == '\r') {
217 c0_ = '\n'; 220 c0_ = '\n';
218 } 221 }
219 } 222 }
220 } 223 }
221 224
222 225
223 // Look ahead 'how_many' characters. Returns the character, or '\0' if 226 // Look ahead 'how_many' characters. Returns the character, or '\0' if
224 // the lookahead position is beyond the end of the string. Does not 227 // the lookahead position is beyond the end of the string. Does not
225 // normalize line end characters into '\n'. 228 // normalize line end characters into '\n'.
226 int32_t Scanner::LookaheadChar(int how_many) { 229 int32_t Scanner::LookaheadChar(int how_many) {
227 ASSERT(how_many >= 0); 230 ASSERT(how_many >= 0);
228 int32_t lookahead_char = '\0'; 231 int32_t lookahead_char = '\0';
229 if (lookahead_pos_ + how_many < source_length_) { 232 if (lookahead_pos_ + how_many < source_length_) {
230 lookahead_char = source_.CharAt(lookahead_pos_ + how_many); 233 lookahead_char = CallCharAt()(source_, lookahead_pos_ + how_many);
231 } 234 }
232 return lookahead_char; 235 return lookahead_char;
233 } 236 }
234 237
235 238
236 void Scanner::ConsumeWhiteSpace() { 239 void Scanner::ConsumeWhiteSpace() {
237 while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n') { 240 while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n') {
238 ReadChar(); 241 ReadChar();
239 } 242 }
240 } 243 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 current_token_.kind = 278 current_token_.kind =
276 (nesting_level == 0) ? Token::kWHITESP : Token::kILLEGAL; 279 (nesting_level == 0) ? Token::kWHITESP : Token::kILLEGAL;
277 } 280 }
278 281
279 282
280 void Scanner::ScanIdentChars(bool allow_dollar) { 283 void Scanner::ScanIdentChars(bool allow_dollar) {
281 ASSERT(IsIdentStartChar(c0_)); 284 ASSERT(IsIdentStartChar(c0_));
282 ASSERT(allow_dollar || (c0_ != '$')); 285 ASSERT(allow_dollar || (c0_ != '$'));
283 int ident_length = 0; 286 int ident_length = 0;
284 int ident_pos = lookahead_pos_; 287 int ident_pos = lookahead_pos_;
285 int32_t ident_char0 = source_.CharAt(ident_pos); 288 int32_t ident_char0 = CallCharAt()(source_, ident_pos);
286 while (IsIdentChar(c0_) && (allow_dollar || (c0_ != '$'))) { 289 while (IsIdentChar(c0_) && (allow_dollar || (c0_ != '$'))) {
287 ReadChar(); 290 ReadChar();
288 ident_length++; 291 ident_length++;
289 } 292 }
290 293
291 // Check whether the characters we read are a known keyword. 294 // Check whether the characters we read are a known keyword.
292 // Note, can't use strcmp since token_chars is not null-terminated. 295 // Note, can't use strcmp since token_chars is not null-terminated.
293 if (('a' <= ident_char0) && (ident_char0 <= 'z')) { 296 if (('a' <= ident_char0) && (ident_char0 <= 'z')) {
294 int i = keywords_char_offset_[ident_char0 - 'a']; 297 int i = keywords_char_offset_[ident_char0 - 'a'];
295 while (i < Token::kNumKeywords && 298 while ((i < Token::kNumKeywords) &&
296 keywords_[i].keyword_chars[0] <= ident_char0) { 299 (keywords_[i].keyword_chars[0] <= ident_char0)) {
297 if (keywords_[i].keyword_len == ident_length) { 300 if (keywords_[i].keyword_len == ident_length) {
298 const char* keyword = keywords_[i].keyword_chars; 301 const char* keyword = keywords_[i].keyword_chars;
299 int char_pos = 1; 302 int char_pos = 1;
300 while ((char_pos < ident_length) && 303 while ((char_pos < ident_length) &&
301 (keyword[char_pos] == source_.CharAt(ident_pos + char_pos))) { 304 (keyword[char_pos] ==
305 CallCharAt()(source_, ident_pos + char_pos))) {
302 char_pos++; 306 char_pos++;
303 } 307 }
304 if (char_pos == ident_length) { 308 if (char_pos == ident_length) {
305 current_token_.literal = keywords_[i].keyword_symbol; 309 current_token_.literal = keywords_[i].keyword_symbol;
306 current_token_.kind = keywords_[i].kind; 310 current_token_.kind = keywords_[i].kind;
307 return; 311 return;
308 } 312 }
309 } 313 }
310 i++; 314 i++;
311 } 315 }
(...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after
951 keywords_[i].keyword_symbol = &Symbols::Keyword(token); 955 keywords_[i].keyword_symbol = &Symbols::Keyword(token);
952 956
953 int ch = keywords_[i].keyword_chars[0] - 'a'; 957 int ch = keywords_[i].keyword_chars[0] - 'a';
954 if (keywords_char_offset_[ch] == Token::kNumKeywords) { 958 if (keywords_char_offset_[ch] == Token::kNumKeywords) {
955 keywords_char_offset_[ch] = i; 959 keywords_char_offset_[ch] = i;
956 } 960 }
957 } 961 }
958 } 962 }
959 963
960 } // namespace dart 964 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/scanner.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698