-
Notifications
You must be signed in to change notification settings - Fork 30k
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: faster case for create buffer from empty string #4414
Conversation
I'm confused, so it got slower after this patch? It went from 2767 ops/sec to 605 ops/sec and 2599 ops/sec to 1249 ops/sec? |
@mscdex I updated the results. the buffer result is noisy because I used old version(not lastest version) to run it. |
@@ -105,6 +105,10 @@ function fromString(string, encoding) { | |||
encoding = 'utf8'; | |||
|
|||
var length = byteLength(string, encoding); | |||
|
|||
if (length === 0) | |||
return new Buffer(0); |
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.
new
is unnecessary anymore since it forces a return of a new Uint8Array
.
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.
@JacksonTian can you address this?
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 prefer use new
with constructor.
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 do, too. I think there's even a linter rule in place for it. Are you fine with it @trevnorris?
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.
@silverwind The code works fine, but we're allocating an unnecessary object. Though I doubt that has an impact on performance either. Part of the point is that Buffer is not an actual constructor anymore. It's now more of a factory method. But again it's nothing serious or is a change that needs to be made.
One comment but LGTM. |
New CI, the last one appears to have died completely: https://ci.nodejs.org/job/node-test-pull-request/1246/ |
@JacksonTian ... can you rebase this and update the |
Hi, @jasnell , the branch rebased.
|
514aa5a
to
03579ba
Compare
|
||
function main(conf) { | ||
var n = +conf.n; | ||
bench.start(); | ||
for (let i = 0; i < n * 1024; i++) { | ||
Buffer.from(zero); | ||
conf.type === 'buffer' ? Buffer.from(zeroBuffer) : Buffer.from(zeroString); |
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.
The change to lib/buffer.js LGTM but I wonder if it makes a difference on the benchmark numbers when you hoist out the conditional like this:
if (conf.type === 'buffer')
for (let i = 0; i < n * 1024; i++) Buffer.from(zeroBuffer);
else if (conf.type === 'string')
for (let i = 0; i < n * 1024; i++) Buffer.from(zeroString);
Also, I'm not sure if crankshaft will constant-fold the expression n * 1024
away, you might want to check if caching it makes a difference.
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 it's unrelated. these code is running in difference process when benchmarking it.
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.
Sorry if I'm being unclear. What I mean is that the overhead of the check and the multiplication may affect the benchmark results. They probably don't but it would be good to check.
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.
You mean the conf.type === 'buffer'
will be run much times?
在 2016年3月23日,下午9:53,Ben Noordhuis <notifications@github.com mailto:notifications@github.com> 写道:
In benchmark/buffers/buffer_zero.js #4414 (comment):
function main(conf) {
var n = +conf.n;
bench.start();
for (let i = 0; i < n * 1024; i++) {
- Buffer.from(zero);
- conf.type === 'buffer' ? Buffer.from(zeroBuffer) : Buffer.from(zeroString);
Sorry if I'm being unclear. What I mean is that the overhead of the check and the multiplication may affect the benchmark results. They probably don't but it would be good to check.—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub https://github.com/nodejs/node/pull/4414/files/03579ba47a711923c67215d63385b60cdf473c98#r57161722
When create Buffer from empty string will touch C++ binding also. This patch can improve edge case ~70% faster.
Hi, @bnoordhuis I update the benchmark script. following is result:
|
LGTM if @bnoordhuis is happy and CI is green |
When create Buffer from empty string will touch C++ binding also. This patch can improve edge case ~70% faster. PR-URL: #4414 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
CI was green. Landed in afd821a |
When create Buffer from empty string will touch C++ binding also. This patch can improve edge case ~70% faster. PR-URL: #4414 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Hm, it looks like this is failing on v5.x right now. Apparently, the following no longer throws an error:
|
It looks like master has https://github.com/nodejs/node/blame/master/lib/buffer.js#L211 and v5.x does not. I'm not sure if that was intentional or not, but I'm a little weary of including this in v5.x for now. I'm going to leave it out of v5.10.0 and we can reevaluate |
Given that the change was an additional throw (and therefore semver major),
|
Sounds good to me. Thanks! |
When create Buffer from empty string will touch
C++ binding also.
This patch can improve edge case ~70% faster.
following is benchmark results.