Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
string_bytes: use extern for length and write utf8
Browse files Browse the repository at this point in the history
If the string is external then the length can be quickly retrieved. This
is especially faster for large strings that are being treated as UTF8.
Also, if the string is external then there's no need for a full
String::WriteUtf8 operation. A simple memcpy will do.
  • Loading branch information
trevnorris committed Sep 3, 2013
1 parent 906a175 commit 7a235f9
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/string_bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ size_t StringBytes::Write(char* buf,
size_t len = 0;
bool is_extern = GetExternalParts(val, &data, &len);

Local<String> str = val->ToString();
Local<String> str = val.As<String>();
len = len < buflen ? len : buflen;

int flags = String::NO_NULL_TERMINATION |
Expand All @@ -295,7 +295,10 @@ size_t StringBytes::Write(char* buf,
break;

case UTF8:
len = str->WriteUtf8(buf, buflen, chars_written, flags);
if (is_extern)
memcpy(buf, data, len);
else
len = str->WriteUtf8(buf, buflen, chars_written, flags);
break;

case UCS2:
Expand Down Expand Up @@ -403,9 +406,12 @@ size_t StringBytes::Size(Handle<Value> val, enum encoding encoding) {
size_t data_size = 0;
bool is_buffer = Buffer::HasInstance(val);

if (is_buffer && (encoding == BUFFER || encoding == BINARY)) {
if (is_buffer && (encoding == BUFFER || encoding == BINARY))
return Buffer::Length(val);
}

const char* data;
if (GetExternalParts(val, &data, &data_size))
return data_size;

Local<String> str = val->ToString();

Expand Down

0 comments on commit 7a235f9

Please sign in to comment.