Skip to content

buffer: implement native C++ fast-path for Buffer.concat #58409

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const {
byteLengthUtf8,
compare: _compare,
compareOffset,
concatNative,
copy: _copy,
fill: bindingFill,
isAscii: bindingIsAscii,
Expand Down Expand Up @@ -570,26 +571,38 @@ Buffer.concat = function concat(list, length) {
if (list.length === 0)
return new FastBuffer();

if (length === undefined) {
length = 0;
for (let i = 0; i < list.length; i++) {
if (list[i].length) {
length += list[i].length;
}
let totalLen = length;
if (totalLen === undefined) {
totalLen = 0;
for (const buf of list) {
if (buf.length) totalLen += buf.length;
}
} else {
validateOffset(length, 'length');
validateOffset(totalLen, 'length');
}

let allValid = true;
for (let i = 0; i < list.length; i++) {
if (!isUint8Array(list[i])) {
allValid = false;
break;
}
}
if (allValid) {
return concatNative(list, totalLen);
}

const buffer = Buffer.allocUnsafe(length);
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
// Instead, find the proper error code for this.
const buffer = Buffer.allocUnsafe(totalLen);
let pos = 0;
for (let i = 0; i < list.length; i++) {
const buf = list[i];
if (!isUint8Array(buf)) {
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
// Instead, find the proper error code for this.
throw new ERR_INVALID_ARG_TYPE(
`list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
`list[${i}]`, ['Buffer', 'Uint8Array'], buf);
}
pos += _copyActual(buf, buffer, pos, 0, buf.length);
}
Expand Down
85 changes: 85 additions & 0 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@
namespace node {
namespace Buffer {

using v8::Array;
using v8::ArrayBuffer;
using v8::ArrayBufferView;
using v8::BackingStore;
using v8::BackingStoreInitializationMode;
using v8::CFunction;
using v8::Context;
using v8::EscapableHandleScope;
using v8::Exception;
using v8::FastApiCallbackOptions;
using v8::FastOneByteString;
using v8::FunctionCallbackInfo;
Expand Down Expand Up @@ -1504,6 +1506,87 @@ void SlowWriteString(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(written);
}

void ConcatNative(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> ctx = isolate->GetCurrentContext();

if (!args[0]->IsArray()) {
isolate->ThrowException(Exception::TypeError(
v8::String::NewFromUtf8(
isolate,
"First argument must be an Array of Buffer or Uint8Array",
v8::NewStringType::kNormal)
.ToLocalChecked()));
return;
}
Local<Array> list = args[0].As<Array>();
const uint32_t n = list->Length();

size_t total = 0;
bool hasProvided = false;
if (args.Length() >= 2 && args[1]->IsUint32()) {
total = args[1].As<Uint32>()->Value();
hasProvided = true;
if (total > kMaxLength) {
isolate->ThrowException(ERR_BUFFER_TOO_LARGE(isolate));
return;
}
}

std::vector<char*> ptrs;
std::vector<size_t> lens;
ptrs.reserve(n);
lens.reserve(n);

for (uint32_t i = 0; i < n; ++i) {
Local<Value> v;
if (!list->Get(ctx, i).ToLocal(&v)) return;
if (!node::Buffer::HasInstance(v)) {
std::string msg = "Element at index " + std::to_string(i) +
" is not a Buffer or Uint8Array";
isolate->ThrowException(Exception::TypeError(
v8::String::NewFromUtf8(
isolate, msg.c_str(), v8::NewStringType::kNormal)
.ToLocalChecked()));
return;
}
Local<Object> obj = v.As<Object>();
char* data = node::Buffer::Data(obj);
size_t len = node::Buffer::Length(obj);

if (!hasProvided) {
if (total > kMaxLength - len) {
isolate->ThrowException(ERR_BUFFER_TOO_LARGE(isolate));
return;
}
total += len;
}

ptrs.push_back(data);
lens.push_back(len);
}

MaybeLocal<Object> mb = node::Buffer::New(isolate, total);
Local<Object> result;
if (!mb.ToLocal(&result)) return;
char* dest = node::Buffer::Data(result);

size_t remaining = total;
char* write_ptr = dest;
for (size_t i = 0; i < ptrs.size() && remaining > 0; ++i) {
size_t toCopy = lens[i] < remaining ? lens[i] : remaining;
memcpy(write_ptr, ptrs[i], toCopy);
write_ptr += toCopy;
remaining -= toCopy;
}

if (remaining > 0) {
memset(write_ptr, 0, remaining);
}

args.GetReturnValue().Set(result);
}

template <encoding encoding>
uint32_t FastWriteString(Local<Value> receiver,
Local<Value> dst_obj,
Expand Down Expand Up @@ -1583,6 +1666,7 @@ void Initialize(Local<Object> target,
.Check();

SetMethodNoSideEffect(context, target, "asciiSlice", StringSlice<ASCII>);
SetMethod(context, target, "concatNative", ConcatNative);
SetMethodNoSideEffect(context, target, "base64Slice", StringSlice<BASE64>);
SetMethodNoSideEffect(
context, target, "base64urlSlice", StringSlice<BASE64URL>);
Expand Down Expand Up @@ -1674,6 +1758,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {

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

} // namespace Buffer
Expand Down
Loading