From fdae14070cde99d19de2ec03697b3151c1b8cb7f Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 1 Apr 2010 16:46:37 -0700 Subject: [PATCH] Move buffer constants at the top of net.js Remove some cruft. --- lib/net.js | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/lib/net.js b/lib/net.js index a4b2e4ad411..dd415f7d23f 100644 --- a/lib/net.js +++ b/lib/net.js @@ -2,6 +2,9 @@ var sys = require("sys"); var fs = require("fs"); var events = require("events"); +var kMinPoolSpace = 128; +var kPoolSize = 40*1024; + var debugLevel = process.env['NODE_DEBUG'] ? 1 : 0; function debug () { if (debugLevel > 0) sys.error.apply(this, arguments); @@ -236,16 +239,10 @@ var ioWatchers = new FreeList("iowatcher", 100, function () { }); -var nb = 0; -var buffers = new FreeList("buffer", 100, function (l) { - return new Buffer(l); -}); - - // Allocated on demand. var pool = null; function allocNewPool () { - pool = new Buffer(40*1024); + pool = new Buffer(kPoolSize); pool.used = 0; } @@ -265,14 +262,10 @@ function initStream (self) { self._readWatcher.callback = function () { // If this is the first recv (pool doesn't exist) or we've used up // most of the pool, allocate a new one. - if (pool) { - if (pool.length - pool.used < 128) { - // discard the old pool. Can't add to the free list because - // users might have refernces to slices on it. - pool = null; - allocNewPool(); - } - } else { + if (!pool || pool.length - pool.used < kMinPoolSpace) { + // discard the old pool. Can't add to the free list because + // users might have refernces to slices on it. + pool = null; allocNewPool(); } @@ -338,8 +331,7 @@ function initStream (self) { }; self.readable = false; - // queue of buffers that need to be written to socket - // XXX use link list? + // Queue of buffers and string that need to be written to socket. self._writeQueue = []; self._writeQueueEncoding = []; @@ -445,7 +437,7 @@ Stream.prototype._writeOut = function (data, encoding) { } else { assert(typeof data == 'string') - if (!pool || pool.length - pool.used < 128) { + if (!pool || pool.length - pool.used < kMinPoolSpace) { pool = null; allocNewPool(); } @@ -665,11 +657,9 @@ Stream.prototype.forceClose = function (exception) { // pool is shared between sockets, so don't need to free it here. var self = this; - var b; - while (this._writeQueue.length) { - b = this._writeQueue.shift(); - if (b instanceof Buffer) buffers.free(b); - } + // TODO would like to set _writeQueue to null to avoid extra object alloc, + // but lots of code assumes this._writeQueue is always an array. + this._writeQueue = []; if (this._writeWatcher) { this._writeWatcher.stop();