Skip to content

Centralize internal reference counting logic - #8614

Merged
normanmaurer merged 3 commits into
netty:4.1from
njhill:refcnt-updater
Apr 9, 2019
Merged

normanmaurer merged 3 commits into
netty:4.1from
njhill:refcnt-updater

Conversation

@njhill

@njhill njhill commented Nov 30, 2018

Copy link
Copy Markdown
Member

Motivation

AbstractReferenceCounted and AbstractReferenceCountedByteBuf contain duplicate logic for managing the volatile refcount in an optimized and consistent manner, which increased in complexity in #8583. It's possible to extract this into a common helper class now that all access is via an
AtomicIntegerFieldUpdater.

Modifications

  • Move duplicate logic into a shared ReferenceCountUpdater class
  • Incorporate some additional simplification for the most common single increment/decrement cases (fewer checks/operations)

Result

Less code duplication, better encapsulation of the "non-trivial" internal volatile refcount manipulation.

@netty-bot

Copy link
Copy Markdown

Can one of the admins verify this patch?

@normanmaurer

Copy link
Copy Markdown
Member

@netty-bot test this please

@franz1981

franz1981 commented Dec 1, 2018 •

Copy link
Copy Markdown
Contributor

@njhill I'm running AbstractReferenceCountedByteBufBenchmark pre/post this PR to check a "couple" of guesses related some performance impacts (and futher improvements) on JDK 8.
To be more specific, I'm measuring (with separate benchs) if:

  1. using a volatile load without using the updater is faster (as suggested here)
  2. having specific methods for retain/release with increment/decrement of 1 (avoiding any shift etc etc) is faster (assuming retain(1)/release(1) to be more frequent on user code)
  3. the effect of the ReferenceCountUpdater abstraction: due to it the Atomic updater is no longer a static final field and probably it won't be trusted anymore by the JVM

Let me know if you have already checked/evaluated some of them, you will save my machine to be on fire :P

@njhill

njhill commented Dec 2, 2018

Copy link
Copy Markdown
Member Author

Thanks @franz1981, much appreciated! I wasn't sure the change would be desirable even without unanticipated performance implications. I didn't have a chance to do any meaningful benchmarks yet and maybe should have done prior to opening this; would be great if you have some comparisons already in progress.

Re (1) using the updater for volatile loads is already done in the current version (although that was only changed recently) and was part of what motivated this refactor. If there are measurable advantages to direct access then maybe it's a showstopper.

Re (3) I'd been hoping that since everything is still static and/or final that there wouldn't be a cost to extracting the logic from an inlining pov, but after reading the great article you referenced I see that's likely not the case given the introduction of non-static and therefore non-trusted finals. A possible workaround for this could be to move the AtomicIntegerFieldUpdater fields back into the respective classes and have the static ReferenceCountUpdater methods take this as an additional field. But that makes things a bit messier and maybe dilutes the simplification to the point of negligible benefit (if it's even considered worthwhile in the first place!)

@franz1981

Copy link
Copy Markdown
Contributor

Sadly I wasn't able to get some meaningful number yet :(
I have run AbstractReferenceCountedByteBufBenchmark on 4.1 getting very noisy results (with delay = 1) even on the uncontended case.
I have taken a look to the produced ASM and I have verified that in any contexts the most of the time is being spent on lock xadd or lock cmpxchg (on retain and release) as expected (skid apart).
On the contended case retain:

(line 336)
  0.03%           0x00007efcfd14be06: mov    $0x2,%r10d
  0.00%           0x00007efcfd14be0c: lock xadd %r10d,0x20(%r9)  ;*invokevirtual getAndAddInt
                                                                ; - java.util.concurrent.atomic.AtomicIntegerFieldUpdater$AtomicIntegerFieldUpdaterImpl::getAndAdd@14 (line 493)
                                                                ; - io.netty.buffer.AbstractReferenceCountedByteBuf::retain0@9 (line 98)
                                                                ; - io.netty.buffer.AbstractReferenceCountedByteBuf::retain@2 (line 87)
                                                                ; - io.netty.buffer.AbstractReferenceCountedByteBufBenchmark::retainReleaseContended@4 (line 65)
                                                                ; - io.netty.buffer.generated.AbstractReferenceCountedByteBufBenchmark_retainReleaseContended_jmhTest::retainReleaseContended_sample_jmhStub@79 (line 336)
 10.97%           0x00007efcfd14be12: mov    %r10d,%ecx

On release:

(line 336)
  0.04%           0x00007efcfd14be87: cmp    $0x1,%r11d
                  0x00007efcfd14be8b: jle    0x00007efcfd14c3d1
  0.05%           0x00007efcfd14be91: mov    %eax,%r10d
                  0x00007efcfd14be94: add    $0xfffffffffffffffe,%r10d
                  0x00007efcfd14be98: lock cmpxchg %r10d,0x20(%rbp)
 10.23%           0x00007efcfd14be9e: sete   %r10b
  0.04%           0x00007efcfd14bea2: movzbl %r10b,%r10d        ;*invokevirtual compareAndSwapInt
                                                                ; - java.util.concurrent.atomic.AtomicIntegerFieldUpdater$AtomicIntegerFieldUpdaterImpl::compareAndSet@15 (line 463)
                                                                ; - io.netty.buffer.AbstractReferenceCountedByteBuf::releaseNonFinal0@15 (line 147)
                                                                ; - io.netty.buffer.AbstractReferenceCountedByteBuf::release0@44 (line 141)
                                                                ; - io.netty.buffer.AbstractReferenceCountedByteBuf::release@2 (line 124)
                                                                ; - io.netty.buffer.AbstractReferenceCountedByteBufBenchmark::retainReleaseContended@20 (line 67)
                                                                ; - io.netty.buffer.generated.AbstractReferenceCountedByteBufBenchmark_retainReleaseContended_jmhTest::retainReleaseContended_sample_jmhStub@79

A similar picture is on the uncontended side: I'm not getting why I have such noisy numbers with no contention TBH.
I think I need to find a better/different machine to run the tests so i will return with more meainingful numbers. 👍

A possible workaround for this could be to move the AtomicIntegerFieldUpdater fields back into the respective classes and have the static ReferenceCountUpdater methods take this as an additional field. But that makes things a bit messier and maybe dilutes the simplification to the point of negligible benefit (if it's even considered worthwhile in the first place!)

True, but just having a single code that manage all the complex logic here means to have (ideally) just one unit test to be maintained, that's good per-se. Even if we can't rely on any object-oriented way to reuse the code I suppose that's a win. Re the perf I would wait some results, because in my experience when xadd or cmpxchg are involved (and are unavoidable), the biggest wins (and looses) are just due to them. Considering the article I have pointed, Alexey is focusing on a primitive usage of atomicXXX while on Netty I suppose that people will do something with the buffer whilst retain/release it and it could mean that having an additional null-check probably won't make a big difference here (just guessing).
While seeking the max perf it will make sense, but I'm more worried that we won't have any visible perf regression, if negligible it is more then fine while saving some code complexities :P

@normanmaurer

Copy link
Copy Markdown
Member

I think as long as the performance does not suffer we should pull this in... @njhill @franz1981 any updates here ?

@njhill

njhill commented Dec 6, 2018

Copy link
Copy Markdown
Member Author

@normanmaurer thanks for the tentative approval and apologies for the delay. I haven't had a chance to dedicate time on my machine to running more benchmarks. Hopefully this isn't too urgent, I'll update as soon as I've managed to do more additional perf verification.

@franz1981

Copy link
Copy Markdown
Contributor

@normanmaurer Apologies for the delay by me as well: haven't had enough time to run other benchs. Will try to do it ASAP :)

@normanmaurer

Copy link
Copy Markdown
Member

@njhill @franz1981 thanks a lot!

@njhill

njhill commented Dec 10, 2018

Copy link
Copy Markdown
Member Author

Here's some benchmarks, the Old ones are the current 4.1 code and non-Old is this PR. As suspected it does look like there's non-negligible performance impact to some of the runs.. up to 5% for one of the uncontended ones. Guessing it's related to non-trusted finals. So this may need some further consideration - I'll try my suggestion in prior comment but am a bit uneasy about how convoluted the resulting code will be.

Benchmark                                                        (delay)    Mode       Cnt         Score   Error  Units
retainReleaseContended                                                 1  sample  34549340       481.406 ± 4.160  ns/op
retainReleaseContended:retainReleaseContended·p0.00                    1  sample                  38.000          ns/op
retainReleaseContended:retainReleaseContended·p0.50                    1  sample                  58.000          ns/op
retainReleaseContended:retainReleaseContended·p0.90                    1  sample                 237.000          ns/op
retainReleaseContended:retainReleaseContended·p0.95                    1  sample                1742.000          ns/op
retainReleaseContended:retainReleaseContended·p0.99                    1  sample               10224.000          ns/op
retainReleaseContended:retainReleaseContended·p0.999                   1  sample               30325.088          ns/op
retainReleaseContended:retainReleaseContended·p0.9999                  1  sample               60928.000          ns/op
retainReleaseContended:retainReleaseContended·p1.00                    1  sample            14450688.000          ns/op
retainReleaseContended                                                10  sample  34143927       537.469 ± 6.267  ns/op
retainReleaseContended:retainReleaseContended·p0.00                   10  sample                  50.000          ns/op
retainReleaseContended:retainReleaseContended·p0.50                   10  sample                  78.000          ns/op
retainReleaseContended:retainReleaseContended·p0.90                   10  sample                 277.000          ns/op
retainReleaseContended:retainReleaseContended·p0.95                   10  sample                2188.000          ns/op
retainReleaseContended:retainReleaseContended·p0.99                   10  sample               10544.000          ns/op
retainReleaseContended:retainReleaseContended·p0.999                  10  sample               30816.000          ns/op
retainReleaseContended:retainReleaseContended·p0.9999                 10  sample               56896.000          ns/op
retainReleaseContended:retainReleaseContended·p1.00                   10  sample            32964608.000          ns/op
retainReleaseContended                                               100  sample  26534751       761.801 ± 7.276  ns/op
retainReleaseContended:retainReleaseContended·p0.00                  100  sample                 223.000          ns/op
retainReleaseContended:retainReleaseContended·p0.50                  100  sample                 382.000          ns/op
retainReleaseContended:retainReleaseContended·p0.90                  100  sample                1672.000          ns/op
retainReleaseContended:retainReleaseContended·p0.95                  100  sample                2920.000          ns/op
retainReleaseContended:retainReleaseContended·p0.99                  100  sample                6328.000          ns/op
retainReleaseContended:retainReleaseContended·p0.999                 100  sample               15872.000          ns/op
retainReleaseContended:retainReleaseContended·p0.9999                100  sample               46817.587          ns/op
retainReleaseContended:retainReleaseContended·p1.00                  100  sample            42008576.000          ns/op
retainReleaseContended                                              1000  sample  30783091      2495.205 ± 5.129  ns/op
retainReleaseContended:retainReleaseContended·p0.00                 1000  sample                1982.000          ns/op
retainReleaseContended:retainReleaseContended·p0.50                 1000  sample                2436.000          ns/op
retainReleaseContended:retainReleaseContended·p0.90                 1000  sample                2484.000          ns/op
retainReleaseContended:retainReleaseContended·p0.95                 1000  sample                2512.000          ns/op
retainReleaseContended:retainReleaseContended·p0.99                 1000  sample                3576.000          ns/op
retainReleaseContended:retainReleaseContended·p0.999                1000  sample               15856.000          ns/op
retainReleaseContended:retainReleaseContended·p0.9999               1000  sample              102528.000          ns/op
retainReleaseContended:retainReleaseContended·p1.00                 1000  sample            12697600.000          ns/op
retainReleaseContended                                             10000  sample  25507193     23511.743 ± 9.214  ns/op
retainReleaseContended:retainReleaseContended·p0.00                10000  sample               20736.000          ns/op
retainReleaseContended:retainReleaseContended·p0.50                10000  sample               23616.000          ns/op
retainReleaseContended:retainReleaseContended·p0.90                10000  sample               23712.000          ns/op
retainReleaseContended:retainReleaseContended·p0.95                10000  sample               23776.000          ns/op
retainReleaseContended:retainReleaseContended·p0.99                10000  sample               27296.000          ns/op
retainReleaseContended:retainReleaseContended·p0.999               10000  sample               57664.000          ns/op
retainReleaseContended:retainReleaseContended·p0.9999              10000  sample              293519.667          ns/op
retainReleaseContended:retainReleaseContended·p1.00                10000  sample            16875520.000          ns/op
retainReleaseContendedOld                                              1  sample  33962105       470.742 ± 3.172  ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.00              1  sample                  39.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.50              1  sample                  68.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.90              1  sample                 269.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.95              1  sample                2032.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.99              1  sample                9232.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.999             1  sample               26368.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.9999            1  sample               53106.522          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p1.00              1  sample            10125312.000          ns/op
retainReleaseContendedOld                                             10  sample  35453031       500.149 ± 2.932  ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.00             10  sample                  46.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.50             10  sample                  72.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.90             10  sample                 248.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.95             10  sample                1762.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.99             10  sample               10464.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.999            10  sample               33472.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.9999           10  sample               62208.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p1.00             10  sample             9699328.000          ns/op
retainReleaseContendedOld                                            100  sample  27208756       751.582 ± 4.863  ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.00            100  sample                 222.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.50            100  sample                 374.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.90            100  sample                1628.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.95            100  sample                2852.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.99            100  sample                6320.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.999           100  sample               15968.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.9999          100  sample               51520.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p1.00            100  sample            15138816.000          ns/op
retainReleaseContendedOld                                           1000  sample  30840396      2484.078 ± 4.297  ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.00           1000  sample                1982.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.50           1000  sample                2432.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.90           1000  sample                2488.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.95           1000  sample                2520.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.99           1000  sample                3664.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.999          1000  sample               15024.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.9999         1000  sample               80768.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p1.00           1000  sample             8749056.000          ns/op
retainReleaseContendedOld                                          10000  sample  25540875     23480.982 ± 8.322  ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.00          10000  sample               21376.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.50          10000  sample               23616.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.90          10000  sample               23712.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.95          10000  sample               23776.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.99          10000  sample               27040.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.999         10000  sample               56704.000          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p0.9999        10000  sample              252905.574          ns/op
retainReleaseContendedOld:retainReleaseContendedOld·p1.00          10000  sample            10502144.000          ns/op
retainReleaseUncontended                                               1  sample   7873966        50.812 ± 0.399  ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.00                1  sample                  37.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.50                1  sample                  41.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.90                1  sample                  52.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.95                1  sample                  55.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.99                1  sample                  61.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.999               1  sample                 168.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.9999              1  sample               18048.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p1.00                1  sample               59264.000          ns/op
retainReleaseUncontended                                              10  sample   6511389        63.407 ± 0.481  ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.00               10  sample                  45.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.50               10  sample                  52.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.90               10  sample                  63.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.95               10  sample                  66.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.99               10  sample                  73.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.999              10  sample                 218.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.9999             10  sample               18368.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p1.00               10  sample               28800.000          ns/op
retainReleaseUncontended                                             100  sample   7159810       251.413 ± 0.639  ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.00              100  sample                 221.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.50              100  sample                 230.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.90              100  sample                 247.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.95              100  sample                 253.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.99              100  sample                 265.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.999             100  sample               12848.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.9999            100  sample               19552.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p1.00              100  sample               44992.000          ns/op
retainReleaseUncontended                                            1000  sample   5847015      2158.081 ± 1.818  ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.00             1000  sample                1972.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.50             1000  sample                1990.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.90             1000  sample                2164.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.95             1000  sample                2224.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.99             1000  sample                2544.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.999            1000  sample               20640.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.9999           1000  sample               24768.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p1.00             1000  sample              104576.000          ns/op
retainReleaseUncontended                                           10000  sample   4718127     21138.669 ± 7.352  ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.00            10000  sample               19488.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.50            10000  sample               19584.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.90            10000  sample               21408.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.95            10000  sample               33856.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.99            10000  sample               47488.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.999           10000  sample               57984.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p0.9999          10000  sample               71296.000          ns/op
retainReleaseUncontended:retainReleaseUncontended·p1.00            10000  sample              116736.000          ns/op
retainReleaseUncontendedOld                                            1  sample   7773171        49.135 ± 0.407  ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.00          1  sample                  36.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.50          1  sample                  40.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.90          1  sample                  44.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.95          1  sample                  45.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.99          1  sample                  53.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.999         1  sample                 156.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.9999        1  sample               18176.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p1.00          1  sample               34688.000          ns/op
retainReleaseUncontendedOld                                           10  sample   6402494        60.484 ± 0.473  ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.00         10  sample                  43.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.50         10  sample                  50.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.90         10  sample                  58.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.95         10  sample                  61.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.99         10  sample                  72.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.999        10  sample                 196.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.9999       10  sample               18208.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p1.00         10  sample               46144.000          ns/op
retainReleaseUncontendedOld                                          100  sample   7254955       249.867 ± 0.639  ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.00        100  sample                 218.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.50        100  sample                 228.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.90        100  sample                 246.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.95        100  sample                 251.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.99        100  sample                 264.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.999       100  sample               12880.704          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.9999      100  sample               19936.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p1.00        100  sample               29792.000          ns/op
retainReleaseUncontendedOld                                         1000  sample   5841954      2156.459 ± 1.766  ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.00       1000  sample                1970.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.50       1000  sample                1988.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.90       1000  sample                2164.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.95       1000  sample                2172.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.99       1000  sample                2388.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.999      1000  sample               20576.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.9999     1000  sample               23808.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p1.00       1000  sample               31840.000          ns/op
retainReleaseUncontendedOld                                        10000  sample   4694326     21250.046 ± 7.519  ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.00      10000  sample               19488.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.50      10000  sample               19584.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.90      10000  sample               21408.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.95      10000  sample               34048.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.99      10000  sample               48000.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.999     10000  sample               58048.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p0.9999    10000  sample               71296.000          ns/op
retainReleaseUncontendedOld:retainReleaseUncontendedOld·p1.00      10000  sample              521728.000          ns/op

@franz1981

Copy link
Copy Markdown
Contributor

Guessing it's related to non-trusted finals

Probably runinng the bench with the larger difference in perf using the LinuxPefAsmProfiler would help to check on the ASM if are performed extra checks due to the non-trusted finals (I could try to help on it with my limited knowledge of ASM)

I suppose that the way to "fix" this using some static utility methods is the safer-way perf-wise: nontheless I have very noisy results from this bench (on 4.1 too) so I'm not 100% sure of it

@franz1981

franz1981 commented Dec 12, 2018 •

Copy link
Copy Markdown
Contributor

@njhill @normanmaurer Probably OT, but wdyt about finding a way (who said Netty 5?) to handle the usually common case where a ByteBuf life-cycle is bound exclusively to a single thread?
It would allow to call retain/release in an exlcusive way ie lazySet/putOrdered or just a plain non-volatile set, making the single threaded case a lot faster. Sometime we just need unshared ref counting.

@normanmaurer

Copy link
Copy Markdown
Member

@njhill @franz1981 what do we want to do with this one ?

@njhill

njhill commented Mar 1, 2019

Copy link
Copy Markdown
Member Author

@normanmaurer I'm not super excited about it given the not-quite-negligible performance degradation and fact that it will make call stacks longer, but let me do a last refactor to see how convoluted it looks when non-static finals are avoided .

@normanmaurer

normanmaurer commented Mar 1, 2019 via email

Copy link
Copy Markdown
Member

@njhill

njhill commented Mar 13, 2019

Copy link
Copy Markdown
Member Author

@normanmaurer @franz1981 I have reworked it slightly to avoid non-trusted finals and verified this closes the performance gap, PTAL. It's not as messy as I had expected so I'm now back on the side of thinking that the change is worthwhile. We can take the simplification even further with java 8 default methods (could do a follow-on for netty 5).

@normanmaurer

Copy link
Copy Markdown
Member

@njhill nice one! I really like how it reduces code-duplication.

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

I really like this a lot!

@normanmaurer normanmaurer added this to the 4.1.35.Final milestone Mar 28, 2019
@normanmaurer normanmaurer self-assigned this Mar 28, 2019

@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! Have u tried some bench right?
The code reduction is already a selling point for me ;)

@normanmaurer

Copy link
Copy Markdown
Member

@franz1981 as far as I understood @njhill said its the same speed....

@njhill

njhill commented Mar 28, 2019

Copy link
Copy Markdown
Member Author

@franz1981 yes, I confirmed the prior suspicion that it was use of non-trusted finals that caused a small perf regression before, and also confirmed that the latest change eliminates that. This also includes some minor tweaks to favour most common cases (inc/dec count == 1 and ref count == 1 or 2), and IIRC looked like there might even be a slight improvement in the numbers.

@franz1981

Copy link
Copy Markdown
Contributor

Yep I was expecting a slight improvement too :) well done!!!

@njhill

njhill commented Mar 29, 2019

Copy link
Copy Markdown
Member Author

Thanks @franz1981 I learnt some useful things from you in the process!

njhill added 3 commits March 29, 2019 12:54
Motivation

AbstractReferenceCounted and AbstractReferenceCountedByteBuf contain
duplicate logic for managing the volatile refcount in an optimized and
consistent manner, which increased in complexity in netty#8583. It's possible
to extract this into a common helper class now that all access is via an
AtomicIntegerFieldUpdater.

Modifications

- Move duplicate logic into a shared ReferenceCountUpdater class
- Incorporate some additional simplification for the most common single
increment/decrement cases (fewer checks/operations)

Result

Less code duplication, better encapsulation of the "non-trivial"
internal volatile refcount manipulation
@njhill

njhill commented Mar 29, 2019

Copy link
Copy Markdown
Member Author

@normanmaurer done! I also renamed a method for consistency, and tidied some comments.

@normanmaurer

Copy link
Copy Markdown
Member

@netty-bot test this please

@normanmaurer
normanmaurer merged commit b26a61a into netty:4.1 Apr 9, 2019
@normanmaurer

Copy link
Copy Markdown
Member

@njhill thanks a lot!

normanmaurer pushed a commit that referenced this pull request Apr 9, 2019
Motivation

AbstractReferenceCounted and AbstractReferenceCountedByteBuf contain
duplicate logic for managing the volatile refcount in an optimized and
consistent manner, which increased in complexity in #8583. It's possible
to extract this into a common helper class now that all access is via an
AtomicIntegerFieldUpdater.

Modifications

- Move duplicate logic into a shared ReferenceCountUpdater class
- Incorporate some additional simplification for the most common single
increment/decrement cases (fewer checks/operations)

Result

Less code duplication, better encapsulation of the "non-trivial"
internal volatile refcount manipulation
@njhill
njhill deleted the refcnt-updater branch April 10, 2019 00:10

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

Brilliant work as usual, @njhill

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants