Skip to content

Commit

Permalink
src: don't call v8::Object::SetHiddenValue()
Browse files Browse the repository at this point in the history
Don't use v8::Object::SetHiddenValue() to keep a reference alive to the
buffer, we can just as easily do that from JS land and it's a lot faster
to boot.

Because the buffer is now a visible property of the write request
object, it's essential that we do *not* log it - we'd be effectively
serializing the whole buffer to a pretty-printed string.
  • Loading branch information
bnoordhuis committed Aug 9, 2013
1 parent f548433 commit f9b7714
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ Socket.prototype.send = function(buffer,
self.emit('error', ex);
}
else if (self._handle) {
var req = {};
var req = { buffer: buffer }; // Keep reference alive.
if (callback) {
req.callback = callback;
req.oncomplete = afterSend;
Expand Down
10 changes: 8 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,13 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
// Retain chunks
if (err === 0) req._chunks = chunks;
} else {
var enc = util.isBuffer(data) ? 'buffer' : encoding;
var enc;
if (util.isBuffer(data)) {
req.buffer = data; // Keep reference alive.
enc = 'buffer';
} else {
enc = encoding;
}
err = createWriteReq(req, this._handle, data, enc);
}

Expand Down Expand Up @@ -743,7 +749,7 @@ function afterWrite(status, handle, req) {
var self = handle.owner;
var state = self._writableState;
if (self !== process.stderr && self !== process.stdout)
debug('afterWrite', status, req);
debug('afterWrite', status);

// callback may come after call to destroy.
if (self.destroyed) {
Expand Down
4 changes: 0 additions & 4 deletions src/stream_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ using v8::Undefined;
using v8::Value;


static Cached<String> buffer_sym;
static Cached<String> bytes_sym;
static Cached<String> write_queue_size_sym;
static Cached<String> onread_sym;
Expand All @@ -63,7 +62,6 @@ void StreamWrap::Initialize(Handle<Object> target) {
initialized = true;

HandleScope scope(node_isolate);
buffer_sym = FIXED_ONE_BYTE_STRING(node_isolate, "buffer");
bytes_sym = FIXED_ONE_BYTE_STRING(node_isolate, "bytes");
write_queue_size_sym = FIXED_ONE_BYTE_STRING(node_isolate, "writeQueueSize");
onread_sym = FIXED_ONE_BYTE_STRING(node_isolate, "onread");
Expand Down Expand Up @@ -215,8 +213,6 @@ void StreamWrap::WriteBuffer(const FunctionCallbackInfo<Value>& args) {
char* storage = new char[sizeof(WriteWrap)];
WriteWrap* req_wrap = new(storage) WriteWrap(req_wrap_obj, wrap);

req_wrap_obj->SetHiddenValue(buffer_sym, buf_obj);

uv_buf_t buf;
WriteBuffer(buf_obj, &buf);

Expand Down
3 changes: 0 additions & 3 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class SendWrap : public ReqWrap<uv_udp_send_t> {


static Persistent<Function> constructor;
static Cached<String> buffer_sym;
static Cached<String> oncomplete_sym;
static Cached<String> onmessage_sym;

Expand Down Expand Up @@ -87,7 +86,6 @@ UDPWrap::~UDPWrap() {
void UDPWrap::Initialize(Handle<Object> target) {
HandleScope scope(node_isolate);

buffer_sym = FIXED_ONE_BYTE_STRING(node_isolate, "buffer");
oncomplete_sym = FIXED_ONE_BYTE_STRING(node_isolate, "oncomplete");
onmessage_sym = FIXED_ONE_BYTE_STRING(node_isolate, "onmessage");

Expand Down Expand Up @@ -261,7 +259,6 @@ void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) {
assert(length <= Buffer::Length(buffer_obj) - offset);

SendWrap* req_wrap = new SendWrap(req_wrap_obj, have_callback);
req_wrap->object()->SetHiddenValue(buffer_sym, buffer_obj);

uv_buf_t buf = uv_buf_init(Buffer::Data(buffer_obj) + offset,
length);
Expand Down

0 comments on commit f9b7714

Please sign in to comment.