Skip to content

RFC: Reimplement Recycler to reduce impact of pathological cases - #8052

Closed
ejona86 wants to merge 2 commits into
netty:4.1from
ejona86:weak-recycler
Closed

ejona86 wants to merge 2 commits into
netty:4.1from
ejona86:weak-recycler

Conversation

@ejona86

@ejona86 ejona86 commented Jun 25, 2018

Copy link
Copy Markdown
Member

Motivation:

Recycler is hard to tune and reduces performance in same cases. Inspired by the
simplicity and large performance gains seen with Go's sync.Pool, which only
reuses objects between GCs, it seemed a more conservative approach could be
applied to Recycler. Note that this looks nothing like sync.Pool, as Recycler
has very different trade-offs and environment.

Modifications:

Reimplement Recycler as a weak reference free list, with low rate of
recycler-enabled objects and heuristics for reusing older objects. Add a
nanobenchmark to better contrast the implementation options, especially with
the 'gc' profiler.

Result:

A Recycler that can improve performance while worst-case would have neglible
impact to GC, memory, and CPU use.


There's no rush for this and it wouldn't bother me to close it. I couldn't find the issue where we were talking about disabling the Recycler. This PR should not be considered an argument against doing that; this was a "fun thing" that had interesting enough results I thought I should share. I honestly don't know if it's worth anyone's time to look into this as a "solution" to Recycler.

I've only run some nanobenchmarks against this. The "generational" behavior in particular is unproven, although it looks interesting. It's possible it is unnecessary and just a single LIFO linked list would be enough, but I went ahead with it because it was the more conservative approach and shouldn't be any worse than the single list approach.

I was a bit worried about whether having so many weak references could cause GC slowdowns; weak refs aren't something I tend to see at large scale. But I think many such concerns are addressed by the 1% sampling.

Although it didn't show up in the code at all, I should note it is really easy to add statistics to the linked list like its length or how reused the objects were, and then have that available after a GC threw the list away by keeping the stats in the WeakRef.

The new implementation doesn't need to use ObjectCleaner 😄.

@ejona86

ejona86 commented Jun 25, 2018

Copy link
Copy Markdown
Member Author

Note that producerConsumer is mainly there for the gc.alloc.rate.norm. The runtimes are coarse, but get the point across; I ran with -Dforks=1 -DwarmupIterations=5 -DmeasureIterations=3

before:
RecyclerBenchmark.plainNew                                               avgt    3     9.088 ±    2.504   ns/op
RecyclerBenchmark.plainNew:·gc.alloc.rate                                avgt    3  5597.032 ± 1510.210  MB/sec
RecyclerBenchmark.plainNew:·gc.alloc.rate.norm                           avgt    3    80.000 ±    0.001    B/op
RecyclerBenchmark.plainNew:·gc.churn.PS_Eden_Space                       avgt    3  5601.251 ±  158.963  MB/sec
RecyclerBenchmark.plainNew:·gc.churn.PS_Eden_Space.norm                  avgt    3    80.071 ±   20.797    B/op
RecyclerBenchmark.plainNew:·gc.churn.PS_Survivor_Space                   avgt    3     0.076 ±    0.218  MB/sec
RecyclerBenchmark.plainNew:·gc.churn.PS_Survivor_Space.norm              avgt    3     0.001 ±    0.003    B/op
RecyclerBenchmark.plainNew:·gc.count                                     avgt    3    99.000             counts
RecyclerBenchmark.plainNew:·gc.time                                      avgt    3    68.000                 ms
RecyclerBenchmark.producerConsumer                                       avgt    3   316.584 ±   77.286   ns/op
RecyclerBenchmark.producerConsumer:consumer                              avgt    3   316.583 ±   77.303   ns/op
RecyclerBenchmark.producerConsumer:producer                              avgt    3   316.584 ±   77.271   ns/op
RecyclerBenchmark.producerConsumer:·gc.alloc.rate                        avgt    3   264.830 ±  125.890  MB/sec
RecyclerBenchmark.producerConsumer:·gc.alloc.rate.norm                   avgt    3    65.978 ±   16.503    B/op
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Eden_Space               avgt    3   273.072 ± 1725.475  MB/sec
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Eden_Space.norm          avgt    3    67.844 ±  417.193    B/op
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Survivor_Space           avgt    3     0.021 ±    0.380  MB/sec
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Survivor_Space.norm      avgt    3     0.005 ±    0.094    B/op
RecyclerBenchmark.producerConsumer:·gc.count                             avgt    3     5.000             counts
RecyclerBenchmark.producerConsumer:·gc.time                              avgt    3     5.000                 ms
RecyclerBenchmark.recyclerGetAndOrphan                                   avgt    3    15.122 ±    1.601   ns/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.alloc.rate                    avgt    3  4704.758 ±  520.253  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.alloc.rate.norm               avgt    3   112.000 ±    0.001    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Eden_Space           avgt    3  4754.013 ±   16.992  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Eden_Space.norm      avgt    3   113.175 ±   12.780    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Survivor_Space       avgt    3     0.173 ±    1.160  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Survivor_Space.norm  avgt    3     0.004 ±    0.027    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.count                         avgt    3    84.000             counts
RecyclerBenchmark.recyclerGetAndOrphan:·gc.time                          avgt    3    55.000                 ms
RecyclerBenchmark.recyclerGetAndRecycle                                  avgt    3    14.302 ±    3.369   ns/op
RecyclerBenchmark.recyclerGetAndRecycle:·gc.alloc.rate                   avgt    3    ≈ 10⁻⁴             MB/sec
RecyclerBenchmark.recyclerGetAndRecycle:·gc.alloc.rate.norm              avgt    3    ≈ 10⁻⁵               B/op
RecyclerBenchmark.recyclerGetAndRecycle:·gc.count                        avgt    3       ≈ 0             counts

after (no 1% sampling):
RecyclerBenchmark.plainNew                                               avgt    3     8.983 ±    0.167   ns/op
RecyclerBenchmark.plainNew:·gc.alloc.rate                                avgt    3  5654.780 ±  154.695  MB/sec
RecyclerBenchmark.plainNew:·gc.alloc.rate.norm                           avgt    3    80.000 ±    0.001    B/op
RecyclerBenchmark.plainNew:·gc.churn.PS_Eden_Space                       avgt    3  5658.427 ± 1857.463  MB/sec
RecyclerBenchmark.plainNew:·gc.churn.PS_Eden_Space.norm                  avgt    3    80.050 ±   24.149    B/op
RecyclerBenchmark.plainNew:·gc.churn.PS_Survivor_Space                   avgt    3     0.062 ±    0.001  MB/sec
RecyclerBenchmark.plainNew:·gc.churn.PS_Survivor_Space.norm              avgt    3     0.001 ±    0.001    B/op
RecyclerBenchmark.plainNew:·gc.count                                     avgt    3   100.000             counts
RecyclerBenchmark.plainNew:·gc.time                                      avgt    3    63.000                 ms
RecyclerBenchmark.producerConsumer                                       avgt    3   264.363 ±  952.286   ns/op
RecyclerBenchmark.producerConsumer:consumer                              avgt    3   264.365 ±  952.373   ns/op
RecyclerBenchmark.producerConsumer:producer                              avgt    3   264.360 ±  952.199   ns/op
RecyclerBenchmark.producerConsumer:·gc.alloc.rate                        avgt    3   388.263 ± 1531.383  MB/sec
RecyclerBenchmark.producerConsumer:·gc.alloc.rate.norm                   avgt    3    78.489 ±    9.565    B/op
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Eden_Space               avgt    3   383.914 ± 1746.368  MB/sec
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Eden_Space.norm          avgt    3    77.384 ±   90.381    B/op
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Survivor_Space           avgt    3     0.021 ±    0.380  MB/sec
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Survivor_Space.norm      avgt    3     0.004 ±    0.084    B/op
RecyclerBenchmark.producerConsumer:·gc.count                             avgt    3     7.000             counts
RecyclerBenchmark.producerConsumer:·gc.time                              avgt    3     4.000                 ms
RecyclerBenchmark.recyclerGetAndOrphan                                   avgt    3    23.962 ±    3.783   ns/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.alloc.rate                    avgt    3  3816.896 ±  562.904  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.alloc.rate.norm               avgt    3   144.000 ±    0.001    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Eden_Space           avgt    3  3847.989 ± 1789.199  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Eden_Space.norm      avgt    3   145.180 ±   71.643    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Survivor_Space       avgt    3     0.250 ±    1.317  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Survivor_Space.norm  avgt    3     0.009 ±    0.051    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.count                         avgt    3    68.000             counts
RecyclerBenchmark.recyclerGetAndOrphan:·gc.time                          avgt    3    48.000                 ms
RecyclerBenchmark.recyclerGetAndRecycle                                  avgt    3    18.690 ±    0.170   ns/op
RecyclerBenchmark.recyclerGetAndRecycle:·gc.alloc.rate                   avgt    3    ≈ 10⁻⁴             MB/sec
RecyclerBenchmark.recyclerGetAndRecycle:·gc.alloc.rate.norm              avgt    3    ≈ 10⁻⁵               B/op
RecyclerBenchmark.recyclerGetAndRecycle:·gc.count                        avgt    3       ≈ 0             counts


# Note the runtime difference in recyclerGetAndRecycle and lack of change to recyclerGetAndOrphan
after (no 1% sampling, non-generational):
RecyclerBenchmark.plainNew                                               avgt    3     9.029 ±    1.148   ns/op
RecyclerBenchmark.plainNew:·gc.alloc.rate                                avgt    3  5629.562 ±  710.186  MB/sec
RecyclerBenchmark.plainNew:·gc.alloc.rate.norm                           avgt    3    80.000 ±    0.001    B/op
RecyclerBenchmark.plainNew:·gc.churn.PS_Eden_Space                       avgt    3  5605.200 ± 3104.981  MB/sec
RecyclerBenchmark.plainNew:·gc.churn.PS_Eden_Space.norm                  avgt    3    79.647 ±   35.767    B/op
RecyclerBenchmark.plainNew:·gc.churn.PS_Survivor_Space                   avgt    3     0.076 ±    0.580  MB/sec
RecyclerBenchmark.plainNew:·gc.churn.PS_Survivor_Space.norm              avgt    3     0.001 ±    0.008    B/op
RecyclerBenchmark.plainNew:·gc.count                                     avgt    3    99.000             counts
RecyclerBenchmark.plainNew:·gc.time                                      avgt    3    63.000                 ms
RecyclerBenchmark.producerConsumer                                       avgt    3   237.773 ± 1211.562   ns/op
RecyclerBenchmark.producerConsumer:consumer                              avgt    3   237.771 ± 1211.542   ns/op
RecyclerBenchmark.producerConsumer:producer                              avgt    3   237.775 ± 1211.582   ns/op
RecyclerBenchmark.producerConsumer:·gc.alloc.rate                        avgt    3   466.422 ± 2551.443  MB/sec
RecyclerBenchmark.producerConsumer:·gc.alloc.rate.norm                   avgt    3    82.588 ±   24.188    B/op
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Eden_Space               avgt    3   439.561 ± 1746.286  MB/sec
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Eden_Space.norm          avgt    3    81.939 ±  546.164    B/op
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Survivor_Space           avgt    3     0.014 ±    0.219  MB/sec
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Survivor_Space.norm      avgt    3     0.003 ±    0.045    B/op
RecyclerBenchmark.producerConsumer:·gc.count                             avgt    3     8.000             counts
RecyclerBenchmark.producerConsumer:·gc.time                              avgt    3     8.000                 ms
RecyclerBenchmark.recyclerGetAndOrphan                                   avgt    3    23.661 ±    4.677   ns/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.alloc.rate                    avgt    3  3864.795 ±  812.263  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.alloc.rate.norm               avgt    3   144.000 ±    0.001    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Eden_Space           avgt    3  3903.223 ±   47.398  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Eden_Space.norm      avgt    3   145.445 ±   30.515    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Survivor_Space       avgt    3     0.277 ±    1.796  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Survivor_Space.norm  avgt    3     0.010 ±    0.066    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.count                         avgt    3    69.000             counts
RecyclerBenchmark.recyclerGetAndOrphan:·gc.time                          avgt    3    43.000                 ms
RecyclerBenchmark.recyclerGetAndRecycle                                  avgt    3    13.730 ±    0.977   ns/op
RecyclerBenchmark.recyclerGetAndRecycle:·gc.alloc.rate                   avgt    3    ≈ 10⁻⁴             MB/sec
RecyclerBenchmark.recyclerGetAndRecycle:·gc.alloc.rate.norm              avgt    3    ≈ 10⁻⁵               B/op
RecyclerBenchmark.recyclerGetAndRecycle:·gc.count                        avgt    3       ≈ 0             counts


after (1% sampling; this is the code you actually see):
RecyclerBenchmark.plainNew                                               avgt    3     9.053 ±    2.028   ns/op
RecyclerBenchmark.plainNew:·gc.alloc.rate                                avgt    3  5616.314 ± 1301.369  MB/sec
RecyclerBenchmark.plainNew:·gc.alloc.rate.norm                           avgt    3    80.000 ±    0.001    B/op
RecyclerBenchmark.plainNew:·gc.churn.PS_Eden_Space                       avgt    3  5601.811 ± 3141.269  MB/sec
RecyclerBenchmark.plainNew:·gc.churn.PS_Eden_Space.norm                  avgt    3    79.781 ±   26.312    B/op
RecyclerBenchmark.plainNew:·gc.churn.PS_Survivor_Space                   avgt    3     0.090 ±    0.791  MB/sec
RecyclerBenchmark.plainNew:·gc.churn.PS_Survivor_Space.norm              avgt    3     0.001 ±    0.011    B/op
RecyclerBenchmark.plainNew:·gc.count                                     avgt    3    99.000             counts
RecyclerBenchmark.plainNew:·gc.time                                      avgt    3    67.000                 ms
RecyclerBenchmark.producerConsumer                                       avgt    3   194.900 ±  351.183   ns/op
RecyclerBenchmark.producerConsumer:consumer                              avgt    3   194.902 ±  351.094   ns/op
RecyclerBenchmark.producerConsumer:producer                              avgt    3   194.897 ±  351.273   ns/op
RecyclerBenchmark.producerConsumer:·gc.alloc.rate                        avgt    3   296.171 ±  552.715  MB/sec
RecyclerBenchmark.producerConsumer:·gc.alloc.rate.norm                   avgt    3    45.155 ±    4.967    B/op
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Eden_Space               avgt    3   273.668 ± 1741.189  MB/sec
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Eden_Space.norm          avgt    3    41.460 ±  244.824    B/op
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Survivor_Space           avgt    3     0.035 ±    0.580  MB/sec
RecyclerBenchmark.producerConsumer:·gc.churn.PS_Survivor_Space.norm      avgt    3     0.005 ±    0.092    B/op
RecyclerBenchmark.producerConsumer:·gc.count                             avgt    3     5.000             counts
RecyclerBenchmark.producerConsumer:·gc.time                              avgt    3     3.000                 ms
RecyclerBenchmark.recyclerGetAndOrphan                                   avgt    3    12.599 ±    6.015   ns/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.alloc.rate                    avgt    3  4083.809 ± 1873.270  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.alloc.rate.norm               avgt    3    81.000 ±    0.001    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Eden_Space           avgt    3  4072.825 ± 3085.454  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Eden_Space.norm      avgt    3    80.764 ±   30.359    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Survivor_Space       avgt    3     0.062 ±    0.378  MB/sec
RecyclerBenchmark.recyclerGetAndOrphan:·gc.churn.PS_Survivor_Space.norm  avgt    3     0.001 ±    0.007    B/op
RecyclerBenchmark.recyclerGetAndOrphan:·gc.count                         avgt    3    72.000             counts
RecyclerBenchmark.recyclerGetAndOrphan:·gc.time                          avgt    3    50.000                 ms
RecyclerBenchmark.recyclerGetAndRecycle                                  avgt    3    19.069 ±    4.756   ns/op
RecyclerBenchmark.recyclerGetAndRecycle:·gc.alloc.rate                   avgt    3    ≈ 10⁻⁴             MB/sec
RecyclerBenchmark.recyclerGetAndRecycle:·gc.alloc.rate.norm              avgt    3    ≈ 10⁻⁵               B/op
RecyclerBenchmark.recyclerGetAndRecycle:·gc.count                        avgt    3       ≈ 0             counts

@normanmaurer

Copy link
Copy Markdown
Member

@ejona86 I like the idea... one thing I am not sure about is how this will work in the scenario where we allocate in one thread and recycle in another thread. In the old implementation it was possible while still get a good "hit count". How do we account for this here ?

@normanmaurer

Copy link
Copy Markdown
Member

@mosesn @bryce-anderson @jasontedor @Scottmitch @vkostyukov @carl-mastrangelo I would like to get your thoughts on this as well.

@vkostyukov as you mentioned you disabled the recycler in finagle in #5968 would it be possible to test this one as well ?

@ejona86 the issue you were searching for is #5968.

@ejona86

ejona86 commented Jun 25, 2018

Copy link
Copy Markdown
Member Author

one thing I am not sure about is how this will work in the scenario where we allocate in one thread and recycle in another thread

This handles that case by the recycling thread receiving "ownership" of the recycled instance (no pushLater). If that receiving thread needs to allocate in the future, it can use the recycled instance. So if coordinating between Netty threads, it should work well.

If the recycling thread won't ever need instances (it's not a producer as well) or there's an imbalance, then no (or limited) reuse will be performed. This is when the 1% sampling kicks in and predominantly uses non-recyclable instances, effectively as if the Recycler wasn't being used. Note that this is opposite to dropHandle() which drops objects when recycling; they are instead created with the inability to be recycled.

It would be possible to have a larger shared pool to put things in when a particular generation's list grows too long, or something. But it would need synchronization and Java's GC seems better suited to that case.

I didn't entirely follow how pushLater worked in the previous implementation. That's partly because I spent time confused until I realized DELAYED_RECYCLED is a static ThreadLocal whereas threadLocal was non-static. gc.alloc.rate.norm before for producerConsumer is 66 B/op. I believe that's halved, because it counts an "op" from both producer and consumer, so it's 132 B/op. So either the benchmark is broken or the previous implementation wouldn't reuse if a consumer is not a producer.

@bryce-anderson

Copy link
Copy Markdown
Contributor

Based on the forewarning by @ejona86 I haven't read the implementation in depth so I can't comment on it one way or the other. In general this sounds like an improvement since it attempts to prevent unnecessary generation migration, but it's hard to say how much. In my limited experience, microbenchmarks haven't done a great job representing how changes that can affect GC behavior will affect real applications.

If we think that recyclers are here to stay then let me know and I'll sink the time into a thorough review.

Fwiw, we just disabled recyclers (twitter/finagle@b8523a4) as @vkostyukov noted on issue #5968. Maybe he's interested in running some benchmarks of the patch and seeing how they do.

@vkostyukov

Copy link
Copy Markdown
Contributor

As Bryce mentioned, we decided to disable Recyclers in Finagle as we observed they come at a non-trivial CPU cost. Disabling them increased RPS/core score in our internal Finagle simulations (with a small penalty in allocations/req) - see this comment for more details.

I'd be happy to try these changes on our internal setup. Will update this thread when I know more.

@ejona86

ejona86 commented Jun 25, 2018

Copy link
Copy Markdown
Member Author

@vkostyukov, thanks for offering to try out this change. It'll be useful to know if you used CMS or G1.

@normanmaurer

normanmaurer commented Jun 25, 2018 •

Copy link
Copy Markdown
Member

@ejona86 yeah thats what I was talking about... I think it can "frequently" happen that allocations happen from a non EventLoop thread that will never recycle by itself. For example if people write ByteBuf from outside the EventLoop.

@ejona86

ejona86 commented Jun 25, 2018

Copy link
Copy Markdown
Member Author

I think it can "frequently" happen that allocations happen from a non EventLoop thread that will never recycle by itself.

I agree it can happen and isn't necessarily rare, although I do question if it happens frequently enough to make a difference in performance, especially since the code would need some slower paths to handle it. Did the previous Recycler implementation optimize that case? The benchmark makes it seem like not, but the code is not quite clear without me looking deeper.

@normanmaurer

Copy link
Copy Markdown
Member

@ejona86 optimised before for the use case no... handled it yes ;).

I just wanted to mention it and it may be not a big deal as you suggested.

@normanmaurer

Copy link
Copy Markdown
Member

@ejona86 could you incorporate the requested changes and @vkostyukov could you do some testings with this ?

@ejona86
ejona86 force-pushed the weak-recycler branch 2 times, most recently from 544b5f7 to 5728c6e Compare July 11, 2018 16:56
@ejona86

ejona86 commented Jul 11, 2018

Copy link
Copy Markdown
Member Author

I rebased past #8064, but the resulting file should be the same. I did not squash.

@normanmaurer

Copy link
Copy Markdown
Member

@ejona86 @bryce-anderson @vkostyukov @mosesn how can me move this forward ? Any takers ?

@netty-bot

Copy link
Copy Markdown

Can one of the admins verify this patch?

@normanmaurer

normanmaurer commented Oct 17, 2018 •

Copy link
Copy Markdown
Member

@netty-bot test this please

@normanmaurer

Copy link
Copy Markdown
Member

@ejona86 can you rebase one last time ?

Motivation:

Recycler is hard to tune and reduces performance in same cases. Inspired by the
simplicity and large performance gains seen with Go's sync.Pool, which only
reuses objects between GCs, it seemed a more conservative approach could be
applied to Recycler. Note that this looks nothing like sync.Pool, as Recycler
has _very_ different trade-offs and environment.

Modifications:

Reimplement Recycler as a weak reference free list, with low rate of
recycler-enabled objects and heuristics for reusing older objects. Add a
nanobenchmark to better contrast the implementation options, especially with
the 'gc' profiler.

Result:

A Recycler that can improve performance while worst-case would have neglible
impact to GC, memory, and CPU use.
@ejona86

ejona86 commented Oct 17, 2018

Copy link
Copy Markdown
Member Author

Rebased past 338ef96 (although I threw away the change to Recycler, because my new implementation didn't have the bug) and squashed commits.

@AbcCreatorDeep

Copy link
Copy Markdown

@ejona86 hi,Man, what is your final solution? There is too much content above, I suggest using soft references to solve this problem, don't you think?

@AbcCreatorDeep

Copy link
Copy Markdown

don't say more,look this code : private DefaultHandle[] elements ; it was used to save object , it's Strong Reference . thougth it's cache , why not use SoftReference instead it ? such as private SoftReference>[] elements , we can in case of OOM ERROR

normanmaurer added a commit that referenced this pull request Oct 25, 2019
…ementions of an Object pool

Motivation:

At the moment we directly extend the Recycler base class in our code which makes it hard to experiment with different Object pool implementation. It would be nice to be able to switch from one to another by using a system property in the future. This would also allow to more easily test things like #8052.

Modifications:

- Introduce ObjectPool class with static method that we now use internally to obtain an ObjectPool implementation.
- Wrap the Recycler into an ObjectPool and return it for now

Result:

Preparation for different ObjectPool implementations
normanmaurer added a commit that referenced this pull request Oct 26, 2019
…ementions of an Object pool (#9715)

Motivation:

At the moment we directly extend the Recycler base class in our code which makes it hard to experiment with different Object pool implementation. It would be nice to be able to switch from one to another by using a system property in the future. This would also allow to more easily test things like #8052.

Modifications:

- Introduce ObjectPool class with static method that we now use internally to obtain an ObjectPool implementation.
- Wrap the Recycler into an ObjectPool and return it for now

Result:

Preparation for different ObjectPool implementations
normanmaurer added a commit that referenced this pull request Oct 26, 2019
…ementions of an Object pool (#9715)

Motivation:

At the moment we directly extend the Recycler base class in our code which makes it hard to experiment with different Object pool implementation. It would be nice to be able to switch from one to another by using a system property in the future. This would also allow to more easily test things like #8052.

Modifications:

- Introduce ObjectPool class with static method that we now use internally to obtain an ObjectPool implementation.
- Wrap the Recycler into an ObjectPool and return it for now

Result:

Preparation for different ObjectPool implementations
@normanmaurer

Copy link
Copy Markdown
Member

@ejona86 let me try to push this over the finish line ...

/cc @chrisvest any thoughts on this ?

@ejona86

ejona86 commented Feb 2, 2021

Copy link
Copy Markdown
Member Author

@normanmaurer, I'd be fine rebasing it or the like. It's more of a question "does anyone want it" and "does the code look fine."

@normanmaurer

Copy link
Copy Markdown
Member

@ejona86 i really like the idea and would like to experiment with it

@ejona86

ejona86 commented Feb 2, 2021

Copy link
Copy Markdown
Member Author

FWIW, G1 is also a lot more common now and there are situations with G1 where the "do no harm" principle doesn't hold. But it still holds promise to be better than the current recycler.

@normanmaurer

Copy link
Copy Markdown
Member

Let me take this over and do some experimental integration

@chrisvest chrisvest 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.

Had a look at the code. Some parts have me stratching my head, but overall this is a lot simpler that the existing Recycler. Mostly because it no longer attempts to recycle objects across threads. I suspect the current implementation has bugs in that area anyway (e.g. #9640), so I think it's worth to explore the approach given here.

This needs a rebase to fix some conflicts, and more conflicts are in the pipeline with #11037.

maxId = parent.currentGenId.get();
if (maxId == genMaxId[i - 1]) {
// This is the first thread to detect the new generation
parent.currentGenId.incrementAndGet();

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.

The check and the increment are not atomic, so isn't this racy?

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.

It'd be quite fair to use a compareAndSet() here to reduce mental burden and worries.

But I don't think the race matters much as few DefaultHandle()s would be allocated within the race window. It mainly "wastes" an id. I do agree that any DefaultHandles()s allocated during the race window will be handled less than ideal, as they might not be treated as eden in some threads.

When writing the code, I thought of currentGenId as just a clock. The clock needs to tick frequently enough to separate generations, but if it runs too frequently the code would react reasonably. It was so long ago that I can't be certain, but I think I might have purposefully avoided CAS here to help prevent myself from assuming a nice, orderly progression to avoid overly relying on "clean" interactions between threads and make sure I handled "when things go wrong" cases.

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.

Ok. If it's fine for them race here, then I think it'd be good to call that out in a comment.

elements[size] = item;
this.size = size + 1;
node.next = genHead[i];
genHead[i] = node.selfRef;

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.

The array business is not obvious to me. Here we are relying on genMaxId.length being one less than genHead.length, which feels shaky.

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.

They are parallel arrays and need to have that relationship; they are very closely related. We could call it out more in the documentation, if that would help.

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.

The genMaxId field javadoc comment could be expanded to explain how these two arrays work together. I think that would be useful as this code is a little unusual :)

}
handle.hasBeenRecycled = true;
if ((allocationCount++ & 63) == 0) {
// Create recyclable objects at a low rate. If they are helpful, they will accumulate

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.

(Score:5, Insightful)

// that won't be reused." While more sophisticated methods of following the
// rule are possible, the class uses a simple approach of making ~1% of the
// new objects be recyclable. If recycling is helping, the recyclable
// objects will accumulate.

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.

Good comment.

@normanmaurer

Copy link
Copy Markdown
Member

I really would like to get something like this in for netty 5.

@AbcCreatorDeep

AbcCreatorDeep commented Aug 25, 2021 via email

Copy link
Copy Markdown

@normanmaurer

Copy link
Copy Markdown
Member

@AbcCreatorDeep

AbcCreatorDeep commented Aug 25, 2021 via email

Copy link
Copy Markdown

@ejona86

ejona86 commented Aug 25, 2021

Copy link
Copy Markdown
Member Author

@normanmaurer, want me to rebase this on 4.1 to avoid the conflicts, or do we think we'll just make the change in 5.0?

@normanmaurer

Copy link
Copy Markdown
Member

Maybe just rebase on 5.0 .

chrisvest added a commit to chrisvest/netty that referenced this pull request Nov 25, 2021
Also amend it to prevent stalls on benchmark shut down.
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.
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.
@chrisvest

Copy link
Copy Markdown
Member

@ejona86 I think we can close this since #11858 was merged?

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.
@normanmaurer

Copy link
Copy Markdown
Member

Let me close this for now

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants