Merge pull request #300 from murgatroid99/node_fast_buffer

Switched extension to return faster sliceable Buffers
pull/308/head
Tim Emiola 10 years ago
commit c0514d1f71
  1. 22
      src/node/ext/byte_buffer.cc
  2. 4
      src/node/ext/byte_buffer.h
  3. 3
      src/node/ext/event.cc

@ -39,13 +39,17 @@
#include "grpc/grpc.h" #include "grpc/grpc.h"
#include "grpc/support/slice.h" #include "grpc/support/slice.h"
#include "byte_buffer.h"
namespace grpc { namespace grpc {
namespace node { namespace node {
#include "byte_buffer.h"
using ::node::Buffer; using ::node::Buffer;
using v8::Context;
using v8::Function;
using v8::Handle; using v8::Handle;
using v8::Object;
using v8::Number;
using v8::Value; using v8::Value;
grpc_byte_buffer *BufferToByteBuffer(Handle<Value> buffer) { grpc_byte_buffer *BufferToByteBuffer(Handle<Value> buffer) {
@ -73,7 +77,19 @@ Handle<Value> ByteBufferToBuffer(grpc_byte_buffer *buffer) {
memcpy(result + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next)); memcpy(result + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next));
offset += GPR_SLICE_LENGTH(next); offset += GPR_SLICE_LENGTH(next);
} }
return NanEscapeScope(NanNewBufferHandle(result, length)); return NanEscapeScope(MakeFastBuffer(NanNewBufferHandle(result, length)));
}
Handle<Value> MakeFastBuffer(Handle<Value> slowBuffer) {
NanEscapableScope();
Handle<Object> globalObj = Context::GetCurrent()->Global();
Handle<Function> bufferConstructor = Handle<Function>::Cast(
globalObj->Get(NanNew("Buffer")));
Handle<Value> consArgs[3] = { slowBuffer,
NanNew<Number>(Buffer::Length(slowBuffer)),
NanNew<Number>(0) };
Handle<Object> fastBuffer = bufferConstructor->NewInstance(3, consArgs);
return NanEscapeScope(fastBuffer);
} }
} // namespace node } // namespace node
} // namespace grpc } // namespace grpc

@ -50,6 +50,10 @@ grpc_byte_buffer *BufferToByteBuffer(v8::Handle<v8::Value> buffer);
/* Convert a grpc_byte_buffer to a Node.js Buffer */ /* Convert a grpc_byte_buffer to a Node.js Buffer */
v8::Handle<v8::Value> ByteBufferToBuffer(grpc_byte_buffer *buffer); v8::Handle<v8::Value> ByteBufferToBuffer(grpc_byte_buffer *buffer);
/* Convert a ::node::Buffer to a fast Buffer, as defined in the Node
Buffer documentation */
v8::Handle<v8::Value> MakeFastBuffer(v8::Handle<v8::Value> slowBuffer);
} // namespace node } // namespace node
} // namespace grpc } // namespace grpc

@ -80,7 +80,8 @@ Handle<Value> ParseMetadata(grpc_metadata *metadata_elements, size_t length) {
metadata_object->Set(key_string, array); metadata_object->Set(key_string, array);
} }
array->Set(index_map[elem->key], array->Set(index_map[elem->key],
NanNewBufferHandle(elem->value, elem->value_length)); MakeFastBuffer(
NanNewBufferHandle(elem->value, elem->value_length)));
index_map[elem->key] += 1; index_map[elem->key] += 1;
} }
return NanEscapeScope(metadata_object); return NanEscapeScope(metadata_object);

Loading…
Cancel
Save