Skip to content

Commit 7615d93

Browse files
committed
Reduce IdleChannel allocations, close AsyncHttpClient#1462
Motivation: We allocate an AtomicBoolean for every IdleChannel. We can avoid this cost, all the more as we allocate a IdleChannel just to be able to remove it from the partition. Modification: Use an AtomicIntegerFieldUpdater instead. Result: Less allocations caused by IdleChannel.
1 parent 45a8b4b commit 7615d93

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

client/src/main/java/org/asynchttpclient/netty/channel/DefaultChannelPool.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,26 @@
1515

1616
import static org.asynchttpclient.util.Assertions.assertNotNull;
1717
import static org.asynchttpclient.util.DateUtils.unpreciseMillisTime;
18-
import io.netty.channel.Channel;
19-
import io.netty.channel.ChannelId;
20-
import io.netty.util.Timeout;
21-
import io.netty.util.Timer;
22-
import io.netty.util.TimerTask;
2318

2419
import java.util.*;
2520
import java.util.concurrent.ConcurrentHashMap;
2621
import java.util.concurrent.ConcurrentLinkedDeque;
2722
import java.util.concurrent.TimeUnit;
2823
import java.util.concurrent.atomic.AtomicBoolean;
24+
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
2925

3026
import org.asynchttpclient.AsyncHttpClientConfig;
3127
import org.asynchttpclient.channel.ChannelPool;
3228
import org.asynchttpclient.channel.ChannelPoolPartitionSelector;
3329
import org.slf4j.Logger;
3430
import org.slf4j.LoggerFactory;
3531

32+
import io.netty.channel.Channel;
33+
import io.netty.channel.ChannelId;
34+
import io.netty.util.Timeout;
35+
import io.netty.util.Timer;
36+
import io.netty.util.TimerTask;
37+
3638
/**
3739
* A simple implementation of {@link ChannelPool} based on a {@link java.util.concurrent.ConcurrentHashMap}
3840
*/
@@ -107,17 +109,21 @@ private static final class ChannelCreation {
107109
}
108110

109111
private static final class IdleChannel {
112+
113+
private static final AtomicIntegerFieldUpdater<IdleChannel> ownedField = AtomicIntegerFieldUpdater.newUpdater(IdleChannel.class, "owned");
114+
110115
final Channel channel;
111116
final long start;
112-
final AtomicBoolean owned = new AtomicBoolean(false);
117+
@SuppressWarnings("unused")
118+
private volatile int owned = 0;
113119

114120
IdleChannel(Channel channel, long start) {
115121
this.channel = assertNotNull(channel, "channel");
116122
this.start = start;
117123
}
118124

119125
public boolean takeOwnership() {
120-
return owned.compareAndSet(false, true);
126+
return ownedField.getAndSet(this, 1) == 0;
121127
}
122128

123129
@Override

0 commit comments

Comments
 (0)