-
-
Notifications
You must be signed in to change notification settings - Fork 236
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
Speed up buffer creation. #92
Conversation
LGTM, but I think @feross would be the most knowledgeable about all the subtleties for this. |
Thanks @dcousens ! @feross - I'm also curious whether you would consider adding an option to not rely on prototype mutation (falling back to "augmenting" array instances). Without it, buffers are still very slow in some (most?) engines. For example here are results for Firefox 43:
I initially thought that clients would just be able to set If this sounds reasonable to you, I'll update this pull request with a tentative implementation. |
We can't get rid of the Removing
I don't want to expose any options that aren't in the node buffer, because then we'll have code that's browser-specific and could end up doing something different in future versions of node. We should solve the issue directly in this module, but that should be done in another issue/PR, IMO. |
Sounds good. I'll add the
I'll restore
I'm fine doing the detection inside this library (though there's the risk of being suboptimal for future browser implementations). Also, maybe I didn't explain myself correctly, I didn't mean an option at instantiation time but rather something like what |
Since 1d20f50 (#40), instances already gained all the `Buffer`'s methods via the prototype re-assignment. Skipping the `_augment` call yields significant speedups: ~50% faster small buffer creation: new(16) before: 595,757 ops/sec ±0.92% (91 runs sampled) new(16) after: 894,462 ops/sec ±0.69% (87 runs sampled) ~20% faster slice: slice before: 2,176,194 ops/sec ±1.59% (89 runs sampled) slice after: 2,624,209 ops/sec ±0.42% (99 runs sampled) Other benchmarks aren't significantly different.
@@ -1322,6 +1328,9 @@ var BP = Buffer.prototype | |||
|
|||
/** | |||
* Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods | |||
* | |||
* This function is also used externally by `typedarray-to-buffer`. |
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 is good.
I can fix up the issues and merge. I'll add new tests to catch the issues I manually caught. |
@@ -1322,6 +1328,9 @@ var BP = Buffer.prototype | |||
|
|||
/** | |||
* Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods | |||
* | |||
* This function is also used externally by `typedarray-to-buffer`. | |||
* | |||
*/ | |||
Buffer._augment = function _augment (arr) { | |||
arr.constructor = Buffer |
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.
I think we still need this line, or is-buffer
will break.
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.
Nevermind. This is inherited from Buffer.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.
Thinking about it a bit more, I think we actually need this line. Not for this package, but for typedarray-to-buffer
to work with is-buffer
. (This doesn't apply if you make the change you mention below about switching to setting the 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.
typedarray-to-buffer
is switched over now, so we don't need this line anymore.
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.
Great!
We can actually just remove |
Released as v4.1.0. |
This is released in browserify v13: https://github.com/substack/node-browserify/blob/master/changelog.markdown |
Since 1d20f50 (#40), instances already gained all the
Buffer
'smethods via prototype re-assignment. Skipping the
_augment
call yields significant speedups:
new(16)
before x 595,757 ops/sec ±0.92% (91 runs sampled)new(16)
after x 894,462 ops/sec ±0.69% (87 runs sampled)slice
before: 2,176,194 ops/sec ±1.59% (89 runs sampled)slice
after: 2,624,209 ops/sec ±0.42% (99 runs sampled)Other benchmarks aren't significantly different. (These numbers are for v8.)
Also removed the similarly redundant
_isBuffer
property. In case you'dlike to keep it (even though it's private), let me know.