GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Allocate bytebuf without magazine lock when threads get collisions by laosijikaichele · Pull Request #14534 · netty/netty

Conversation

@laosijikaichele

@laosijikaichele laosijikaichele commented Dec 4, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Motivation:

For the AdaptivePoolingAllocator, thread picks a magazine based on the thread id.

It's possible that multi-threads pick the same magazine, typically when total threads counts on AdaptivePoolingAllocator exceed AdaptivePoolingAllocator.MAX_STRIPES, this will likely cause threads collisions on acquiring the same magazine lock, which will hurt performance.

To improve performance, we can try to allocate bytebuf without magazine lock when threads get collision.

Modification:

When threads get collision on allocating bytebuf, try to allocate from the NEXT_IN_LINE or centralQueue, which does not need holding the magazine's lock.

Result:

On my computer, the AdaptivePoolingAllocator.MAX_STRIPES value is 16, so I set threads number to 32 on the JMH methods with: @Threads(32), which will make multi-threads collision on the same magazine easily happen.

JMH benchmark class: AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark

Before PR:

Benchmark                                                                         (size)   Mode  Cnt         Score         Error  Units
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   00064  thrpt   20  46739546.208 ± 2160446.387  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   00256  thrpt   20  47588931.166 ± 1381959.617  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   01024  thrpt   20  45249206.622 ± 1402279.203  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   04096  thrpt   20  42326001.800 ±  850575.489  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     00064  thrpt   20  41291766.588 ± 2900577.209  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     00256  thrpt   20  44765730.938 ± 1015982.545  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     01024  thrpt   20  47533832.397 ± 4210977.985  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     04096  thrpt   20  43889158.191 ± 2304917.563  ops/s

[358.474s][info   ][gc,heap,exit] Heap
[358.474s][info   ][gc,heap,exit]  garbage-first heap   total 266240K, used 51436K [0x0000000700000000, 0x0000000800000000)
[358.474s][info   ][gc,heap,exit]   region size 2048K, 25 young (51200K), 3 survivors (6144K)
[358.474s][info   ][gc,heap,exit]  Metaspace       used 12310K, committed 12544K, reserved 1114112K
[358.474s][info   ][gc,heap,exit]   class space    used 1506K, committed 1600K, reserved 1048576K

After PR:

Benchmark                                                                         (size)   Mode  Cnt         Score         Error  Units
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   00064  thrpt   20  57809451.614 ± 2048986.729  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   00256  thrpt   20  56674394.370 ± 1417278.749  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   01024  thrpt   20  49802027.805 ± 1380102.638  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   04096  thrpt   20  51298374.215 ±  890586.221  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     00064  thrpt   20  48191848.295 ± 1178419.524  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     00256  thrpt   20  48231634.875 ± 4475455.680  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     01024  thrpt   20  52460274.226 ± 1018550.798  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     04096  thrpt   20  49182390.329 ± 1455722.168  ops/s

[360.090s][info   ][gc,heap,exit] Heap
[360.090s][info   ][gc,heap,exit]  garbage-first heap   total 266240K, used 50938K [0x0000000700000000, 0x0000000800000000)
[360.090s][info   ][gc,heap,exit]   region size 2048K, 24 young (49152K), 3 survivors (6144K)
[360.090s][info   ][gc,heap,exit]  Metaspace       used 12320K, committed 12544K, reserved 1114112K
[360.090s][info   ][gc,heap,exit]   class space    used 1506K, committed 1600K, reserved 1048576K

We can see relatively 7%~24% performance improvement.

This approach will not record the size histograms as it does not hold the magazine lock.

It's reasonable to not record the size on this scenario:

  1. Because compute the preferred chunk size is a somewhat expensive operation, so we skip it when threads get collisions, which means at the moment, it's more important to reduce the contention overhead than record the size histograms.
  2. If we successfully allocated bytebuf without lock, which means the size is not too much deviated than current histograms, which make it less urgent to re-calculate the size.
  3. If we failed allocated bytebuf without lock, then it will continue to try to acquire the magazine lock and will record the size once it get the lock.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

He-Pin, linking12, and franz1981 reacted with thumbs up emoji
All reactions
  • 👍 3 reactions
@laosijikaichele
laosijikaichele force-pushed the 4.1-light-lock branch from a55126c to af1c110 Compare December 4, 2024 07:07
@normanmaurer
normanmaurer requested a review from chrisvest December 4, 2024 15:25
@normanmaurer

normanmaurer commented Dec 4, 2024

Copy link
Copy Markdown
Member

@laosijikaichele nice work!

laosijikaichele reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

normanmaurer
normanmaurer approved these changes Dec 4, 2024
View reviewed changes
@normanmaurer
normanmaurer requested a review from franz1981 December 4, 2024 15:27
@normanmaurer normanmaurer added this to the 4.1.116.Final milestone Dec 4, 2024
@franz1981

franz1981 commented Dec 4, 2024

Copy link
Copy Markdown
Contributor

I'm trying to understand the drawbacks here, so let me ask some questions...

  1. if the lock is not acquired and NEXT in line is available to be used, it means that a whole Chunk of some capacity is used, which is good
  2. what happen to the acquired chunk (let's say 1 MB chunk - for 1K allocation)?

My understanding will say: it will fully return once is released - but this assume that release happen immediately which is not always the case...
If the same thread has to further perform other allocations (let's say it has performed a write and the network is congested...i.e. the chunk cannot be released back) - it cannot make use of the same chunk and will compete again for a new one (or the one which it didn't acquired before!).
This means as well that such chunk is not available to the colliding other threads which probably could reuse it and amortize the cost of owning it - thanks to the size hint collected (assuming to be accurate to predict the allocation rate/size).
Makes sense?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

chrisvest
chrisvest reviewed Dec 4, 2024
View reviewed changes

@chrisvest chrisvest left a comment

Copy link
Copy Markdown
Member

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 reduces the likelihood of expanding the magazine array. If we are generally able to maintain on-par or better performance with fewer magazines, then overall that's a win, as we in theory will do the same work with less memory.

I'm less convinced about pulling from the central queue. Those chunks are less likely to be used "soon" relative to any magazine buffers, and the central queue is, I think, very likely to have chunks available. The central queue is also a place where we'd consider trimming off excess memory and return it to the system, but we can't do that if we've nibbled some allocations off those chunks.

Have you tried running the benchmark with the 'central queue' bit removed?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

normanmaurer reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 4, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Thanks for the review, I will do more tests to answer the questions rose by @chrisvest and @franz1981.

Besides, there are some points may worth to discuss:

  1. Based on my benchmark on AdaptivePoolingAllocator, the main bottleneck of the performance is the threads collisions on acquiring the magazine lock, back to the JMH benchmark case, if we change the AdaptivePoolingAllocator.MAX_STRIPES value to 32, which equals with the JMH benchmark threads number, it will make a far better performance results than current PR: relatively 100% performance improvements. So if we enlarge the threshold value of AdaptivePoolingAllocator.MAX_STRIPES to reduce threads collisions, then the performance will be better, but this comes to a question, how to choose a proper MAX value of AdaptivePoolingAllocator.MAX_STRIPES? Or we make it to be customizable by the users? And this leads to the next point: 'adaptive'.

  2. For the name of AdaptivePoolingAllocator, the magazines array can 'adaptively' expand in response to the threads contention, should we also make the magazines array be able to SHRINK when the threads contention becomes low? Then we should find a proper way to detect the threads contention level and choose a proper time-point to shrink the magazines array.

  3. This PR comes as a 'by-product' of the previous points, which try to reduce threads collisions to improve the performance.

I'm very looking forward to hear suggestions. @normanmaurer @chrisvest @franz1981

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Dec 4, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

@laosijikaichele I got a qq for you: since that trying to lock (and fail) all of the magazines has its costs, why not trying to pick a random one to start with, after the failed attempt?
Another approach can be to xorshift hash the thread id (to always hit the same), to better distribute it (which help with Virtual threads since their id tends to increase and not been reused!) - and than fallback with a random choice to re-start trying.

Right now we're not saving any telemetry about it, but since magazines eventually get acquired - using a local variable to register how many attempts are required before getting one - and save it in the magazine acquired, would help (and we can report it on the tear down of the benchmark, too) to better understand where things fall down the cliff with few stripes.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest

chrisvest commented Dec 4, 2024

Copy link
Copy Markdown
Member

if we change the AdaptivePoolingAllocator.MAX_STRIPES value to 32, which equals with the JMH benchmark threads number, it will make a far better performance results than current PR: relatively 100% performance improvements.

This indicates the heuristic "twice the number of CPU cores" is too low? We also don't want to get too crazy, as systems have 192 cores or more.

should we also make the magazines array be able to SHRINK when the threads contention becomes low?

The challenge is how to efficiently determine that contention is too low. It'd need to discern between right-sized and over-sized magazine array. We don't want to get into a mode where the magazines periodically grows and shrinks, back and forth.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 5, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

This indicates the heuristic "twice the number of CPU cores" is too low?

The "twice the number of CPU cores" is still correct.

But, it's a little different in the magazines array scenario.
To make it simple, let's assume the computer has 4 cores, which means the NettyRuntime.availableProcessors() returns 4, then the AdaptivePoolingAllocator.MAX_STRIPES value is 8, assume we use 16 threads to run the allocating. As following graph shows:

截屏2024-12-05 上午8 52 23

As we can see, there are 4 cores running at the moment, but 'thread-id-8' and 'thread-id-16' get collision on magazine[0], 'thread-id-1' and 'thread-id-9' get collision on magazine[1], which means there are only 2 cores doing the real allocating work, which slowed down the performance, that's why when we change the AdaptivePoolingAllocator.MAX_STRIPES value to be equals with the JMH benchmark threads number, then the performance increased about 100%.
@chrisvest

chrisvest reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 5, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Have you tried running the benchmark with the 'central queue' bit removed?

Some benchmark results:

  1. Base: the current 4.1-branch code:
Benchmark                                                                         (size)   Mode  Cnt         Score         Error  Units
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   00064  thrpt   20  44765852.405 ± 2158267.449  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   00256  thrpt   20  50426422.617 ± 1625518.316  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   01024  thrpt   20  47962141.687 ± 1288288.181  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   04096  thrpt   20  47645849.622 ± 4059710.794  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     00064  thrpt   20  45218005.401 ±  728359.822  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     00256  thrpt   20  45705011.551 ± 1090326.331  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     01024  thrpt   20  40320395.856 ± 3147962.697  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     04096  thrpt   20  42041312.718 ± 1050791.102  ops/s
  1. This PR:
Benchmark                                                                         (size)   Mode  Cnt         Score         Error  Units
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   00064  thrpt   20  59228348.215 ± 1813789.557  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   00256  thrpt   20  58336037.049 ± 1425381.453  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   01024  thrpt   20  55490531.505 ± 1725257.117  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   04096  thrpt   20  61100119.082 ± 2169315.148  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     00064  thrpt   20  55866042.594 ± 2250531.728  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     00256  thrpt   20  48192452.569 ± 2183073.224  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     01024  thrpt   20  52565337.762 ± 1783352.384  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     04096  thrpt   20  52622865.686 ± 3086720.804  ops/s
  1. This PR: removed 'central queue' part:
Benchmark                                                                         (size)   Mode  Cnt         Score         Error  Units
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   00064  thrpt   20  44214899.313 ± 1265912.293  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   00256  thrpt   20  49909651.024 ± 2109215.889  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   01024  thrpt   20  42327685.343 ± 2913376.452  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   04096  thrpt   20  45835790.531 ± 2275833.994  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     00064  thrpt   20  45252360.734 ± 3398428.018  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     00256  thrpt   20  48799484.414 ± 3868804.633  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     01024  thrpt   20  46676173.107 ± 1816229.061  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     04096  thrpt   20  45385528.191 ± 1233630.633  ops/s
  1. This PR: removed 'NEXT_IN_LINE' part:
Benchmark                                                                         (size)   Mode  Cnt         Score         Error  Units
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   00064  thrpt   20  50789341.282 ± 2186182.068  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   00256  thrpt   20  46420384.352 ± 1927074.344  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   01024  thrpt   20  49353791.249 ± 2145425.592  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   04096  thrpt   20  45599316.202 ± 1293919.331  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     00064  thrpt   20  44419510.423 ± 1979362.192  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     00256  thrpt   20  43488683.449 ± 2683059.025  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     01024  thrpt   20  45239700.847 ± 2573862.252  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     04096  thrpt   20  42303418.350 ± 1073820.743  ops/s
  1. The current 4.1-branch code: Enlarged the AdaptivePoolingAllocator.MAX_STRIPES value to 32:
Benchmark                                                                         (size)   Mode  Cnt         Score          Error  Units
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   00064  thrpt   20  92044907.610 ±  5216575.193  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   00256  thrpt   20  86464537.298 ±  3298538.728  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   01024  thrpt   20  86211156.667 ±  1902848.102  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseDirectAdaptive   04096  thrpt   20  82572653.737 ±  2046694.213  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     00064  thrpt   20  80209937.801 ± 13561768.134  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     00256  thrpt   20  89055010.211 ±  2713997.414  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     01024  thrpt   20  84096155.412 ±  1031039.806  ops/s
AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.allocateReleaseHeapAdaptive     04096  thrpt   20  78620975.247 ±  2006552.766  ops/s

The benchmark-5 has the best performance, and the benchmark-2(current PR) has the second best performance.
Additionally, benchmark-5 has about 100% performance increased than the benchmark-1(base).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Dec 5, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

The benchmark-5 has the best performance, and the benchmark-2(current PR) has the second best performance.
Additionally, benchmark-5 has about 100% performance increased than the benchmark-1(base).

As said, since a successful lock acquisition on a magazine enable amortizing further "bump pointer up" allocations, by reusing the same Chunk - it's not surprising.
I've tried to explain it at #14534 (comment)
In any case, without telemetry, perfnorm and perfasm analysis, it's just a series of wild guesses...

Which means: if you want to amortize further allocations over the same Chunk you really want to use magazines: so...now the problem is, how to better use what you got?
I suggest again #14534 (comment)
i.e. use a better way to distributes lock requests vs magazines

since, especially for virtual threads which are NOT reused (and get a new thread id over and over) can be very annoying.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 5, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

using a local variable to register how many attempts are required before getting one

I used to record these failed attempts counts and the proportion when I did the benchmark on the base 4.1-branch a few days ago, the numbers seemed gradually decreased in a reasonable way along with the loop-attempts, if I remembered it correctly. :)
Anyway I will re-run the benchmark and record those accurate numbers and share them later.

why not trying to pick a random one to start with, after the failed attempt?

Another approach can be to xorshift hash the thread id (to always hit the same), to better distribute it (which help with Virtual threads since their id tends to increase and not been reused!) - and than fallback with a random choice to re-start trying.

When the threads counts exceeded the AdaptivePoolingAllocator.MAX_STRIPES, there is not much we can do to avoid the threads get collisions on the same magazine, because we can not Interfere how the CPU schedule the threads, which illustrated in #14534 (comment)

@franz1981

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 5, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Currently, I can see two options to decrease the threads collisions:

  1. Enlarge AdaptivePoolingAllocator.MAX_STRIPES to a proper value, which may need to do some evaluating/research to determine which value is proper, or we make it to be customizable by the users. Then expand the magazines array when the contention level goes up(which currently 4.1-branch doing), and shrink the magazines array when contention level goes down, this may introduce some code implementation complexity. Or we do not do the shrink.
  2. Try to make some parallelism on the collision magazine, like the current PR did.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Dec 5, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

I was suggesting something like this

    private static int probe() {
        // Multiplicative Fibonacci hashing implementation
        // 0x9e3779b9 is the integral part of the Golden Ratio's fractional part 0.61803398875… (sqrt(5)-1)/2
        // multiplied by 2^32, which has the best possible scattering properties.
        int probe = (int) ((Thread.currentThread().getId() * 0x9e3779b9) & Integer.MAX_VALUE);
        // xorshift
        probe ^= probe << 13;
        probe ^= probe >>> 17;
        probe ^= probe << 5;
        return probe;
    }

    private AdaptiveByteBuf allocate(int size, int maxCapacity, Thread currentThread, AdaptiveByteBuf buf) {
        if (size <= MAX_CHUNK_SIZE) {
            int sizeBucket = AllocationStatistics.sizeBucket(size); // Compute outside of Magazine lock for better ILP.
            FastThreadLocal<Object> threadLocalMagazine = this.threadLocalMagazine;
            if (threadLocalMagazine != null && currentThread instanceof FastThreadLocalThread) {
                Object mag = threadLocalMagazine.get();
                if (mag != NO_MAGAZINE) {
                    Magazine magazine = (Magazine) mag;
                    if (buf == null) {
                        buf = magazine.newBuffer();
                    }
                    boolean allocated = magazine.tryAllocate(size, sizeBucket, maxCapacity, buf);
                    assert allocated : "Allocation of threadLocalMagazine must always succeed";
                    return buf;
                }
            }
            final int probe = probe();
            Magazine[] mags;
            int expansions = 0;
            do {
                mags = magazines;
                int mask = mags.length - 1;
                int magazineIndex = probe & mask;
                Magazine mag = mags[magazineIndex];
                if (buf == null) {
                    buf = mag.newBuffer();
                }
                if (mag.tryAllocate(size, sizeBucket, maxCapacity, buf)) {
                    return buf;
                }
                // If the first magazine failed, start from another random
                int randomIndex = (magazineIndex + ThreadLocalRandom.current().nextInt(1, mask + 1)) & mask;
                for (int i = 0; i < mask; i++) {
                    mag = mags[(randomIndex + i) & mask];
                    if (mag.tryAllocate(size, sizeBucket, maxCapacity, buf)) {
                        return buf;
                    }
                }
            } while (expansions++ <= EXPANSION_ATTEMPTS && tryExpandMagazines(mags.length));
        }

BEWARE: don't use it as it is, because I have probably messed up with the math after the first unsuccesful attempt -- and TBH the whole loop which start from a random point should be fine if it tries, again, on the first choice, although at the last resort (while probe should be decent already).
Been more obsessive while trying to pick the magazine is fine, since the time to attempt doing it is decent (not too many attempts, overall).
While, what we're missing, is to properly avoid false sharing on the different locks we attempt to acquire; we should have a proper (and better) data structure there IMO.
I'll measure something with perf c2c because performing a full cycle trying all the locks is very likely will end up trashing each magazin's lock cache lines - if the GC decide to keep the locks too near to each others.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest

chrisvest commented Dec 6, 2024

Copy link
Copy Markdown
Member

I was pondering this PR and remembered something Franz told me recently, that hammering a concurrent data structure like this in a loop with no other work going on, will over-estimate the amount of contention we'll realistically see in practice.
For instance, you're not likely to see real code allocate and release buffers as aggressively as the benchmark used here.
So even though we can make changes that'll improve the benchmark, they might not translate into any tangible performance improvements in practice.

What I'm thinking is that, since the magazine stripes are the primary way we deal with contention, we should perhaps just introduce a system property for overriding the MAX_STRIPES. And maybe we want to expose a metric, or some mechanism, by which integrators can be informed if the allocator wants to expand the stripe array but the array is already at max size.

That would both be a smaller change, with a greater potential performance impact (that people can control), and opens a door to get more information about what contention is seen in practice.

franz1981 reacted with thumbs up emoji franz1981 reacted with heart emoji
All reactions
  • 👍 1 reaction
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 6, 2024

Copy link
Copy Markdown
Contributor Author

I will re-run the benchmark and record those accurate numbers and share them later.

Some initial results about the success counts distribution during allocation:
截屏2024-12-06 下午4 17 41

We can see that, For 4.1-branch, 82% successful allocation happened at 'expansions_0_pointerBump_0', for current PR, 99% successful allocation happened at 'expansions_0_pointerBump_0'.

Will add more details....

franz1981 reacted with eyes emoji
All reactions
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Dec 6, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Can you try the PR I've sent by fixing the logic of the random part fallback (after the first failed attempt with probe)?
As said, the distribution under contention, can be very relevant, but as @chrisvest said, we should add BlackHole::consumeCPU between acquire/release and eventually handle burst/batches of acquire/release, to emulate cases like HTTP2 where header/body uses different buffers, encode something within, and write them to the while, which, if there is no congestion, will release them in batch

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 6, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

we should add BlackHole::consumeCPU between acquire/release and eventually handle burst/batches of acquire/release

@franz1981
Did you mean the benchmark method should be like this?

 @Benchmark
 @Threads(32)
 public void allocateReleaseHeapAdaptive(Blackhole blackhole) {
     ByteBuf buf = adaptiveAllocator.heapBuffer(size);
     blackhole.consume(buf);
     blackhole.consume(buf.release());
 }
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Dec 6, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Nope, you should have a int work parameter

And call BlackHole.consumeCPU(work) (a static method!) if work > 0 between the buffer acquisition and release.

The work values could be 0, 10, 100.

Additionally you can have a new int batch field in the benchmark and a thread local state (in JMH) which expose a ArrayQueue field and you can acquire the buffers in batch (before the consumeCPU), place them in the thread local state stack/queue, and release them in burst draining from the stack in a sensible order (FIFO, LIFO? I think FIFO to be the way, if you think about the http 2 case...).
In this case the batch size can be 1, 10, 100

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 6, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@franz1981
Forgive my stupidity, I do not quite understood it, may be you post the benchmark method code here? I will be happy to run it and record the results... 😄

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Dec 6, 2024

Copy link
Copy Markdown
Contributor

No stupidity, producing code by text description is not trivial!
Start with this

netty/microbench/src/main/java/io/netty/microbench/buffer/SimpleByteBufPooledAllocatorBenchmark.java

Line 87 in 2eb7fce

Blackhole.consumeCPU(tokens);

laosijikaichele reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 6, 2024

Copy link
Copy Markdown
Contributor Author

Blackhole.consumeCPU(tokens);

Thanks @franz1981, I will try it, will update here once have progress.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Dec 6, 2024

Copy link
Copy Markdown
Contributor

Additionally, I made this for JCTools (which I am a developer) - https://github.com/JCTools/JCTools/blob/a17b56f09cc9ec7d773707578c89f3ca9c11f968/jctools-benchmarks/src/main/java/org/jctools/jmh/latency/QueueAsPoolBurstCost.java#L91

Which add the batching I have mentioned.

I suggest to do the same here, because it's a type of pattern we have in Netty: sadly there is still a missing bit which I would like to capture, but let's talk about it when you modify the benchmark with both these changes.

laosijikaichele reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 6, 2024

Copy link
Copy Markdown
Contributor Author

And call BlackHole.consumeCPU(work) (a static method!) if work > 0 between the buffer acquisition and release.
The work values could be 0, 10, 100.

Done this part so far.

  1. The 4.1-branch results:
4.1-branch size = 123, tokens = 0 size = 123, tokens = 5 size = 123, tokens = 10 size = 123, tokens = 100 size = 1234, tokens = 0 size = 1234, tokens = 5 size = 1234, tokens = 10 size = 1234, tokens = 100 size = 12345, tokens = 0 size = 12345, tokens = 5 size = 12345, tokens = 10 size = 12345, tokens = 100 size = 123456, tokens = 0 size = 123456, tokens = 5 size = 123456, tokens = 10 size = 123456, tokens = 100 size = 1234567, tokens = 0 size = 1234567, tokens = 5 size = 1234567, tokens = 10 size = 1234567, tokens = 100
total_allocate_counts 214254916 217367769 210315954 235856697 216316100 213340946 204965371 201097085 202935425 212224972 204924222 192961743 7904007 8069286 8004592 8024263 245131381 246621681 210477630 182198975
success_counts_at_expansions_0_pointerBump_0 184080241 189316486 183654849 203136810 187533439 185616076 175082056 175519979 174208401 187253041 175014753 165460919 1952710 1794491 1869813 2039150 174484268 184802619 142790563 137149366
success_counts_at_expansions_0_pointerBump_1 20931962 19772935 19157378 23800493 22061678 19644826 21439797 19167610 21338545 19120088 22638007 21700909 1350511 1530030 1483877 1448596 42254729 39386349 45216521 30139261
success_counts_at_expansions_0_pointerBump_2 5883271 5688887 5307883 5847105 4659207 5520573 5262642 4098660 4933545 4366556 4969204 4216007 1077966 1153831 1075405 1110443 20040673 14059802 16064265 10259393
success_counts_at_expansions_0_pointerBump_3 1694417 1547865 1294298 1631441 1298755 1449541 1984094 1269280 1592985 1045316 1385642 1064650 800015 874659 839396 817692 7542723 7676952 5717658 4119370
success_counts_at_expansions_1_pointerBump_0 75652 54774 62829 74854 72923 63926 94666 53183 70612 54225 80445 50907 2685 2870 2987 6373 188151 160636 144788 100279
success_counts_at_expansions_1_pointerBump_1 50441 37289 40988 44135 45676 45385 60907 34073 52868 37769 55073 37079 1539 2055 1736 3965 133003 112661 114382 70958
success_counts_at_expansions_1_pointerBump_2 35660 33191 31716 33811 26827 30763 43584 20207 30560 23425 35438 20188 1054 1357 1212 2503 80553 61361 67305 38189
success_counts_at_expansions_1_pointerBump_3 17489 16216 13875 15907 14286 14637 26270 13500 17302 10660 18933 11230 701 803 711 1577 28182 21509 23770 14418
success_counts_at_expansions_2_pointerBump_0 14320 8268 9330 11088 11406 10858 13971 9239 9820 7269 10278 6453 972 972 1123 1811 22855 18272 17488 13912
success_counts_at_expansions_2_pointerBump_1 11628 7773 6086 8084 8312 8336 10264 6281 9173 5058 8836 5415 639 849 863 1375 17469 13934 15396 10000
success_counts_at_expansions_2_pointerBump_2 9994 8808 7264 7866 5857 6718 11113 5217 7793 4904 7553 4378 653 721 753 1265 14039 10807 12184 7357
success_counts_at_expansions_2_pointerBump_3 11166 6845 4810 9289 5516 6125 10617 5609 7896 3762 7058 4293 572 648 696 1013 8381 7064 7320 6040
success_counts_at_expansions_3_pointerBump_0 11627 6609 6513 8946 9001 8115 10053 6682 7068 5903 6427 3922 859 883 1070 1736 11649 10865 10187 9458
success_counts_at_expansions_3_pointerBump_1 9610 5086 3827 6453 5788 5965 6811 5463 7193 3423 5484 3495 624 798 872 1280 9741 8522 9527 7048
success_counts_at_expansions_3_pointerBump_2 8965 8344 6380 5981 4662 5662 9095 4627 6397 3311 5723 2956 638 720 767 1160 8790 8139 8603 5994
success_counts_at_expansions_3_pointerBump_3 8918 8240 5593 8862 5043 6608 9806 6170 8247 3956 6114 4123 656 737 743 1045 6900 6776 6898 6182
success_counts_at_allocateFallback 1399555 840153 702335 1205572 547724 896832 889625 871305 627020 276306 669254 364819 2711213 2702862 2722568 2583279 279275 255413 250775 241750
  1. Current PR results:
current-PR size = 123, tokens = 0 size = 123, tokens = 5 size = 123, tokens = 10 size = 123, tokens = 100 size = 1234, tokens = 0 size = 1234, tokens = 5 size = 1234, tokens = 10 size = 1234, tokens = 100 size = 12345, tokens = 0 size = 12345, tokens = 5 size = 12345, tokens = 10 size = 12345, tokens = 100 size = 123456, tokens = 0 size = 123456, tokens = 5 size = 123456, tokens = 10 size = 123456, tokens = 100 size = 1234567, tokens = 0 size = 1234567, tokens = 5 size = 1234567, tokens = 10 size = 1234567, tokens = 100
total_allocate_counts 301434249 200717576 207727540 202524927 213958453 311556773 210252727 191639942 200670408 211401452 297834767 233786702 321846873 330986127 310886936 297527400 217752753 203583541 305363823 181861413
success_counts_at_expansions_0_pointerBump_0 301430185 200712828 207719733 202522574 213956583 311554613 210251380 191637844 200667392 211398109 297830477 233779925 321846641 330985868 310886623 297527105 217458203 203355926 305162621 181702829
success_counts_at_expansions_0_pointerBump_1 3124 2775 5196 1290 1432 1850 1147 1550 2941 3220 4049 5936 88 90 95 66 157332 163216 124054 103550
success_counts_at_expansions_0_pointerBump_2 573 1129 852 630 263 181 102 420 56 97 215 822 40 17 44 44 81326 45304 47990 40038
success_counts_at_expansions_0_pointerBump_3 133 347 824 395 96 60 64 57 17 11 3 7 25 39 28 12 51291 14333 20884 10902
success_counts_at_expansions_1_pointerBump_0 43 57 241 8 17 35 2 10 0 4 20 2 1 6 13 11 891 1431 3396 803
success_counts_at_expansions_1_pointerBump_1 28 48 105 2 8 1 0 3 0 1 0 0 1 8 6 3 709 730 750 423
success_counts_at_expansions_1_pointerBump_2 29 123 30 5 7 4 1 2 0 0 0 1 1 4 4 5 226 364 527 334
success_counts_at_expansions_1_pointerBump_3 15 29 183 11 5 4 14 5 0 0 0 0 2 7 1 2 470 148 1222 335
success_counts_at_expansions_2_pointerBump_0 15 16 16 3 3 1 0 3 0 0 0 0 6 2 8 4 111 159 209 104
success_counts_at_expansions_2_pointerBump_1 6 12 40 1 1 1 0 1 0 1 0 2 1 7 1 2 336 135 133 107
success_counts_at_expansions_2_pointerBump_2 9 30 13 0 15 0 0 1 0 0 0 0 1 2 2 0 43 51 251 28
success_counts_at_expansions_2_pointerBump_3 5 15 53 5 1 1 7 1 0 1 0 0 0 6 1 1 63 48 108 143
success_counts_at_expansions_3_pointerBump_0 11 9 9 0 1 2 0 1 0 1 0 0 1 2 2 2 60 55 57 61
success_counts_at_expansions_3_pointerBump_1 9 2 27 0 2 0 0 1 0 0 0 1 1 3 2 0 110 63 27 81
success_counts_at_expansions_3_pointerBump_2 3 2 19 0 2 1 1 2 0 0 0 0 0 1 0 2 23 14 75 25
success_counts_at_expansions_3_pointerBump_3 4 1 17 1 2 2 3 0 0 0 0 0 1 2 1 1 37 15 89 80
success_counts_at_allocateFallback 57 153 182 2 15 17 6 41 2 7 3 6 63 63 105 140 1522 1549 1430 1570

The benchmark method code looks like:

private static final AdaptiveByteBufAllocator adaptiveAllocator =
            new AdaptiveByteBufAllocator(true, false);

    @Param({
            "123",
            "1234",
            "12345",
            "123456",
            "1234567",
    })
    public int size;

    @Param({
            "0",
            "5",
            "10",
            "100",
    })
    public long tokens;

    public AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark2() {
        super(false, true);
    }

    @Benchmark
    @Threads(32)
    public void allocateReleaseHeapAdaptive(Blackhole blackhole) {
        ByteBuf buf = adaptiveAllocator.heapBuffer(size);
        if (tokens > 0) {
            Blackhole.consumeCPU(tokens);
        }
        blackhole.consume(buf.release());
    }

@franz1981

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Dec 6, 2024

Copy link
Copy Markdown
Contributor

As you can see with tokens at 100 the before/after probably won't show much difference (and that's what real world will look like). Do you have the ns/op?

Ok, about using the snippet I suggested at #14534 (comment) (to be modified)?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 7, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

about using the snippet I suggested at #14534 (comment) (to be modified)?

The results, with 4.1-branch + 'randomProb':

4.1-branch, with randomProb size = 1234, tokens = 0 size = 1234, tokens = 100
total_allocate_counts 219591307 181831112
expansions_0_before_pointerBump 155150620 142771233
expansions_1_before_pointerBump 6931 1009
expansions_2_before_pointerBump 1244 197
expansions_3_before_pointerBump 1244 197
success_counts_at_expansions_0_pointerBump_0 42404185 29689043
success_counts_at_expansions_0_pointerBump_1 12606060 6464614
success_counts_at_expansions_0_pointerBump_2 4960809 1934550
success_counts_at_expansions_0_pointerBump_3 2114302 600568
success_counts_at_expansions_1_pointerBump_0 7241 597
success_counts_at_expansions_1_pointerBump_1 6068 507
success_counts_at_expansions_1_pointerBump_2 5106 429
success_counts_at_expansions_1_pointerBump_3 4814 472
success_counts_at_expansions_2_pointerBump_0 1337 163
success_counts_at_expansions_2_pointerBump_1 1262 157
success_counts_at_expansions_2_pointerBump_2 1173 142
success_counts_at_expansions_2_pointerBump_3 1176 148
success_counts_at_expansions_3_pointerBump_0 543 99
success_counts_at_expansions_3_pointerBump_1 539 67
success_counts_at_expansions_3_pointerBump_2 516 82
success_counts_at_expansions_3_pointerBump_3 510 87
success_counts_at_allocateFallback 15520 6007

Seems there is no performance improvements than the 4.1-branch code, the first successful alloc distribution(expansions_0_before_pointerBump) is about 70%~78%.

@franz1981

Honestly, I have tried the similar random method to observe the performance change few days ago, it showed the similar results with this 'randomProb' implementation.

Again, as I have said, For the 'hash(thread)-->magazine' part, there is not much we can do when the threads counts exceeded MAX_STRIPES, because we can not forecast how the CPU will schedule the threads, which illustrated in
#14534 (comment).

Currently, there are still the two options available: #14534 (comment).

Which the 'option-1' has the similar thought with @chrisvest : 'make MAX_STRIPES to be customizable by the users.'

franz1981 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Dec 7, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Thanks to have tried, although it is a bit weird, since I would have expected a fairest distribution..

I see that success_counts_at_expansions_0_pointerBump_0-1-2-3 are much higher than they were before.
And that expansions_0-1-2-3_before_pointerBump is now present while it wasn't in any of your benchmark before.
What is the meaning of these counters? Do you have some code/summary or just the code to show what it means?
And I don't see yet what it means for the latency of the allocations...

Said that..

As you can see with tokens at 100 the before/after probably won't show much difference (and that's what real world will look like)

Which means that, as @chrisvest suggested; this is an improvement which disappear as the user does something (token 100), if I read correctly your data; if not, can you share the ns/op before/after your changes?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 7, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

expansions_0-1-2-3_before_pointerBump is now present while it wasn't in any of your benchmark before

Because the code snippet in #14534 (comment) introduced an extra allocate operation before the for-loop:

                if (mag.tryAllocate(size, sizeBucket, maxCapacity, buf)) {
                    // Here: expansions_0-1-2-3_before_pointerBump.
                    return buf;
                }
                int randomIndex = (magazineIndex + ThreadLocalRandom.current().nextInt(1, mask + 1)) & mask;
                for (int i = 0; i < mask; i++) {
                    mag = mags[(randomIndex + i) & mask];
                    if (mag.tryAllocate(size, sizeBucket, maxCapacity, buf)) {
                        // Here: success_counts_at_expansions_0-1-2-3_pointerBump_0-1-2-3....-mask
                        return buf;
                    }
                }

Additionally, I did not record all of the pointerBump_[0,1,2,3 - mask] but pointerBump_0-1-2-3.
@franz1981

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 7, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

this is an improvement which disappear as the user does something (token 100)

Sorry I don't get the meaning, could you please explain it more? @franz1981

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 8, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

As said, the distribution under contention, can be very relevant, but as @chrisvest said, we should add BlackHole::consumeCPU between acquire/release and eventually handle burst/batches of acquire/release

To do this, I modified the benchmark code to be AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark2

