-
Notifications
You must be signed in to change notification settings - Fork 229
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
Partially solves perf issues with the UInt8Array checks. #304
Conversation
Using
|
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.
looks good, do you also want me to update processNextickArgs to add that other case statement ?
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.
As discussed separately, the instanceof
check is not semantically equivalent to the type check carried out via O.p.toString
, but I suppose it's OKish.
build/files.js
Outdated
function _uint8ArrayToBuffer(chunk) { | ||
return Buffer.from(chunk); | ||
} | ||
function _isUint8Array(obj) { | ||
return Object.prototype.toString.call(obj) === '[object Uint8Array]' || Buffer.isBuffer(obj); | ||
return Buffer.isBuffer(obj) || (typeof obj !== 'string' && obj instanceof OurUint8Array); |
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.
Why not typeof obj === 'object'
?
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.
Actually now that I think about it, the typeof
check is just unnecessary complexity. instanceof
itself already does a type check anyways (as one of the first steps).
lib/_stream_readable.js
Outdated
function _uint8ArrayToBuffer(chunk) { | ||
return Buffer.from(chunk); | ||
} | ||
function _isUint8Array(obj) { | ||
return Object.prototype.toString.call(obj) === '[object Uint8Array]' || Buffer.isBuffer(obj); | ||
return Buffer.isBuffer(obj) || typeof obj !== 'string' && obj instanceof OurUint8Array; |
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.
Same.
lib/_stream_writable.js
Outdated
function _uint8ArrayToBuffer(chunk) { | ||
return Buffer.from(chunk); | ||
} | ||
function _isUint8Array(obj) { | ||
return Object.prototype.toString.call(obj) === '[object Uint8Array]' || Buffer.isBuffer(obj); | ||
return Buffer.isBuffer(obj) || typeof obj !== 'string' && obj instanceof OurUint8Array; |
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.
Same.
@@ -180,11 +180,12 @@ const headRegexp = /(^module.exports = \w+;?)/m | |||
/(?:var|const) Buffer = require\('buffer'\)\.Buffer;/ | |||
, `/*<replacement>*/ | |||
const Buffer = require('safe-buffer').Buffer | |||
const OurUint8Array = global.Uint8Array || function () {} |
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.
Another thing: Use var
instead of const
here (and in the other files below), to ensure that the instanceof
optimization definitely triggers.
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.
we are passing through babel anyway, these are all vars.
@bmeurer thanks, updated. |
The move to using In contrast, the |
I do not think so. Considering the cost of using the |
Ideally there'd be an instance type check for |
Fixes #302
This is a partial fix.
Node 6.11, using readable-stream 2.2.11:
Node 6.11, using readable-stream 2.3.2:
Node 6.11, using this patch:
I think we can do better.
cc @bmeurer