Skip to content

Commit

Permalink
stream: micro-optimize high water mark calculation
Browse files Browse the repository at this point in the history
Don't iterate over all 32 bits, use some hacker's delight bit twiddling
to compute the next power of two.

The logic can be reduced to `n = 1 << 32 - Math.clz32(n)` but then it
can't easily be backported to v2.x; Math.clz32() was added in V8 4.3.

PR-URL: #2479
Reviewed-By: Chris Dickinson <[email protected]>
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
  • Loading branch information
bnoordhuis authored and Fishrock123 committed Aug 24, 2015
1 parent f1f4b4c commit 1c6e014
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ function roundUpToNextPowerOf2(n) {
} else {
// Get the next highest power of 2
n--;
for (var p = 1; p < 32; p <<= 1) n |= n >> p;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
n++;
}
return n;
Expand Down

0 comments on commit 1c6e014

Please sign in to comment.