GV

GeezerVison

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

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

On this site

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

This view

  • Original page
  • New address

GeezerVison is showing github.com — Simplify reference counting by chrisvest · Pull Request #15764 · netty/netty

Conversation

@chrisvest

@chrisvest chrisvest commented Oct 16, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Motivation:
Our current reference counting algorithm is quite complicated, and the compiled native code size puts pressure on the JIT inliner, which in turn hurts performance.

Modification:
Simplify the code by removing special case checks for certain constants.
Reorganize the code to use fewer branches.
Remove the different uses of ReferenceCountUpdater and introduce a RefCnt.
Avoid inheritance and use composition instead, so that we have a single implementation at runtime which helps both the C2 JIT compiler and Graal native-image.

Result:
Simpler code that's more JIT friendly, without loss of performance.

Sorry, something went wrong.

Uh oh!

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

franz1981, nkonev, and He-Pin reacted with heart emoji
All reactions
  • ❤️ 3 reactions
@chrisvest
Simplify reference counting
fafa429
Motivation:
Our current reference counting algorithm is quite complicated, trying to guard for word-tearing of 32-bit integers, which isn't possible in Java.

The complicated code compiles to larger amounts of native code, which in turn can prevent inlining as it comes up against code size heuristics in the JIT.

Modification:
Simplify the code by removing special case checks for certain constants.
Reorganize the code to use fewer branches

Result:
Simpler code that's more JIT friendly, without loss of performance.
@chrisvest
chrisvest requested a review from franz1981 October 16, 2025 18:16
@chrisvest

chrisvest commented Oct 16, 2025

Copy link
Copy Markdown
Member Author

@franz1981 Please try benchmarking these changes. On my M1, I get performance in AbstractReferenceCountedByteBufBenchmark that is on par with the current code.

If I change retain to use a CAS loop, its performance drops dramatically as soon as there's contention. I kept the even/odd reference counting scheme because of this, because it allows us to use the getAndAdd intrinsics, which scale much better.

franz1981 reacted with rocket emoji
All reactions
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

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

@chrisvest
chrisvest requested a review from normanmaurer October 16, 2025 18:20
@franz1981

franz1981 commented Oct 16, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

But to be fair, it matters?
I mean, I would optimize for normal uncontended use cases if I have to pick a poison. This will improve the unconteded cases because the comparisons doesn't need to strip the parity bit and become (if predictable) super cheap.
If we want to go really fast (under contention) a double sequence scheme is the way because the two sequences keep on increasing and can use increment and get separately

All reactions

Sorry, something went wrong.

Uh oh!

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

@franz1981

franz1981 commented Oct 16, 2025

Copy link
Copy Markdown
Contributor

You can try applying this on top of my adaptive pull and see how to perform since I didn't get rid of the chunk ref count for size classed because I was waiting this ❤️

All reactions

Sorry, something went wrong.

Uh oh!

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

@chrisvest

chrisvest commented Oct 16, 2025

Copy link
Copy Markdown
Member Author

It wasn't faster without the parity bit on my machine.

All reactions

Sorry, something went wrong.

Uh oh!

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

@franz1981

franz1981 commented Oct 16, 2025

Copy link
Copy Markdown
Contributor

I believe it - I will try on my x86 as well to check how it behaves. I have to check to if it solves the problem in the issue related set bytes too

All reactions

Sorry, something went wrong.

Uh oh!

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

@chrisvest

chrisvest commented Oct 16, 2025

Copy link
Copy Markdown
Member Author

Also, I suspect the contended case is relevant for the chunks.

All reactions

Sorry, something went wrong.

Uh oh!

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

@franz1981

franz1981 commented Oct 16, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Uh, you are right. It is indeed (for shared magazines).
Although I have another solution there to get rid of it

All reactions

Sorry, something went wrong.

Uh oh!

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

@chrisvest

chrisvest commented Oct 16, 2025

Copy link
Copy Markdown
Member Author

Huh, interesting. I'm actually getting pretty reliable JIT compiler crash on the ReferenceCountUpdater::release0 method!

franz1981 reacted with eyes emoji
All reactions
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

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

@chrisvest

chrisvest commented Oct 16, 2025

Copy link
Copy Markdown
Member Author

Looks like I can make the compiler crash go away by collapsing the implementations down as well, so we only have AbstractReferenceCounted and no longer need the impls in Chunk and AbstrsctReferenceCountedByteBuf. And a few other small tweaks.

All reactions

Sorry, something went wrong.

Uh oh!

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

@chrisvest
chrisvest force-pushed the 4.2-refcount branch from 4f58b3e to c6afb03 Compare October 16, 2025 21:33
@chrisvest

chrisvest commented Oct 16, 2025

Copy link
Copy Markdown
Member Author

To work-around the JIT bug, I've had to increase the scope of this PR to also collapse the three different ReferenceCounted implementations into one; AbstractReferenceCounted. This means that Chunk and AbstractReferenceCountedByteBuf no longer integrate with ReferenceCountUpdater directly, but instead all pull their implementations from AbstractReferenceCounted.

All reactions

Sorry, something went wrong.

Uh oh!

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

@chrisvest
Collapse the ReferenceCounted implementations to all rely on Abstract…
f410cc2
…ReferenceCounted

And also work-around what appears to be a C2 JIT compiler bug in Java 17 through 25+.
@chrisvest
chrisvest force-pushed the 4.2-refcount branch from c6afb03 to f410cc2 Compare October 16, 2025 22:55
franz1981
franz1981 reviewed Oct 17, 2025
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java Outdated
}
}

private static class Chunk extends AbstractReferenceCounted implements ChunkInfo {

@franz1981 franz1981 Oct 17, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Consider that w 8953bbe I plan to make Chunk's reference count to exist only for the BumpChunk (bad name I know :"()

Sorry, something went wrong.

Uh oh!

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

All reactions

@chrisvest chrisvest Oct 22, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yeah. And this is also breaking the graal build as-is… need to think about what to do here.

Sorry, something went wrong.

Uh oh!

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

All reactions

@franz1981 franz1981 Oct 23, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Check franz1981#1

Sorry, something went wrong.

Uh oh!

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

All reactions
@franz1981

franz1981 commented Oct 17, 2025

Copy link
Copy Markdown
Contributor

The idea of collapsing the hierarchy is very similar to what @yawkat has done on franz1981#1 and will likely avoid the problem of bimoprhic inlining + fat compiled methods observed in #15736 (comment).
But the only way to know it is try this change on #15736 as well.

All reactions

Sorry, something went wrong.

Uh oh!

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

franz1981
franz1981 reviewed Oct 17, 2025
View reviewed changes
Comment thread common/src/main/java/io/netty/util/internal/ReferenceCountUpdater.java Resolved

Uh oh!

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

Comment thread common/src/main/java/io/netty/util/internal/ReferenceCountUpdater.java Resolved

Uh oh!

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

@franz1981

franz1981 commented Oct 17, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

I have cherry picked both your changes on top of #15741 getting

Benchmark                                               (allocatorType)  (pollutionIterations)  Mode  Cnt   Score   Error  Units
ByteBufAllocatorAllocPatternBenchmark.directAllocation         ADAPTIVE                      0  avgt  100  29.725 ± 0.261  ns/op
ByteBufAllocatorAllocPatternBenchmark.directAllocation         ADAPTIVE                 200000  avgt  100  32.376 ± 0.453  ns/op

whilst my last commit was delivering

Benchmark                                               (allocatorType)  (pollutionIterations)  Mode  Cnt   Score   Error  Units
ByteBufAllocatorAllocPatternBenchmark.directAllocation         ADAPTIVE                      0  avgt  100  27.484 ± 0.251  ns/op
ByteBufAllocatorAllocPatternBenchmark.directAllocation         ADAPTIVE                 200000  avgt  100  32.874 ± 0.585  ns/op

which shows a slightly regressed performance in the non-"polluted" case

With the change at #15764 (comment) the performance is (nearly) restored:

Benchmark                                               (allocatorType)  (pollutionIterations)  Mode  Cnt   Score   Error  Units
ByteBufAllocatorAllocPatternBenchmark.directAllocation         ADAPTIVE                      0  avgt  100  28.091 ± 0.431  ns/op
ByteBufAllocatorAllocPatternBenchmark.directAllocation         ADAPTIVE                 200000  avgt  100  32.044 ± 0.457  ns/op

Adding #15764 (comment) too (to the previous one) completely restore the performance:

Benchmark                                               (allocatorType)  (pollutionIterations)  Mode  Cnt   Score   Error  Units
ByteBufAllocatorAllocPatternBenchmark.directAllocation         ADAPTIVE                      0  avgt  100  27.636 ± 0.142  ns/op
ByteBufAllocatorAllocPatternBenchmark.directAllocation         ADAPTIVE                 200000  avgt  100  31.993 ± 0.531  ns/op

I have the suspect we can get better on retain0 as well since we can assume to optimize for the not taken branch there too (i.e. no thrown exception) - I will post a suggestion for that one too.
And that should help the chunk and (retained slices) use cases (e.g. for HTTP 2)

All reactions

Sorry, something went wrong.

Uh oh!

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

franz1981
franz1981 requested changes Oct 17, 2025
View reviewed changes
Comment thread common/src/main/java/io/netty/util/internal/ReferenceCountUpdater.java Resolved

Uh oh!

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

franz1981
franz1981 reviewed Oct 17, 2025
View reviewed changes
Comment thread common/src/main/java/io/netty/util/internal/ReferenceCountUpdater.java Resolved

Uh oh!

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

franz1981
franz1981 requested changes Oct 17, 2025
View reviewed changes
Comment thread common/src/main/java/io/netty/util/internal/ReferenceCountUpdater.java Resolved

Uh oh!

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

@chrisvest
Apply some micro-opts that help x86 a bit
45633cd
@chrisvest

chrisvest commented Oct 17, 2025

Copy link
Copy Markdown
Member Author

@franz1981 I applied the optimizations you suggested. They don't seem to make any noticeable difference on M1, but if they help x86 we'll of course take that win.

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

Sorry, something went wrong.

Uh oh!

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

chrisvest added 2 commits October 17, 2025 16:31
@chrisvest
Merge branch '4.2' into 4.2-refcount
7952927
@chrisvest
Merge branch '4.2' into 4.2-refcount
d72d545
franz1981
franz1981 reviewed Oct 21, 2025
View reviewed changes
Comment thread common/src/main/java/io/netty/util/internal/ReferenceCountUpdater.java Resolved

Uh oh!

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

@chrisvest
Fix the native image build
b077572
@chrisvest
chrisvest force-pushed the 4.2-refcount branch from 78ccf85 to b077572 Compare October 23, 2025 19:05
chrisvest added 2 commits October 30, 2025 15:49
@chrisvest
Fix revapi warning
9b726f9
@chrisvest
Deprecate ReferenceCountUpdater
4c09131
@chrisvest
chrisvest force-pushed the 4.2-refcount branch from e5c3b63 to 4c09131 Compare October 30, 2025 22:49
@chrisvest
chrisvest requested a review from franz1981 October 30, 2025 22:51
@chrisvest

chrisvest commented Oct 30, 2025

Copy link
Copy Markdown
Member Author

@franz1981 @normanmaurer Please take a look.

All reactions

Sorry, something went wrong.

Uh oh!

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

franz1981
franz1981 requested changes Oct 31, 2025
View reviewed changes

@franz1981 franz1981 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

In term of performance it will have some impact for the most common case (as explained in the description I already gave), but in a follow up PR it enables us to likely re-enable VarHandle for native image as well.

I will run few benchmarks, likely early next week; in case, we still have the option B which would remove any chance for regression - with @normanmaurer blessing

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread common/src/main/java/io/netty/util/internal/RefCnt.java Resolved

Uh oh!

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

Comment thread common/src/main/java/io/netty/util/internal/RefCnt.java Resolved

Uh oh!

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

Comment thread common/src/main/java/io/netty/util/internal/RefCnt.java Resolved

Uh oh!

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

Comment thread common/src/main/java/io/netty/util/internal/RefCnt.java Resolved

Uh oh!

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

Comment thread common/src/main/java/io/netty/util/internal/RefCnt.java Resolved

Uh oh!

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

Comment thread common/src/main/java/io/netty/util/internal/RefCnt.java Resolved

Uh oh!

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

@chrisvest
Address review comments
e0ba069
franz1981
franz1981 reviewed Nov 2, 2025
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AbstractReferenceCountedByteBuf.java Resolved

Uh oh!

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

franz1981
franz1981 requested changes Nov 2, 2025
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AbstractReferenceCountedByteBuf.java Resolved

Uh oh!

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

Comment thread buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java Resolved

Uh oh!

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

Comment thread buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java Resolved

Uh oh!

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

@normanmaurer

normanmaurer commented Nov 3, 2025

Copy link
Copy Markdown
Member

@chrisvest @franz1981 let me know once I should review

All reactions

Sorry, something went wrong.

Uh oh!

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

@franz1981

franz1981 commented Nov 3, 2025

Copy link
Copy Markdown
Contributor

I think @normanmaurer this is ready to go, apart from the small comments I sent. I still believe this is bringing unwanted performance effects which the option 2. B would save, as I have explained in another comment for you 🙏

All reactions

Sorry, something went wrong.

Uh oh!

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

normanmaurer
normanmaurer approved these changes Nov 3, 2025
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/Unpooled.java Resolved

Uh oh!

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

Comment thread common/src/main/java/io/netty/util/internal/RefCnt.java Resolved

Uh oh!

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

chrisvest added 2 commits November 3, 2025 15:00
@chrisvest
Merge branch '4.2' into 4.2-refcount
be180ee
@chrisvest
Address review comments
9f0571f
@chrisvest

chrisvest commented Nov 4, 2025

Copy link
Copy Markdown
Member Author

@normanmaurer @franz1981 Last few comments addressed.

All reactions

Sorry, something went wrong.

Uh oh!

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

normanmaurer
normanmaurer reviewed Nov 4, 2025
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/EmptyByteBuf.java

static final int EMPTY_BYTE_BUF_HASH_CODE = 1;
private static final ByteBuffer EMPTY_BYTE_BUFFER = PlatformDependent.allocateDirect(0).buffer();
private static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.allocateDirect(0);

@normanmaurer normanmaurer Nov 4, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

seems unrelated...

Sorry, something went wrong.

Uh oh!

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

All reactions

@chrisvest chrisvest Nov 4, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

On Graal 25, PlatformDependent.allocateDirect will use our MemorySegment based cleaner implementation. Turns out Graal will refuse to put native memory segments on the image heap because they contain native pointers which when initialized at build time may not be valid at runtime, even though in this case the segment is zero bytes in length and it would almost certainly be fine.

Since the EMPTY_BYTE_BUFFER instance is never deallocated and doesn't contribute to native memory usage (or only contributes one pointer size of bytes), I thought making this change would be a fair trade off.

Sorry, something went wrong.

Uh oh!

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

All reactions
normanmaurer
normanmaurer requested changes Nov 4, 2025
View reviewed changes
Comment thread common/src/main/java/io/netty/util/internal/RefCnt.java Outdated
* compared to when {@link ReferenceCountUpdater} is used.
*/
@SuppressWarnings("deprecation")
public class RefCnt {

@normanmaurer normanmaurer Nov 4, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

hmmm after thinking a bit more about it wouldn't that basically break again on older android versions:

#15654

Sorry, something went wrong.

Uh oh!

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

All reactions

@chrisvest chrisvest Nov 4, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Looking at how #15661 solved it, we may be in the clear with our switch guards.
/cc @yawkat

Sorry, something went wrong.

Uh oh!

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

All reactions

@normanmaurer normanmaurer Nov 6, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@chrisvest I think the problem exists here as well as we import the VarHandle.

Sorry, something went wrong.

Uh oh!

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

All reactions

@yawkat yawkat Nov 6, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I think it's fine because all VarHandle uses happen in VarHandleRefCnt which is a separate class that is never loaded when VarHandle is not supported

Sorry, something went wrong.

Uh oh!

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

All reactions

@normanmaurer normanmaurer Nov 6, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

ok if that works I am happy :)

Sorry, something went wrong.

Uh oh!

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

chrisvest reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread common/src/main/java/io/netty/util/internal/RefCnt.java Resolved

Uh oh!

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

franz1981
franz1981 requested changes Nov 6, 2025
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java Resolved

Uh oh!

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

@chrisvest
Make RefCnt final
3f42263
normanmaurer
normanmaurer approved these changes Nov 6, 2025
View reviewed changes
@chrisvest
chrisvest merged commit a8e370d into netty:4.2 Nov 6, 2025
@chrisvest
chrisvest deleted the 4.2-refcount branch November 6, 2025 20:39
This was referenced Nov 7, 2025
native-image should benefit of VarHandle #15827
Open
Make AdaptiveByteBuf.setBytes faster #15736
Merged
Improve adaptive allocator thread local performance #15741
Merged
chrisvest added a commit to chrisvest/netty that referenced this pull request Nov 7, 2025
@chrisvest
Revert "Simplify reference counting (netty#15764)"
c85740a
This reverts commit a8e370d.
@chrisvest chrisvest mentioned this pull request Nov 7, 2025
Revert "Simplify reference counting (#15764)" #15831
Closed
@chrisvest chrisvest mentioned this pull request Nov 18, 2025
(rawCnt == 2 || rawCnt == 4 || (rawCnt & 1) == 0 equivalent to rawCnt&1 ==0 #15269
Closed
@dreamlike-ocean dreamlike-ocean mentioned this pull request Mar 4, 2026
Backport request: workaround for JDK-8374463 (Netty PR #15764) #16414
Closed
@Songdoeon Songdoeon mentioned this pull request Mar 30, 2026
Enable VarHandle in native-image environments #16573
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@yawkat yawkat yawkat left review comments
@franz1981 franz1981 franz1981 requested changes
@normanmaurer normanmaurer normanmaurer approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

5 participants

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