Skip to content

Rewrite and simplify Recycler - #11858

Merged
chrisvest merged 4 commits into
netty:4.1from
chrisvest:41-recycler
Nov 26, 2021
Merged

chrisvest merged 4 commits into
netty:4.1from
chrisvest:41-recycler

Conversation

@chrisvest

Copy link
Copy Markdown
Member

Motivation:
We have in the past gotten reports of weird errors that we haven't been able to reproduce, but which could be explained by bugs in the Recycler.
The reason is that the Recycler was mixing concurrency, and interactions with the GC, in ways that were difficult to reason about.
It is unwise to leave suspicous-looking code unaddressed.

Modification:
The Recycler has been rewritten to use a much simpler algorithm, and now completely avoids the use of weak references.

The WeakOrderQueue is gone, and there is no longer a concept of a shared max capacity - only thread-local capacity.

There is also no more scavenging.
It turns out that other code was relying on the ability to forget references to objects they acquired from a Recycler, and this is hard to support if there is a shared pool for moving handles between threads.

Each thread now has a local pool (an ArrayDeque), and an external inbox (also an ArrayDeque, but access is synchronised).
Calls to get() and recycle() in the same thread will just operate on the local pool like a stack.
Calls to recycle() from a thread that did not get() that object (the object was acquired in another thread) will place the object in the originating pools inbox.
When a thread then goes to get() another object, they will first check if they have anything in their inbox, and if so, drain it into their local pool.

Result:
The Recycler code is much easier to understand.

@chrisvest chrisvest added this to the 4.1.71.Final milestone Nov 24, 2021
@chrisvest chrisvest linked an issue Nov 24, 2021 that may be closed by this pull request
Motivation:
We have in the past gotten reports of weird errors that we haven't been able to reproduce, but which could be explained by bugs in the Recycler.
The reason is that the Recycler was mixing concurrency, and interactions with the GC, in ways that were difficult to reason about.
It is unwise to leave suspicous-looking code unaddressed.

Modification:
The Recycler has been rewritten to use a much simpler algorithm, and now completely avoids the use of weak references.

The WeakOrderQueue is gone, and there is no longer a concept of a shared max capacity - only thread-local capacity.

There is also no more scavenging.
It turns out that other code was relying on the ability to forget references to objects they acquired from a Recycler, and this is hard to support if there is a shared pool for moving handles between threads.

Each thread now has a local pool (an ArrayDeque), and an external inbox (also an ArrayDeque, but access is synchronised).
Calls to get() and recycle() in the same thread will just operate on the local pool like a stack.
Calls to recycle() from a thread that did not get() that object (the object was acquired in another thread) will place the object in the originating pools inbox.
When a thread then goes to get() another object, they will first check if they have anything in their inbox, and if so, drain it into their local pool.

Result:
The Recycler code is much easier to understand.
@chrisvest

Copy link
Copy Markdown
Member Author

An alternative take is #8052. Not sure which one is best, but I'll point out that this version has fewer changes to the tests, and does away with weak references entirely.
I just noticed the benchmark, which would be good to copy over.

@franz1981

Copy link
Copy Markdown
Contributor

Very happy that we're getting rid of weak ref really: anything that's not using it is a +100 from me :P

@chrisvest

Copy link
Copy Markdown
Member Author

@franz1981 Updated. Please take a look.

@franz1981 franz1981 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@chrisvest

Copy link
Copy Markdown
Member Author

Got benchmark numbers.

Old version:

Benchmark                                                                Mode  Cnt     Score     Error   Units
RecyclerBenchmark.plainNew                                               avgt   20     7.048 ±   0.087   ns/op
RecyclerBenchmark.plainNew:·gc.alloc.rate                                avgt   20  7210.104 ±  88.082  MB/sec
RecyclerBenchmark.plainNew:·gc.alloc.rate.norm                           avgt   20    80.000 ±   0.001    B/op
RecyclerBenchmark.plainNew:·gc.churn.PS_Eden_Space                       avgt   20  7211.559 ± 118.469  MB/sec
RecyclerBenchmark.plainNew:·gc.churn.PS_Eden_Space.norm                  avgt   20    80.015 ±   0.764    B/op
RecyclerBenchmark.plainNew:·gc.churn.PS_Survivor_Space                   avgt   20     0.059 ±   0.019  MB/sec
RecyclerBenchmark.plainNew:·gc.churn.PS_Survivor_Space.norm              avgt   20     0.001 ±   0.001    B/op
RecyclerBenchmark.plainNew:·gc.count                                     avgt   20   851.000            counts
RecyclerBenchmark.plainNew:·gc.time                                      avgt   20   640.000                ms
RecyclerBenchmark.producerConsumer                                       avgt   20   350.694 ±  21.272   ns/op
RecyclerBenchmark.producerConsumer:consumer                              avgt   20   350.693 ±  21.272   ns/op
RecyclerBenchmark.producerConsumer:producer                              avgt   20   350.694 ±  21.272   ns/op
RecyclerBenchmark.producerConsumer:·gc.alloc.rate                        avgt   20    50.777 ±   2.966  MB/sec
RecyclerBenchmark.producerConsumer:·gc.alloc.rate.norm                   avgt   20    13.990 ±   0.807    B/op
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Eden_Space               avgt   20    42.011 ±  57.813  MB/sec
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Eden_Space.norm          avgt   20    11.477 ±  15.721    B/op
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Survivor_Space           avgt   20     0.151 ±   0.393  MB/sec
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Survivor_Space.norm      avgt   20     0.039 ±   0.102    B/op
RecyclerBenchmark.producerConsumer:·gc.count                             avgt   20     6.000            counts
RecyclerBenchmark.producerConsumer:·gc.time                              avgt   20    11.000                ms
RecyclerBenchmark.recyclerGetAndOrphan                                   avgt   20    11.686 ±   0.087   ns/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.alloc.rate                    avgt   20  6088.941 ±  44.558  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.alloc.rate.norm               avgt   20   112.000 ±   0.001    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Eden_Space           avgt   20  6092.570 ±  90.371  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Eden_Space.norm      avgt   20   112.064 ±   1.242    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Survivor_Space       avgt   20     0.153 ±   0.066  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Survivor_Space.norm  avgt   20     0.003 ±   0.001    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.count                         avgt   20   719.000            counts
RecyclerBenchmark.recyclerGetAndOrphan:·gc.time                          avgt   20   573.000                ms
RecyclerBenchmark.recyclerGetAndRecycle                                  avgt   20    16.333 ±   0.732   ns/op
RecyclerBenchmark.recyclerGetAndRecycle:·gc.alloc.rate                   avgt   20     0.001 ±   0.002  MB/sec
RecyclerBenchmark.recyclerGetAndRecycle:·gc.alloc.rate.norm              avgt   20    ≈ 10⁻⁵              B/op
RecyclerBenchmark.recyclerGetAndRecycle:·gc.count                        avgt   20       ≈ 0            counts

This version:

Benchmark                                                                Mode  Cnt     Score     Error   Units
RecyclerBenchmark.plainNew                                               avgt   20     6.878 ±   0.032   ns/op
RecyclerBenchmark.plainNew:·gc.alloc.rate                                avgt   20  7387.032 ±  34.000  MB/sec
RecyclerBenchmark.plainNew:·gc.alloc.rate.norm                           avgt   20    80.000 ±   0.001    B/op
RecyclerBenchmark.plainNew:·gc.churn.PS_Eden_Space                       avgt   20  7396.146 ±  69.736  MB/sec
RecyclerBenchmark.plainNew:·gc.churn.PS_Eden_Space.norm                  avgt   20    80.098 ±   0.544    B/op
RecyclerBenchmark.plainNew:·gc.churn.PS_Survivor_Space                   avgt   20     0.061 ±   0.023  MB/sec
RecyclerBenchmark.plainNew:·gc.churn.PS_Survivor_Space.norm              avgt   20     0.001 ±   0.001    B/op
RecyclerBenchmark.plainNew:·gc.count                                     avgt   20   873.000            counts
RecyclerBenchmark.plainNew:·gc.time                                      avgt   20   681.000                ms
RecyclerBenchmark.producerConsumer                                       avgt   20   354.463 ±  28.344   ns/op
RecyclerBenchmark.producerConsumer:consumer                              avgt   20   354.464 ±  28.346   ns/op
RecyclerBenchmark.producerConsumer:producer                              avgt   20   354.463 ±  28.342   ns/op
RecyclerBenchmark.producerConsumer:·gc.alloc.rate                        avgt   20    38.304 ±   2.483  MB/sec
RecyclerBenchmark.producerConsumer:·gc.alloc.rate.norm                   avgt   20    10.647 ±   0.650    B/op
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Eden_Space               avgt   20    38.295 ±  52.115  MB/sec
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Eden_Space.norm          avgt   20    10.657 ±  14.570    B/op
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Survivor_Space           avgt   20     0.003 ±   0.008  MB/sec
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Survivor_Space.norm      avgt   20     0.001 ±   0.002    B/op
RecyclerBenchmark.producerConsumer:·gc.count                             avgt   20     6.000            counts
RecyclerBenchmark.producerConsumer:·gc.time                              avgt   20    12.000                ms
RecyclerBenchmark.recyclerGetAndOrphan                                   avgt   20     9.557 ±   0.350   ns/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.alloc.rate                    avgt   20  5590.135 ± 201.291  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.alloc.rate.norm               avgt   20    84.000 ±   0.001    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Eden_Space           avgt   20  5597.228 ± 219.528  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Eden_Space.norm      avgt   20    84.101 ±   0.973    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Survivor_Space       avgt   20     0.114 ±   0.057  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Survivor_Space.norm  avgt   20     0.002 ±   0.001    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.count                         avgt   20   661.000            counts
RecyclerBenchmark.recyclerGetAndOrphan:·gc.time                          avgt   20   519.000                ms
RecyclerBenchmark.recyclerGetAndRecycle                                  avgt   20    20.349 ±   0.170   ns/op
RecyclerBenchmark.recyclerGetAndRecycle:·gc.alloc.rate                   avgt   20     0.001 ±   0.002  MB/sec
RecyclerBenchmark.recyclerGetAndRecycle:·gc.alloc.rate.norm              avgt   20    ≈ 10⁻⁴              B/op
RecyclerBenchmark.recyclerGetAndRecycle:·gc.count                        avgt   20       ≈ 0            counts

Summary:

  • Very small performance improvement, except in recyclerGetAndRecycle, which regressed a bit.
  • Allocation rate is generally down, despite more work done. I suspect much of this being because DefaultHandle objects are now smaller.

@normanmaurer normanmaurer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just one nit really... feel free to merge

LAST_RECYCLED_ID_UPDATER = (AtomicIntegerFieldUpdater<DefaultHandle<?>>) updater;
AtomicIntegerFieldUpdater<?> updater = AtomicIntegerFieldUpdater.newUpdater(DefaultHandle.class, "state");
//noinspection unchecked
STATE_UPDATER = (AtomicIntegerFieldUpdater<DefaultHandle<?>>) updater;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why not just create this directly ? I think there is really no need for the extra static block here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because that would require two casts in a row in order to get the generics to line up. And that would look ugly. We've done static blocks for this situation in other places, so it's not a new pattern.

@chrisvest
chrisvest merged commit 40196a6 into netty:4.1 Nov 26, 2021
@chrisvest
chrisvest deleted the 41-recycler branch November 26, 2021 09:28
chrisvest added a commit that referenced this pull request Nov 26, 2021
Motivation:
We have in the past gotten reports of weird errors that we haven't been able to reproduce, but which could be explained by bugs in the Recycler.
The reason is that the Recycler was mixing concurrency, and interactions with the GC, in ways that were difficult to reason about.
It is unwise to leave suspicous-looking code unaddressed.
Concrete issues, like #11864, should also be addressed.

Modification:
The Recycler has been rewritten to use a much simpler algorithm, and now completely avoids the use of weak references.

The WeakOrderQueue is gone, and there is no longer a concept of a shared max capacity - only thread-local capacity.

There is also no more scavenging.
It turns out that other code was relying on the ability to forget references to objects they acquired from a Recycler, and this is hard to support if there is a shared pool for moving handles between threads.

Each thread now has a local pool (an MPSC queue).
Calls to get() and recycle() in the same thread will just operate on the local pool queue.
Calls to recycle() from a thread that did not get() that object (the object was acquired in another thread) will rely on the multi-producer safety of the queue to directly release it back to the local pool that the object came from.
We rely on setting an upper bound for the queue sizes, and the fallible offer() method, for restricting the max thread-local capacity of the recycler.

