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

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: Now relative to tip of bleeding edge. 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 if (!src->IsFlat()) FlattenString(src);
Vyacheslav Egorov (Chromium) 2010/11/18 10:36:08 src = FlattenGetString(src)? this will allow to r
Lasse Reichstein 2010/11/18 10:40:55 Done.
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 // Extract flattened substring of cons string before determining asciiness.
521 if (position != -1) { 545 String* seq_src = *src;
522 position++; 546 if (seq_src->IsConsString()) seq_src = ConsString::cast(seq_src)->first();
523 } 547 // Dispatch on type of strings.
524 if (position != -1) { 548 if (seq_src->IsAsciiRepresentation()) {
525 line_count++; 549 CalculateLineEnds(&line_ends, seq_src->ToAsciiVector(), with_last_line);
526 } else if (with_imaginary_last_new_line) { 550 } else {
527 // Even if the last line misses a line end, it is counted. 551 CalculateLineEnds(&line_ends, seq_src->ToUC16Vector(), with_last_line);
528 line_count++;
529 } 552 }
530 } 553 }
531 554 int line_count = line_ends.length();
532 // Pass 2: Fill in line ends positions
533 Handle<FixedArray> array = Factory::NewFixedArray(line_count); 555 Handle<FixedArray> array = Factory::NewFixedArray(line_count);
534 int array_index = 0; 556 for (int i = 0; i < line_count; i++) {
535 position = 0; 557 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 } 558 }
546 ASSERT(array_index == line_count);
547
548 return array; 559 return array;
549 } 560 }
550 561
551 562
552 // Convert code position into line number. 563 // Convert code position into line number.
553 int GetScriptLineNumber(Handle<Script> script, int code_pos) { 564 int GetScriptLineNumber(Handle<Script> script, int code_pos) {
554 InitScriptLineEnds(script); 565 InitScriptLineEnds(script);
555 AssertNoAllocation no_allocation; 566 AssertNoAllocation no_allocation;
556 FixedArray* line_ends_array = FixedArray::cast(script->line_ends()); 567 FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
557 const int line_ends_len = line_ends_array->length(); 568 const int line_ends_len = line_ends_array->length();
558 569
559 if (!line_ends_len) 570 if (!line_ends_len) return -1;
560 return -1;
561 571
562 if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) 572 if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) {
563 return script->line_offset()->value(); 573 return script->line_offset()->value();
574 }
564 575
565 int left = 0; 576 int left = 0;
566 int right = line_ends_len; 577 int right = line_ends_len;
567 while (int half = (right - left) / 2) { 578 while (int half = (right - left) / 2) {
568 if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) { 579 if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) {
569 right -= half; 580 right -= half;
570 } else { 581 } else {
571 left += half; 582 left += half;
572 } 583 }
573 } 584 }
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 879
869 OptimizedObjectForAddingMultipleProperties:: 880 OptimizedObjectForAddingMultipleProperties::
870 ~OptimizedObjectForAddingMultipleProperties() { 881 ~OptimizedObjectForAddingMultipleProperties() {
871 // Reoptimize the object to allow fast property access. 882 // Reoptimize the object to allow fast property access.
872 if (has_been_transformed_) { 883 if (has_been_transformed_) {
873 TransformToFastProperties(object_, unused_property_fields_); 884 TransformToFastProperties(object_, unused_property_fields_);
874 } 885 }
875 } 886 }
876 887
877 } } // namespace v8::internal 888 } } // 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