Browse Source

Try out Flatten API

Speeds up WriteUtf8 significantly when dealing with strings made by the
concatenation of many others.
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
38041fcaa0
  1. 7
      deps/v8/include/v8.h
  2. 7
      deps/v8/src/api.cc
  3. 25
      deps/v8/test/cctest/test-strings.cc
  4. 1
      src/node.cc
  5. 2
      src/node_buffer.cc

7
deps/v8/include/v8.h

@ -865,6 +865,13 @@ class V8EXPORT String : public Primitive {
int length = -1, int length = -1,
int* nchars = NULL) const; // UTF-8 int* nchars = NULL) const; // UTF-8
/**
* Flatten internal memory. Operations on the string tend to run faster
* after flattening especially if the string is a concatenation of many
* others.
*/
void Flatten();
/** /**
* A zero length string. * A zero length string.
*/ */

7
deps/v8/src/api.cc

@ -2731,6 +2731,13 @@ int String::Write(uint16_t* buffer, int start, int length) const {
} }
void v8::String::Flatten() {
EnsureInitialized("v8::String::Flatten()");
i::Handle<i::String> str = Utils::OpenHandle(this);
i::FlattenString(str);
}
bool v8::String::IsExternal() const { bool v8::String::IsExternal() const {
EnsureInitialized("v8::String::IsExternal()"); EnsureInitialized("v8::String::IsExternal()");
i::Handle<i::String> str = Utils::OpenHandle(this); i::Handle<i::String> str = Utils::OpenHandle(this);

25
deps/v8/test/cctest/test-strings.cc

@ -347,6 +347,31 @@ TEST(Utf8Conversion) {
} }
TEST(StringConcatFlatten) {
InitializeVM();
v8::HandleScope handle_scope;
const char* stringA = "abc";
const char* stringB = "def";
v8::Local<v8::String> a = v8::String::New(stringA);
v8::Local<v8::String> b = v8::String::New(stringB);
v8::Local<v8::String> cons = v8::String::Concat(a,b);
cons->Flatten();
char buffer[7];
cons->WriteUtf8(buffer);
int i;
for (i = 0; i < 3; i++)
CHECK_EQ(stringA[i], buffer[i]);
for (i = 0; i < 3; i++)
CHECK_EQ(stringB[i], buffer[i+3]);
}
TEST(ExternalShortStringAdd) { TEST(ExternalShortStringAdd) {
ZoneScope zone(DELETE_ON_EXIT); ZoneScope zone(DELETE_ON_EXIT);

1
src/node.cc

@ -327,6 +327,7 @@ ssize_t DecodeWrite(char *buf,
uint16_t * twobytebuf = new uint16_t[buflen]; uint16_t * twobytebuf = new uint16_t[buflen];
str->Flatten();
str->Write(twobytebuf, 0, buflen); str->Write(twobytebuf, 0, buflen);
for (size_t i = 0; i < buflen; i++) { for (size_t i = 0; i < buflen; i++) {

2
src/node_buffer.cc

@ -308,6 +308,7 @@ Handle<Value> Buffer::Utf8Write(const Arguments &args) {
const char *p = buffer->data() + offset; const char *p = buffer->data() + offset;
s->Flatten();
int written = s->WriteUtf8((char*)p, buffer->length_ - offset); int written = s->WriteUtf8((char*)p, buffer->length_ - offset);
if (written > 0 && p[written-1] == '\0') written--; if (written > 0 && p[written-1] == '\0') written--;
@ -340,6 +341,7 @@ Handle<Value> Buffer::AsciiWrite(const Arguments &args) {
size_t towrite = MIN((unsigned long) s->Length(), buffer->length_ - offset); size_t towrite = MIN((unsigned long) s->Length(), buffer->length_ - offset);
s->Flatten();
int written = s->WriteAscii((char*)p, 0, towrite); int written = s->WriteAscii((char*)p, 0, towrite);
return scope.Close(Integer::New(written)); return scope.Close(Integer::New(written));
} }

Loading…
Cancel
Save