The following are the benchmark results:

  1. 4.1-branch:
Benchmark                      (size)  (tokens)  Mode  Cnt     Score         Error      Units
allocateReleaseHeapAdaptive      123         0  thrpt   20  49600569.565 ± 3541877.971  ops/s
allocateReleaseHeapAdaptive      123         5  thrpt   20  52596593.636 ± 1472694.081  ops/s
allocateReleaseHeapAdaptive      123        10  thrpt   20  43768030.709 ± 1184420.657  ops/s
allocateReleaseHeapAdaptive      123       100  thrpt   20  18626691.915 ±  476974.252  ops/s
allocateReleaseHeapAdaptive     1234         0  thrpt   20  55033762.901 ± 1610645.207  ops/s
allocateReleaseHeapAdaptive     1234         5  thrpt   20  49146623.483 ± 1101063.739  ops/s
allocateReleaseHeapAdaptive     1234        10  thrpt   20  48111169.236 ± 1509283.542  ops/s
allocateReleaseHeapAdaptive     1234       100  thrpt   20  18762242.666 ±  333892.841  ops/s
allocateReleaseHeapAdaptive    12345         0  thrpt   20  52435911.748 ± 1850134.115  ops/s
allocateReleaseHeapAdaptive    12345         5  thrpt   20  49430211.528 ± 2192686.978  ops/s
allocateReleaseHeapAdaptive    12345        10  thrpt   20  42536581.878 ± 3363863.074  ops/s
allocateReleaseHeapAdaptive    12345       100  thrpt   20  18799263.626 ±  744005.641  ops/s
allocateReleaseHeapAdaptive   123456         0  thrpt   20    365191.379 ±    4097.401  ops/s
allocateReleaseHeapAdaptive   123456         5  thrpt   20    366485.563 ±    5019.905  ops/s
allocateReleaseHeapAdaptive   123456        10  thrpt   20    358996.492 ±    8624.480  ops/s
allocateReleaseHeapAdaptive   123456       100  thrpt   20    367667.187 ±    5783.807  ops/s
allocateReleaseHeapAdaptive  1234567         0  thrpt   20  37227841.126 ± 1392361.728  ops/s
allocateReleaseHeapAdaptive  1234567         5  thrpt   20  35079905.508 ± 1633816.587  ops/s
allocateReleaseHeapAdaptive  1234567        10  thrpt   20  32707449.358 ± 2821133.525  ops/s
allocateReleaseHeapAdaptive  1234567       100  thrpt   20  13373001.783 ±  648725.973  ops/s
  1. 4.1-branch with @franz1981 'randomProb' implementation:
Benchmark                      (size)  (tokens)  Mode  Cnt     Score         Error      Units
allocateReleaseHeapAdaptive      123         0  thrpt   20  32012619.071 ± 2749938.105  ops/s
allocateReleaseHeapAdaptive      123         5  thrpt   20  31282002.783 ± 1569598.159  ops/s
allocateReleaseHeapAdaptive      123        10  thrpt   20  30366831.990 ± 1584817.658  ops/s
allocateReleaseHeapAdaptive      123       100  thrpt   20  15742239.592 ±  346598.796  ops/s
allocateReleaseHeapAdaptive     1234         0  thrpt   20  34656269.980 ± 1711087.653  ops/s
allocateReleaseHeapAdaptive     1234         5  thrpt   20  31310085.694 ± 1533949.376  ops/s
allocateReleaseHeapAdaptive     1234        10  thrpt   20  29051633.496 ±  918231.946  ops/s
allocateReleaseHeapAdaptive     1234       100  thrpt   20  16231425.631 ±  332671.316  ops/s
allocateReleaseHeapAdaptive    12345         0  thrpt   20  31237676.285 ± 1559837.123  ops/s
allocateReleaseHeapAdaptive    12345         5  thrpt   20  27743366.146 ± 1013648.387  ops/s
allocateReleaseHeapAdaptive    12345        10  thrpt   20  28474031.904 ± 1601307.744  ops/s
allocateReleaseHeapAdaptive    12345       100  thrpt   20  15359808.346 ±  471578.403  ops/s
allocateReleaseHeapAdaptive   123456         0  thrpt   20    364882.114 ±    7803.780  ops/s
allocateReleaseHeapAdaptive   123456         5  thrpt   20    363344.221 ±    5323.685  ops/s
allocateReleaseHeapAdaptive   123456        10  thrpt   20    361630.220 ±    6682.916  ops/s
allocateReleaseHeapAdaptive   123456       100  thrpt   20    369328.294 ±    4427.815  ops/s
allocateReleaseHeapAdaptive  1234567         0  thrpt   20  20225717.622 ± 2757264.861  ops/s
allocateReleaseHeapAdaptive  1234567         5  thrpt   20  17191142.957 ± 1638853.157  ops/s
allocateReleaseHeapAdaptive  1234567        10  thrpt   20  15318177.193 ± 1202633.061  ops/s
allocateReleaseHeapAdaptive  1234567       100  thrpt   20  10775905.274 ±  343227.446  ops/s
  1. Current PR:
