Large message latency improvements: remove a memcpy and a Buffer construtor call

pull/10416/head
murgatroid99 8 years ago
parent a1eefcea5f
commit c64601421b
  1. 39
      src/node/ext/byte_buffer.cc
  2. 4
      src/node/ext/byte_buffer.h
  3. 5
      src/node/ext/slice.cc

@ -45,6 +45,7 @@
namespace grpc { namespace grpc {
namespace node { namespace node {
using Nan::Callback;
using Nan::MaybeLocal; using Nan::MaybeLocal;
using v8::Function; using v8::Function;
@ -62,7 +63,11 @@ grpc_byte_buffer *BufferToByteBuffer(Local<Value> buffer) {
} }
namespace { namespace {
void delete_buffer(char *data, void *hint) { delete[] data; } void delete_buffer(char *data, void *hint) {
grpc_slice *slice = static_cast<grpc_slice *>(hint);
grpc_slice_unref(*slice);
delete slice;
}
} }
Local<Value> ByteBufferToBuffer(grpc_byte_buffer *buffer) { Local<Value> ByteBufferToBuffer(grpc_byte_buffer *buffer) {
@ -75,31 +80,15 @@ Local<Value> ByteBufferToBuffer(grpc_byte_buffer *buffer) {
Nan::ThrowError("Error initializing byte buffer reader."); Nan::ThrowError("Error initializing byte buffer reader.");
return scope.Escape(Nan::Undefined()); return scope.Escape(Nan::Undefined());
} }
grpc_slice slice = grpc_byte_buffer_reader_readall(&reader); grpc_slice *slice = new grpc_slice;
size_t length = GRPC_SLICE_LENGTH(slice); *slice = grpc_byte_buffer_reader_readall(&reader);
char *result = new char[length]; grpc_byte_buffer_reader_destroy(&reader);
memcpy(result, GRPC_SLICE_START_PTR(slice), length); char *result = reinterpret_cast<char *>(GRPC_SLICE_START_PTR(*slice));
grpc_slice_unref(slice); size_t length = GRPC_SLICE_LENGTH(*slice);
return scope.Escape(MakeFastBuffer( Local<Value> buf =
Nan::NewBuffer(result, length, delete_buffer, NULL).ToLocalChecked())); Nan::NewBuffer(result, length, delete_buffer, slice).ToLocalChecked();
return scope.Escape(buf);
} }
Local<Value> MakeFastBuffer(Local<Value> slowBuffer) {
Nan::EscapableHandleScope scope;
Local<Object> globalObj = Nan::GetCurrentContext()->Global();
MaybeLocal<Value> constructorValue = Nan::Get(
globalObj, Nan::New("Buffer").ToLocalChecked());
Local<Function> bufferConstructor = Local<Function>::Cast(
constructorValue.ToLocalChecked());
const int argc = 3;
Local<Value> consArgs[argc] = {
slowBuffer,
Nan::New<Number>(::node::Buffer::Length(slowBuffer)),
Nan::New<Number>(0)
};
MaybeLocal<Object> fastBuffer = Nan::NewInstance(bufferConstructor,
argc, consArgs);
return scope.Escape(fastBuffer.ToLocalChecked());
}
} // namespace node } // namespace node
} // namespace grpc } // namespace grpc

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

@ -37,7 +37,6 @@
#include <grpc/support/alloc.h> #include <grpc/support/alloc.h>
#include "slice.h" #include "slice.h"
#include "byte_buffer.h"
namespace grpc { namespace grpc {
namespace node { namespace node {
@ -93,9 +92,9 @@ Local<Value> CreateBufferFromSlice(const grpc_slice slice) {
Nan::EscapableHandleScope scope; Nan::EscapableHandleScope scope;
grpc_slice *slice_ptr = new grpc_slice; grpc_slice *slice_ptr = new grpc_slice;
*slice_ptr = grpc_slice_ref(slice); *slice_ptr = grpc_slice_ref(slice);
return scope.Escape(MakeFastBuffer(Nan::NewBuffer( return scope.Escape(Nan::NewBuffer(
const_cast<char *>(reinterpret_cast<const char *>(GRPC_SLICE_START_PTR(*slice_ptr))), const_cast<char *>(reinterpret_cast<const char *>(GRPC_SLICE_START_PTR(*slice_ptr))),
GRPC_SLICE_LENGTH(*slice_ptr), SliceFreeCallback, slice_ptr).ToLocalChecked())); GRPC_SLICE_LENGTH(*slice_ptr), SliceFreeCallback, slice_ptr).ToLocalChecked());
} }
} // namespace node } // namespace node

Loading…
Cancel
Save