Skip to content

Commit

Permalink
buffer: improve btoa performance
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Apr 8, 2024
1 parent db17461 commit 6efc7fa
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
20 changes: 20 additions & 0 deletions benchmark/buffers/buffer-btoa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const common = require('../common.js');
const assert = require('node:assert');

const bench = common.createBenchmark(main, {
size: [16, 32, 64, 128, 256, 1024],
n: [1e6],
});

function main({ n, size }) {
const input = 'A'.repeat(size);
let out = 0;

bench.start();
for (let i = 0; i < n; i++) {
out += btoa(input).length;
}
bench.end(n);
assert(out > 0);
}
9 changes: 2 additions & 7 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const {
kMaxLength,
kStringMaxLength,
atob: _atob,
btoa: _btoa,
} = internalBinding('buffer');
const {
constants: {
Expand Down Expand Up @@ -1249,13 +1250,7 @@ function btoa(input) {
if (arguments.length === 0) {
throw new ERR_MISSING_ARGS('input');
}
input = `${input}`;
for (let n = 0; n < input.length; n++) {
if (input[n].charCodeAt(0) > 0xff)
throw lazyDOMException('Invalid character', 'InvalidCharacterError');
}
const buf = Buffer.from(input, 'latin1');
return buf.toString('base64');
return _btoa(`${input}`);
}

function atob(input) {
Expand Down
37 changes: 37 additions & 0 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,41 @@ void DetachArrayBuffer(const FunctionCallbackInfo<Value>& args) {
}
}

static void Btoa(const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(args.Length(), 1);
Environment* env = Environment::GetCurrent(args);
THROW_AND_RETURN_IF_NOT_STRING(env, args[0], "argument");

Local<String> input = args[0].As<String>();
MaybeStackBuffer<char> buffer;
size_t written;

if (input->IsExternalOneByte()) { // 8-bit case
auto ext = input->GetExternalOneByteStringResource();
size_t expected_length = simdutf::base64_length_from_binary(ext->length());
buffer.AllocateSufficientStorage(expected_length + 1);
buffer.SetLengthAndZeroTerminate(expected_length);
written = simdutf::binary_to_base64(
ext->data(), ext->length(), buffer.out(), simdutf::base64_default);
} else { // 16-bit case
String::Value value(env->isolate(), input);
auto data = reinterpret_cast<const char*>(*value);
size_t expected_length = simdutf::base64_length_from_binary(value.length());
buffer.AllocateSufficientStorage(expected_length + 1);
buffer.SetLengthAndZeroTerminate(expected_length);
written = simdutf::binary_to_base64(
data, value.length(), buffer.out(), simdutf::base64_default);
}

auto value =
String::NewFromOneByte(env->isolate(),
reinterpret_cast<const uint8_t*>(buffer.out()),
NewStringType::kNormal,
written)
.ToLocalChecked();
return args.GetReturnValue().Set(value);
}

// In case of success, the decoded string is returned.
// In case of error, a negative value is returned:
// * -1 indicates a single character remained,
Expand Down Expand Up @@ -1329,6 +1364,7 @@ void Initialize(Local<Object> target,
Isolate* isolate = env->isolate();

SetMethodNoSideEffect(context, target, "atob", Atob);
SetMethodNoSideEffect(context, target, "btoa", Btoa);

SetMethod(context, target, "setBufferPrototype", SetBufferPrototype);
SetMethodNoSideEffect(context, target, "createFromString", CreateFromString);
Expand Down Expand Up @@ -1433,6 +1469,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(CopyArrayBuffer);

registry->Register(Atob);
registry->Register(Btoa);
}

} // namespace Buffer
Expand Down

0 comments on commit 6efc7fa

Please sign in to comment.