Move buffer constants at the top of net.js
Remove some cruft.
This commit is contained in:
parent
e232cf3100
commit
fdae14070c
30
lib/net.js
30
lib/net.js
@ -2,6 +2,9 @@ var sys = require("sys");
|
|||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var events = require("events");
|
var events = require("events");
|
||||||
|
|
||||||
|
var kMinPoolSpace = 128;
|
||||||
|
var kPoolSize = 40*1024;
|
||||||
|
|
||||||
var debugLevel = process.env['NODE_DEBUG'] ? 1 : 0;
|
var debugLevel = process.env['NODE_DEBUG'] ? 1 : 0;
|
||||||
function debug () {
|
function debug () {
|
||||||
if (debugLevel > 0) sys.error.apply(this, arguments);
|
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.
|
// Allocated on demand.
|
||||||
var pool = null;
|
var pool = null;
|
||||||
function allocNewPool () {
|
function allocNewPool () {
|
||||||
pool = new Buffer(40*1024);
|
pool = new Buffer(kPoolSize);
|
||||||
pool.used = 0;
|
pool.used = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -265,16 +262,12 @@ function initStream (self) {
|
|||||||
self._readWatcher.callback = function () {
|
self._readWatcher.callback = function () {
|
||||||
// If this is the first recv (pool doesn't exist) or we've used up
|
// If this is the first recv (pool doesn't exist) or we've used up
|
||||||
// most of the pool, allocate a new one.
|
// most of the pool, allocate a new one.
|
||||||
if (pool) {
|
if (!pool || pool.length - pool.used < kMinPoolSpace) {
|
||||||
if (pool.length - pool.used < 128) {
|
|
||||||
// discard the old pool. Can't add to the free list because
|
// discard the old pool. Can't add to the free list because
|
||||||
// users might have refernces to slices on it.
|
// users might have refernces to slices on it.
|
||||||
pool = null;
|
pool = null;
|
||||||
allocNewPool();
|
allocNewPool();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
allocNewPool();
|
|
||||||
}
|
|
||||||
|
|
||||||
//debug('pool.used ' + pool.used);
|
//debug('pool.used ' + pool.used);
|
||||||
var bytesRead;
|
var bytesRead;
|
||||||
@ -338,8 +331,7 @@ function initStream (self) {
|
|||||||
};
|
};
|
||||||
self.readable = false;
|
self.readable = false;
|
||||||
|
|
||||||
// queue of buffers that need to be written to socket
|
// Queue of buffers and string that need to be written to socket.
|
||||||
// XXX use link list?
|
|
||||||
self._writeQueue = [];
|
self._writeQueue = [];
|
||||||
self._writeQueueEncoding = [];
|
self._writeQueueEncoding = [];
|
||||||
|
|
||||||
@ -445,7 +437,7 @@ Stream.prototype._writeOut = function (data, encoding) {
|
|||||||
} else {
|
} else {
|
||||||
assert(typeof data == 'string')
|
assert(typeof data == 'string')
|
||||||
|
|
||||||
if (!pool || pool.length - pool.used < 128) {
|
if (!pool || pool.length - pool.used < kMinPoolSpace) {
|
||||||
pool = null;
|
pool = null;
|
||||||
allocNewPool();
|
allocNewPool();
|
||||||
}
|
}
|
||||||
@ -665,11 +657,9 @@ Stream.prototype.forceClose = function (exception) {
|
|||||||
// pool is shared between sockets, so don't need to free it here.
|
// pool is shared between sockets, so don't need to free it here.
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var b;
|
// TODO would like to set _writeQueue to null to avoid extra object alloc,
|
||||||
while (this._writeQueue.length) {
|
// but lots of code assumes this._writeQueue is always an array.
|
||||||
b = this._writeQueue.shift();
|
this._writeQueue = [];
|
||||||
if (b instanceof Buffer) buffers.free(b);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this._writeWatcher) {
|
if (this._writeWatcher) {
|
||||||
this._writeWatcher.stop();
|
this._writeWatcher.stop();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user