The constructors have been modified to deprecate the many configurations that are no longer relevant.
A new configuration parameter, the queue chunk-size, is exposed as a constructor parameter.
The queues will grow and shrink in units of this chunk size.

The benchmark from #8052 is copied over and updated, and shows that these changes help overall, with no hidden surprises.
Performance is roughly the same, and allocation rate is down a notch in all the benchmarks.

Result:
The Recycler code is much easier to understand.
laosijikaichele pushed a commit to laosijikaichele/netty that referenced this pull request Dec 16, 2021
Motivation:
We have in the past gotten reports of weird errors that we haven't been able to reproduce, but which could be explained by bugs in the Recycler.
The reason is that the Recycler was mixing concurrency, and interactions with the GC, in ways that were difficult to reason about.
It is unwise to leave suspicous-looking code unaddressed.
Concrete issues, like netty#11864, should also be addressed.

Modification:
The Recycler has been rewritten to use a much simpler algorithm, and now completely avoids the use of weak references.

The WeakOrderQueue is gone, and there is no longer a concept of a shared max capacity - only thread-local capacity.

There is also no more scavenging.
It turns out that other code was relying on the ability to forget references to objects they acquired from a Recycler, and this is hard to support if there is a shared pool for moving handles between threads.

Each thread now has a local pool (an MPSC queue).
Calls to get() and recycle() in the same thread will just operate on the local pool queue.
Calls to recycle() from a thread that did not get() that object (the object was acquired in another thread) will rely on the multi-producer safety of the queue to directly release it back to the local pool that the object came from.
We rely on setting an upper bound for the queue sizes, and the fallible offer() method, for restricting the max thread-local capacity of the recycler.

The constructors have been modified to deprecate the many configurations that are no longer relevant.
A new configuration parameter, the queue chunk-size, is exposed as a constructor parameter.
The queues will grow and shrink in units of this chunk size.

The benchmark from netty#8052 is copied over and updated, and shows that these changes help overall, with no hidden surprises.
Performance is roughly the same, and allocation rate is down a notch in all the benchmarks.

Result:
The Recycler code is much easier to understand.
laosijikaichele pushed a commit to laosijikaichele/netty that referenced this pull request Dec 16, 2021
Motivation:
We have in the past gotten reports of weird errors that we haven't been able to reproduce, but which could be explained by bugs in the Recycler.
The reason is that the Recycler was mixing concurrency, and interactions with the GC, in ways that were difficult to reason about.
It is unwise to leave suspicous-looking code unaddressed.
Concrete issues, like netty#11864, should also be addressed.

Modification:
The Recycler has been rewritten to use a much simpler algorithm, and now completely avoids the use of weak references.

The WeakOrderQueue is gone, and there is no longer a concept of a shared max capacity - only thread-local capacity.

There is also no more scavenging.
It turns out that other code was relying on the ability to forget references to objects they acquired from a Recycler, and this is hard to support if there is a shared pool for moving handles between threads.

Each thread now has a local pool (an MPSC queue).
Calls to get() and recycle() in the same thread will just operate on the local pool queue.
Calls to recycle() from a thread that did not get() that object (the object was acquired in another thread) will rely on the multi-producer safety of the queue to directly release it back to the local pool that the object came from.
We rely on setting an upper bound for the queue sizes, and the fallible offer() method, for restricting the max thread-local capacity of the recycler.

The constructors have been modified to deprecate the many configurations that are no longer relevant.
A new configuration parameter, the queue chunk-size, is exposed as a constructor parameter.
The queues will grow and shrink in units of this chunk size.

The benchmark from netty#8052 is copied over and updated, and shows that these changes help overall, with no hidden surprises.
Performance is roughly the same, and allocation rate is down a notch in all the benchmarks.

Result:
The Recycler code is much easier to understand.
raidyue pushed a commit to raidyue/netty that referenced this pull request Jul 8, 2022
Motivation:
We have in the past gotten reports of weird errors that we haven't been able to reproduce, but which could be explained by bugs in the Recycler.
The reason is that the Recycler was mixing concurrency, and interactions with the GC, in ways that were difficult to reason about.
It is unwise to leave suspicous-looking code unaddressed.
Concrete issues, like netty#11864, should also be addressed.

