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

Side by Side Diff: src/scanner.cc

Issue 377006: Remove unnecessary buffer doubling and content copying. (Closed)
Patch Set: Addressed review comments Created 11 years, 1 month 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
« no previous file with comments | « src/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 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 unibrow::Predicate<unibrow::LineTerminator, 128> Scanner::kIsLineTerminator; 42 unibrow::Predicate<unibrow::LineTerminator, 128> Scanner::kIsLineTerminator;
43 unibrow::Predicate<unibrow::WhiteSpace, 128> Scanner::kIsWhiteSpace; 43 unibrow::Predicate<unibrow::WhiteSpace, 128> Scanner::kIsWhiteSpace;
44 44
45 45
46 StaticResource<Scanner::Utf8Decoder> Scanner::utf8_decoder_; 46 StaticResource<Scanner::Utf8Decoder> Scanner::utf8_decoder_;
47 47
48 48
49 // ---------------------------------------------------------------------------- 49 // ----------------------------------------------------------------------------
50 // UTF8Buffer 50 // UTF8Buffer
51 51
52 UTF8Buffer::UTF8Buffer() { 52 UTF8Buffer::UTF8Buffer() : data_(NULL), limit_(NULL) { }
53 static const int kInitialCapacity = 1 * KB;
54 data_ = NewArray<char>(kInitialCapacity);
55 limit_ = ComputeLimit(data_, kInitialCapacity);
56 Reset();
57 ASSERT(Capacity() == kInitialCapacity && pos() == 0);
58 }
59 53
60 54
61 UTF8Buffer::~UTF8Buffer() { 55 UTF8Buffer::~UTF8Buffer() {
62 DeleteArray(data_); 56 DeleteArray(data_);
63 } 57 }
64 58
65 59
66 void UTF8Buffer::AddCharSlow(uc32 c) { 60 void UTF8Buffer::AddCharSlow(uc32 c) {
67 static const int kCapacityGrowthLimit = 1 * MB; 61 static const int kCapacityGrowthLimit = 1 * MB;
68 if (cursor_ > limit_) { 62 if (cursor_ > limit_) {
69 int old_capacity = Capacity(); 63 int old_capacity = Capacity();
70 int old_position = pos(); 64 int old_position = pos();
71 int new_capacity = 65 int new_capacity =
72 Min(old_capacity * 2, old_capacity + kCapacityGrowthLimit); 66 Min(old_capacity * 3, old_capacity + kCapacityGrowthLimit);
73 char* new_data = NewArray<char>(new_capacity); 67 char* new_data = NewArray<char>(new_capacity);
74 memcpy(new_data, data_, old_position); 68 memcpy(new_data, data_, old_position);
75 DeleteArray(data_); 69 DeleteArray(data_);
76 data_ = new_data; 70 data_ = new_data;
77 cursor_ = new_data + old_position; 71 cursor_ = new_data + old_position;
78 limit_ = ComputeLimit(new_data, new_capacity); 72 limit_ = ComputeLimit(new_data, new_capacity);
79 ASSERT(Capacity() == new_capacity && pos() == old_position); 73 ASSERT(Capacity() == new_capacity && pos() == old_position);
80 } 74 }
81 if (static_cast<unsigned>(c) <= unibrow::Utf8::kMaxOneByteChar) { 75 if (static_cast<unsigned>(c) <= unibrow::Utf8::kMaxOneByteChar) {
82 *cursor_++ = c; // Common case: 7-bit ASCII. 76 *cursor_++ = c; // Common case: 7-bit ASCII.
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 two_byte_string_buffer_.Initialize( 333 two_byte_string_buffer_.Initialize(
340 Handle<ExternalTwoByteString>::cast(source)); 334 Handle<ExternalTwoByteString>::cast(source));
341 source_ = &two_byte_string_buffer_; 335 source_ = &two_byte_string_buffer_;
342 } else { 336 } else {
343 char_stream_buffer_.Initialize(source, stream); 337 char_stream_buffer_.Initialize(source, stream);
344 source_ = &char_stream_buffer_; 338 source_ = &char_stream_buffer_;
345 } 339 }
346 340
347 position_ = position; 341 position_ = position;
348 342
349 // Reset literals buffer
350 literals_.Reset();
351
352 // Set c0_ (one character ahead) 343 // Set c0_ (one character ahead)
353 ASSERT(kCharacterLookaheadBufferSize == 1); 344 ASSERT(kCharacterLookaheadBufferSize == 1);
354 Advance(); 345 Advance();
355 346
356 // Skip initial whitespace allowing HTML comment ends just like 347 // Skip initial whitespace allowing HTML comment ends just like
357 // after a newline and scan first token. 348 // after a newline and scan first token.
358 has_line_terminator_before_next_ = true; 349 has_line_terminator_before_next_ = true;
359 SkipWhiteSpace(); 350 SkipWhiteSpace();
360 Scan(); 351 Scan();
361 } 352 }
362 353
363 354
364 Handle<String> Scanner::SubString(int start, int end) { 355 Handle<String> Scanner::SubString(int start, int end) {
365 return source_->SubString(start - position_, end - position_); 356 return source_->SubString(start - position_, end - position_);
366 } 357 }
367 358
368 359
369 Token::Value Scanner::Next() { 360 Token::Value Scanner::Next() {
370 // BUG 1215673: Find a thread safe way to set a stack limit in 361 // BUG 1215673: Find a thread safe way to set a stack limit in
371 // pre-parse mode. Otherwise, we cannot safely pre-parse from other 362 // pre-parse mode. Otherwise, we cannot safely pre-parse from other
372 // threads. 363 // threads.
373 current_ = next_; 364 current_ = next_;
374 // Check for stack-overflow before returning any tokens. 365 // Check for stack-overflow before returning any tokens.
375 StackLimitCheck check; 366 StackLimitCheck check;
376 if (check.HasOverflowed()) { 367 if (check.HasOverflowed()) {
377 stack_overflow_ = true; 368 stack_overflow_ = true;
378 next_.token = Token::ILLEGAL; 369 next_.token = Token::ILLEGAL;
370 next_.literal_buffer = NULL;
379 } else { 371 } else {
380 Scan(); 372 Scan();
381 } 373 }
382 return current_.token; 374 return current_.token;
383 } 375 }
384 376
385 377
386 void Scanner::StartLiteral() { 378 void Scanner::StartLiteral() {
387 next_.literal_pos = literals_.pos(); 379 // Use the first buffer, unless it's currently in use by the current_ token.
380 // In most cases, we won't have two literals/identifiers in a row, so
381 // the second buffer won't be used very often and is unlikely to grow much.
382 UTF8Buffer* free_buffer =
383 (current_.literal_buffer != &literal_buffer_1_) ? &literal_buffer_1_
384 : &literal_buffer_2_;
385 next_.literal_buffer = free_buffer;
386 free_buffer->Reset();
388 } 387 }
389 388
390 389
391 void Scanner::AddChar(uc32 c) { 390 void Scanner::AddChar(uc32 c) {
392 literals_.AddChar(c); 391 next_.literal_buffer->AddChar(c);
393 } 392 }
394 393
395 394
396 void Scanner::TerminateLiteral() { 395 void Scanner::TerminateLiteral() {
397 next_.literal_end = literals_.pos();
398 AddChar(0); 396 AddChar(0);
399 } 397 }
400 398
401 399
402 void Scanner::AddCharAdvance() { 400 void Scanner::AddCharAdvance() {
403 AddChar(c0_); 401 AddChar(c0_);
404 Advance(); 402 Advance();
405 } 403 }
406 404
407 405
(...skipping 681 matching lines...) Expand 10 before | Expand all | Expand 10 after
1089 } 1087 }
1090 AddCharAdvance(); 1088 AddCharAdvance();
1091 } 1089 }
1092 TerminateLiteral(); 1090 TerminateLiteral();
1093 1091
1094 next_.location.end_pos = source_pos() - 1; 1092 next_.location.end_pos = source_pos() - 1;
1095 return true; 1093 return true;
1096 } 1094 }
1097 1095
1098 } } // namespace v8::internal 1096 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/scanner.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698