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 Stream = require('stream');
var util = require('util'); var util = require('util');
var assert = require('assert');
var StringDecoder; var StringDecoder;
util.inherits(Readable, Stream); util.inherits(Readable, Stream);
@ -33,29 +32,21 @@ function ReadableState(options, stream) {
options = options || {}; options = options || {};
// the argument passed to this._read(n,cb) // the argument passed to this._read(n,cb)
this.bufferSize = options.hasOwnProperty('bufferSize') ? this.bufferSize = options.bufferSize || 16 * 1024;
options.bufferSize : 16 * 1024;
// the point at which it stops calling _read() to fill the buffer // the point at which it stops calling _read() to fill the buffer
this.highWaterMark = options.hasOwnProperty('highWaterMark') ? // Note: 0 is a valid value, means "don't call _read preemptively ever"
options.highWaterMark : 16 * 1024; var hwm = options.highWaterMark;
this.highWaterMark = (hwm || hwm === 0) ? hwm : 16 * 1024;
// the minimum number of bytes to buffer before emitting 'readable' // the minimum number of bytes to buffer before emitting 'readable'
// default to pushing everything out as fast as possible. // default to pushing everything out as fast as possible.
this.lowWaterMark = options.hasOwnProperty('lowWaterMark') ? this.lowWaterMark = options.lowWaterMark || 0;
options.lowWaterMark : 0;
// cast to ints. // cast to ints.
assert(typeof this.bufferSize === 'number');
assert(typeof this.lowWaterMark === 'number');
assert(typeof this.highWaterMark === 'number');
this.bufferSize = ~~this.bufferSize; this.bufferSize = ~~this.bufferSize;
this.lowWaterMark = ~~this.lowWaterMark; this.lowWaterMark = ~~this.lowWaterMark;
this.highWaterMark = ~~this.highWaterMark; this.highWaterMark = ~~this.highWaterMark;
assert(this.bufferSize >= 0);
assert(this.lowWaterMark >= 0);
assert(this.highWaterMark >= this.lowWaterMark,
this.highWaterMark + '>=' + this.lowWaterMark);
this.buffer = []; this.buffer = [];
this.length = 0; this.length = 0;

18
lib/_stream_writable.js

@ -36,22 +36,18 @@ function WritableState(options, stream) {
options = options || {}; options = options || {};
// the point at which write() starts returning false // the point at which write() starts returning false
this.highWaterMark = options.hasOwnProperty('highWaterMark') ? // Note: 0 is a valid value, means that we always return false if
options.highWaterMark : 16 * 1024; // 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) // the point that it has to get to before we call _write(chunk,cb)
// default to pushing everything out as fast as possible. // default to pushing everything out as fast as possible.
this.lowWaterMark = options.hasOwnProperty('lowWaterMark') ? this.lowWaterMark = options.lowWaterMark || 0;
options.lowWaterMark : 0;
// cast to ints. // cast to ints.
assert(typeof this.lowWaterMark === 'number');
assert(typeof this.highWaterMark === 'number');
this.lowWaterMark = ~~this.lowWaterMark; this.lowWaterMark = ~~this.lowWaterMark;
this.highWaterMark = ~~this.highWaterMark; this.highWaterMark = ~~this.highWaterMark;
assert(this.lowWaterMark >= 0);
assert(this.highWaterMark >= this.lowWaterMark,
this.highWaterMark + '>=' + this.lowWaterMark);
this.needDrain = false; this.needDrain = false;
// at the start of calling end() // at the start of calling end()
@ -66,8 +62,8 @@ function WritableState(options, stream) {
// should we decode strings into buffers before passing to _write? // should we decode strings into buffers before passing to _write?
// this is here so that some node-core streams can optimize string // this is here so that some node-core streams can optimize string
// handling at a lower level. // handling at a lower level.
this.decodeStrings = options.hasOwnProperty('decodeStrings') ? var noDecode = options.decodeStrings === false;
options.decodeStrings : true; this.decodeStrings = !noDecode;
// not an actual buffer we keep track of, but a measurement // not an actual buffer we keep track of, but a measurement
// of how much we're waiting to get pushed to some underlying // of how much we're waiting to get pushed to some underlying

Loading…
Cancel
Save