Repository navigation
Rewrite and simplify Recycler - #11858
Conversation
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.
9e021e9 to
5a0a8ed
Compare
|
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. |
|
Very happy that we're getting rid of weak ref really: anything that's not using it is a +100 from me :P |
Also amend it to prevent stalls on benchmark shut down.
|
@franz1981 Updated. Please take a look. |
|
Got benchmark numbers. Old version: This version: Summary:
|
normanmaurer
left a comment
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
nit: why not just create this directly ? I think there is really no need for the extra static block here.
There was a problem hiding this comment.
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.
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.
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.
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.
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.
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
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]>
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]>
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.