From e6f0c98096b30e8294693c0e41f895182edd3715 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Tue, 9 Apr 2024 21:30:23 -0400 Subject: [PATCH 1/2] buffer: even faster atob --- src/node_buffer.cc | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index b31beada451bc8..a80f3d303dc467 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -1229,19 +1229,33 @@ static void Atob(const FunctionCallbackInfo& 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 { // 16-bit case + } else if(input->IsOneByte()) { + MaybeStackBuffer stack_buf(input->Length()); + input->WriteOneByte(args.GetIsolate(), + stack_buf.out(), + 0, + input->Length(), + String::NO_NULL_TERMINATION); + const char* data = reinterpret_cast(*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(*value); size_t expected_length = simdutf::maximal_binary_length_from_base64(data, value.length()); - buffer.AllocateSufficientStorage(expected_length + 1); - buffer.SetLengthAndZeroTerminate(expected_length); + buffer.AllocateSufficientStorage(expected_length); + buffer.SetLength(expected_length); result = simdutf::base64_to_binary( - data, value.length(), buffer.out(), simdutf::base64_default); + data, value.length(), buffer.out()); } if (result.error == simdutf::error_code::SUCCESS) { From a23668cc89d4aaa2012e172f51015c378b7f9f6e Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Tue, 9 Apr 2024 22:10:11 -0400 Subject: [PATCH 2/2] lint --- src/node_buffer.cc | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index a80f3d303dc467..440b101ed3a4cc 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -1233,29 +1233,27 @@ static void Atob(const FunctionCallbackInfo& args) { buffer.SetLength(expected_length); result = simdutf::base64_to_binary( ext->data(), ext->length(), buffer.out(), simdutf::base64_default); - } else if(input->IsOneByte()) { + } else if (input->IsOneByte()) { MaybeStackBuffer stack_buf(input->Length()); input->WriteOneByte(args.GetIsolate(), - stack_buf.out(), - 0, - input->Length(), - String::NO_NULL_TERMINATION); + stack_buf.out(), + 0, + input->Length(), + String::NO_NULL_TERMINATION); const char* data = reinterpret_cast(*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 + result = simdutf::base64_to_binary(data, input->Length(), buffer.out()); + } else { // 16-bit case String::Value value(env->isolate(), input); auto data = reinterpret_cast(*value); size_t expected_length = simdutf::maximal_binary_length_from_base64(data, value.length()); buffer.AllocateSufficientStorage(expected_length); buffer.SetLength(expected_length); - result = simdutf::base64_to_binary( - data, value.length(), buffer.out()); + result = simdutf::base64_to_binary(data, value.length(), buffer.out()); } if (result.error == simdutf::error_code::SUCCESS) {