From 65249ccd9bb7edae8bbaafc2e00cd25c9c5d640d Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Tue, 22 Jan 2013 09:09:12 -0800 Subject: [PATCH] buffer: slow buffer copy compatibility fix Fix issue where SlowBuffers couldn't be passed as target to Buffer copy(). Also included checks to see if Argument parameters are defined before assigning their values. This offered ~3x's performance gain. Backport of 16bbecc from master branch. Closes #4633. --- lib/buffer.js | 4 ++-- src/node_buffer.cc | 8 ++++---- test/simple/test-buffer.js | 8 ++++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 978a566bc1..6bfbc93cd9 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -545,8 +545,8 @@ Buffer.prototype.copy = function(target, target_start, start, end) { end = target.length - target_start + start; } - return this.parent.copy(target.parent, - target_start + target.offset, + return this.parent.copy(target.parent || target, + target_start + (target.offset || 0), start + this.offset, end + this.offset); }; diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 4a6e836653..3f8ebefe06 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -412,10 +412,10 @@ Handle Buffer::Copy(const Arguments &args) { Local target = args[0]->ToObject(); char* target_data = Buffer::Data(target); size_t target_length = Buffer::Length(target); - size_t target_start = args[1]->Uint32Value(); - size_t source_start = args[2]->Uint32Value(); - size_t source_end = args[3]->IsUint32() ? args[3]->Uint32Value() - : source->length_; + size_t target_start = args[1]->IsUndefined() ? 0 : args[1]->Uint32Value(); + size_t source_start = args[2]->IsUndefined() ? 0 : args[2]->Uint32Value(); + size_t source_end = args[3]->IsUndefined() ? source->length_ + : args[3]->Uint32Value(); if (source_end < source_start) { return ThrowException(Exception::Error(String::New( diff --git a/test/simple/test-buffer.js b/test/simple/test-buffer.js index c1f5dea8c4..11165f88c1 100644 --- a/test/simple/test-buffer.js +++ b/test/simple/test-buffer.js @@ -79,6 +79,14 @@ for (var i = 0; i < c.length; i++) { assert.equal(i % 256, c[i]); } +// copy from fast to slow buffer +var sb = new SlowBuffer(b.length); +var copied = b.copy(sb); +console.log('copied %d bytes from b into sb'); +for (var i = 0; i < sb.length; i++) { + assert.strictEqual(sb[i], b[i]); +} + var caught_error = null; // try to copy from before the beginning of b