Skip to content
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.

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

Ensure WeakOrderQueue can be collected fast enough #5569

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 29 additions & 24 deletions common/src/main/java/io/netty/util/Recycler.java
Expand Up @@ -223,7 +223,7 @@ private static final class Link extends AtomicInteger {
private WeakOrderQueue next;
private final WeakReference<Thread> owner;
private final int id = ID_GENERATOR.getAndIncrement();
private final Stack<?> stack;
private final AtomicInteger availableSharedCapacity;

WeakOrderQueue(Stack<?> stack, Thread thread) {
head = tail = new Link();
Expand All @@ -232,19 +232,42 @@ private static final class Link extends AtomicInteger {
next = stack.head;
stack.head = this;
}
this.stack = stack;

// Its important that we not store the Stack itself in the WeakOrderQueue as the Stack also is used in
// the WeakHashMap as key. So just store the enclosed AtomicInteger which should allow to have the
// Stack itself GCed.
availableSharedCapacity = stack.availableSharedCapacity;
Copy link
Member

Choose a reason for hiding this comment

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

the stack doesn't actually use this variable ... should we move it outside the stack and lazy create it here in the WeakOrderQueue?

Copy link
Member Author

Choose a reason for hiding this comment

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

@Scottmitch nope as it needs to be shared across all WeakOrderQueueinstances that belong to a Stack.

Copy link
Member

Choose a reason for hiding this comment

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

+1 makes sense


// We allocated a Link so reserve the space
boolean reserved = stack.reserveSpace(LINK_CAPACITY);
boolean reserved = reserveSpace(LINK_CAPACITY);
assert reserved;
}

private boolean reserveSpace(int space) {
assert space >= 0;
for (;;) {
int available = availableSharedCapacity.get();
if (available < space) {
return false;
}
if (availableSharedCapacity.compareAndSet(available, available - space)) {
return true;
}
}
}

private void reclaimSpace(int space) {
assert space >= 0;
availableSharedCapacity.addAndGet(space);
}

void add(DefaultHandle<?> handle) {
handle.lastRecycledId = id;

Link tail = this.tail;
int writeIndex;
if ((writeIndex = tail.get()) == LINK_CAPACITY) {
if (!stack.reserveSpace(LINK_CAPACITY)) {
if (!reserveSpace(LINK_CAPACITY)) {
// Drop it.
return;
}
Expand Down Expand Up @@ -314,7 +337,7 @@ boolean transfer(Stack<?> dst) {

if (srcEnd == LINK_CAPACITY && head.next != null) {
// Add capacity back as the Link is GCed.
stack.reclaimSpace(LINK_CAPACITY);
reclaimSpace(LINK_CAPACITY);

this.head = head.next;
}
Expand All @@ -339,7 +362,7 @@ static final class Stack<T> {
private DefaultHandle<?>[] elements;
private final int maxCapacity;
private int size;
private final AtomicInteger availableSharedCapacity;
final AtomicInteger availableSharedCapacity;

private volatile WeakOrderQueue head;
private WeakOrderQueue cursor, prev;
Expand All @@ -352,24 +375,6 @@ static final class Stack<T> {
elements = new DefaultHandle[min(INITIAL_CAPACITY, maxCapacity)];
}

boolean reserveSpace(int space) {
assert space >= 0;
for (;;) {
int available = availableSharedCapacity.get();
if (available < space) {
return false;
}
if (availableSharedCapacity.compareAndSet(available, available - space)) {
return true;
}
}
}

void reclaimSpace(int space) {
assert space >= 0;
availableSharedCapacity.addAndGet(space);
}

int increaseCapacity(int expectedCapacity) {
int newCapacity = elements.length;
int maxCapacity = this.maxCapacity;
Expand Down