Skip to content
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

stream: avoid possible slow paths w UInt8Array #13956

Closed
wants to merge 1 commit into from

Conversation

mcollina
Copy link
Member

@mcollina mcollina commented Jun 28, 2017

A chunk validity checks verifie if a chunk is a UInt8Array.
We should defer it as it might be very expensive in older Node.js
platforms.

It avoid the UInt8Array checks if the streams are in objectMode: true.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

stream

@nodejs-github-bot nodejs-github-bot added the stream Issues and PRs related to the stream subsystem. label Jun 28, 2017
@mcollina
Copy link
Member Author

@mscdex
Copy link
Contributor

mscdex commented Jun 28, 2017

Have you ran benchmarks to check the effects of this? I would think non-object mode streams are more common than object mode streams and this could negatively non-object mode streams because the Uint8Array check would allow the conditional to finish immediately in those cases.

@mcollina
Copy link
Member Author

Have you ran benchmarks to check the effects of this?

This is mostly related to readable-stream, after the whole transpiling process. In Node core, things should be equivalent. I discovered some of those while hunting for nodejs/readable-stream#304.

I would think non-object mode streams are more common than object mode streams and this could negatively non-object mode streams because the Uint8Array check would allow the conditional to finish immediately in those cases.

All conditions are at && so for Buffer and UInt8Array should not change anything as all conditions would have to be positively evaluated.

chunk !== undefined &&
!state.objectMode) {
!state.objectMode &&
!Stream._isUint8Array(chunk)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mscdex are you referring to this one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right about this, I will need to check if this does not negatively effect benchmarks.

@@ -248,7 +248,7 @@ function validChunk(stream, state, chunk, cb) {
Writable.prototype.write = function(chunk, encoding, cb) {
var state = this._writableState;
var ret = false;
var isBuf = Stream._isUint8Array(chunk) && !state.objectMode;
var isBuf = !state.objectMode && Stream._isUint8Array(chunk);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mscdex this is equivalent.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my mind this is surely going to be be faster when !state.objectMode.

A chunk validity checks verifie if a chunk is a UInt8Array.
We should defer it as it might be very expensive in older Node.js
platforms.
@mcollina mcollina force-pushed the change-boolean-checks branch from 4f6fa5c to e66fe47 Compare June 28, 2017 13:28
@mcollina
Copy link
Member Author

@mscdex I've updated the problematic part, and just left with the change on _stream_writable.

@mscdex
Copy link
Contributor

mscdex commented Jun 28, 2017

LGTM now.

@mcollina
Copy link
Member Author

Landed as 0279602

@mcollina mcollina closed this Jun 30, 2017
@mcollina mcollina deleted the change-boolean-checks branch June 30, 2017 17:18
mcollina added a commit that referenced this pull request Jun 30, 2017
A chunk validity checks verifie if a chunk is a UInt8Array.
We should defer it as it might be very expensive in older Node.js
platforms.

PR-URL: #13956
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
addaleax pushed a commit to addaleax/node that referenced this pull request Jul 3, 2017
A chunk validity checks verifie if a chunk is a UInt8Array.
We should defer it as it might be very expensive in older Node.js
platforms.

PR-URL: nodejs#13956
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
@addaleax addaleax mentioned this pull request Jul 3, 2017
addaleax pushed a commit that referenced this pull request Jul 11, 2017
A chunk validity checks verifie if a chunk is a UInt8Array.
We should defer it as it might be very expensive in older Node.js
platforms.

PR-URL: #13956
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
addaleax pushed a commit that referenced this pull request Jul 18, 2017
A chunk validity checks verifie if a chunk is a UInt8Array.
We should defer it as it might be very expensive in older Node.js
platforms.

PR-URL: #13956
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
@MylesBorins
Copy link
Contributor

MylesBorins commented Aug 14, 2017

@mcollina I'm pretty sure this doesn't apply to v6.x the check is way different.

var isBuf = (chunk instanceof Buffer);

Can you add dont-land-on-v6.x label if this shouldn't be backported

@mcollina
Copy link
Member Author

@MylesBorins this should not be backported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.