Commit f02af745 authored by yangguo@chromium.org's avatar yangguo@chromium.org

Cleanup StringCharacterStream and add initial test cases.

BUG=

Review URL: https://chromiumcodereview.appspot.com/11438046
Patch from Dan Carney <dcarney@google.com>.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13189 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 72dfb279
...@@ -2521,14 +2521,12 @@ void String::Visit( ...@@ -2521,14 +2521,12 @@ void String::Visit(
String* string, String* string,
unsigned offset, unsigned offset,
Visitor& visitor, Visitor& visitor,
ConsOp& consOp, ConsOp& cons_op,
int32_t type, int32_t type,
unsigned length) { unsigned length) {
ASSERT(length == static_cast<unsigned>(string->length())); ASSERT(length == static_cast<unsigned>(string->length()));
ASSERT(offset <= length); ASSERT(offset <= length);
unsigned slice_offset = offset;
unsigned sliceOffset = offset;
while (true) { while (true) {
ASSERT(type == string->map()->instance_type()); ASSERT(type == string->map()->instance_type());
...@@ -2536,35 +2534,36 @@ void String::Visit( ...@@ -2536,35 +2534,36 @@ void String::Visit(
case kSeqStringTag | kOneByteStringTag: case kSeqStringTag | kOneByteStringTag:
visitor.VisitOneByteString( visitor.VisitOneByteString(
reinterpret_cast<const uint8_t*>( reinterpret_cast<const uint8_t*>(
SeqOneByteString::cast(string)->GetChars()) + sliceOffset, SeqOneByteString::cast(string)->GetChars()) + slice_offset,
length - offset); length - offset);
return; return;
case kSeqStringTag | kTwoByteStringTag: case kSeqStringTag | kTwoByteStringTag:
visitor.VisitTwoByteString( visitor.VisitTwoByteString(
reinterpret_cast<const uint16_t*>( reinterpret_cast<const uint16_t*>(
SeqTwoByteString::cast(string)->GetChars()) + sliceOffset, SeqTwoByteString::cast(string)->GetChars()) + slice_offset,
length - offset); length - offset);
return; return;
case kExternalStringTag | kOneByteStringTag: case kExternalStringTag | kOneByteStringTag:
visitor.VisitOneByteString( visitor.VisitOneByteString(
reinterpret_cast<const uint8_t*>( reinterpret_cast<const uint8_t*>(
ExternalAsciiString::cast(string)->GetChars()) + sliceOffset, ExternalAsciiString::cast(string)->GetChars()) + slice_offset,
length - offset); length - offset);
return; return;
case kExternalStringTag | kTwoByteStringTag: case kExternalStringTag | kTwoByteStringTag:
visitor.VisitTwoByteString( visitor.VisitTwoByteString(
reinterpret_cast<const uint16_t*>( reinterpret_cast<const uint16_t*>(
ExternalTwoByteString::cast(string)->GetChars()) + sliceOffset, ExternalTwoByteString::cast(string)->GetChars())
+ slice_offset,
length - offset); length - offset);
return; return;
case kSlicedStringTag | kOneByteStringTag: case kSlicedStringTag | kOneByteStringTag:
case kSlicedStringTag | kTwoByteStringTag: { case kSlicedStringTag | kTwoByteStringTag: {
SlicedString* slicedString = SlicedString::cast(string); SlicedString* slicedString = SlicedString::cast(string);
sliceOffset += slicedString->offset(); slice_offset += slicedString->offset();
string = slicedString->parent(); string = slicedString->parent();
type = string->map()->instance_type(); type = string->map()->instance_type();
continue; continue;
...@@ -2572,10 +2571,10 @@ void String::Visit( ...@@ -2572,10 +2571,10 @@ void String::Visit(
case kConsStringTag | kOneByteStringTag: case kConsStringTag | kOneByteStringTag:
case kConsStringTag | kTwoByteStringTag: case kConsStringTag | kTwoByteStringTag:
string = consOp.Operate(ConsString::cast(string), &offset, &type, string = cons_op.Operate(ConsString::cast(string), &offset, &type,
&length); &length);
if (string == NULL) return; if (string == NULL) return;
sliceOffset = offset; slice_offset = offset;
ASSERT(length == static_cast<unsigned>(string->length())); ASSERT(length == static_cast<unsigned>(string->length()));
continue; continue;
...@@ -2770,34 +2769,14 @@ unsigned ConsStringIteratorOp::OffsetForDepth(unsigned depth) { ...@@ -2770,34 +2769,14 @@ unsigned ConsStringIteratorOp::OffsetForDepth(unsigned depth) {
} }
uint32_t ConsStringIteratorOp::MaskForDepth(unsigned depth) {
return 1 << OffsetForDepth(depth);
}
void ConsStringIteratorOp::SetRightDescent() {
trace_ |= MaskForDepth(depth_ - 1);
}
void ConsStringIteratorOp::ClearRightDescent() {
trace_ &= ~MaskForDepth(depth_ - 1);
}
void ConsStringIteratorOp::PushLeft(ConsString* string) { void ConsStringIteratorOp::PushLeft(ConsString* string) {
frames_[depth_++ & kDepthMask] = string; frames_[depth_++ & kDepthMask] = string;
} }
void ConsStringIteratorOp::PushRight(ConsString* string, int32_t type) { void ConsStringIteratorOp::PushRight(ConsString* string) {
// Inplace update // Inplace update.
frames_[(depth_-1) & kDepthMask] = string; frames_[(depth_-1) & kDepthMask] = string;
if (depth_ != 1) return;
// Optimization: can replace root in this case.
root_ = string;
root_type_ = type;
root_length_ = string->length();
} }
...@@ -2814,8 +2793,8 @@ void ConsStringIteratorOp::Pop() { ...@@ -2814,8 +2793,8 @@ void ConsStringIteratorOp::Pop() {
void ConsStringIteratorOp::Reset() { void ConsStringIteratorOp::Reset() {
consumed_ = 0; depth_ = 0;
ResetStack(); maximum_depth_ = 0;
} }
...@@ -2824,19 +2803,13 @@ bool ConsStringIteratorOp::HasMore() { ...@@ -2824,19 +2803,13 @@ bool ConsStringIteratorOp::HasMore() {
} }
void ConsStringIteratorOp::ResetStack() {
depth_ = 0;
maximum_depth_ = 0;
}
bool ConsStringIteratorOp::ContinueOperation(ContinueResponse* response) { bool ConsStringIteratorOp::ContinueOperation(ContinueResponse* response) {
bool blewStack; bool blew_stack;
int32_t type; int32_t type;
String* string = NextLeaf(&blewStack, &type); unsigned length;
String* string = NextLeaf(&blew_stack, &type, &length);
// String found. // String found.
if (string != NULL) { if (string != NULL) {
unsigned length = string->length();
consumed_ += length; consumed_ += length;
response->string_ = string; response->string_ = string;
response->offset_ = 0; response->offset_ = 0;
...@@ -2845,9 +2818,11 @@ bool ConsStringIteratorOp::ContinueOperation(ContinueResponse* response) { ...@@ -2845,9 +2818,11 @@ bool ConsStringIteratorOp::ContinueOperation(ContinueResponse* response) {
return true; return true;
} }
// Traversal complete. // Traversal complete.
if (!blewStack) return false; if (!blew_stack) return false;
// Restart search. // Restart search.
ResetStack(); Reset();
// TODO(dcarney) This is unnecessary.
// After a reset, we don't need a String::Visit
response->string_ = root_; response->string_ = root_;
response->offset_ = consumed_; response->offset_ = consumed_;
response->length_ = root_length_; response->length_ = root_length_;
...@@ -2857,14 +2832,14 @@ bool ConsStringIteratorOp::ContinueOperation(ContinueResponse* response) { ...@@ -2857,14 +2832,14 @@ bool ConsStringIteratorOp::ContinueOperation(ContinueResponse* response) {
uint16_t StringCharacterStream::GetNext() { uint16_t StringCharacterStream::GetNext() {
ASSERT(buffer8_ != NULL); ASSERT((buffer8_ == NULL && end_ == NULL) || buffer8_ < end_);
return is_one_byte_ ? *buffer8_++ : *buffer16_++; return is_one_byte_ ? *buffer8_++ : *buffer16_++;
} }
StringCharacterStream::StringCharacterStream( StringCharacterStream::StringCharacterStream(
String* string, unsigned offset, ConsStringIteratorOp* op) String* string, unsigned offset, ConsStringIteratorOp* op)
: is_one_byte_(true), : is_one_byte_(false),
buffer8_(NULL), buffer8_(NULL),
end_(NULL), end_(NULL),
op_(op) { op_(op) {
...@@ -2878,11 +2853,7 @@ bool StringCharacterStream::HasMore() { ...@@ -2878,11 +2853,7 @@ bool StringCharacterStream::HasMore() {
if (buffer8_ != end_) return true; if (buffer8_ != end_) return true;
if (!op_->HasMore()) return false; if (!op_->HasMore()) return false;
ConsStringIteratorOp::ContinueResponse response; ConsStringIteratorOp::ContinueResponse response;
// This has been checked above if (!op_->ContinueOperation(&response)) return false;
if (!op_->ContinueOperation(&response)) {
UNREACHABLE();
return false;
}
String::Visit(response.string_, String::Visit(response.string_,
response.offset_, *this, *op_, response.type_, response.length_); response.offset_, *this, *op_, response.type_, response.length_);
return true; return true;
......
...@@ -7026,71 +7026,67 @@ void StringInputBuffer::Seek(unsigned pos) { ...@@ -7026,71 +7026,67 @@ void StringInputBuffer::Seek(unsigned pos) {
} }
String* ConsStringIteratorOp::Operate(ConsString* consString, String* ConsStringIteratorOp::Operate(ConsString* cons_string,
unsigned* outerOffset, int32_t* typeOut, unsigned* lengthOut) { unsigned* offset_out, int32_t* type_out, unsigned* length_out) {
ASSERT(*lengthOut == (unsigned)consString->length()); ASSERT(*length_out == (unsigned)cons_string->length());
ASSERT(depth_ == 0);
// Push the root string. // Push the root string.
PushLeft(consString); PushLeft(cons_string);
root_ = consString; root_ = cons_string;
root_type_ = *typeOut; root_type_ = *type_out;
root_length_ = *lengthOut; root_length_ = *length_out;
unsigned targetOffset = *outerOffset; consumed_ = *offset_out;
unsigned targetOffset = *offset_out;
unsigned offset = 0; unsigned offset = 0;
while (true) { while (true) {
// Loop until the string is found which contains the target offset. // Loop until the string is found which contains the target offset.
String* string = consString->first(); String* string = cons_string->first();
unsigned length = string->length(); unsigned length = string->length();
int32_t type; int32_t type;
if (targetOffset < offset + length) { if (targetOffset < offset + length) {
// Target offset is in the left branch. // Target offset is in the left branch.
// Mark the descent.
ClearRightDescent();
// Keep going if we're still in a ConString. // Keep going if we're still in a ConString.
type = string->map()->instance_type(); type = string->map()->instance_type();
if ((type & kStringRepresentationMask) == kConsStringTag) { if ((type & kStringRepresentationMask) == kConsStringTag) {
consString = ConsString::cast(string); cons_string = ConsString::cast(string);
PushLeft(consString); PushLeft(cons_string);
continue; continue;
} }
// Tell the stack we're done decending.
AdjustMaximumDepth();
} else { } else {
// Descend right. // Descend right.
// Update progress through the string. // Update progress through the string.
offset += length; offset += length;
// Keep going if we're still in a ConString. // Keep going if we're still in a ConString.
string = consString->second(); string = cons_string->second();
type = string->map()->instance_type(); type = string->map()->instance_type();
if ((type & kStringRepresentationMask) == kConsStringTag) { if ((type & kStringRepresentationMask) == kConsStringTag) {
consString = ConsString::cast(string); cons_string = ConsString::cast(string);
PushRight(consString, type); PushRight(cons_string);
// TODO(dcarney) Add back root optimization.
continue; continue;
} }
// Mark the descent.
SetRightDescent();
// Need this to be updated for the current string. // Need this to be updated for the current string.
length = string->length(); length = string->length();
// Account for the possibility of an empty right leaf. // Account for the possibility of an empty right leaf.
while (length == 0) { // This happens only if we have asked for an offset outside the string.
bool blewStack; if (length == 0) {
// Need to adjust maximum depth for NextLeaf to work. Reset();
AdjustMaximumDepth();
string = NextLeaf(&blewStack, &type);
if (string == NULL) {
// Luckily, this case is impossible.
ASSERT(!blewStack);
return NULL; return NULL;
} }
length = string->length();
}
}
// Tell the stack we're done decending. // Tell the stack we're done decending.
AdjustMaximumDepth(); AdjustMaximumDepth();
// Pop stack so next iteration is in correct place.
Pop();
}
ASSERT(length != 0); ASSERT(length != 0);
// Adjust return values and exit. // Adjust return values and exit.
unsigned innerOffset = targetOffset - offset; unsigned innerOffset = targetOffset - offset;
consumed_ += length - innerOffset; consumed_ += length - innerOffset;
*outerOffset = innerOffset; *offset_out = innerOffset;
*typeOut = type; *type_out = type;
*lengthOut = length; *length_out = length;
return string; return string;
} }
UNREACHABLE(); UNREACHABLE();
...@@ -7098,52 +7094,49 @@ String* ConsStringIteratorOp::Operate(ConsString* consString, ...@@ -7098,52 +7094,49 @@ String* ConsStringIteratorOp::Operate(ConsString* consString,
} }
String* ConsStringIteratorOp::NextLeaf(bool* blewStack, int32_t* typeOut) { String* ConsStringIteratorOp::NextLeaf(
bool* blew_stack, int32_t* type_out, unsigned* length_out) {
while (true) { while (true) {
// Tree traversal complete. // Tree traversal complete.
if (depth_ == 0) { if (depth_ == 0) {
*blewStack = false; *blew_stack = false;
return NULL; return NULL;
} }
// We've lost track of higher nodes. // We've lost track of higher nodes.
if (maximum_depth_ - depth_ == kStackSize) { if (maximum_depth_ - depth_ == kStackSize) {
*blewStack = true; *blew_stack = true;
return NULL; return NULL;
} }
// Check if we're done with this level.
bool haveAlreadyReadRight = trace_ & MaskForDepth(depth_ - 1);
if (haveAlreadyReadRight) {
Pop();
continue;
}
// Go right. // Go right.
ConsString* consString = frames_[OffsetForDepth(depth_ - 1)]; ConsString* cons_string = frames_[OffsetForDepth(depth_ - 1)];
String* string = consString->second(); String* string = cons_string->second();
int32_t type = string->map()->instance_type(); int32_t type = string->map()->instance_type();
if ((type & kStringRepresentationMask) != kConsStringTag) { if ((type & kStringRepresentationMask) != kConsStringTag) {
// Don't need to mark the descent here.
// Pop stack so next iteration is in correct place. // Pop stack so next iteration is in correct place.
Pop(); Pop();
*typeOut = type; unsigned length = (unsigned) string->length();
// Could be a flattened ConsString.
if (length == 0) continue;
*length_out = length;
*type_out = type;
return string; return string;
} }
// No need to mark the descent. cons_string = ConsString::cast(string);
consString = ConsString::cast(string); // TODO(dcarney) Add back root optimization.
PushRight(consString, type); PushRight(cons_string);
// Need to traverse all the way left. // Need to traverse all the way left.
while (true) { while (true) {
// Continue left. // Continue left.
// Update marker. string = cons_string->first();
ClearRightDescent();
string = consString->first();
type = string->map()->instance_type(); type = string->map()->instance_type();
if ((type & kStringRepresentationMask) != kConsStringTag) { if ((type & kStringRepresentationMask) != kConsStringTag) {
AdjustMaximumDepth(); AdjustMaximumDepth();
*typeOut = type; *type_out = type;
*length_out = string->length();
return string; return string;
} }
consString = ConsString::cast(string); cons_string = ConsString::cast(string);
PushLeft(consString); PushLeft(cons_string);
} }
} }
UNREACHABLE(); UNREACHABLE();
......
...@@ -7514,7 +7514,7 @@ class String: public HeapObject { ...@@ -7514,7 +7514,7 @@ class String: public HeapObject {
static inline void Visit(String* string, static inline void Visit(String* string,
unsigned offset, unsigned offset,
Visitor& visitor, Visitor& visitor,
ConsOp& consOp, ConsOp& cons_op,
int32_t type, int32_t type,
unsigned length); unsigned length);
...@@ -7985,8 +7985,8 @@ class ConsStringIteratorOp { ...@@ -7985,8 +7985,8 @@ class ConsStringIteratorOp {
int32_t type_; int32_t type_;
}; };
inline ConsStringIteratorOp() {} inline ConsStringIteratorOp() {}
String* Operate(ConsString* consString, unsigned* outerOffset, String* Operate(ConsString* cons_string, unsigned* offset_out,
int32_t* typeOut, unsigned* lengthOut); int32_t* type_out, unsigned* length_out);
inline bool ContinueOperation(ContinueResponse* response); inline bool ContinueOperation(ContinueResponse* response);
inline void Reset(); inline void Reset();
inline bool HasMore(); inline bool HasMore();
...@@ -7998,20 +7998,17 @@ class ConsStringIteratorOp { ...@@ -7998,20 +7998,17 @@ class ConsStringIteratorOp {
static const unsigned kDepthMask = kStackSize-1; static const unsigned kDepthMask = kStackSize-1;
STATIC_ASSERT(IS_POWER_OF_TWO(kStackSize)); STATIC_ASSERT(IS_POWER_OF_TWO(kStackSize));
static inline unsigned OffsetForDepth(unsigned depth); static inline unsigned OffsetForDepth(unsigned depth);
static inline uint32_t MaskForDepth(unsigned depth);
inline void ClearRightDescent();
inline void SetRightDescent();
inline void PushLeft(ConsString* string); inline void PushLeft(ConsString* string);
inline void PushRight(ConsString* string, int32_t type); inline void PushRight(ConsString* string);
inline void AdjustMaximumDepth(); inline void AdjustMaximumDepth();
inline void Pop(); inline void Pop();
inline void ResetStack(); String* NextLeaf(bool* blew_stack, int32_t* type_out, unsigned* length_out);
String* NextLeaf(bool* blewStack, int32_t* typeOut);
unsigned depth_; unsigned depth_;
unsigned maximum_depth_; unsigned maximum_depth_;
uint32_t trace_; // Stack must always contain only frames for which right traversal
// has not yet been performed.
ConsString* frames_[kStackSize]; ConsString* frames_[kStackSize];
unsigned consumed_; unsigned consumed_;
ConsString* root_; ConsString* root_;
......
...@@ -15,16 +15,57 @@ ...@@ -15,16 +15,57 @@
#include "cctest.h" #include "cctest.h"
#include "zone-inl.h" #include "zone-inl.h"
unsigned int seed = 123; // Adapted from http://en.wikipedia.org/wiki/Multiply-with-carry
class RandomNumberGenerator {
public:
RandomNumberGenerator() {
init();
}
static uint32_t gen() { void init(uint32_t seed = 0x5688c73e) {
uint64_t z; static const uint32_t phi = 0x9e3779b9;
z = seed; c = 362436;
z *= 279470273; i = kQSize-1;
z %= 4294967291U; Q[0] = seed;
seed = static_cast<unsigned int>(z); Q[1] = seed + phi;
return static_cast<uint32_t>(seed >> 16); Q[2] = seed + phi + phi;
} for (unsigned j = 3; j < kQSize; j++) {
Q[j] = Q[j - 3] ^ Q[j - 2] ^ phi ^ j;
}
}
uint32_t next() {
uint64_t a = 18782;
uint32_t r = 0xfffffffe;
i = (i + 1) & (kQSize-1);
uint64_t t = a * Q[i] + c;
c = (t >> 32);
uint32_t x = t + c;
if (x < c) {
x++;
c++;
}
return (Q[i] = r - x);
}
uint32_t next(int max) {
return next() % max;
}
bool next(double threshold) {
ASSERT(threshold >= 0.0 && threshold <= 1.0);
if (threshold == 1.0) return true;
if (threshold == 0.0) return false;
uint32_t value = next() % 100000;
return threshold > static_cast<double>(value)/100000.0;
}
private:
static const uint32_t kQSize = 4096;
uint32_t Q[kQSize];
uint32_t c;
uint32_t i;
};
using namespace v8::internal; using namespace v8::internal;
...@@ -44,7 +85,7 @@ static void InitializeVM() { ...@@ -44,7 +85,7 @@ static void InitializeVM() {
} }
static const int NUMBER_OF_BUILDING_BLOCKS = 128; static const int NUMBER_OF_BUILDING_BLOCKS = 256;
static const int DEEP_DEPTH = 8 * 1024; static const int DEEP_DEPTH = 8 * 1024;
static const int SUPER_DEEP_DEPTH = 80 * 1024; static const int SUPER_DEEP_DEPTH = 80 * 1024;
...@@ -79,21 +120,42 @@ class AsciiResource: public v8::String::ExternalAsciiStringResource, ...@@ -79,21 +120,42 @@ class AsciiResource: public v8::String::ExternalAsciiStringResource,
}; };
static void InitializeBuildingBlocks( static void InitializeBuildingBlocks(Handle<String>* building_blocks,
Handle<String> building_blocks[NUMBER_OF_BUILDING_BLOCKS]) { int bb_length,
bool long_blocks,
RandomNumberGenerator* rng) {
// A list of pointers that we don't have any interest in cleaning up. // A list of pointers that we don't have any interest in cleaning up.
// If they are reachable from a root then leak detection won't complain. // If they are reachable from a root then leak detection won't complain.
Zone* zone = Isolate::Current()->runtime_zone(); Zone* zone = Isolate::Current()->runtime_zone();
for (int i = 0; i < NUMBER_OF_BUILDING_BLOCKS; i++) { for (int i = 0; i < bb_length; i++) {
int len = gen() % 16; int len = rng->next(16);
if (len > 14) { int slice_head_chars = 0;
int slice_tail_chars = 0;
int slice_depth = 0;
for (int j = 0; j < 3; j++) {
if (rng->next(0.35)) slice_depth++;
}
// Must truncate something for a slice string. Loop until
// at least one end will be sliced.
while (slice_head_chars == 0 && slice_tail_chars == 0) {
slice_head_chars = rng->next(15);
slice_tail_chars = rng->next(12);
}
if (long_blocks) {
// Generate building blocks which will never be merged
len += ConsString::kMinLength + 1;
} else if (len > 14) {
len += 1234; len += 1234;
} }
switch (gen() % 4) { // Don't slice 0 length strings.
if (len == 0) slice_depth = 0;
int slice_length = slice_depth*(slice_head_chars + slice_tail_chars);
len += slice_length;
switch (rng->next(4)) {
case 0: { case 0: {
uc16 buf[2000]; uc16 buf[2000];
for (int j = 0; j < len; j++) { for (int j = 0; j < len; j++) {
buf[j] = gen() % 65536; buf[j] = rng->next(0x10000);
} }
building_blocks[i] = building_blocks[i] =
FACTORY->NewStringFromTwoByte(Vector<const uc16>(buf, len)); FACTORY->NewStringFromTwoByte(Vector<const uc16>(buf, len));
...@@ -105,7 +167,7 @@ static void InitializeBuildingBlocks( ...@@ -105,7 +167,7 @@ static void InitializeBuildingBlocks(
case 1: { case 1: {
char buf[2000]; char buf[2000];
for (int j = 0; j < len; j++) { for (int j = 0; j < len; j++) {
buf[j] = gen() % 128; buf[j] = rng->next(0x80);
} }
building_blocks[i] = building_blocks[i] =
FACTORY->NewStringFromAscii(Vector<const char>(buf, len)); FACTORY->NewStringFromAscii(Vector<const char>(buf, len));
...@@ -117,7 +179,7 @@ static void InitializeBuildingBlocks( ...@@ -117,7 +179,7 @@ static void InitializeBuildingBlocks(
case 2: { case 2: {
uc16* buf = zone->NewArray<uc16>(len); uc16* buf = zone->NewArray<uc16>(len);
for (int j = 0; j < len; j++) { for (int j = 0; j < len; j++) {
buf[j] = gen() % 65536; buf[j] = rng->next(0x10000);
} }
Resource* resource = new(zone) Resource(Vector<const uc16>(buf, len)); Resource* resource = new(zone) Resource(Vector<const uc16>(buf, len));
building_blocks[i] = FACTORY->NewExternalStringFromTwoByte(resource); building_blocks[i] = FACTORY->NewExternalStringFromTwoByte(resource);
...@@ -127,19 +189,26 @@ static void InitializeBuildingBlocks( ...@@ -127,19 +189,26 @@ static void InitializeBuildingBlocks(
break; break;
} }
case 3: { case 3: {
char* buf = NewArray<char>(len); char* buf = zone->NewArray<char>(len);
for (int j = 0; j < len; j++) { for (int j = 0; j < len; j++) {
buf[j] = gen() % 128; buf[j] = rng->next(128);
} }
building_blocks[i] = AsciiResource* resource =
FACTORY->NewStringFromAscii(Vector<const char>(buf, len)); new(zone) AsciiResource(Vector<const char>(buf, len));
building_blocks[i] = FACTORY->NewExternalStringFromAscii(resource);
for (int j = 0; j < len; j++) { for (int j = 0; j < len; j++) {
CHECK_EQ(buf[j], building_blocks[i]->Get(j)); CHECK_EQ(buf[j], building_blocks[i]->Get(j));
} }
DeleteArray<char>(buf);
break; break;
} }
} }
for (int j = slice_depth; j > 0; j--) {
building_blocks[i] = FACTORY->NewSubString(
building_blocks[i],
slice_head_chars,
building_blocks[i]->length() - slice_tail_chars);
}
CHECK(len == building_blocks[i]->length() + slice_length);
} }
} }
...@@ -198,18 +267,27 @@ static Handle<String> ConstructBalanced( ...@@ -198,18 +267,27 @@ static Handle<String> ConstructBalanced(
static StringInputBuffer buffer; static StringInputBuffer buffer;
static ConsStringIteratorOp cons_string_iterator_op_1;
static ConsStringIteratorOp cons_string_iterator_op_2;
static void Traverse(Handle<String> s1, Handle<String> s2) { static void Traverse(Handle<String> s1, Handle<String> s2) {
int i = 0; int i = 0;
buffer.Reset(*s1); buffer.Reset(*s1);
StringCharacterStream character_stream_1(*s1, 0, &cons_string_iterator_op_1);
StringCharacterStream character_stream_2(*s2, 0, &cons_string_iterator_op_2);
StringInputBuffer buffer2(*s2); StringInputBuffer buffer2(*s2);
while (buffer.has_more()) { while (buffer.has_more()) {
CHECK(buffer2.has_more()); CHECK(buffer2.has_more());
CHECK(character_stream_1.HasMore());
CHECK(character_stream_2.HasMore());
uint16_t c = buffer.GetNext(); uint16_t c = buffer.GetNext();
CHECK_EQ(c, buffer2.GetNext()); CHECK_EQ(c, buffer2.GetNext());
CHECK_EQ(c, character_stream_1.GetNext());
CHECK_EQ(c, character_stream_2.GetNext());
i++; i++;
} }
CHECK(!character_stream_1.HasMore());
CHECK(!character_stream_2.HasMore());
CHECK_EQ(s1->length(), i); CHECK_EQ(s1->length(), i);
CHECK_EQ(s2->length(), i); CHECK_EQ(s2->length(), i);
} }
...@@ -219,10 +297,16 @@ static void TraverseFirst(Handle<String> s1, Handle<String> s2, int chars) { ...@@ -219,10 +297,16 @@ static void TraverseFirst(Handle<String> s1, Handle<String> s2, int chars) {
int i = 0; int i = 0;
buffer.Reset(*s1); buffer.Reset(*s1);
StringInputBuffer buffer2(*s2); StringInputBuffer buffer2(*s2);
StringCharacterStream character_stream_1(*s1, 0, &cons_string_iterator_op_1);
StringCharacterStream character_stream_2(*s2, 0, &cons_string_iterator_op_2);
while (buffer.has_more() && i < chars) { while (buffer.has_more() && i < chars) {
CHECK(buffer2.has_more()); CHECK(buffer2.has_more());
CHECK(character_stream_1.HasMore());
CHECK(character_stream_2.HasMore());
uint16_t c = buffer.GetNext(); uint16_t c = buffer.GetNext();
CHECK_EQ(c, buffer2.GetNext()); CHECK_EQ(c, buffer2.GetNext());
CHECK_EQ(c, character_stream_1.GetNext());
CHECK_EQ(c, character_stream_2.GetNext());
i++; i++;
} }
s1->Get(s1->length() - 1); s1->Get(s1->length() - 1);
...@@ -236,7 +320,10 @@ TEST(Traverse) { ...@@ -236,7 +320,10 @@ TEST(Traverse) {
v8::HandleScope scope; v8::HandleScope scope;
Handle<String> building_blocks[NUMBER_OF_BUILDING_BLOCKS]; Handle<String> building_blocks[NUMBER_OF_BUILDING_BLOCKS];
ZoneScope zone(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); ZoneScope zone(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT);
InitializeBuildingBlocks(building_blocks); RandomNumberGenerator rng;
rng.init();
InitializeBuildingBlocks(
building_blocks, NUMBER_OF_BUILDING_BLOCKS, false, &rng);
Handle<String> flat = ConstructBalanced(building_blocks); Handle<String> flat = ConstructBalanced(building_blocks);
FlattenString(flat); FlattenString(flat);
Handle<String> left_asymmetric = ConstructLeft(building_blocks, DEEP_DEPTH); Handle<String> left_asymmetric = ConstructLeft(building_blocks, DEEP_DEPTH);
...@@ -275,6 +362,324 @@ TEST(Traverse) { ...@@ -275,6 +362,324 @@ TEST(Traverse) {
} }
class ConsStringStats {
public:
ConsStringStats() {
Reset();
}
void Reset();
void VerifyEqual(const ConsStringStats& that) const;
unsigned leaves_;
unsigned empty_leaves_;
unsigned chars_;
unsigned left_traversals_;
unsigned right_traversals_;
private:
DISALLOW_COPY_AND_ASSIGN(ConsStringStats);
};
void ConsStringStats::Reset() {
leaves_ = 0;
empty_leaves_ = 0;
chars_ = 0;
left_traversals_ = 0;
right_traversals_ = 0;
}
void ConsStringStats::VerifyEqual(const ConsStringStats& that) const {
CHECK(this->leaves_ == that.leaves_);
CHECK(this->empty_leaves_ == that.empty_leaves_);
CHECK(this->chars_ == that.chars_);
CHECK(this->left_traversals_ == that.left_traversals_);
CHECK(this->right_traversals_ == that.right_traversals_);
}
class ConsStringGenerationData {
public:
ConsStringGenerationData();
void Reset();
// Input variables.
double early_termination_threshold_;
double leftness_;
double rightness_;
double empty_leaf_threshold_;
unsigned max_leaves_;
// Cached data.
Handle<String> building_blocks_[NUMBER_OF_BUILDING_BLOCKS];
String* empty_string_;
RandomNumberGenerator rng_;
// Stats.
ConsStringStats stats_;
unsigned early_terminations_;
private:
DISALLOW_COPY_AND_ASSIGN(ConsStringGenerationData);
};
ConsStringGenerationData::ConsStringGenerationData() {
rng_.init();
InitializeBuildingBlocks(
building_blocks_, NUMBER_OF_BUILDING_BLOCKS, true, &rng_);
empty_string_ = Isolate::Current()->heap()->empty_string();
Reset();
}
void ConsStringGenerationData::Reset() {
early_termination_threshold_ = 0.01;
leftness_ = 0.75;
rightness_ = 0.75;
empty_leaf_threshold_ = 0.02;
max_leaves_ = 1000;
stats_.Reset();
early_terminations_ = 0;
}
void VerifyConsString(ConsString* cons_string, ConsStringStats* stats) {
int left_length = cons_string->first()->length();
int right_length = cons_string->second()->length();
CHECK(cons_string->length() == left_length + right_length);
// Check left side.
if (cons_string->first()->IsConsString()) {
stats->left_traversals_++;
VerifyConsString(ConsString::cast(cons_string->first()), stats);
} else {
CHECK_NE(left_length, 0);
stats->leaves_++;
stats->chars_ += left_length;
}
// Check right side.
if (cons_string->second()->IsConsString()) {
stats->right_traversals_++;
VerifyConsString(ConsString::cast(cons_string->second()), stats);
} else {
if (right_length == 0) stats->empty_leaves_++;
stats->leaves_++;
stats->chars_ += right_length;
}
}
void VerifyConsStringWithOperator(
ConsString* cons_string, ConsStringStats* stats) {
// Init op.
ConsStringIteratorOp op;
op.Reset();
// Use response for initial search and on blown stack.
ConsStringIteratorOp::ContinueResponse response;
response.string_ = cons_string;
response.offset_ = 0;
response.type_ = cons_string->map()->instance_type();
response.length_ = (uint32_t) cons_string->length();
while (true) {
String* string = op.Operate(ConsString::cast(response.string_),
&response.offset_,
&response.type_,
&response.length_);
CHECK(string != NULL);
while (true) {
// Accumulate stats.
stats->leaves_++;
stats->chars_ += string->length();
// Check for completion.
bool keep_going_fast_check = op.HasMore();
bool keep_going = op.ContinueOperation(&response);
if (!keep_going) return;
// Verify no false positives for fast check.
CHECK(keep_going_fast_check);
CHECK(response.string_ != NULL);
// Blew stack. Restart outer loop.
if (response.string_->IsConsString()) break;
string = response.string_;
}
};
}
void VerifyConsString(Handle<String> root, ConsStringGenerationData* data) {
// Verify basic data.
CHECK(root->IsConsString());
CHECK((unsigned)root->length() == data->stats_.chars_);
// Recursive verify.
ConsStringStats stats;
VerifyConsString(ConsString::cast(*root), &stats);
stats.VerifyEqual(data->stats_);
// Iteratively verify.
stats.Reset();
VerifyConsStringWithOperator(ConsString::cast(*root), &stats);
// Don't see these. Must copy over.
stats.empty_leaves_ = data->stats_.empty_leaves_;
stats.left_traversals_ = data->stats_.left_traversals_;
stats.right_traversals_ = data->stats_.right_traversals_;
// Adjust total leaves to compensate.
stats.leaves_ += stats.empty_leaves_;
stats.VerifyEqual(data->stats_);
}
static Handle<String> ConstructRandomString(ConsStringGenerationData* data,
unsigned max_recursion) {
// Compute termination characteristics.
bool terminate = false;
bool flat = data->rng_.next(data->empty_leaf_threshold_);
bool terminate_early = data->rng_.next(data->early_termination_threshold_);
if (terminate_early) data->early_terminations_++;
// The obvious condition.
terminate |= max_recursion == 0;
// Flat cons string terminate by definition.
terminate |= flat;
// Cap for max leaves.
terminate |= data->stats_.leaves_ >= data->max_leaves_;
// Roll the dice.
terminate |= terminate_early;
// Compute termination characteristics for each side.
bool terminate_left = terminate || !data->rng_.next(data->leftness_);
bool terminate_right = terminate || !data->rng_.next(data->rightness_);
// Generate left string.
Handle<String> left;
if (terminate_left) {
left = data->building_blocks_[data->rng_.next(NUMBER_OF_BUILDING_BLOCKS)];
data->stats_.leaves_++;
data->stats_.chars_ += left->length();
} else {
left = ConstructRandomString(data, max_recursion - 1);
data->stats_.left_traversals_++;
}
// Generate right string.
Handle<String> right;
if (terminate_right) {
right = data->building_blocks_[data->rng_.next(NUMBER_OF_BUILDING_BLOCKS)];
data->stats_.leaves_++;
data->stats_.chars_ += right->length();
} else {
right = ConstructRandomString(data, max_recursion - 1);
data->stats_.right_traversals_++;
}
// Build the cons string.
Handle<String> root = FACTORY->NewConsString(left, right);
CHECK(root->IsConsString() && !root->IsFlat());
// Special work needed for flat string.
if (flat) {
data->stats_.empty_leaves_++;
FlattenString(root);
CHECK(root->IsConsString() && root->IsFlat());
}
return root;
}
static const int kCharacterStreamRandomCases = 150;
static const int kCharacterStreamEdgeCases =
kCharacterStreamRandomCases + 5;
static Handle<String> BuildConsStrings(int testCase,
ConsStringGenerationData* data) {
// For random constructions, need to reset the generator.
data->rng_.init();
for (int j = 0; j < testCase * 50; j++) {
data->rng_.next();
}
Handle<String> string;
switch (testCase) {
case 0:
return ConstructBalanced(data->building_blocks_);
case 1:
return ConstructLeft(data->building_blocks_, DEEP_DEPTH);
case 2:
return ConstructRight(data->building_blocks_, DEEP_DEPTH);
case 3:
return ConstructLeft(data->building_blocks_, 10);
case 4:
return ConstructRight(data->building_blocks_, 10);
case 5:
return FACTORY->NewConsString(
data->building_blocks_[0], data->building_blocks_[1]);
default:
if (testCase >= kCharacterStreamEdgeCases) {
CHECK(false);
return string;
}
// Random test case.
data->Reset();
string = ConstructRandomString(data, 200);
AssertNoAllocation no_alloc;
VerifyConsString(string, data);
#ifdef DEBUG
printf(
"%s: [%d], %s: [%d], %s: [%d], %s: [%d], %s: [%d], %s: [%d]\n",
"leaves", data->stats_.leaves_,
"empty", data->stats_.empty_leaves_,
"chars", data->stats_.chars_,
"lefts", data->stats_.left_traversals_,
"rights", data->stats_.right_traversals_,
"early_terminations", data->early_terminations_);
#endif
return string;
}
}
static void VerifyCharacterStream(
String* flat_string, String* cons_string) {
// Do not want to test ConString traversal on flat string.
CHECK(flat_string->IsFlat());
CHECK(!flat_string->IsConsString());
CHECK(cons_string->IsConsString());
// TODO(dcarney) Test stream reset as well.
int length = flat_string->length();
// Iterate start search in multiple places in the string.
int outer_iterations = length > 20 ? 20 : length;
for (int j = 0; j <= outer_iterations; j++) {
int offset = static_cast<double>(length)*j/outer_iterations;
if (offset < 0) offset = 0;
// Want to test the offset == length case.
if (offset > length) offset = length;
StringCharacterStream flat_stream(
flat_string, (unsigned) offset, &cons_string_iterator_op_1);
StringCharacterStream cons_stream(
cons_string, (unsigned) offset, &cons_string_iterator_op_2);
for (int i = offset; i < length; i++) {
uint16_t c = flat_string->Get(i);
CHECK(flat_stream.HasMore());
CHECK(cons_stream.HasMore());
CHECK_EQ(c, flat_stream.GetNext());
CHECK_EQ(c, cons_stream.GetNext());
}
CHECK(!flat_stream.HasMore());
CHECK(!cons_stream.HasMore());
}
}
TEST(StringCharacterStreamEdgeCases) {
printf("TestStringCharacterStreamEdgeCases\n");
InitializeVM();
Isolate* isolate = Isolate::Current();
HandleScope outer_scope(isolate);
ZoneScope zone(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT);
ConsStringGenerationData data;
for (int i = 0; i < kCharacterStreamEdgeCases; i++) {
printf("%d\n", i);
isolate->heap()->CollectAllGarbage(
Heap::kNoGCFlags, "must not allocate in loop");
AlwaysAllocateScope always_allocate;
HandleScope inner_scope(isolate);
Handle<String> cons_string = BuildConsStrings(i, &data);
Handle<String> flat_string = BuildConsStrings(i, &data);
FlattenString(flat_string);
AssertNoAllocation no_alloc;
CHECK(flat_string->IsConsString() && flat_string->IsFlat());
VerifyCharacterStream(ConsString::cast(*flat_string)->first(),
*cons_string);
}
}
static const int DEEP_ASCII_DEPTH = 100000; static const int DEEP_ASCII_DEPTH = 100000;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment