-
-
Notifications
You must be signed in to change notification settings - Fork 35.3k
src: use stack allocation for small string encoding #62431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thisalihassan
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
thisalihassan:src-string-bytes-stack-encode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+84
−81
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -145,12 +145,49 @@ class ExternString: public ResourceType { | |
| size_t length_; | ||
| }; | ||
|
|
||
| typedef ExternString<String::ExternalOneByteStringResource, char> | ||
| ExternOneByteString; | ||
| typedef ExternString<String::ExternalStringResource, uint16_t> | ||
| ExternTwoByteString; | ||
|
|
||
| template <typename EncodeFn> | ||
| static MaybeLocal<Value> EncodeOneByteString(Isolate* isolate, | ||
| size_t length, | ||
| EncodeFn encode) { | ||
| // 512B stack threshold: covers common small outputs (hex SHA-256/512, UUIDs). | ||
| // Larger thresholds were benchmarked | ||
| MaybeStackBuffer<char, 512> buf(length); | ||
| encode(buf.out()); | ||
| // Copy stack-backed data, but release heap-backed storage to V8. | ||
| if (buf.IsAllocated()) { | ||
| char* data = buf.out(); | ||
| buf.Release(); | ||
| return ExternOneByteString::New(isolate, data, length); | ||
| } | ||
| return String::NewFromOneByte(isolate, | ||
| reinterpret_cast<const uint8_t*>(buf.out()), | ||
| v8::NewStringType::kNormal, | ||
| static_cast<int>(length)); | ||
| } | ||
|
|
||
| typedef ExternString<String::ExternalOneByteStringResource, | ||
| char> ExternOneByteString; | ||
| typedef ExternString<String::ExternalStringResource, | ||
| uint16_t> ExternTwoByteString; | ||
|
|
||
| template <typename EncodeFn> | ||
| static MaybeLocal<Value> EncodeTwoByteString(Isolate* isolate, | ||
| size_t char_length, | ||
| EncodeFn encode) { | ||
| // 256 uint16_t = 512 bytes on the stack, matching the one-byte | ||
| MaybeStackBuffer<uint16_t, 256> buf(char_length); | ||
| encode(buf.out()); | ||
| // Copy stack-backed data, but release heap-backed storage to V8. | ||
| if (buf.IsAllocated()) { | ||
| uint16_t* data = buf.out(); | ||
| buf.Release(); | ||
| return ExternTwoByteString::New(isolate, data, char_length); | ||
| } | ||
| return String::NewFromTwoByte(isolate, | ||
| buf.out(), | ||
| v8::NewStringType::kNormal, | ||
| static_cast<int>(char_length)); | ||
| } | ||
|
|
||
| template <> | ||
| MaybeLocal<Value> ExternOneByteString::NewExternal( | ||
|
|
@@ -513,27 +550,23 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate, | |
| MaybeLocal<String> val; | ||
|
|
||
| switch (encoding) { | ||
| case BUFFER: | ||
| { | ||
| auto maybe_buf = Buffer::Copy(isolate, buf, buflen); | ||
| Local<v8::Object> buf; | ||
| if (!maybe_buf.ToLocal(&buf)) { | ||
| isolate->ThrowException(node::ERR_MEMORY_ALLOCATION_FAILED(isolate)); | ||
| } | ||
| return buf; | ||
| case BUFFER: { | ||
| auto maybe_buf = Buffer::Copy(isolate, buf, buflen); | ||
| Local<v8::Object> buf; | ||
| if (!maybe_buf.ToLocal(&buf)) { | ||
| isolate->ThrowException(node::ERR_MEMORY_ALLOCATION_FAILED(isolate)); | ||
| } | ||
| return buf; | ||
| } | ||
|
||
|
|
||
| case ASCII: | ||
| buflen = keep_buflen_in_range(buflen); | ||
| if (simdutf::validate_ascii_with_errors(buf, buflen).error) { | ||
| // The input contains non-ASCII bytes. | ||
| char* out = node::UncheckedMalloc(buflen); | ||
| if (out == nullptr) { | ||
| isolate->ThrowException(node::ERR_MEMORY_ALLOCATION_FAILED(isolate)); | ||
| return MaybeLocal<Value>(); | ||
| } | ||
| nbytes::ForceAscii(buf, out, buflen); | ||
| return ExternOneByteString::New(isolate, out, buflen); | ||
|
|
||
| return EncodeOneByteString(isolate, buflen, [buf, buflen](char* dst) { | ||
| nbytes::ForceAscii(buf, dst, buflen); | ||
| }); | ||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| return ExternOneByteString::NewFromCopy(isolate, buf, buflen); | ||
| } | ||
|
|
@@ -557,14 +590,12 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate, | |
| isolate->ThrowException(ERR_STRING_TOO_LONG(isolate)); | ||
| return MaybeLocal<Value>(); | ||
| } | ||
| uint16_t* dst = node::UncheckedMalloc<uint16_t>(u16size); | ||
| if (u16size != 0 && dst == nullptr) { | ||
| THROW_ERR_MEMORY_ALLOCATION_FAILED(isolate); | ||
| return MaybeLocal<Value>(); | ||
| } | ||
| size_t utf16len = simdutf::convert_valid_utf8_to_utf16( | ||
| buf, buflen, reinterpret_cast<char16_t*>(dst)); | ||
| return ExternTwoByteString::New(isolate, dst, utf16len); | ||
| return EncodeTwoByteString( | ||
| isolate, u16size, [buf, buflen, u16size](uint16_t* dst) { | ||
| size_t written = simdutf::convert_valid_utf8_to_utf16( | ||
| buf, buflen, reinterpret_cast<char16_t*>(dst)); | ||
| CHECK_EQ(written, u16size); | ||
| }); | ||
| } | ||
|
|
||
| val = | ||
|
|
@@ -583,77 +614,52 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate, | |
| case BASE64: { | ||
| buflen = keep_buflen_in_range(buflen); | ||
| size_t dlen = simdutf::base64_length_from_binary(buflen); | ||
| char* dst = node::UncheckedMalloc(dlen); | ||
| if (dst == nullptr) { | ||
| isolate->ThrowException(node::ERR_MEMORY_ALLOCATION_FAILED(isolate)); | ||
| return MaybeLocal<Value>(); | ||
| } | ||
|
|
||
| size_t written = simdutf::binary_to_base64(buf, buflen, dst); | ||
| CHECK_EQ(written, dlen); | ||
|
|
||
| return ExternOneByteString::New(isolate, dst, dlen); | ||
| return EncodeOneByteString(isolate, dlen, [buf, buflen, dlen](char* dst) { | ||
| size_t written = simdutf::binary_to_base64(buf, buflen, dst); | ||
| CHECK_EQ(written, dlen); | ||
| }); | ||
| } | ||
|
|
||
| case BASE64URL: { | ||
| buflen = keep_buflen_in_range(buflen); | ||
| size_t dlen = | ||
| simdutf::base64_length_from_binary(buflen, simdutf::base64_url); | ||
| char* dst = node::UncheckedMalloc(dlen); | ||
| if (dst == nullptr) { | ||
| isolate->ThrowException(node::ERR_MEMORY_ALLOCATION_FAILED(isolate)); | ||
| return MaybeLocal<Value>(); | ||
| } | ||
|
|
||
| size_t written = | ||
| simdutf::binary_to_base64(buf, buflen, dst, simdutf::base64_url); | ||
| CHECK_EQ(written, dlen); | ||
|
|
||
| return ExternOneByteString::New(isolate, dst, dlen); | ||
| return EncodeOneByteString(isolate, dlen, [buf, buflen, dlen](char* dst) { | ||
| size_t written = | ||
| simdutf::binary_to_base64(buf, buflen, dst, simdutf::base64_url); | ||
| CHECK_EQ(written, dlen); | ||
| }); | ||
| } | ||
|
|
||
| case HEX: { | ||
| buflen = keep_buflen_in_range(buflen); | ||
| size_t dlen = buflen * 2; | ||
| char* dst = node::UncheckedMalloc(dlen); | ||
| if (dst == nullptr) { | ||
| isolate->ThrowException(node::ERR_MEMORY_ALLOCATION_FAILED(isolate)); | ||
| return MaybeLocal<Value>(); | ||
| } | ||
| size_t written = nbytes::HexEncode(buf, buflen, dst, dlen); | ||
| CHECK_EQ(written, dlen); | ||
|
|
||
| return ExternOneByteString::New(isolate, dst, dlen); | ||
| return EncodeOneByteString(isolate, dlen, [buf, buflen, dlen](char* dst) { | ||
| size_t written = nbytes::HexEncode(buf, buflen, dst, dlen); | ||
| CHECK_EQ(written, dlen); | ||
| }); | ||
| } | ||
|
|
||
| case UCS2: { | ||
| buflen = keep_buflen_in_range(buflen); | ||
| size_t str_len = buflen / 2; | ||
| if constexpr (IsBigEndian()) { | ||
| uint16_t* dst = node::UncheckedMalloc<uint16_t>(str_len); | ||
| if (str_len != 0 && dst == nullptr) { | ||
| isolate->ThrowException(node::ERR_MEMORY_ALLOCATION_FAILED(isolate)); | ||
| return MaybeLocal<Value>(); | ||
| } | ||
| for (size_t i = 0, k = 0; k < str_len; i += 2, k += 1) { | ||
| // The input is in *little endian*, because that's what Node.js | ||
| // expects, so the high byte comes after the low byte. | ||
| const uint8_t hi = static_cast<uint8_t>(buf[i + 1]); | ||
| const uint8_t lo = static_cast<uint8_t>(buf[i + 0]); | ||
| dst[k] = static_cast<uint16_t>(hi) << 8 | lo; | ||
| } | ||
| return ExternTwoByteString::New(isolate, dst, str_len); | ||
| return EncodeTwoByteString( | ||
| isolate, str_len, [buf, str_len](uint16_t* dst) { | ||
| for (size_t i = 0, k = 0; k < str_len; i += 2, k += 1) { | ||
| // The input is in *little endian*, because that's what Node.js | ||
| // expects, so the high byte comes after the low byte. | ||
| const uint8_t hi = static_cast<uint8_t>(buf[i + 1]); | ||
| const uint8_t lo = static_cast<uint8_t>(buf[i + 0]); | ||
| dst[k] = static_cast<uint16_t>(hi) << 8 | lo; | ||
| } | ||
| }); | ||
| } | ||
| if (reinterpret_cast<uintptr_t>(buf) % 2 != 0) { | ||
| // Unaligned data still means we can't directly pass it to V8. | ||
| char* dst = node::UncheckedMalloc(buflen); | ||
| if (dst == nullptr) { | ||
| isolate->ThrowException(node::ERR_MEMORY_ALLOCATION_FAILED(isolate)); | ||
| return MaybeLocal<Value>(); | ||
| } | ||
| memcpy(dst, buf, buflen); | ||
| return ExternTwoByteString::New( | ||
| isolate, reinterpret_cast<uint16_t*>(dst), str_len); | ||
| return EncodeTwoByteString( | ||
| isolate, str_len, [buf, buflen](uint16_t* dst) { | ||
| memcpy(dst, buf, buflen); | ||
| }); | ||
| } | ||
| return ExternTwoByteString::NewFromCopy( | ||
| isolate, reinterpret_cast<const uint16_t*>(buf), str_len); | ||
|
|
@@ -675,15 +681,11 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate, | |
| // https://nodejs.org/api/buffer.html regarding Node's "ucs2" | ||
| // encoding specification | ||
| if constexpr (IsBigEndian()) { | ||
| uint16_t* dst = node::UncheckedMalloc<uint16_t>(buflen); | ||
| if (dst == nullptr) { | ||
| isolate->ThrowException(node::ERR_MEMORY_ALLOCATION_FAILED(isolate)); | ||
| return MaybeLocal<Value>(); | ||
| } | ||
| size_t nbytes = buflen * sizeof(uint16_t); | ||
| memcpy(dst, buf, nbytes); | ||
| CHECK(nbytes::SwapBytes16(reinterpret_cast<char*>(dst), nbytes)); | ||
| return ExternTwoByteString::New(isolate, dst, buflen); | ||
| return EncodeTwoByteString(isolate, buflen, [buf, buflen](uint16_t* dst) { | ||
| size_t nbytes = buflen * sizeof(uint16_t); | ||
| memcpy(dst, buf, nbytes); | ||
| CHECK(nbytes::SwapBytes16(reinterpret_cast<char*>(dst), nbytes)); | ||
| }); | ||
| } else { | ||
| return ExternTwoByteString::NewFromCopy(isolate, buf, buflen); | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.