Browse Source

Optimize: Use callbacks instead of events in net2

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
979f5889d5
  1. 9
      lib/http2.js
  2. 32
      lib/net.js

9
lib/http2.js

@ -387,12 +387,11 @@ function connectionListener (socket) {
// we need to keep track of the order they were sent. // we need to keep track of the order they were sent.
var responses = []; var responses = [];
socket.addListener('dataLite', function (d, start, end) { socket.ondata = function (d, start, end) {
parser.execute(d, start, end - start); parser.execute(d, start, end - start);
}); };
// is this really needed? socket.onend = function () {
socket.addListener('end', function () {
parser.finish(); parser.finish();
// unref the parser for easy gc // unref the parser for easy gc
freeParser(parser); freeParser(parser);
@ -402,7 +401,7 @@ function connectionListener (socket) {
} else { } else {
responses[responses.length-1].closeOnFinish = true; responses[responses.length-1].closeOnFinish = true;
} }
}); };
parser.socket = socket; parser.socket = socket;
// The following callback is issued after the headers have been read on a // The following callback is issued after the headers have been read on a

32
lib/net.js

@ -1,3 +1,4 @@
var sys = require("./sys");
var debugLevel = 0; var debugLevel = 0;
if ('NODE_DEBUG' in process.ENV) debugLevel = 1; if ('NODE_DEBUG' in process.ENV) debugLevel = 1;
function debug (x) { function debug (x) {
@ -118,19 +119,23 @@ function Socket (peerInfo) {
if (!recvMsg.fd && bytesRead == 0) { if (!recvMsg.fd && bytesRead == 0) {
self.readable = false; self.readable = false;
self._readWatcher.stop(); self._readWatcher.stop();
self.emit('end');
if (self._events && self._events['end']) self.emit('end');
if (self.onend) self.onend();
if (!self.writable) self.forceClose(); if (!self.writable) self.forceClose();
} else if (bytesRead > 0) { } else if (bytesRead > 0) {
var start = recvBuffer.used; var start = recvBuffer.used;
var end = recvBuffer.used + bytesRead; var end = recvBuffer.used + bytesRead;
if (self.listeners('data').length) {
if (self._events && self._events['data']) {
// emit a slice // emit a slice
self.emit('data', recvBuffer.slice(start, end)); self.emit('data', recvBuffer.slice(start, end));
} }
if (self.listeners('dataLite').length) {
// emit the original buffer with end points. // Optimization: emit the original buffer with end points
self.emit('dataLite', recvBuffer, start, end); if (self.ondata) self.ondata(recvBuffer, start, end);
}
recvBuffer.used += bytesRead; recvBuffer.used += bytesRead;
} }
}; };
@ -141,15 +146,17 @@ function Socket (peerInfo) {
self.sendQueueSize = 0; // in bytes, not to be confused with sendQueue.length! self.sendQueueSize = 0; // in bytes, not to be confused with sendQueue.length!
self.sendMessageQueueSize = 0; // number of messages remaining to be sent self.sendMessageQueueSize = 0; // number of messages remaining to be sent
self._doFlush = function () { self._doFlush = function () {
/* Socket becomes writeable on connect() but don't flush if there's // Socket becomes writeable on connect() but don't flush if there's
* nothing actually to write */ // nothing actually to write
if ((self.sendQueueSize == 0) && (self.sendMessageQueueSize == 0)) { if ((self.sendQueueSize == 0) && (self.sendMessageQueueSize == 0)) {
return; return;
} }
if (self.flush()) { if (self.flush()) {
assert(self.sendQueueSize == 0); assert(self.sendQueueSize == 0);
assert(self.sendMessageQueueSize == 0); assert(self.sendMessageQueueSize == 0);
self.emit("drain");
if (self._events && self._events['drain']) self.emit("drain");
if (self.ondrain) self.ondrain(); // Optimization
} }
}; };
self._writeWatcher = ioWatchers.alloc(); self._writeWatcher = ioWatchers.alloc();
@ -302,7 +309,8 @@ Socket.prototype.sendFD = function(socketToPass) {
}; };
// Flushes the write buffer out. Emits "drain" if the buffer is empty. // Flushes the write buffer out.
// Returns true if the entire buffer was flushed.
Socket.prototype.flush = function () { Socket.prototype.flush = function () {
var self = this; var self = this;
@ -314,7 +322,7 @@ Socket.prototype.flush = function () {
if (b == END_OF_FILE) { if (b == END_OF_FILE) {
self._shutdown(); self._shutdown();
break; return false;
} }
if (b.sent == b.used) { if (b.sent == b.used) {
@ -338,7 +346,7 @@ Socket.prototype.flush = function () {
} }
} catch (e) { } catch (e) {
self.forceClose(e); self.forceClose(e);
return; return false;
} }
if (bytesWritten === null) { if (bytesWritten === null) {

Loading…
Cancel
Save