-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
stream: call _final synchronously #30597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const Transform = require('stream').Transform; | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| n: [2e6], | ||
| sync: ['yes', 'no'], | ||
| callback: ['yes', 'no'] | ||
| }); | ||
|
|
||
| function main({ n, sync, callback }) { | ||
| const b = Buffer.allocUnsafe(1024); | ||
| const s = new Transform(); | ||
| sync = sync === 'yes'; | ||
|
|
||
| const writecb = (cb) => { | ||
| if (sync) | ||
| cb(); | ||
| else | ||
| process.nextTick(cb); | ||
| }; | ||
|
|
||
| s._transform = (chunk, encoding, cb) => writecb(cb); | ||
|
|
||
| const cb = callback === 'yes' ? () => {} : null; | ||
|
|
||
| bench.start(); | ||
|
|
||
| let k = 0; | ||
| function run() { | ||
| while (k++ < n && s.write(b, cb)); | ||
| if (k >= n) | ||
| bench.end(n); | ||
| } | ||
| s.on('drain', run); | ||
| s.on('data', () => {}); | ||
| run(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2004,16 +2004,21 @@ class Http2Stream extends Duplex { | |
| _final(cb) { | ||
| const handle = this[kHandle]; | ||
| if (this.pending) { | ||
| this.once('ready', () => this._final(cb)); | ||
| this.once('ready', function() { | ||
| process.nextTick(() => this._final(cb)); | ||
| }); | ||
| } else if (handle !== undefined) { | ||
| debugStreamObj(this, '_final shutting down'); | ||
| const req = new ShutdownWrap(); | ||
| req.oncomplete = afterShutdown; | ||
| req.callback = cb; | ||
| req.handle = handle; | ||
| const err = handle.shutdown(req); | ||
| if (err === 1) // synchronous finish | ||
| return afterShutdown.call(req, 0); | ||
| // TODO: Why does this need to be in nextTick? | ||
|
||
| process.nextTick(() => { | ||
| const req = new ShutdownWrap(); | ||
| req.oncomplete = afterShutdown; | ||
| req.callback = cb; | ||
| req.handle = handle; | ||
| const err = handle.shutdown(req); | ||
| if (err === 1) // synchronous finish | ||
| return afterShutdown.call(req, 0); | ||
| }); | ||
| } else { | ||
| cb(); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this maybe be a good reason to keep
_finalbehind a nextTick, at least in a semver-patch PR? Or maybe, why does this need to be changed here but not in the other places where we use a similar pattern (e.g. net, http2)?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's one reason to keep
_finalbehind a nextTick but there are other reasons which make it not a good idea. I don't think this becomes a relevant problem once #29656 is merged.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should probably be changed in those places as well.