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

Side by Side Diff: src/objects.cc

Issue 6991007: Don't flatten every time we call CharCodeAt Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Handle flat cons string. Flatten entire string. Created 9 years, 7 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
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 5164 matching lines...) Expand 10 before | Expand all | Expand 10 after
5175 } 5175 }
5176 } 5176 }
5177 5177
5178 5178
5179 uint16_t ConsString::ConsStringGet(int index) { 5179 uint16_t ConsString::ConsStringGet(int index) {
5180 ASSERT(index >= 0 && index < this->length()); 5180 ASSERT(index >= 0 && index < this->length());
5181 5181
5182 // Check for a flattened cons string 5182 // Check for a flattened cons string
5183 if (second()->length() == 0) { 5183 if (second()->length() == 0) {
5184 String* left = first(); 5184 String* left = first();
5185 return left->Get(index); 5185 return FlatString::cast(left)->FlatStringGet(index);
5186 } 5186 }
5187 5187
5188 String* string = String::cast(this); 5188 String* string = String::cast(this);
5189 5189
5190 while (true) { 5190 while (true) {
5191 if (StringShape(string).IsCons()) { 5191 if (StringShape(string).IsCons()) {
5192 ConsString* cons_string = ConsString::cast(string); 5192 ConsString* cons_string = ConsString::cast(string);
5193 String* left = cons_string->first(); 5193 String* left = cons_string->first();
5194 if (left->length() > index) { 5194 if (left->length() > index) {
5195 string = left; 5195 string = left;
5196 } else { 5196 } else {
5197 index -= left->length(); 5197 index -= left->length();
5198 string = cons_string->second(); 5198 string = cons_string->second();
5199 } 5199 }
5200 } else { 5200 } else {
5201 return string->Get(index); 5201 return FlatString::cast(string)->FlatStringGet(index);
5202 } 5202 }
5203 } 5203 }
5204 5204
5205 UNREACHABLE(); 5205 UNREACHABLE();
5206 return 0; 5206 return 0;
5207 } 5207 }
5208 5208
5209 5209
5210 MaybeObject* ConsString::ConsStringGetMayFlatten(int index) {
5211 ASSERT(index >= 0);
5212 ASSERT(index < this->length());
5213
5214 String* string = this;
5215 if (this->GetIsolate()->ShouldFlattenString()) {
5216 MaybeObject* flatten_result = this->TryFlatten();
5217 if (flatten_result->IsFailure()) return flatten_result;
5218 string = String::cast(flatten_result->ToObjectUnchecked());
5219 } else {
5220 int recurse_limit = 7;
Vitaly Repeshko 2011/05/12 15:54:53 Make this a (documented) constant in ConsString.
5221 String* cursor = string;
5222 int index_in_cursor = index;
5223 while (cursor->IsConsString()) {
5224 ConsString* cons = ConsString::cast(cursor);
5225 String* first = cons->first();
5226 int first_length = first->length();
5227 if (index_in_cursor < first_length) {
5228 cursor = first;
5229 } else {
5230 cursor = cons->second();
5231 index_in_cursor -= first_length;
5232 }
5233 if (--recurse_limit <= 0) {
5234 MaybeObject* flatten_result = this->TryFlatten();
5235 if (flatten_result->IsFailure()) return flatten_result;
5236 cursor = String::cast(flatten_result->ToObjectUnchecked());
Vitaly Repeshko 2011/05/12 15:54:53 It's more straightforward to simply return Smi::F
5237 index_in_cursor = index;
5238 break;
5239 }
5240 }
5241 string = cursor;
5242 index = index_in_cursor;
5243 }
5244 return Smi::FromInt(string->Get(index));
5245 }
5246
5247
5210 template <typename sinkchar> 5248 template <typename sinkchar>
5211 void String::WriteToFlat(String* src, 5249 void String::WriteToFlat(String* src,
5212 sinkchar* sink, 5250 sinkchar* sink,
5213 int f, 5251 int f,
5214 int t) { 5252 int t) {
5215 String* source = src; 5253 String* source = src;
5216 int from = f; 5254 int from = f;
5217 int to = t; 5255 int to = t;
5218 while (true) { 5256 while (true) {
5219 ASSERT(0 <= from && from <= to && to <= source->length()); 5257 ASSERT(0 <= from && from <= to && to <= source->length());
(...skipping 5202 matching lines...) Expand 10 before | Expand all | Expand 10 after
10422 if (break_point_objects()->IsUndefined()) return 0; 10460 if (break_point_objects()->IsUndefined()) return 0;
10423 // Single beak point. 10461 // Single beak point.
10424 if (!break_point_objects()->IsFixedArray()) return 1; 10462 if (!break_point_objects()->IsFixedArray()) return 1;
10425 // Multiple break points. 10463 // Multiple break points.
10426 return FixedArray::cast(break_point_objects())->length(); 10464 return FixedArray::cast(break_point_objects())->length();
10427 } 10465 }
10428 #endif 10466 #endif
10429 10467
10430 10468
10431 } } // namespace v8::internal 10469 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/objects.h ('k') | src/objects-inl.h » ('j') | src/objects-inl.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698