Benchmark                      (size)  (tokens)  Mode  Cnt      Score          Error      Units
allocateReleaseHeapAdaptive      123         0  thrpt   20   76775268.663 ±  1712651.792  ops/s
allocateReleaseHeapAdaptive      123         5  thrpt   20   65730696.539 ±  1664060.591  ops/s
allocateReleaseHeapAdaptive      123        10  thrpt   20   58170042.646 ±  6412294.126  ops/s
allocateReleaseHeapAdaptive      123       100  thrpt   20   20419676.377 ±   242858.555  ops/s
allocateReleaseHeapAdaptive     1234         0  thrpt   20   61923322.493 ±  6520030.367  ops/s
allocateReleaseHeapAdaptive     1234         5  thrpt   20   60468843.566 ±  3128129.438  ops/s
allocateReleaseHeapAdaptive     1234        10  thrpt   20   58892722.014 ±  2503542.297  ops/s
allocateReleaseHeapAdaptive     1234       100  thrpt   20   19984648.170 ±   410064.453  ops/s
allocateReleaseHeapAdaptive    12345         0  thrpt   20   65496279.721 ±  5480641.901  ops/s
allocateReleaseHeapAdaptive    12345         5  thrpt   20   57451786.964 ±  2827528.935  ops/s
allocateReleaseHeapAdaptive    12345        10  thrpt   20   52137740.145 ±  2594627.374  ops/s
allocateReleaseHeapAdaptive    12345       100  thrpt   20   19507948.566 ±   276660.639  ops/s
allocateReleaseHeapAdaptive   123456         0  thrpt   20  123998109.885 ± 13292543.621  ops/s
allocateReleaseHeapAdaptive   123456         5  thrpt   20  101797528.387 ±  8081467.032  ops/s
allocateReleaseHeapAdaptive   123456        10  thrpt   20  102573508.117 ±  8332524.166  ops/s
allocateReleaseHeapAdaptive   123456       100  thrpt   20   25848586.858 ±  1204460.494  ops/s
allocateReleaseHeapAdaptive  1234567         0  thrpt   20   55967554.848 ±  1006042.586  ops/s
allocateReleaseHeapAdaptive  1234567         5  thrpt   20   55495435.614 ±  4690964.986  ops/s
allocateReleaseHeapAdaptive  1234567        10  thrpt   20   48915941.704 ±  2846835.362  ops/s
allocateReleaseHeapAdaptive  1234567       100  thrpt   20   18897445.489 ±   435000.581  ops/s

We can see that current-PR has the best performance, and 4.1-branch is the second.

It's interesting that when the size goes to '123456', the current-PR makes a HUGE performance improvement than 4.1-branch, around 70 - 250 TIMES performance improvement. @franz1981 @chrisvest

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele laosijikaichele mentioned this pull request Dec 8, 2024
Suspicious performance downgrade when buffer size reaches 123456 #14550
Open
@laosijikaichele

laosijikaichele commented Dec 9, 2024

Copy link
Copy Markdown
Contributor Author

Which means that, as @chrisvest suggested; this is an improvement which disappear as the user does something (token 100), if I read correctly your data.

I think you have mis-read the benchmark results, based on all the benchmark results so far, the improvement of this PR has never been disappeared. 😃 @franz1981

franz1981 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Dec 9, 2024

Copy link
Copy Markdown
Contributor

Yep @laosijikaichele but I want to try it in some box and better understand what's going on first; that's why I asked for some clarification about the counters and metrics and their meaning

laosijikaichele reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 9, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

But, there is a point still need to clarify:
This PR only kicks in when threads collide on the same magazine, so if there is no threads collision on the same magazine, we can say this PR does not take effect on the performance improvement.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Dec 9, 2024

Copy link
Copy Markdown
Contributor

Did you modified the code to make it happen?
If not I can think about it

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 9, 2024

Copy link
Copy Markdown
Contributor Author

Did you modified the code to make it happen? If not I can think about it

Did you mean 'This PR only kicks in when threads collide on the same magazine'?
This PR already done this.
@franz1981

franz1981 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Dec 16, 2024

Copy link
Copy Markdown
Contributor

Can we check the numbers after the many latest changes? @normanmaurer wdyt?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer

normanmaurer commented Dec 16, 2024

Copy link
Copy Markdown
Member

Yeah please

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele laosijikaichele closed this Dec 17, 2024
@laosijikaichele
laosijikaichele force-pushed the 4.1-light-lock branch from e04959a to a739efa Compare December 17, 2024 06:49
@laosijikaichele

laosijikaichele commented Dec 17, 2024

Copy link
Copy Markdown
Contributor Author

Sorry, wrongly closed it when trying to resolve the conflicts, I will re-open it or file a new PR soon.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele laosijikaichele mentioned this pull request Dec 17, 2024
Allocate bytebuf without magazine lock when threads get collisions #14594
Merged
@laosijikaichele

laosijikaichele commented Dec 17, 2024

Copy link
Copy Markdown
Contributor Author

New PR was filed, please move to #14594, sorry for the disturbing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 17, 2024

Copy link
Copy Markdown
Contributor Author

Can we check the numbers after the many latest changes? @normanmaurer wdyt?

Will re-do the test...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Dec 18, 2024

Copy link
Copy Markdown
Contributor Author

Can we check the numbers after the many latest changes? @normanmaurer wdyt?

Will re-do the test...

JMH results updated: #14594 (comment)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

franz1981 pushed a commit that referenced this pull request Jan 24, 2025
@laosijikaichele @normanmaurer
Allocate bytebuf without magazine lock when threads get collisions (#…
c29f4a5
…14594)

This PR continued from #14534 which
was mis-closed.

---------

Co-authored-by: laosijikaichele <laosijikaichele>
Co-authored-by: Norman Maurer <[email protected]>
franz1981 pushed a commit to franz1981/netty that referenced this pull request Jan 24, 2025
@laosijikaichele @normanmaurer @franz1981
Allocate bytebuf without magazine lock when threads get collisions (n…
4ad7157
…etty#14594)

This PR continued from netty#14534 which
was mis-closed.

---------

Co-authored-by: laosijikaichele <laosijikaichele>
Co-authored-by: Norman Maurer <[email protected]>
@franz1981 franz1981 mentioned this pull request Jan 24, 2025
Allocate bytebuf without magazine lock when threads get collisions (#14594) #14729
Merged
normanmaurer added a commit that referenced this pull request Jan 28, 2025
@franz1981 @laosijikaichele @normanmaurer
Allocate bytebuf without magazine lock when threads get collisions (#…
8f81927
…14594) (#14729)

This PR continued from #14534 which
was mis-closed.

Co-authored-by: old driver <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@chrisvest chrisvest chrisvest left review comments
@normanmaurer normanmaurer normanmaurer approved these changes
@franz1981 franz1981 Awaiting requested review from franz1981

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.1.116.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@laosijikaichele @normanmaurer @franz1981 @chrisvest
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.