src: use subarray() in Buffer#slice() for speedup

Use the built-in Typed Array method subarray() to improve performance of
Buffer#slice().

Benchmark improvements:

    benchmark/buffer-slice: 40%
    benchmark/buffer-creation (pool): 25%

Additional tests also added.

PR-URL: https://github.com/nodejs/node/pull/2777
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
This commit is contained in:
Karl Skomski authored and Jeremiah Senkpiel committed 2015-09-16 11:40:59 -04:00
1 parent ba47511976
commit 0a5f80a11f
3 files changed
+37 -50

No files matched your search

-24
View File
@@ -456,29 +456,6 @@ void CreateFromArrayBuffer(const FunctionCallbackInfo<Value>& args) {
}
void Slice(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsUint8Array());
CHECK(args[1]->IsNumber());
CHECK(args[2]->IsNumber());
Environment* env = Environment::GetCurrent(args);
Local<Uint8Array> ab_ui = args[0].As<Uint8Array>();
Local<ArrayBuffer> ab = ab_ui->Buffer();
ArrayBuffer::Contents ab_c = ab->GetContents();
size_t offset = ab_ui->ByteOffset();
size_t start = args[1]->NumberValue() + offset;
size_t end = args[2]->NumberValue() + offset;
CHECK_GE(end, start);
size_t size = end - start;
CHECK_GE(ab_c.ByteLength(), start + size);
Local<Uint8Array> ui = Uint8Array::New(ab, start, size);
Maybe<bool> mb =
ui->SetPrototype(env->context(), env->buffer_prototype_object());
if (!mb.FromMaybe(false))
return env->ThrowError("Unable to set Object prototype");
args.GetReturnValue().Set(ui);
}
template <encoding encoding>
void StringSlice(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
@@ -1002,7 +979,6 @@ void Initialize(Local<Object> target,
env->SetMethod(target, "createFromString", CreateFromString);
env->SetMethod(target, "createFromArrayBuffer", CreateFromArrayBuffer);
env->SetMethod(target, "slice", Slice);
env->SetMethod(target, "byteLengthUtf8", ByteLengthUtf8);
env->SetMethod(target, "compare", Compare);
env->SetMethod(target, "fill", Fill);