Skip to content

Commit

Permalink
Faster Recycler's claim/release (Fixes netty#13153)
Browse files Browse the repository at this point in the history
Motivation:

Recycler's claim/release can be made faster by saving expensive volatile ops, when not needed.
For claim, always, while for release, if the owner thread is performing release itself.

Modification:

Replacing expensive volatile ops with ordered ones.

Result:

Faster Recycler's claim/release
  • Loading branch information
franz1981 committed Feb 22, 2023
1 parent 59aa6e6 commit 5815720
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions common/src/main/java/io/netty/util/Recycler.java
Expand Up @@ -252,7 +252,15 @@ void set(T value) {

void toClaimed() {
assert state == STATE_AVAILABLE;
state = STATE_CLAIMED;
STATE_UPDATER.lazySet(this, STATE_CLAIMED);
}

void toOrderedAvailable() {
int prev = state;
if (prev == STATE_AVAILABLE) {
throw new IllegalStateException("Object has been recycled already.");
}
STATE_UPDATER.lazySet(this, STATE_AVAILABLE);
}

void toAvailable() {
Expand Down Expand Up @@ -302,9 +310,14 @@ DefaultHandle<T> claim() {
}

void release(DefaultHandle<T> handle) {
handle.toAvailable();
Thread owner = this.owner;
if (owner != null && Thread.currentThread() == owner && batch.size() < chunkSize) {
final Thread owner = this.owner;
final boolean releaseByOwner = owner != null && Thread.currentThread() == owner;
if (releaseByOwner) {
handle.toOrderedAvailable();
} else {
handle.toAvailable();
}
if (releaseByOwner && batch.size() < chunkSize) {
accept(handle);
} else if (owner != null && owner.getState() == Thread.State.TERMINATED) {
this.owner = null;
Expand Down

0 comments on commit 5815720

Please sign in to comment.