-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
buffer: improve fill(number) performance #31489
Conversation
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.
LGTM % comments.
lib/buffer.js
Outdated
@@ -101,6 +104,13 @@ const { | |||
addBufferPrototypeMethods | |||
} = require('internal/buffer'); | |||
|
|||
const TypedArrayPrototype = ObjectGetPrototypeOf(Uint8Array.prototype); |
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.
const TypedArrayPrototype = ObjectGetPrototypeOf(Uint8Array.prototype); | |
const Uint8ArrayPrototype = ObjectGetPrototypeOf(Uint8Array.prototype); |
Uint8Array.prototype.constructor.name
is Uint8Array, not TypedArray.
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.
This line is correct as-is. The prototype of Uint8Array.prototype
is the %TypedArray%.prototype
.
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.
@devsnek is correct, this is the prototype of the prototype, which is TypedArray
. The same thing is done in internal/util/types.js
.
039c099
to
aa45188
Compare
I think we could also directly add TypedArray and its prototype to the primordials. I'll do that after this PR. |
aa45188
to
ac9de4f
Compare
if (res < 0) { | ||
if (res === -1) | ||
throw new ERR_INVALID_ARG_VALUE('value', value); | ||
throw new ERR_BUFFER_OUT_OF_BOUNDS(); |
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.
Future enhancement: res < 0
is sometimes a coercion-to-nan, sometimes not because bindingFill()
returns either undefined
or a number.
Changing Fill()
in node_buffer.cc to always return a number is probably beneficial for performance.
PR-URL: nodejs#31489 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
ac9de4f
to
59cba9a
Compare
This seems to have landed without first getting a clean CI run. |
Although if there was an impressive-benchmarks-mean-you-can-land-without-a-clean-CI rule, this would definitely qualify. 😍 |
PR-URL: #31489 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
PR-URL: #31489 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
PR-URL: #31489 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
Uses the native
typedArray.fill()
when filling with a number instead of calling into the binding fill function.Benchmark results:
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes