 Chromium Code Reviews
 Chromium Code Reviews Issue 7477045:
  Tentative implementation of string slices (hidden under the flag --string-slices).  (Closed) 
  Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
    
  
    Issue 7477045:
  Tentative implementation of string slices (hidden under the flag --string-slices).  (Closed) 
  Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge| Index: src/objects.cc | 
| diff --git a/src/objects.cc b/src/objects.cc | 
| index a423ae4c25df2caaff6ade6de2f2684f94286522..745305f8302a4b8c4f3d7baaa8c6542becf0de9b 100644 | 
| --- a/src/objects.cc | 
| +++ b/src/objects.cc | 
| @@ -733,62 +733,56 @@ MaybeObject* String::SlowTryFlatten(PretenureFlag pretenure) { | 
| #ifdef DEBUG | 
| // Do not attempt to flatten in debug mode when allocation is not | 
| // allowed. This is to avoid an assertion failure when allocating. | 
| - // Flattening strings is the only case where we always allow | 
| - // allocation because no GC is performed if the allocation fails. | 
| + // Flattening and truncating strings are the only cases where we always | 
| + // allow allocation because no GC is performed if the allocation fails. | 
| if (!HEAP->IsAllocationAllowed()) return this; | 
| #endif | 
| + ASSERT(StringShape(this).representation_tag() == kConsStringTag); | 
| + | 
| Heap* heap = GetHeap(); | 
| - switch (StringShape(this).representation_tag()) { | 
| - case kConsStringTag: { | 
| - ConsString* cs = ConsString::cast(this); | 
| - if (cs->second()->length() == 0) { | 
| - return cs->first(); | 
| - } | 
| - // There's little point in putting the flat string in new space if the | 
| - // cons string is in old space. It can never get GCed until there is | 
| - // an old space GC. | 
| - PretenureFlag tenure = heap->InNewSpace(this) ? pretenure : TENURED; | 
| - int len = length(); | 
| - Object* object; | 
| - String* result; | 
| - if (IsAsciiRepresentation()) { | 
| - { MaybeObject* maybe_object = heap->AllocateRawAsciiString(len, tenure); | 
| - if (!maybe_object->ToObject(&object)) return maybe_object; | 
| - } | 
| - result = String::cast(object); | 
| - String* first = cs->first(); | 
| - int first_length = first->length(); | 
| - char* dest = SeqAsciiString::cast(result)->GetChars(); | 
| - WriteToFlat(first, dest, 0, first_length); | 
| - String* second = cs->second(); | 
| - WriteToFlat(second, | 
| - dest + first_length, | 
| - 0, | 
| - len - first_length); | 
| - } else { | 
| - { MaybeObject* maybe_object = | 
| - heap->AllocateRawTwoByteString(len, tenure); | 
| - if (!maybe_object->ToObject(&object)) return maybe_object; | 
| - } | 
| - result = String::cast(object); | 
| - uc16* dest = SeqTwoByteString::cast(result)->GetChars(); | 
| - String* first = cs->first(); | 
| - int first_length = first->length(); | 
| - WriteToFlat(first, dest, 0, first_length); | 
| - String* second = cs->second(); | 
| - WriteToFlat(second, | 
| - dest + first_length, | 
| - 0, | 
| - len - first_length); | 
| - } | 
| - cs->set_first(result); | 
| - cs->set_second(heap->empty_string()); | 
| - return result; | 
| - } | 
| - default: | 
| - return this; | 
| - } | 
| + ConsString* cs = ConsString::cast(this); | 
| 
Vitaly Repeshko
2011/08/17 19:20:23
If there are no intended semantic changes here, pl
 | 
| + // There's little point in putting the flat string in new space if the | 
| + // cons string is in old space. It can never get GCed until there is | 
| + // an old space GC. | 
| + PretenureFlag tenure = heap->InNewSpace(this) ? pretenure : TENURED; | 
| + int len = length(); | 
| + Object* object; | 
| + String* result; | 
| + if (IsAsciiRepresentation()) { | 
| + { MaybeObject* maybe_object = heap->AllocateRawAsciiString(len, tenure); | 
| + if (!maybe_object->ToObject(&object)) return maybe_object; | 
| + } | 
| + result = String::cast(object); | 
| + String* first = cs->first(); | 
| + int first_length = first->length(); | 
| + char* dest = SeqAsciiString::cast(result)->GetChars(); | 
| + WriteToFlat(first, dest, 0, first_length); | 
| + String* second = cs->second(); | 
| + WriteToFlat(second, | 
| + dest + first_length, | 
| + 0, | 
| + len - first_length); | 
| + } else { | 
| + { MaybeObject* maybe_object = | 
| + heap->AllocateRawTwoByteString(len, tenure); | 
| + if (!maybe_object->ToObject(&object)) return maybe_object; | 
| + } | 
| + result = String::cast(object); | 
| + uc16* dest = SeqTwoByteString::cast(result)->GetChars(); | 
| + String* first = cs->first(); | 
| + int first_length = first->length(); | 
| + WriteToFlat(first, dest, 0, first_length); | 
| + String* second = cs->second(); | 
| + WriteToFlat(second, | 
| + dest + first_length, | 
| + 0, | 
| + len - first_length); | 
| + } | 
| + cs->set_first(result); | 
| + cs->set_second(heap->empty_string()); | 
| + ASSERT(this->IsFlat()); | 
| + return result; | 
| } | 
| @@ -1169,6 +1163,9 @@ void HeapObject::IterateBody(InstanceType type, int object_size, | 
| case kConsStringTag: | 
| ConsString::BodyDescriptor::IterateBody(this, v); | 
| break; | 
| + case kSlicedStringTag: | 
| + SlicedString::BodyDescriptor::IterateBody(this, v); | 
| + break; | 
| case kExternalStringTag: | 
| if ((type & kStringEncodingMask) == kAsciiStringTag) { | 
| reinterpret_cast<ExternalAsciiString*>(this)-> | 
| @@ -5245,15 +5242,22 @@ Vector<const char> String::ToAsciiVector() { | 
| ASSERT(cons->second()->length() == 0); | 
| string = cons->first(); | 
| string_tag = StringShape(string).representation_tag(); | 
| + ASSERT(string_tag != kConsStringTag); | 
| } | 
| - if (string_tag == kSeqStringTag) { | 
| - SeqAsciiString* seq = SeqAsciiString::cast(string); | 
| - char* start = seq->GetChars(); | 
| + if (string_tag == kSlicedStringTag) { | 
| + // Note that the parent of a slice cannot be a cons. | 
| 
Vitaly Repeshko
2011/08/17 19:20:23
... or a slice.
 | 
| + SlicedString* slice = SlicedString::cast(string); | 
| + offset = slice->offset(); | 
| + string = slice->parent(); | 
| 
Vitaly Repeshko
2011/08/17 19:20:23
string_tag also should be updated. Either add a TO
 | 
| + } | 
| + if (string_tag == kExternalStringTag) { | 
| + ExternalAsciiString* ext = ExternalAsciiString::cast(string); | 
| + const char* start = ext->resource()->data(); | 
| return Vector<const char>(start + offset, length); | 
| } | 
| - ASSERT(string_tag == kExternalStringTag); | 
| - ExternalAsciiString* ext = ExternalAsciiString::cast(string); | 
| - const char* start = ext->resource()->data(); | 
| + ASSERT(StringShape(string).representation_tag() == kSeqStringTag); | 
| + SeqAsciiString* seq = SeqAsciiString::cast(string); | 
| + char* start = seq->GetChars(); | 
| return Vector<const char>(start + offset, length); | 
| } | 
| @@ -5271,16 +5275,23 @@ Vector<const uc16> String::ToUC16Vector() { | 
| ASSERT(cons->second()->length() == 0); | 
| string = cons->first(); | 
| string_tag = StringShape(string).representation_tag(); | 
| + ASSERT(string_tag != kConsStringTag); | 
| + } | 
| + if (string_tag == kSlicedStringTag) { | 
| + // Note that the parent of a slice cannot be a cons. | 
| 
Vitaly Repeshko
2011/08/17 19:20:23
See above.
 | 
| + SlicedString* slice = SlicedString::cast(string); | 
| + offset = slice->offset(); | 
| + string = slice->parent(); | 
| } | 
| - if (string_tag == kSeqStringTag) { | 
| - SeqTwoByteString* seq = SeqTwoByteString::cast(string); | 
| - return Vector<const uc16>(seq->GetChars() + offset, length); | 
| + if (string_tag == kExternalStringTag) { | 
| + ExternalTwoByteString* ext = ExternalTwoByteString::cast(string); | 
| + const uc16* start = | 
| + reinterpret_cast<const uc16*>(ext->resource()->data()); | 
| + return Vector<const uc16>(start + offset, length); | 
| } | 
| - ASSERT(string_tag == kExternalStringTag); | 
| - ExternalTwoByteString* ext = ExternalTwoByteString::cast(string); | 
| - const uc16* start = | 
| - reinterpret_cast<const uc16*>(ext->resource()->data()); | 
| - return Vector<const uc16>(start + offset, length); | 
| + ASSERT(StringShape(string).representation_tag() == kSeqStringTag); | 
| + SeqTwoByteString* seq = SeqTwoByteString::cast(string); | 
| + return Vector<const uc16>(seq->GetChars() + offset, length); | 
| } | 
| @@ -5358,6 +5369,10 @@ const uc16* String::GetTwoByteData(unsigned start) { | 
| case kExternalStringTag: | 
| return ExternalTwoByteString::cast(this)-> | 
| ExternalTwoByteStringGetData(start); | 
| + case kSlicedStringTag: { | 
| + SlicedString* slice = SlicedString::cast(this); | 
| + return slice->parent()->GetTwoByteData(start + slice->offset()); | 
| + } | 
| case kConsStringTag: | 
| UNREACHABLE(); | 
| return NULL; | 
| @@ -5648,6 +5663,10 @@ const unibrow::byte* String::ReadBlock(String* input, | 
| max_chars); | 
| return rbb->util_buffer; | 
| } | 
| + case kSlicedStringTag: | 
| + return SlicedString::cast(input)->SlicedStringReadBlock(rbb, | 
| + offset_ptr, | 
| + max_chars); | 
| default: | 
| break; | 
| } | 
| @@ -5789,6 +5808,11 @@ void String::ReadBlockIntoBuffer(String* input, | 
| max_chars); | 
| } | 
| return; | 
| + case kSlicedStringTag: | 
| + SlicedString::cast(input)->SlicedStringReadBlockIntoBuffer(rbb, | 
| + offset_ptr, | 
| + max_chars); | 
| + return; | 
| default: | 
| break; | 
| } | 
| @@ -5923,6 +5947,31 @@ uint16_t ConsString::ConsStringGet(int index) { | 
| } | 
| +uint16_t SlicedString::SlicedStringGet(int index) { | 
| + return parent()->Get(offset() + index); | 
| +} | 
| + | 
| + | 
| +const unibrow::byte* SlicedString::SlicedStringReadBlock( | 
| + ReadBlockBuffer* buffer, unsigned* offset_ptr, unsigned chars) { | 
| + unsigned offset = this->offset(); | 
| + *offset_ptr += offset; | 
| + const unibrow::byte* answer = String::ReadBlock(String::cast(parent()), | 
| + buffer, offset_ptr, chars); | 
| + *offset_ptr -= offset; | 
| + return answer; | 
| +} | 
| + | 
| + | 
| +void SlicedString::SlicedStringReadBlockIntoBuffer( | 
| + ReadBlockBuffer* buffer, unsigned* offset_ptr, unsigned chars) { | 
| + unsigned offset = this->offset(); | 
| + *offset_ptr += offset; | 
| + String::ReadBlockIntoBuffer(String::cast(parent()), | 
| + buffer, offset_ptr, chars); | 
| + *offset_ptr -= offset; | 
| +} | 
| + | 
| template <typename sinkchar> | 
| void String::WriteToFlat(String* src, | 
| sinkchar* sink, | 
| @@ -5990,6 +6039,13 @@ void String::WriteToFlat(String* src, | 
| } | 
| break; | 
| } | 
| + case kAsciiStringTag | kSlicedStringTag: | 
| + case kTwoByteStringTag | kSlicedStringTag: { | 
| + SlicedString* slice = SlicedString::cast(source); | 
| + unsigned offset = slice->offset(); | 
| + WriteToFlat(slice->parent(), sink, from + offset, to + offset); | 
| + return; | 
| + } | 
| } | 
| } | 
| } |