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

Side by Side Diff: src/handles.cc

Issue 5121007: Make count-line-ends use StringSearch and only iterate over the source once. (Closed)
Patch Set: Address review comment. Created 10 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 | « no previous file | 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 2009 the V8 project authors. All rights reserved. 1 // Copyright 2009 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 19 matching lines...) Expand all
30 #include "accessors.h" 30 #include "accessors.h"
31 #include "api.h" 31 #include "api.h"
32 #include "arguments.h" 32 #include "arguments.h"
33 #include "bootstrapper.h" 33 #include "bootstrapper.h"
34 #include "compiler.h" 34 #include "compiler.h"
35 #include "debug.h" 35 #include "debug.h"
36 #include "execution.h" 36 #include "execution.h"
37 #include "global-handles.h" 37 #include "global-handles.h"
38 #include "natives.h" 38 #include "natives.h"
39 #include "runtime.h" 39 #include "runtime.h"
40 #include "string-search.h"
40 #include "stub-cache.h" 41 #include "stub-cache.h"
41 42
42 namespace v8 { 43 namespace v8 {
43 namespace internal { 44 namespace internal {
44 45
45 46
46 v8::ImplementationUtilities::HandleScopeData HandleScope::current_ = 47 v8::ImplementationUtilities::HandleScopeData HandleScope::current_ =
47 { NULL, NULL, 0 }; 48 { NULL, NULL, 0 };
48 49
49 50
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 502
502 if (*array != Heap::empty_fixed_array()) { 503 if (*array != Heap::empty_fixed_array()) {
503 array->set_map(Heap::fixed_cow_array_map()); 504 array->set_map(Heap::fixed_cow_array_map());
504 } 505 }
505 506
506 script->set_line_ends(*array); 507 script->set_line_ends(*array);
507 ASSERT(script->line_ends()->IsFixedArray()); 508 ASSERT(script->line_ends()->IsFixedArray());
508 } 509 }
509 510
510 511
512 template <typename SourceChar>
513 static void CalculateLineEnds(List<int>* line_ends,
514 Vector<const SourceChar> src,
515 bool with_last_line) {
516 const int src_len = src.length();
517 StringSearch<char, SourceChar> search(CStrVector("\n"));
518
519 // Find and record line ends.
520 int position = 0;
521 while (position != -1 && position < src_len) {
522 position = search.Search(src, position);
523 if (position != -1) {
524 line_ends->Add(position);
525 position++;
526 } else if (with_last_line) {
527 // Even if the last line misses a line end, it is counted.
528 line_ends->Add(src_len);
529 return;
530 }
531 }
532 }
533
534
511 Handle<FixedArray> CalculateLineEnds(Handle<String> src, 535 Handle<FixedArray> CalculateLineEnds(Handle<String> src,
512 bool with_imaginary_last_new_line) { 536 bool with_last_line) {
513 const int src_len = src->length(); 537 src = FlattenGetString(src);
514 Handle<String> new_line = Factory::NewStringFromAscii(CStrVector("\n")); 538 // Rough estimate of line count based on a roughly estimated average
515 539 // length of (unpacked) code.
516 // Pass 1: Identify line count. 540 int line_count_estimate = src->length() >> 4;
517 int line_count = 0; 541 List<int> line_ends(line_count_estimate);
518 int position = 0; 542 {
519 while (position != -1 && position < src_len) { 543 AssertNoAllocation no_heap_allocation; // ensure vectors stay valid.
520 position = Runtime::StringMatch(src, new_line, position); 544 // Dispatch on type of strings.
521 if (position != -1) { 545 if (src->IsAsciiRepresentation()) {
522 position++; 546 CalculateLineEnds(&line_ends, src->ToAsciiVector(), with_last_line);
523 } 547 } else {
524 if (position != -1) { 548 CalculateLineEnds(&line_ends, src->ToUC16Vector(), with_last_line);
525 line_count++;
526 } else if (with_imaginary_last_new_line) {
527 // Even if the last line misses a line end, it is counted.
528 line_count++;
529 } 549 }
530 } 550 }
531 551 int line_count = line_ends.length();
532 // Pass 2: Fill in line ends positions
533 Handle<FixedArray> array = Factory::NewFixedArray(line_count); 552 Handle<FixedArray> array = Factory::NewFixedArray(line_count);
534 int array_index = 0; 553 for (int i = 0; i < line_count; i++) {
535 position = 0; 554 array->set(i, Smi::FromInt(line_ends[i]));
536 while (position != -1 && position < src_len) {
537 position = Runtime::StringMatch(src, new_line, position);
538 if (position != -1) {
539 array->set(array_index++, Smi::FromInt(position++));
540 } else if (with_imaginary_last_new_line) {
541 // If the script does not end with a line ending add the final end
542 // position as just past the last line ending.
543 array->set(array_index++, Smi::FromInt(src_len));
544 }
545 } 555 }
546 ASSERT(array_index == line_count);
547
548 return array; 556 return array;
549 } 557 }
550 558
551 559
552 // Convert code position into line number. 560 // Convert code position into line number.
553 int GetScriptLineNumber(Handle<Script> script, int code_pos) { 561 int GetScriptLineNumber(Handle<Script> script, int code_pos) {
554 InitScriptLineEnds(script); 562 InitScriptLineEnds(script);
555 AssertNoAllocation no_allocation; 563 AssertNoAllocation no_allocation;
556 FixedArray* line_ends_array = FixedArray::cast(script->line_ends()); 564 FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
557 const int line_ends_len = line_ends_array->length(); 565 const int line_ends_len = line_ends_array->length();
558 566
559 if (!line_ends_len) 567 if (!line_ends_len) return -1;
560 return -1;
561 568
562 if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) 569 if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) {
563 return script->line_offset()->value(); 570 return script->line_offset()->value();
571 }
564 572
565 int left = 0; 573 int left = 0;
566 int right = line_ends_len; 574 int right = line_ends_len;
567 while (int half = (right - left) / 2) { 575 while (int half = (right - left) / 2) {
568 if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) { 576 if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) {
569 right -= half; 577 right -= half;
570 } else { 578 } else {
571 left += half; 579 left += half;
572 } 580 }
573 } 581 }
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 876
869 OptimizedObjectForAddingMultipleProperties:: 877 OptimizedObjectForAddingMultipleProperties::
870 ~OptimizedObjectForAddingMultipleProperties() { 878 ~OptimizedObjectForAddingMultipleProperties() {
871 // Reoptimize the object to allow fast property access. 879 // Reoptimize the object to allow fast property access.
872 if (has_been_transformed_) { 880 if (has_been_transformed_) {
873 TransformToFastProperties(object_, unused_property_fields_); 881 TransformToFastProperties(object_, unused_property_fields_);
874 } 882 }
875 } 883 }
876 884
877 } } // namespace v8::internal 885 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698