Skip to content

Commit

Permalink
buffer: even faster atob
Browse files Browse the repository at this point in the history
PR-URL: #52443
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
lemire authored and targos committed May 13, 2024
1 parent a589de0 commit 67fcb6b
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1286,19 +1286,31 @@ static void Atob(const FunctionCallbackInfo<Value>& args) {
auto ext = input->GetExternalOneByteStringResource();
size_t expected_length =
simdutf::maximal_binary_length_from_base64(ext->data(), ext->length());
buffer.AllocateSufficientStorage(expected_length + 1);
buffer.SetLengthAndZeroTerminate(expected_length);
buffer.AllocateSufficientStorage(expected_length);
buffer.SetLength(expected_length);
result = simdutf::base64_to_binary(
ext->data(), ext->length(), buffer.out(), simdutf::base64_default);
} else if (input->IsOneByte()) {
MaybeStackBuffer<uint8_t> stack_buf(input->Length());
input->WriteOneByte(args.GetIsolate(),
stack_buf.out(),
0,
input->Length(),
String::NO_NULL_TERMINATION);
const char* data = reinterpret_cast<const char*>(*stack_buf);
size_t expected_length =
simdutf::maximal_binary_length_from_base64(data, input->Length());
buffer.AllocateSufficientStorage(expected_length);
buffer.SetLength(expected_length);
result = simdutf::base64_to_binary(data, input->Length(), buffer.out());
} else { // 16-bit case
String::Value value(env->isolate(), input);
auto data = reinterpret_cast<const char16_t*>(*value);
size_t expected_length =
simdutf::maximal_binary_length_from_base64(data, value.length());
buffer.AllocateSufficientStorage(expected_length + 1);
buffer.SetLengthAndZeroTerminate(expected_length);
result = simdutf::base64_to_binary(
data, value.length(), buffer.out(), simdutf::base64_default);
buffer.AllocateSufficientStorage(expected_length);
buffer.SetLength(expected_length);
result = simdutf::base64_to_binary(data, value.length(), buffer.out());
}

if (result.error == simdutf::error_code::SUCCESS) {
Expand Down

0 comments on commit 67fcb6b

Please sign in to comment.