Skip to content

Commit

Permalink
Reduce IdleChannel allocations, close #1462
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
slandelle committed Sep 25, 2017
1 parent 468b98f commit 0923f0e
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@

import static org.asynchttpclient.util.Assertions.assertNotNull;
import static org.asynchttpclient.util.DateUtils.unpreciseMillisTime;
import io.netty.channel.Channel;
import io.netty.channel.ChannelId;
import io.netty.util.Timeout;
import io.netty.util.Timer;
import io.netty.util.TimerTask;

import java.net.InetSocketAddress;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand All @@ -36,6 +32,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.channel.Channel;
import io.netty.channel.ChannelId;
import io.netty.util.Timeout;
import io.netty.util.Timer;
import io.netty.util.TimerTask;

/**
* A simple implementation of {@link ChannelPool} based on a {@link java.util.concurrent.ConcurrentHashMap}
*/
Expand Down Expand Up @@ -106,17 +108,21 @@ private static final class ChannelCreation {
}

private static final class IdleChannel {

private static final AtomicIntegerFieldUpdater<IdleChannel> ownedField = AtomicIntegerFieldUpdater.newUpdater(IdleChannel.class, "owned");

final Channel channel;
final long start;
final AtomicBoolean owned = new AtomicBoolean(false);
@SuppressWarnings("unused")
private volatile int owned = 0;

IdleChannel(Channel channel, long start) {
this.channel = assertNotNull(channel, "channel");
this.start = start;
}

public boolean takeOwnership() {
return owned.compareAndSet(false, true);
return ownedField.getAndSet(this, 1) == 0;
}

public Channel getChannel() {
Expand Down

0 comments on commit 0923f0e

Please sign in to comment.