From 344243db9232e668bff0467043735ecbbcaa1386 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 28 Jan 2010 19:13:13 -0800 Subject: [PATCH] Simplify send buffer logic --- lib/net.js | 36 ++++++++---------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/lib/net.js b/lib/net.js index ab79493bc2..93e9366af8 100644 --- a/lib/net.js +++ b/lib/net.js @@ -57,15 +57,15 @@ var ioWatchers = new FreeList("iowatcher", 100, function () { return new IOWatcher(); }); +var nb = 0; var buffers = new FreeList("buffer", 100, function (l) { - return new process.Buffer(1000); + return new process.Buffer(500); }); // Allocated on demand. var recvBuffer = null; - function allocRecvBuffer () { - recvBuffer = new process.Buffer(8*1024); + recvBuffer = new process.Buffer(40*1024); recvBuffer.used = 0; } @@ -192,19 +192,9 @@ Socket.prototype._sendString = function (data, encoding) { if (self.sendQueue.length == 0) { buffer = self._allocateSendBuffer(); } else { - // walk through the sendQueue, find the buffer with free space - for (var i = 0; i < self.sendQueue.length; i++) { - if (self.sendQueue[i].used == 0) { - buffer = self.sendQueue[i]; - break; - } - } - // if we didn't find one, take the last - // TODO what if this isn't empty but encoding == fd ? - if (!buffer) { - buffer = self._sendQueueLast(); - // if last buffer is used up - if (buffer.length == buffer.used) buffer = self._allocateSendBuffer(); + buffer = self._sendQueueLast(); + if (buffer.used == buffer.length) { + buffer = self._allocateSendBuffer(); } } @@ -279,17 +269,7 @@ Socket.prototype.send = function (data, encoding) { var inserted = false; data.sent = 0; data.used = data.length; - for (var i = 0; i < self.sendQueue.length; i++) { - if (self.sendQueue[i].used == 0) { - // if found, insert the data there - self.sendQueue.splice(i, 0, data); - inserted = true; - break; - } - } - if (!inserted) self.sendQueue.push(data); - self.sendQueueSize += data.used; } return this.flush(); @@ -322,7 +302,7 @@ Socket.prototype.flush = function () { var self = this; var bytesWritten; - while (self.sendQueue.length > 0) { + while (self.sendQueue.length) { if (!self.writable) throw new Error('Socket is not writable'); var b = self.sendQueue[0]; @@ -333,7 +313,7 @@ Socket.prototype.flush = function () { } if (b.sent == b.used) { - // this can be improved - save the buffer for later? + // shift! self.sendQueue.shift(); buffers.free(b); continue;