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

Simplify reference counting #15764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
chrisvest merged 20 commits into netty:4.2 from chrisvest:4.2-refcount
Nov 6, 2025

Uh oh!

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

Merged

Simplify reference counting #15764

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
fafa429
Simplify reference counting
chrisvest Oct 15, 2025
f410cc2
Collapse the ReferenceCounted implementations to all rely on Abstract…
chrisvest Oct 16, 2025
45633cd
Apply some micro-opts that help x86 a bit
chrisvest Oct 17, 2025
7952927
Merge branch '4.2' into 4.2-refcount
chrisvest Oct 17, 2025
d72d545
Merge branch '4.2' into 4.2-refcount
chrisvest Oct 20, 2025
b077572
Fix the native image build
chrisvest Oct 23, 2025
7ef7d25
Reduce the codegen for retain0
chrisvest Oct 24, 2025
a49f4d3
Fix graal 21 build
chrisvest Oct 24, 2025
4e306ad
Merge branch '4.2' into 4.2-refcount
chrisvest Oct 24, 2025
ba3f5f5
Revert "Fix graal 21 build"
chrisvest Oct 30, 2025
51c6c69
Revert "Fix the native image build"
chrisvest Oct 30, 2025
a4db143
Revert "Collapse the ReferenceCounted implementations to all rely on …
chrisvest Oct 30, 2025
3b4f83e
new RefCnt class
franz1981 Oct 28, 2025
06dcfef
Properly integrate the RefCnt into the adaptive allocator and fix nat…
chrisvest Oct 30, 2025
9b726f9
Fix revapi warning
chrisvest Oct 30, 2025
4c09131
Deprecate ReferenceCountUpdater
chrisvest Oct 30, 2025
e0ba069
Address review comments
chrisvest Oct 31, 2025
be180ee
Merge branch '4.2' into 4.2-refcount
chrisvest Nov 3, 2025
9f0571f
Address review comments
chrisvest Nov 4, 2025
3f42263
Make RefCnt final
chrisvest Nov 6, 2025
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

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

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

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

Diff view
Diff view
Prev Previous commit
Next Next commit
new RefCnt class
  • Loading branch information

Uh oh!

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

@franz1981 @chrisvest
franz1981 authored and chrisvest committed Oct 30, 2025
commit 3b4f83e89a827e50e7549f2e8382bdc758495a27

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

87 changes: 15 additions & 72 deletions 87 buffer/src/main/java/io/netty/buffer/AbstractReferenceCountedByteBuf.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,114 +16,57 @@

package io.netty.buffer;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

import io.netty.util.internal.AtomicReferenceCountUpdater;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.ReferenceCountUpdater;
import io.netty.util.internal.ReferenceCountUpdater.UpdaterType;
import io.netty.util.internal.UnsafeReferenceCountUpdater;
import io.netty.util.internal.VarHandleReferenceCountUpdater;

import static io.netty.util.internal.ReferenceCountUpdater.getUnsafeOffset;
import static java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater;
import io.netty.util.internal.RefCnt;

/**
* Abstract base class for {@link ByteBuf} implementations that count references.
*/
public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
private static final long REFCNT_FIELD_OFFSET;
private static final AtomicIntegerFieldUpdater<AbstractReferenceCountedByteBuf> AIF_UPDATER;
private static final Object REFCNT_FIELD_VH;
private static final ReferenceCountUpdater<AbstractReferenceCountedByteBuf> updater;

static {
UpdaterType updaterType = ReferenceCountUpdater.updaterTypeOf(AbstractReferenceCountedByteBuf.class, "refCnt");
switch (updaterType) {
case Atomic:
AIF_UPDATER = newUpdater(AbstractReferenceCountedByteBuf.class, "refCnt");
REFCNT_FIELD_OFFSET = -1;
REFCNT_FIELD_VH = null;
updater = new AtomicReferenceCountUpdater<AbstractReferenceCountedByteBuf>() {
@Override
protected AtomicIntegerFieldUpdater<AbstractReferenceCountedByteBuf> updater() {
return AIF_UPDATER;
}
};
break;
case Unsafe:
AIF_UPDATER = null;
REFCNT_FIELD_OFFSET = getUnsafeOffset(AbstractReferenceCountedByteBuf.class, "refCnt");
REFCNT_FIELD_VH = null;
updater = new UnsafeReferenceCountUpdater<AbstractReferenceCountedByteBuf>() {
@Override
protected long refCntFieldOffset() {
return REFCNT_FIELD_OFFSET;
}
};
break;
case VarHandle:
AIF_UPDATER = null;
REFCNT_FIELD_OFFSET = -1;
REFCNT_FIELD_VH = PlatformDependent.findVarHandleOfIntField(MethodHandles.lookup(),
AbstractReferenceCountedByteBuf.class, "refCnt");
updater = new VarHandleReferenceCountUpdater<AbstractReferenceCountedByteBuf>() {
@Override
protected VarHandle varHandle() {
return (VarHandle) REFCNT_FIELD_VH;
}
};
break;
default:
throw new Error("Unexpected updater type for AbstractReferenceCountedByteBuf: " + updaterType);
}
}

// Value might not equal "real" reference count, all access should be via the updater
@SuppressWarnings({"unused", "FieldMayBeFinal"})
private volatile int refCnt;
private final RefCnt refCnt;
Comment thread
franz1981 marked this conversation as resolved.
Resolved

Uh oh!

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


protected AbstractReferenceCountedByteBuf(int maxCapacity) {
super(maxCapacity);
updater.setInitialValue(this);
// this is setting the ref cnt to the initial value
refCnt = new RefCnt();
Comment thread
franz1981 marked this conversation as resolved.
Resolved

Uh oh!

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

}

@Override
boolean isAccessible() {
// Try to do non-volatile read for performance as the ensureAccessible() is racy anyway and only provide
// a best-effort guard.
return updater.isLiveNonVolatile(this);
return RefCnt.isLiveNonVolatile(refCnt);
}

@Override
public int refCnt() {
return updater.refCnt(this);
return RefCnt.refCnt(refCnt);
}

/**
* An unsafe operation intended for use by a subclass that sets the reference count of the buffer directly
*/
protected final void setRefCnt(int refCnt) {
updater.setRefCnt(this, refCnt);
protected final void setRefCnt(int count) {
RefCnt.setRefCnt(refCnt, count);
}

/**
* An unsafe operation intended for use by a subclass that resets the reference count of the buffer to 1
*/
protected final void resetRefCnt() {
updater.resetRefCnt(this);
RefCnt.resetRefCnt(refCnt);
}

@Override
public ByteBuf retain() {
return updater.retain(this);
RefCnt.retain(refCnt);
return this;
}

@Override
public ByteBuf retain(int increment) {
return updater.retain(this, increment);
RefCnt.retain(refCnt, increment);
return this;
}

@Override
Expand All @@ -138,12 +81,12 @@ public ByteBuf touch(Object hint) {

@Override
public boolean release() {
return handleRelease(updater.release(this));
return handleRelease(RefCnt.release(refCnt));
}

@Override
public boolean release(int decrement) {
return handleRelease(updater.release(this, decrement));
return handleRelease(RefCnt.release(refCnt, decrement));
}

private boolean handleRelease(boolean result) {
Expand Down
113 changes: 8 additions & 105 deletions 113 buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.netty.buffer;

import io.netty.util.AbstractReferenceCounted;
import io.netty.util.ByteProcessor;
import io.netty.util.CharsetUtil;
import io.netty.util.IllegalReferenceCountException;
Expand All @@ -25,22 +26,17 @@
import io.netty.util.concurrent.FastThreadLocal;
import io.netty.util.concurrent.FastThreadLocalThread;
import io.netty.util.concurrent.MpscIntQueue;
import io.netty.util.internal.AtomicReferenceCountUpdater;
import io.netty.util.internal.ObjectPool;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.ReferenceCountUpdater;
import io.netty.util.internal.RefCnt;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.ThreadExecutorMap;
import io.netty.util.internal.UnsafeReferenceCountUpdater;
import io.netty.util.internal.UnstableApi;
import io.netty.util.internal.VarHandleReferenceCountUpdater;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.ClosedChannelException;
Expand All @@ -52,15 +48,11 @@
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.atomic.LongAdder;
import java.util.concurrent.locks.StampedLock;
import java.util.function.IntSupplier;

import static io.netty.util.internal.ReferenceCountUpdater.getUnsafeOffset;
import static java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater;

/**
* An auto-tuning pooling allocator, that follows an anti-generational hypothesis.
* <p>
Expand Down Expand Up @@ -1121,54 +1113,7 @@ public void remove(Chunk chunk) {
}
}

private static class Chunk implements ReferenceCounted, ChunkInfo {
private static final long REFCNT_FIELD_OFFSET;
private static final AtomicIntegerFieldUpdater<Chunk> AIF_UPDATER;
private static final Object REFCNT_FIELD_VH;
private static final ReferenceCountUpdater<Chunk> updater;

static {
ReferenceCountUpdater.UpdaterType updaterType = ReferenceCountUpdater.updaterTypeOf(Chunk.class, "refCnt");
switch (updaterType) {
case Atomic:
AIF_UPDATER = newUpdater(Chunk.class, "refCnt");
REFCNT_FIELD_OFFSET = -1;
REFCNT_FIELD_VH = null;
updater = new AtomicReferenceCountUpdater<Chunk>() {
@Override
protected AtomicIntegerFieldUpdater<Chunk> updater() {
return AIF_UPDATER;
}
};
break;
case Unsafe:
AIF_UPDATER = null;
REFCNT_FIELD_OFFSET = getUnsafeOffset(Chunk.class, "refCnt");
REFCNT_FIELD_VH = null;
updater = new UnsafeReferenceCountUpdater<Chunk>() {
@Override
protected long refCntFieldOffset() {
return REFCNT_FIELD_OFFSET;
}
};
break;
case VarHandle:
AIF_UPDATER = null;
REFCNT_FIELD_OFFSET = -1;
REFCNT_FIELD_VH = PlatformDependent.findVarHandleOfIntField(MethodHandles.lookup(),
Chunk.class, "refCnt");
updater = new VarHandleReferenceCountUpdater<Chunk>() {
@Override
protected VarHandle varHandle() {
return (VarHandle) REFCNT_FIELD_VH;
}
};
break;
default:
throw new Error("Unexpected updater type for Chunk: " + updaterType);
}
}

private static class Chunk extends AbstractReferenceCounted implements ChunkInfo {
Comment thread Outdated

@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
protected final AbstractByteBuf delegate;
protected Magazine magazine;
private final AdaptivePoolingAllocator allocator;
Expand All @@ -1177,10 +1122,6 @@ protected VarHandle varHandle() {
private final boolean pooled;
protected int allocatedBytes;

// Value might not equal "real" reference count, all access should be via the updater
@SuppressWarnings({"unused", "FieldMayBeFinal"})
private volatile int refCnt;

Chunk() {
// Constructor only used by the MAGAZINE_FREED sentinel.
delegate = null;
Expand All @@ -1196,7 +1137,6 @@ protected VarHandle varHandle() {
this.delegate = delegate;
this.pooled = pooled;
capacity = delegate.capacity();
updater.setInitialValue(this);
attachToMagazine(magazine);

// We need the top-level allocator so ByteBuf.capacity(int) can call reallocate()
Expand Down Expand Up @@ -1231,48 +1171,10 @@ void attachToMagazine(Magazine magazine) {
}

@Override
public Chunk touch(Object hint) {
public ReferenceCounted touch(Object hint) {
return this;
}

@Override
public int refCnt() {
return updater.refCnt(this);
}

@Override
public Chunk retain() {
return updater.retain(this);
}

@Override
public Chunk retain(int increment) {
return updater.retain(this, increment);
}

@Override
public Chunk touch() {
return this;
}

@Override
public boolean release() {
if (updater.release(this)) {
deallocate();
return true;
}
return false;
}

@Override
public boolean release(int decrement) {
if (updater.release(this, decrement)) {
deallocate();
return true;
}
return false;
}

/**
* Called when a magazine is done using this chunk, probably because it was emptied.
*/
Expand All @@ -1287,7 +1189,8 @@ boolean releaseSegment(int ignoredSegmentId) {
return release();
}

private void deallocate() {
@Override
protected void deallocate() {
Magazine mag = magazine;
int chunkSize = delegate.capacity();
if (!pooled || chunkReleasePredicate.shouldReleaseChunk(chunkSize) || mag == null) {
Expand All @@ -1298,7 +1201,7 @@ private void deallocate() {
allocator.chunkRegistry.remove(this);
delegate.release();
} else {
updater.resetRefCnt(this);
RefCnt.resetRefCnt(this);
delegate.setIndex(0, 0);
allocatedBytes = 0;
if (!mag.trySetNextInLine(this)) {
Expand All @@ -1307,7 +1210,7 @@ private void deallocate() {
if (!mag.offerToQueue(this)) {
// The central queue is full. Ensure we release again as we previously did use resetRefCnt()
// which did increase the reference count by 1.
boolean released = updater.release(this);
boolean released = RefCnt.release(this);
onRelease();
allocator.chunkRegistry.remove(this);
delegate.release();
Expand Down
Loading
Oops, something went wrong.

Uh oh!

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

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 01402894 — 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.