From f9caf7020c65d543f29cb81692c53027da2c564f Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 18 Dec 2012 18:49:42 -0800 Subject: [PATCH] streams: Speed up by doing less work in the state ctors --- lib/_stream_readable.js | 19 +++++-------------- lib/_stream_writable.js | 18 +++++++----------- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 9baa2e6bfa..f8982b1f08 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -24,7 +24,6 @@ Readable.ReadableState = ReadableState; var Stream = require('stream'); var util = require('util'); -var assert = require('assert'); var StringDecoder; util.inherits(Readable, Stream); @@ -33,29 +32,21 @@ function ReadableState(options, stream) { options = options || {}; // the argument passed to this._read(n,cb) - this.bufferSize = options.hasOwnProperty('bufferSize') ? - options.bufferSize : 16 * 1024; + this.bufferSize = options.bufferSize || 16 * 1024; // the point at which it stops calling _read() to fill the buffer - this.highWaterMark = options.hasOwnProperty('highWaterMark') ? - options.highWaterMark : 16 * 1024; + // Note: 0 is a valid value, means "don't call _read preemptively ever" + var hwm = options.highWaterMark; + this.highWaterMark = (hwm || hwm === 0) ? hwm : 16 * 1024; // the minimum number of bytes to buffer before emitting 'readable' // default to pushing everything out as fast as possible. - this.lowWaterMark = options.hasOwnProperty('lowWaterMark') ? - options.lowWaterMark : 0; + this.lowWaterMark = options.lowWaterMark || 0; // cast to ints. - assert(typeof this.bufferSize === 'number'); - assert(typeof this.lowWaterMark === 'number'); - assert(typeof this.highWaterMark === 'number'); this.bufferSize = ~~this.bufferSize; this.lowWaterMark = ~~this.lowWaterMark; this.highWaterMark = ~~this.highWaterMark; - assert(this.bufferSize >= 0); - assert(this.lowWaterMark >= 0); - assert(this.highWaterMark >= this.lowWaterMark, - this.highWaterMark + '>=' + this.lowWaterMark); this.buffer = []; this.length = 0; diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 3b015663e0..1a907daa41 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -36,22 +36,18 @@ function WritableState(options, stream) { options = options || {}; // the point at which write() starts returning false - this.highWaterMark = options.hasOwnProperty('highWaterMark') ? - options.highWaterMark : 16 * 1024; + // Note: 0 is a valid value, means that we always return false if + // the entire buffer is not flushed immediately on write() + var hwm = options.highWaterMark; + this.highWaterMark = (hwm || hwm === 0) ? hwm : 16 * 1024; // the point that it has to get to before we call _write(chunk,cb) // default to pushing everything out as fast as possible. - this.lowWaterMark = options.hasOwnProperty('lowWaterMark') ? - options.lowWaterMark : 0; + this.lowWaterMark = options.lowWaterMark || 0; // cast to ints. - assert(typeof this.lowWaterMark === 'number'); - assert(typeof this.highWaterMark === 'number'); this.lowWaterMark = ~~this.lowWaterMark; this.highWaterMark = ~~this.highWaterMark; - assert(this.lowWaterMark >= 0); - assert(this.highWaterMark >= this.lowWaterMark, - this.highWaterMark + '>=' + this.lowWaterMark); this.needDrain = false; // at the start of calling end() @@ -66,8 +62,8 @@ function WritableState(options, stream) { // should we decode strings into buffers before passing to _write? // this is here so that some node-core streams can optimize string // handling at a lower level. - this.decodeStrings = options.hasOwnProperty('decodeStrings') ? - options.decodeStrings : true; + var noDecode = options.decodeStrings === false; + this.decodeStrings = !noDecode; // not an actual buffer we keep track of, but a measurement // of how much we're waiting to get pushed to some underlying