Skip to content

(DO NOT LAND YET) experiment: socket.read(buf) #6923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,15 @@ function howMuchToRead(n, state) {
}

// you can override either this method, or the async _read(n) below.
Readable.prototype.read = function(n) {
Readable.prototype.read = function(arg) {
var n;
var buf;
if (arg instanceof Buffer) {
buf = arg;
} else {
n = arg;
}

debug('read', n);
var state = this._readableState;
var nOrig = n;
Expand Down Expand Up @@ -352,7 +360,7 @@ Readable.prototype.read = function(n) {
if (state.length === 0)
state.needReadable = true;
// call internal read method
this._read(state.highWaterMark);
this._read(state.highWaterMark, buf);
state.sync = false;
}

Expand Down
15 changes: 12 additions & 3 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,14 @@ Object.defineProperty(Socket.prototype, 'bufferSize', {


// Just call handle.readStart until we have enough in the buffer
Socket.prototype._read = function(n) {
Socket.prototype._read = function(n, buf) {
debug('_read');

if (this._handle && !this._handle[0])
this._handle[0] = buf;
if (this.connecting || !this._handle) {
debug('_read wait for connection');
this.once('connect', () => this._read(n));
this.once('connect', () => this._read(n, buf));
} else if (!this._handle.reading) {
// not already reading, start the flow
debug('Socket._read readStart');
Expand Down Expand Up @@ -526,6 +528,9 @@ function onread(nread, buffer) {
var self = handle.owner;
assert(handle === self._handle, 'handle != self._handle');

var prealloc = buffer === handle[0];
handle[0] = undefined;

self._unrefTimer();

debug('onread', nread);
Expand All @@ -539,7 +544,11 @@ function onread(nread, buffer) {
// called again.

// Optimization: emit the original buffer with end points
var ret = self.push(buffer);
var ret;
if (prealloc)
ret = self.push(buffer.slice(0, nread));
else
ret = self.push(buffer);

if (handle.reading && !ret) {
handle.reading = false;
Expand Down
45 changes: 35 additions & 10 deletions src/stream_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,26 @@ void StreamWrap::OnAlloc(uv_handle_t* handle,


void StreamWrap::OnAllocImpl(size_t size, uv_buf_t* buf, void* ctx) {
buf->base = static_cast<char*>(malloc(size));
buf->len = size;
StreamWrap* wrap = static_cast<StreamWrap*>(ctx);
Environment* env = wrap->env();
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());

if (buf->base == nullptr && size > 0) {
FatalError(
"node::StreamWrap::DoAlloc(size_t, uv_buf_t*, void*)",
"Out Of Memory");
Local<Value> prealloc = wrap->object()->Get(0);
if (Buffer::HasInstance(prealloc)) {
buf->base = Buffer::Data(prealloc);
buf->len = Buffer::Length(prealloc);
wrap->allocated_ = false;
} else {
buf->base = static_cast<char*>(malloc(size));
buf->len = size;
wrap->allocated_ = true;

if (buf->base == nullptr && size > 0) {
FatalError(
"node::StreamWrap::DoAlloc(size_t, uv_buf_t*, void*)",
"Out Of Memory");
}
}
}

Expand Down Expand Up @@ -192,19 +205,25 @@ void StreamWrap::OnReadImpl(ssize_t nread,
Local<Object> pending_obj;

if (nread < 0) {
if (buf->base != nullptr)
if (buf->base != nullptr && wrap->allocated_)
free(buf->base);
wrap->allocated_ = false;
wrap->EmitData(nread, Local<Object>(), pending_obj);
return;
}

if (nread == 0) {
if (buf->base != nullptr)
if (buf->base != nullptr && wrap->allocated_)
free(buf->base);
wrap->allocated_ = false;
return;
}

char* base = static_cast<char*>(realloc(buf->base, nread));
char* base;
if (wrap->allocated_)
base = static_cast<char*>(realloc(buf->base, nread));
else
base = buf->base;
CHECK_LE(static_cast<size_t>(nread), buf->len);

if (pending == UV_TCP) {
Expand All @@ -217,8 +236,14 @@ void StreamWrap::OnReadImpl(ssize_t nread,
CHECK_EQ(pending, UV_UNKNOWN_HANDLE);
}

Local<Object> obj = Buffer::New(env, base, nread).ToLocalChecked();
Local<Object> obj;
if (!wrap->allocated_) {
obj = wrap->object()->Get(0).As<Object>();
} else {
obj = Buffer::New(env, base, nread).ToLocalChecked();
}
wrap->EmitData(nread, obj, pending_obj);
wrap->allocated_ = false;
}


Expand Down
1 change: 1 addition & 0 deletions src/stream_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class StreamWrap : public HandleWrap, public StreamBase {
void* ctx);

uv_stream_t* const stream_;
bool allocated_;
};


Expand Down