Browse Source

streams: Speed up by doing less work in the state ctors

v0.9.4-release
isaacs 12 years ago
parent
commit
f9caf7020c
  1. 19
      lib/_stream_readable.js
  2. 18
      lib/_stream_writable.js

19
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;

18
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

Loading…
Cancel
Save