Modification:
The Recycler has been rewritten to use a much simpler algorithm, and now completely avoids the use of weak references.

The WeakOrderQueue is gone, and there is no longer a concept of a shared max capacity - only thread-local capacity.

There is also no more scavenging.
It turns out that other code was relying on the ability to forget references to objects they acquired from a Recycler, and this is hard to support if there is a shared pool for moving handles between threads.

Each thread now has a local pool (an MPSC queue).
Calls to get() and recycle() in the same thread will just operate on the local pool queue.
Calls to recycle() from a thread that did not get() that object (the object was acquired in another thread) will rely on the multi-producer safety of the queue to directly release it back to the local pool that the object came from.
We rely on setting an upper bound for the queue sizes, and the fallible offer() method, for restricting the max thread-local capacity of the recycler.

The constructors have been modified to deprecate the many configurations that are no longer relevant.
A new configuration parameter, the queue chunk-size, is exposed as a constructor parameter.
The queues will grow and shrink in units of this chunk size.

The benchmark from netty#8052 is copied over and updated, and shows that these changes help overall, with no hidden surprises.
Performance is roughly the same, and allocation rate is down a notch in all the benchmarks.

Result:
The Recycler code is much easier to understand.
chrisvest pushed a commit that referenced this pull request Oct 11, 2022
Motivation:

Some methods in `Recycler` have been marked deprecated since [#11858](#11858). Now they are not used any more. Maybe it's a good time to drop them in Netty 5.

Modification:

Remove some unused deprecated methods in `Recycler`.

Result:

Clean up work
chrisvest added a commit to chrisvest/netty that referenced this pull request Jan 31, 2023
Motivation:
The Recycler implementation was changed in netty#11858 to rely on an MPSC queue implementation for delivering released objects back to their originating thread local pool.
Typically, the release will often happen from the same thread that claimed the object, so the overhead of having a thread-safe release goes to waste.

Modification:
We add an unsynchronized ArrayDeque for batching claims out of the `pooledHandles`.
This amortises `claim` calls.

We then also re-introduce the concept of an owner thread (but by default only if said thread is a FastThreadLocalThread), and release directly into the claim `batch` if the release is from the owner thread.

Result:
The `RecyclerBenchmark.recyclerGetAndRecycle` benchmark sees a 27.4% improvement, and the `RecyclerBenchmark.producerConsumer` benchmark sees a 22.5% improvement.

Fixes netty#13153
normanmaurer added a commit that referenced this pull request Feb 2, 2023
Motivation:
The Recycler implementation was changed in #11858 to rely on an MPSC queue implementation for delivering released objects back to their originating thread local pool.
Typically, the release will often happen from the same thread that claimed the object, so the overhead of having a thread-safe release goes to waste.

Modification:
We add an unsynchronized ArrayDeque for batching claims out of the `pooledHandles`.
This amortises `claim` calls.

We then also re-introduce the concept of an owner thread (but by default only if said thread is a FastThreadLocalThread), and release directly into the claim `batch` if the release is from the owner thread.

Result:
The `RecyclerBenchmark.recyclerGetAndRecycle` benchmark sees a 27.4% improvement, and the `RecyclerBenchmark.producerConsumer` benchmark sees a 22.5% improvement.

Fixes #13153

Co-authored-by: Norman Maurer <[email protected]>
normanmaurer added a commit that referenced this pull request Feb 2, 2023
Motivation:
The Recycler implementation was changed in #11858 to rely on an MPSC queue implementation for delivering released objects back to their originating thread local pool.
Typically, the release will often happen from the same thread that claimed the object, so the overhead of having a thread-safe release goes to waste.

Modification:
We add an unsynchronized ArrayDeque for batching claims out of the `pooledHandles`.
This amortises `claim` calls.

We then also re-introduce the concept of an owner thread (but by default only if said thread is a FastThreadLocalThread), and release directly into the claim `batch` if the release is from the owner thread.

Result:
The `RecyclerBenchmark.recyclerGetAndRecycle` benchmark sees a 27.4% improvement, and the `RecyclerBenchmark.producerConsumer` benchmark sees a 22.5% improvement.

Fixes #13153

Co-authored-by: Norman Maurer <[email protected]>
@chrisvest chrisvest mentioned this pull request Oct 1, 2025
3 of 4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants