Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

writable: make processBuffer inline-friendly

  • Loading branch information...
commit c130f72b08d20d90c3ca006d63a235e06f9f7c41 1 parent 313bad1
@isaacs authored
Showing with 17 additions and 30 deletions.
  1. +17 −30 lib/_stream_writable.js
View
47 lib/_stream_writable.js
@@ -198,43 +198,30 @@ function onwrite(stream, state, er) {
if (!finished) {
onwriteDrain(state, stream);
- onwriteProcessBuffer(state, stream);
+ if (!state.bufferProcessing && state.buffer.length)
+ onwriteProcessBuffer(state, stream);
}
}
+// if there's something in the buffer waiting, then process it
+// It would be nice if there were TCO in JS, and we could just
+// shift the top off the buffer and _write that, but that approach
+// causes RangeErrors when you have a very large number of very
+// small writes, and is not very efficient otherwise.
+//
+// if we didn't call the onwrite immediately, then
+// it means that we need to wait until it does.
+// also, that means that the chunk and cb are currently
+// being processed, so move the buffer counter past them.
function onwriteProcessBuffer(state, stream) {
- if (state.bufferProcessing || !state.buffer.length)
- return;
-
- // if there's something in the buffer waiting, then process it
- // It would be nice if there were TCO in JS, and we could just
- // shift the top off the buffer and _write that, but that approach
- // causes RangeErrors when you have a very large number of very
- // small writes, and is not very efficient otherwise.
state.bufferProcessing = true;
- for (var c = 0, l = state.buffer.length; c < l; c++) {
- var pending = state.buffer[c];
- var chunk = pending.chunk;
- var enc = pending.encoding;
- var cb = pending.cb;
-
- state.writecb = cb;
- state.writechunk = chunk;
- state.writing = true;
- state.sync = true;
- stream._write(chunk, enc, state.onwrite);
- state.sync = false;
-
- // if we didn't call the onwrite immediately, then
- // it means that we need to wait until it does.
- // also, that means that the chunk and cb are currently
- // being processed, so move the buffer counter past them.
- if (state.writing) {
- c++;
- break;
- }
+ for (var c = 0, l = state.buffer.length; c < l && !state.writing; c++) {
+ var p = state.buffer[c];
+ doWrite(stream, state, p.chunk, p.encoding, p.cb);
}
+ if (state.writing)
+ c++;
state.bufferProcessing = false;
if (c < state.buffer.length)
Please sign in to comment.
Something went wrong with that request. Please try again.