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

Side by Side Diff: src/objects.cc

Issue 8304021: Improve speed of Utf8Write by always flattening the string first and (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 9 years, 2 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 | « src/objects.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 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 5501 matching lines...) Expand 10 before | Expand all | Expand 10 after
5512 } 5512 }
5513 #endif 5513 #endif
5514 5514
5515 5515
5516 bool String::LooksValid() { 5516 bool String::LooksValid() {
5517 if (!Isolate::Current()->heap()->Contains(this)) return false; 5517 if (!Isolate::Current()->heap()->Contains(this)) return false;
5518 return true; 5518 return true;
5519 } 5519 }
5520 5520
5521 5521
5522 int String::Utf8Length() {
5523 if (IsAsciiRepresentation()) return length();
5524 // Attempt to flatten before accessing the string. It probably
5525 // doesn't make Utf8Length faster, but it is very likely that
5526 // the string will be accessed later (for example by WriteUtf8)
5527 // so it's still a good idea.
5528 Heap* heap = GetHeap();
5529 TryFlatten();
5530 Access<StringInputBuffer> buffer(
5531 heap->isolate()->objects_string_input_buffer());
5532 buffer->Reset(0, this);
5533 int result = 0;
5534 while (buffer->has_more())
5535 result += unibrow::Utf8::Length(buffer->GetNext());
5536 return result;
5537 }
5538
5539
5540 String::FlatContent String::GetFlatContent() { 5522 String::FlatContent String::GetFlatContent() {
5541 int length = this->length(); 5523 int length = this->length();
5542 StringShape shape(this); 5524 StringShape shape(this);
5543 String* string = this; 5525 String* string = this;
5544 int offset = 0; 5526 int offset = 0;
5545 if (shape.representation_tag() == kConsStringTag) { 5527 if (shape.representation_tag() == kConsStringTag) {
5546 ConsString* cons = ConsString::cast(string); 5528 ConsString* cons = ConsString::cast(string);
5547 if (cons->second()->length() != 0) { 5529 if (cons->second()->length() != 0) {
5548 return FlatContent(); 5530 return FlatContent();
5549 } 5531 }
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after
5953 max_chars); 5935 max_chars);
5954 default: 5936 default:
5955 break; 5937 break;
5956 } 5938 }
5957 5939
5958 UNREACHABLE(); 5940 UNREACHABLE();
5959 return 0; 5941 return 0;
5960 } 5942 }
5961 5943
5962 5944
5945 // This method determines the type of string involved and then gets the UTF8
5946 // length of the string. It doesn't flatten the string and has log(n) recursion
5947 // for a string of length n.
5948 int String::Utf8Length(String* input, int from, int to) {
5949 if (from == to) return 0;
5950 int total = 0;
5951 while (true) {
5952 if (input->IsAsciiRepresentation()) return total + to - from;
5953 switch (StringShape(input).representation_tag()) {
5954 case kConsStringTag: {
5955 ConsString* str = ConsString::cast(input);
5956 String* first = str->first();
5957 String* second = str->second();
5958 int first_length = first->length();
5959 if (first_length - from < to - first_length) {
5960 if (first_length > from) {
5961 // Left hand side is shorter.
5962 total += Utf8Length(first, from, first_length);
5963 input = second;
5964 from = 0;
5965 to -= first_length;
5966 } else {
5967 // We only need the right hand side.
5968 input = second;
5969 from -= first_length;
5970 to -= first_length;
5971 }
5972 } else {
5973 if (first_length <= to) {
5974 // Right hand side is shorter.
5975 total += Utf8Length(second, 0, to - first_length);
5976 input = first;
5977 to = first_length;
5978 } else {
5979 // We only need the left hand side.
5980 input = first;
5981 }
5982 }
5983 continue;
5984 }
5985 case kExternalStringTag:
5986 case kSeqStringTag: {
5987 Vector<const uc16> vector = input->GetFlatContent().ToUC16Vector();
5988 const uc16* p = vector.start();
5989 for (int i = from; i < to; i++) {
5990 total += unibrow::Utf8::Length(p[i]);
5991 }
5992 return total;
5993 }
5994 case kSlicedStringTag: {
5995 SlicedString* str = SlicedString::cast(input);
5996 int offset = str->offset();
5997 input = str->parent();
5998 from += offset;
5999 to += offset;
6000 continue;
6001 }
6002 default:
6003 break;
6004 }
6005 UNREACHABLE();
6006 return 0;
6007 }
6008 return 0;
6009 }
6010
6011
5963 void Relocatable::PostGarbageCollectionProcessing() { 6012 void Relocatable::PostGarbageCollectionProcessing() {
5964 Isolate* isolate = Isolate::Current(); 6013 Isolate* isolate = Isolate::Current();
5965 Relocatable* current = isolate->relocatable_top(); 6014 Relocatable* current = isolate->relocatable_top();
5966 while (current != NULL) { 6015 while (current != NULL) {
5967 current->PostGarbageCollection(); 6016 current->PostGarbageCollection();
5968 current = current->prev_; 6017 current = current->prev_;
5969 } 6018 }
5970 } 6019 }
5971 6020
5972 6021
(...skipping 6281 matching lines...) Expand 10 before | Expand all | Expand 10 after
12254 if (break_point_objects()->IsUndefined()) return 0; 12303 if (break_point_objects()->IsUndefined()) return 0;
12255 // Single break point. 12304 // Single break point.
12256 if (!break_point_objects()->IsFixedArray()) return 1; 12305 if (!break_point_objects()->IsFixedArray()) return 1;
12257 // Multiple break points. 12306 // Multiple break points.
12258 return FixedArray::cast(break_point_objects())->length(); 12307 return FixedArray::cast(break_point_objects())->length();
12259 } 12308 }
12260 #endif // ENABLE_DEBUGGER_SUPPORT 12309 #endif // ENABLE_DEBUGGER_SUPPORT
12261 12310
12262 12311
12263 } } // namespace v8::internal 12312 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/objects.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698