recvBuffer -> pool

This commit is contained in:
Ryan Dahl 2010-03-19 23:09:16 -07:00
parent cc053e7df7
commit c66a0a739f

View File

@ -243,10 +243,10 @@ var buffers = new FreeList("buffer", 100, function (l) {
// Allocated on demand. // Allocated on demand.
var recvBuffer = null; var pool = null;
function allocRecvBuffer () { function allocNewPool () {
recvBuffer = new Buffer(40*1024); pool = new Buffer(40*1024);
recvBuffer.used = 0; pool.used = 0;
} }
@ -267,27 +267,27 @@ function _doFlush () {
function initStream (self) { function initStream (self) {
self._readWatcher = ioWatchers.alloc(); self._readWatcher = ioWatchers.alloc();
self._readWatcher.callback = function () { self._readWatcher.callback = function () {
// If this is the first recv (recvBuffer 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 recvBuffer, allocate a new one. // most of the pool, allocate a new one.
if (recvBuffer) { if (pool) {
if (recvBuffer.length - recvBuffer.used < 128) { if (pool.length - pool.used < 128) {
// discard the old recvBuffer. 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.
recvBuffer = null; pool = null;
allocRecvBuffer(); allocNewPool();
} }
} else { } else {
allocRecvBuffer(); allocNewPool();
} }
//debug('recvBuffer.used ' + recvBuffer.used); //debug('pool.used ' + pool.used);
var bytesRead; var bytesRead;
try { try {
bytesRead = read(self.fd, bytesRead = read(self.fd,
recvBuffer, pool,
recvBuffer.used, pool.used,
recvBuffer.length - recvBuffer.used); pool.length - pool.used);
} catch (e) { } catch (e) {
self.forceClose(e); self.forceClose(e);
return; return;
@ -307,30 +307,30 @@ function initStream (self) {
timeout.active(self); timeout.active(self);
var start = recvBuffer.used; var start = pool.used;
var end = recvBuffer.used + bytesRead; var end = pool.used + bytesRead;
if (!self._encoding) { if (!self._encoding) {
if (self._events && self._events['data']) { if (self._events && self._events['data']) {
// emit a slice // emit a slice
self.emit('data', recvBuffer.slice(start, end)); self.emit('data', pool.slice(start, end));
} }
// Optimization: emit the original buffer with end points // Optimization: emit the original buffer with end points
if (self.ondata) self.ondata(recvBuffer, start, end); if (self.ondata) self.ondata(pool, start, end);
} else { } else {
// TODO remove me - we should only output Buffer // TODO remove me - we should only output Buffer
var string; var string;
switch (self._encoding) { switch (self._encoding) {
case 'utf8': case 'utf8':
string = recvBuffer.utf8Slice(start, end); string = pool.utf8Slice(start, end);
break; break;
case 'ascii': case 'ascii':
string = recvBuffer.asciiSlice(start, end); string = pool.asciiSlice(start, end);
break; break;
case 'binary': case 'binary':
string = recvBuffer.binarySlice(start, end); string = pool.binarySlice(start, end);
break; break;
default: default:
throw new Error('Unsupported encoding ' + self._encoding + '. Use Buffer'); throw new Error('Unsupported encoding ' + self._encoding + '. Use Buffer');
@ -339,7 +339,7 @@ function initStream (self) {
} }
recvBuffer.used += bytesRead; pool.used += bytesRead;
} }
}; };
self.readable = false; self.readable = false;
@ -501,26 +501,26 @@ Stream.prototype.write = function (data, encoding) {
//debug('write string :' + JSON.stringify(data)); //debug('write string :' + JSON.stringify(data));
if (!recvBuffer) allocRecvBuffer(); if (!pool) allocNewPool();
if (recvBuffer.length - recvBuffer.used < bytes) { if (pool.length - pool.used < bytes) {
// not enough room - go to slow case // not enough room - go to slow case
return self._queueWrite(data, encoding); return self._queueWrite(data, encoding);
} }
var charsWritten; var charsWritten;
if (encoding == 'utf8') { if (encoding == 'utf8') {
recvBuffer.utf8Write(data, recvBuffer.used); pool.utf8Write(data, pool.used);
} else if (encoding == 'ascii') { } else if (encoding == 'ascii') {
// ascii // ascii
recvBuffer.asciiWrite(data, recvBuffer.used); pool.asciiWrite(data, pool.used);
} else { } else {
// binary // binary
recvBuffer.binaryWrite(data, recvBuffer.used); pool.binaryWrite(data, pool.used);
} }
buffer = recvBuffer; buffer = pool;
off = recvBuffer.used; off = pool.used;
len = bytes; len = bytes;
} }
@ -536,8 +536,8 @@ Stream.prototype.write = function (data, encoding) {
//debug('wrote ' + bytesWritten); //debug('wrote ' + bytesWritten);
// Note: if using the recvBuffer - we don't need to increase // Note: if using the pool - we don't need to increase
// recvBuffer.used because it was all sent. Just reuse that space. // pool.used because it was all sent. Just reuse that space.
if (bytesWritten == len) return true; if (bytesWritten == len) return true;
@ -548,8 +548,8 @@ Stream.prototype.write = function (data, encoding) {
data.used = data.length; data.used = data.length;
} else { } else {
// string // string
recvBuffer.used += bytesWritten; pool.used += bytesWritten;
data = recvBuffer.slice(off+bytesWritten, off+len+bytesWritten); data = pool.slice(off+bytesWritten, off+len+bytesWritten);
data.sent = 0; data.sent = 0;
data.used = data.length; data.used = data.length;
} }
@ -733,7 +733,7 @@ Stream.prototype.resume = function () {
Stream.prototype.forceClose = function (exception) { Stream.prototype.forceClose = function (exception) {
// recvBuffer 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; var b;