Skip to content

Commit

Permalink
buffer: even faster atob
Browse files Browse the repository at this point in the history
PR-URL: nodejs#52443
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
lemire authored and EliphazBouye committed Jun 20, 2024
1 parent c300d67 commit 0a3cec9
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 0a3cec9

Please sign in to comment.