question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optimize log2 in PoolThreadCache

See original GitHub issue

Optimize log2 in PoolThreadCache

Currently io.netty.buffer.PoolThreadCache uses log2 implementation based on a shift loop. It is not a most efficient way: https://gist.github.com/netudima/e1e8206bf58ff0ab6e2d637a4362bfc2

NettyLog2Benchmark.bits           avgt   25   2.612 ± 0.009  ns/op
NettyLog2Benchmark.cycle          avgt   25  10.909 ± 0.157  ns/op
NettyLog2Benchmark.leadingZeroes  avgt   25   1.921 ± 0.006  ns/op

# JMH version: 1.21
# VM version: JDK 11.0.5, OpenJDK 64-Bit Server VM, 11.0.5+10-b520.38
# CPU: 2,6 GHz 6-Core Intel Core i7

It looks like for the current versions of JDK using Integer.numberOfLeadingZeros (intrinsic) is an optimal approach.

Note: PoolChunk also has log2 but it is already implemented based on Integer.numberOfLeadingZeros.

Netty version

4.1.48, 4.0.56

JVM version (e.g. java -version)

JDK 11.0.5, OpenJDK 64-Bit Server VM, 11.0.5+10-b520.38

OS version (e.g. uname -a)

Darwin MBP 19.3.0 Darwin Kernel Version 19.3.0: Thu Jan 9 20:58:23 PST 2020; root:xnu-6153.81.5~1/RELEASE_X86_64 x86_64

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
netudimacommented, Mar 22, 2020

Thank you for the feedback and the tip. The results of the bench: https://gist.github.com/netudima/787176639eac62ae8e4ab8430f95e883

Loop:

Benchmark                              (dutyTokens)  Mode  Cnt    Score   Error  Units
NettyAllocatorBenchmark.getAndRelease             0  avgt   15  104.455 ± 1.604  ns/op
NettyAllocatorBenchmark.getAndRelease             5  avgt   15  107.287 ± 1.437  ns/op
NettyAllocatorBenchmark.getAndRelease            10  avgt   15  113.684 ± 4.852  ns/op
NettyAllocatorBenchmark.getAndRelease           100  avgt   15  258.181 ± 2.718  ns/op

LeadingZeroes:

Benchmark                              (dutyTokens)  Mode  Cnt    Score   Error  Units
NettyAllocatorBenchmark.getAndRelease             0  avgt   15   97.994 ± 0.797  ns/op
NettyAllocatorBenchmark.getAndRelease             5  avgt   15  102.724 ± 1.672  ns/op
NettyAllocatorBenchmark.getAndRelease            10  avgt   15  106.537 ± 1.813  ns/op
NettyAllocatorBenchmark.getAndRelease           100  avgt   15  255.368 ± 2.089  ns/op

I will send a PR soon.

1reaction
franz1981commented, Mar 22, 2020

Anyway, feel free to send the PR “regardless” the result of the bench (hopefully positive): it’s something that makes sense to change 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Francesco Nigro on Twitter: "#java #perftip: as @nitsanw has ...
Optimize log2 in PoolThreadCache Currently io.netty.buffer.PoolThreadCache uses log2 implementation based on a shift loop. It is not a most efficient way: ...
Read more >
4.1.49.Final Milestone - GitHub
Propagate ref to pool thread cache down in the allocation stack to av… #10166 by netudima was merged on ... Optimize log2 in...
Read more >
Fast computing of log2 for 64-bit integers - Stack Overflow
Intrinsic functions are really fast, but still are insufficient for a truly cross-platform, compiler-independent implementation of log2.
Read more >
PoolThreadCache xref - Netty
37 */ 38 final class PoolThreadCache { 39 40 private static final InternalLogger logger ... Small); 78 79 numShiftsNormalDirect = log2(directArena.
Read more >
Base-2 logarithm of symbolic input - MATLAB log2 - MathWorks
This MATLAB function returns the logarithm to the base 2 of X such that 2Y = X.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found