From 7bd3250c2accd5e62dfe07b160cf4333ca216a0e Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Thu, 29 Jan 2015 18:02:17 -0800 Subject: [PATCH 1/2] Switched extension to return faster sliceable Buffers --- src/node/ext/byte_buffer.cc | 22 +++++++++++++++++++--- src/node/ext/byte_buffer.h | 4 ++++ src/node/ext/event.cc | 3 ++- src/node/interop/interop_client.js | 1 + 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/node/ext/byte_buffer.cc b/src/node/ext/byte_buffer.cc index 142951475a1..695ecedd344 100644 --- a/src/node/ext/byte_buffer.cc +++ b/src/node/ext/byte_buffer.cc @@ -39,13 +39,17 @@ #include "grpc/grpc.h" #include "grpc/support/slice.h" +#include "byte_buffer.h" + namespace grpc { namespace node { -#include "byte_buffer.h" - using ::node::Buffer; +using v8::Context; +using v8::Function; using v8::Handle; +using v8::Object; +using v8::Number; using v8::Value; grpc_byte_buffer *BufferToByteBuffer(Handle buffer) { @@ -73,7 +77,19 @@ Handle ByteBufferToBuffer(grpc_byte_buffer *buffer) { memcpy(result + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next)); offset += GPR_SLICE_LENGTH(next); } - return NanEscapeScope(NanNewBufferHandle(result, length)); + return NanEscapeScope(MakeFastBuffer(NanNewBufferHandle(result, length))); +} + +Handle MakeFastBuffer(Handle slowBuffer) { + NanEscapableScope(); + Handle globalObj = Context::GetCurrent()->Global(); + Handle bufferConstructor = Handle::Cast( + globalObj->Get(NanNew("Buffer"))); + Handle consArgs[3] = { slowBuffer, + NanNew(Buffer::Length(slowBuffer)), + NanNew(0) }; + Handle fastBuffer = bufferConstructor->NewInstance(3, consArgs); + return NanEscapeScope(fastBuffer); } } // namespace node } // namespace grpc diff --git a/src/node/ext/byte_buffer.h b/src/node/ext/byte_buffer.h index ee2b4c0d158..5f1903a42eb 100644 --- a/src/node/ext/byte_buffer.h +++ b/src/node/ext/byte_buffer.h @@ -50,6 +50,10 @@ grpc_byte_buffer *BufferToByteBuffer(v8::Handle buffer); /* Convert a grpc_byte_buffer to a Node.js Buffer */ v8::Handle ByteBufferToBuffer(grpc_byte_buffer *buffer); +/* Convert a ::node::Buffer to a fast Buffer, as defined in the Node + Buffer documentation */ +v8::Handle MakeFastBuffer(v8::Handle slowBuffer); + } // namespace node } // namespace grpc diff --git a/src/node/ext/event.cc b/src/node/ext/event.cc index fcf046b6978..b9446062d71 100644 --- a/src/node/ext/event.cc +++ b/src/node/ext/event.cc @@ -80,7 +80,8 @@ Handle ParseMetadata(grpc_metadata *metadata_elements, size_t length) { metadata_object->Set(key_string, array); } array->Set(index_map[elem->key], - NanNewBufferHandle(elem->value, elem->value_length)); + MakeFastBuffer( + NanNewBufferHandle(elem->value, elem->value_length))); index_map[elem->key] += 1; } return NanEscapeScope(metadata_object); diff --git a/src/node/interop/interop_client.js b/src/node/interop/interop_client.js index ce18f77fe71..6978480c8ee 100644 --- a/src/node/interop/interop_client.js +++ b/src/node/interop/interop_client.js @@ -178,6 +178,7 @@ function pingPong(client, done) { payload: {body: zeroBuffer(payload_sizes[index])} }); call.on('data', function(response) { + debugger; assert.strictEqual(response.payload.type, testProto.PayloadType.COMPRESSABLE); assert.equal(response.payload.body.limit - response.payload.body.offset, From 67c44110f88c0c0fd60a9de30b1115e0c35403a8 Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Thu, 29 Jan 2015 18:03:12 -0800 Subject: [PATCH 2/2] Removed stray debugger statement --- src/node/interop/interop_client.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/node/interop/interop_client.js b/src/node/interop/interop_client.js index 6978480c8ee..ce18f77fe71 100644 --- a/src/node/interop/interop_client.js +++ b/src/node/interop/interop_client.js @@ -178,7 +178,6 @@ function pingPong(client, done) { payload: {body: zeroBuffer(payload_sizes[index])} }); call.on('data', function(response) { - debugger; assert.strictEqual(response.payload.type, testProto.PayloadType.COMPRESSABLE); assert.equal(response.payload.body.limit - response.payload.body.offset,