From 47abd168c2049f0fb2cc43c237d27e4f3a9d5f26 Mon Sep 17 00:00:00 2001 From: Karl Skomski Date: Wed, 9 Sep 2015 18:35:04 +0200 Subject: [PATCH] src: use subarray() in Buffer.slice() for speedup benchmark/buffer-slice: 40% benchmark/buffer-creation (pool): 25% --- lib/buffer.js | 31 +++++-------------------------- src/node_buffer.cc | 24 ------------------------ test/parallel/test-buffer.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 50 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 39cd5f9e58b9f8..37ed0f9ecef012 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -73,7 +73,7 @@ function allocate(size) { if (size < (Buffer.poolSize >>> 1)) { if (size > (poolSize - poolOffset)) createPool(); - var b = binding.slice(allocPool, poolOffset, poolOffset + size); + var b = allocPool.slice(poolOffset, poolOffset + size); poolOffset += size; alignPool(); return b; @@ -94,7 +94,7 @@ function fromString(string, encoding) { if (length > (poolSize - poolOffset)) createPool(); var actual = allocPool.write(string, poolOffset, encoding); - var b = binding.slice(allocPool, poolOffset, poolOffset + actual); + var b = allocPool.slice(poolOffset, poolOffset + actual); poolOffset += actual; alignPool(); return b; @@ -552,30 +552,9 @@ Buffer.prototype.toJSON = function() { // TODO(trevnorris): currently works like Array.prototype.slice(), which // doesn't follow the new standard for throwing on out of range indexes. Buffer.prototype.slice = function slice(start, end) { - var len = this.length; - start = ~~start; - end = end === undefined ? len : ~~end; - - if (start < 0) { - start += len; - if (start < 0) - start = 0; - } else if (start > len) { - start = len; - } - - if (end < 0) { - end += len; - if (end < 0) - end = 0; - } else if (end > len) { - end = len; - } - - if (end < start) - end = start; - - return binding.slice(this, start, end); + const buffer = this.subarray(start, end); + Object.setPrototypeOf(buffer, Buffer.prototype); + return buffer; }; diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 878c6c5e006c35..dc0c742e3b7198 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -456,29 +456,6 @@ void CreateFromArrayBuffer(const FunctionCallbackInfo& args) { } -void Slice(const FunctionCallbackInfo& args) { - CHECK(args[0]->IsUint8Array()); - CHECK(args[1]->IsNumber()); - CHECK(args[2]->IsNumber()); - Environment* env = Environment::GetCurrent(args); - Local ab_ui = args[0].As(); - Local 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 ui = Uint8Array::New(ab, start, size); - Maybe 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 void StringSlice(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); @@ -1002,7 +979,6 @@ void Initialize(Local 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); diff --git a/test/parallel/test-buffer.js b/test/parallel/test-buffer.js index c8f9dce775d8ea..ab9cea30782844 100644 --- a/test/parallel/test-buffer.js +++ b/test/parallel/test-buffer.js @@ -1061,14 +1061,46 @@ assert.equal(buf.readInt8(0), -1); assert.equal(buf.slice(-10, 10), '0123456789'); assert.equal(buf.slice(-20, 10), '0123456789'); assert.equal(buf.slice(-20, -10), ''); + assert.equal(buf.slice(), '0123456789'); + assert.equal(buf.slice(0), '0123456789'); + assert.equal(buf.slice(0, 0), ''); + assert.equal(buf.slice(undefined), '0123456789'); + assert.equal(buf.slice('foobar'), '0123456789'); + assert.equal(buf.slice(undefined, undefined), '0123456789'); + + assert.equal(buf.slice(2), '23456789'); + assert.equal(buf.slice(5), '56789'); + assert.equal(buf.slice(10), ''); + assert.equal(buf.slice(5, 8), '567'); + assert.equal(buf.slice(8, -1), '8'); + assert.equal(buf.slice(-10), '0123456789'); + assert.equal(buf.slice(0, -9), '0'); + assert.equal(buf.slice(0, -10), ''); assert.equal(buf.slice(0, -1), '012345678'); assert.equal(buf.slice(2, -2), '234567'); assert.equal(buf.slice(0, 65536), '0123456789'); assert.equal(buf.slice(65536, 0), ''); + assert.equal(buf.slice(-5, -8), ''); + assert.equal(buf.slice(-5, -3), '56'); + assert.equal(buf.slice(-10, 10), '0123456789'); for (var i = 0, s = buf.toString(); i < buf.length; ++i) { + assert.equal(buf.slice(i), s.slice(i)); + assert.equal(buf.slice(0, i), s.slice(0, i)); assert.equal(buf.slice(-i), s.slice(-i)); assert.equal(buf.slice(0, -i), s.slice(0, -i)); } + + var utf16Buf = new Buffer('0123456789', 'utf16le'); + assert.deepEqual(utf16Buf.slice(0, 6), Buffer('012', 'utf16le')); + + assert.equal(buf.slice('0', '1'), '0'); + assert.equal(buf.slice('-5', '10'), '56789'); + assert.equal(buf.slice('-10', '10'), '0123456789'); + assert.equal(buf.slice('-10', '-5'), '01234'); + assert.equal(buf.slice('-10', '-0'), ''); + assert.equal(buf.slice('111'), ''); + assert.equal(buf.slice('0', '-111'), ''); + // try to slice a zero length Buffer // see https://github.com/joyent/node/issues/5881 SlowBuffer(0).slice(0, 1);