Skip to content

Commit

Permalink
fixup: fast call
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jul 29, 2024
1 parent ce07761 commit f3ff14e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
14 changes: 4 additions & 10 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const {
byteLengthUtf8,
compare: _compare,
compareOffset,
copyArrayBuffer,
copy: _copy,
createFromString,
fill: bindingFill,
isAscii: bindingIsAscii,
Expand Down Expand Up @@ -204,7 +204,7 @@ function toInteger(n, defaultVal) {
return defaultVal;
}

function _copy(source, target, targetStart, sourceStart, sourceEnd) {
function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
if (!isUint8Array(source))
throw new ERR_INVALID_ARG_TYPE('source', ['Buffer', 'Uint8Array'], source);
if (!isUint8Array(target))
Expand Down Expand Up @@ -253,13 +253,7 @@ function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
return 0;

if (sourceStart !== 0 || sourceEnd < source.length) {
copyArrayBuffer(
target.buffer,
target.byteOffset + targetStart,
source.buffer,
source.byteOffset + sourceStart,
nb,
);
_copy(source, target, targetStart, sourceStart, sourceEnd);
} else {
TypedArrayPrototypeSet(target, source, targetStart);
}
Expand Down Expand Up @@ -816,7 +810,7 @@ ObjectDefineProperty(Buffer.prototype, 'offset', {

Buffer.prototype.copy =
function copy(target, targetStart, sourceStart, sourceEnd) {
return _copy(this, target, targetStart, sourceStart, sourceEnd);
return copyImpl(this, target, targetStart, sourceStart, sourceEnd);
};

// No need to verify that "buf.length <= MAX_UINT32" since it's a read-only
Expand Down
34 changes: 32 additions & 2 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ void StringSlice(const FunctionCallbackInfo<Value>& args) {
}

// bytesCopied = copy(buffer, target[, targetStart][, sourceStart][, sourceEnd])
void Copy(const FunctionCallbackInfo<Value> &args) {
void SlowCopy(const FunctionCallbackInfo<Value> &args) {
Environment* env = Environment::GetCurrent(args);

THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
Expand Down Expand Up @@ -606,6 +606,34 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
args.GetReturnValue().Set(to_copy);
}

uint32_t FastCopy(Local<Value> receiver,
const v8::FastApiArrayBufferView& source,
v8::FastApiArrayBufferView& target,
uint32_t target_start,
uint32_t source_start,
uint32_t source_end,
v8::FastApiCallbackOptions& options) {
// Copy 0 bytes; we're done
if (target_start >= target.byte_length || source_start >= source_end)
return 0;

assert(source_start <= source_end);

if (source_end - source_start > target.byte_length - target_start)
source_end = source_start + target.byte_length - target_start;

uint32_t to_copy = std::min<size_t>(
std::min<size_t>(source_end - source_start, target.byte_length - target_start),
source.byte_length - source_start);

memmove(reinterpret_cast<char*>(target.data) + target_start,
reinterpret_cast<char*>(source.data) + source_start, to_copy);

return to_copy;
}

static v8::CFunction fast_copy(
v8::CFunction::Make(FastCopy));

void Fill(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Expand Down Expand Up @@ -1275,7 +1303,7 @@ void Initialize(Local<Object> target,
"byteLengthUtf8",
SlowByteLengthUtf8,
&fast_byte_length_utf8);
SetMethod(context, target, "copy", Copy);
SetFastMethod(context, target, "copy", SlowCopy, &fast_copy);
SetMethodNoSideEffect(context, target, "compare", Compare);
SetMethodNoSideEffect(context, target, "compareOffset", CompareOffset);
SetMethod(context, target, "fill", Fill);
Expand Down Expand Up @@ -1335,6 +1363,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(fast_byte_length_utf8.GetTypeInfo());
registry->Register(FastByteLengthUtf8);
registry->Register(Copy);
registry->Register(FastCopy);
registry->Register(fast_copy.GetTypeInfo());
registry->Register(Compare);
registry->Register(CompareOffset);
registry->Register(Fill);
Expand Down

0 comments on commit f3ff14e

Please sign in to comment.