Skip to content

Commit 908653f

Browse files
committed
fixup: fast call
1 parent ce07761 commit 908653f

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

lib/buffer.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const {
5858
byteLengthUtf8,
5959
compare: _compare,
6060
compareOffset,
61-
copyArrayBuffer,
61+
copy: _copy,
6262
createFromString,
6363
fill: bindingFill,
6464
isAscii: bindingIsAscii,
@@ -204,7 +204,7 @@ function toInteger(n, defaultVal) {
204204
return defaultVal;
205205
}
206206

207-
function _copy(source, target, targetStart, sourceStart, sourceEnd) {
207+
function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
208208
if (!isUint8Array(source))
209209
throw new ERR_INVALID_ARG_TYPE('source', ['Buffer', 'Uint8Array'], source);
210210
if (!isUint8Array(target))
@@ -253,13 +253,7 @@ function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
253253
return 0;
254254

255255
if (sourceStart !== 0 || sourceEnd < source.length) {
256-
copyArrayBuffer(
257-
target.buffer,
258-
target.byteOffset + targetStart,
259-
source.buffer,
260-
source.byteOffset + sourceStart,
261-
nb,
262-
);
256+
_copy(source, target, targetStart, sourceStart, sourceEnd);
263257
} else {
264258
TypedArrayPrototypeSet(target, source, targetStart);
265259
}
@@ -816,7 +810,7 @@ ObjectDefineProperty(Buffer.prototype, 'offset', {
816810

817811
Buffer.prototype.copy =
818812
function copy(target, targetStart, sourceStart, sourceEnd) {
819-
return _copy(this, target, targetStart, sourceStart, sourceEnd);
813+
return copyImpl(this, target, targetStart, sourceStart, sourceEnd);
820814
};
821815

822816
// No need to verify that "buf.length <= MAX_UINT32" since it's a read-only

src/node_buffer.cc

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ void StringSlice(const FunctionCallbackInfo<Value>& args) {
569569
}
570570

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

575575
THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
@@ -606,6 +606,37 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
606606
args.GetReturnValue().Set(to_copy);
607607
}
608608

609+
uint32_t FastCopy(Local<Value> receiver,
610+
const v8::FastApiArrayBufferView& source,
611+
v8::FastApiArrayBufferView& target,
612+
uint32_t target_start,
613+
uint32_t source_start,
614+
uint32_t source_end,
615+
v8::FastApiCallbackOptions& options) {
616+
// Copy 0 bytes; we're done
617+
if (target_start >= target.byte_length || source_start >= source_end)
618+
return 0;
619+
620+
if (source_start > source.byte_length) {
621+
options.fallback = true;
622+
return 0;
623+
}
624+
625+
if (source_end - source_start > target.byte_length - target_start)
626+
source_end = source_start + target.byte_length - target_start;
627+
628+
uint32_t to_copy = std::min<size_t>(
629+
std::min<size_t>(source_end - source_start, target.byte_length - target_start),
630+
source.byte_length - source_start);
631+
632+
memmove(reinterpret_cast<char*>(target.data) + target_start,
633+
reinterpret_cast<char*>(source.data) + source_start, to_copy);
634+
635+
return to_copy;
636+
}
637+
638+
static v8::CFunction fast_copy(
639+
v8::CFunction::Make(FastCopy));
609640

610641
void Fill(const FunctionCallbackInfo<Value>& args) {
611642
Environment* env = Environment::GetCurrent(args);
@@ -1275,7 +1306,7 @@ void Initialize(Local<Object> target,
12751306
"byteLengthUtf8",
12761307
SlowByteLengthUtf8,
12771308
&fast_byte_length_utf8);
1278-
SetMethod(context, target, "copy", Copy);
1309+
SetFastMethod(context, target, "copy", SlowCopy, &fast_copy);
12791310
SetMethodNoSideEffect(context, target, "compare", Compare);
12801311
SetMethodNoSideEffect(context, target, "compareOffset", CompareOffset);
12811312
SetMethod(context, target, "fill", Fill);
@@ -1335,6 +1366,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
13351366
registry->Register(fast_byte_length_utf8.GetTypeInfo());
13361367
registry->Register(FastByteLengthUtf8);
13371368
registry->Register(Copy);
1369+
registry->Register(FastCopy);
1370+
registry->Register(fast_copy.GetTypeInfo());
13381371
registry->Register(Compare);
13391372
registry->Register(CompareOffset);
13401373
registry->Register(Fill);

0 commit comments

Comments
 (0)