Skip to content

Commit

Permalink
Move buffer constants at the top of net.js
Browse files Browse the repository at this point in the history
Remove some cruft.
  • Loading branch information
ry committed Apr 1, 2010
1 parent e232cf3 commit fdae140
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand All @@ -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();
}

Expand Down Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit fdae140

Please sign in to comment.