Skip to content

Commit

Permalink
[JENKINS-36871] Using a single final class allows for inlining of hot…
Browse files Browse the repository at this point in the history
… method
  • Loading branch information
stephenc committed Aug 5, 2016
1 parent 75c4cf7 commit 6da1cb4
Showing 1 changed file with 28 additions and 76 deletions.
104 changes: 28 additions & 76 deletions src/main/java/org/jenkinsci/remoting/protocol/IOHub.java
Expand Up @@ -107,7 +107,7 @@ public class IOHub implements Executor, Closeable, Runnable, ByteBufferPool {
* thread).We could process these using a {@link Runnable} on {@link #selectorTasks} but we want to optimize
* detecting when to call {@link Selector#selectNow()}.
*/
private final Queue<AbstractInterestOps> interestOps = new ConcurrentLinkedQueue<AbstractInterestOps>();
private final Queue<InterestOps> interestOps = new ConcurrentLinkedQueue<InterestOps>();
/**
* Counts the # of select loops. Ocassionally useful for diagnosing whether the selector
* thread is spending too much CPU time.
Expand Down Expand Up @@ -252,7 +252,7 @@ public void close() throws IOException {
* @param key the key.
*/
public final void addInterestAccept(SelectionKey key) {
interestOps.add(new AddInterestOps(key, SelectionKey.OP_ACCEPT));
interestOps.add(new InterestOps(key, SelectionKey.OP_ACCEPT, 0));
selector.wakeup();
}

Expand All @@ -262,7 +262,7 @@ public final void addInterestAccept(SelectionKey key) {
* @param key the key.
*/
public final void removeInterestAccept(SelectionKey key) {
interestOps.add(new RemoveInterestOps(key, SelectionKey.OP_ACCEPT));
interestOps.add(new InterestOps(key, 0, SelectionKey.OP_ACCEPT));
selector.wakeup();
}

Expand All @@ -272,7 +272,7 @@ public final void removeInterestAccept(SelectionKey key) {
* @param key the key.
*/
public final void addInterestConnect(SelectionKey key) {
interestOps.add(new AddInterestOps(key, SelectionKey.OP_CONNECT));
interestOps.add(new InterestOps(key, SelectionKey.OP_CONNECT, 0));
selector.wakeup();
}

Expand All @@ -282,7 +282,7 @@ public final void addInterestConnect(SelectionKey key) {
* @param key the key.
*/
public final void removeInterestConnect(SelectionKey key) {
interestOps.add(new RemoveInterestOps(key, SelectionKey.OP_CONNECT));
interestOps.add(new InterestOps(key, 0, SelectionKey.OP_CONNECT));
selector.wakeup();
}

Expand All @@ -292,7 +292,7 @@ public final void removeInterestConnect(SelectionKey key) {
* @param key the key.
*/
public final void addInterestRead(SelectionKey key) {
interestOps.add(new AddInterestOps(key, SelectionKey.OP_READ));
interestOps.add(new InterestOps(key, SelectionKey.OP_READ, 0));
selector.wakeup();
}

Expand All @@ -302,7 +302,7 @@ public final void addInterestRead(SelectionKey key) {
* @param key the key.
*/
public final void removeInterestRead(SelectionKey key) {
interestOps.add(new RemoveInterestOps(key, SelectionKey.OP_READ));
interestOps.add(new InterestOps(key, 0, SelectionKey.OP_READ));
selector.wakeup();
}

Expand All @@ -312,7 +312,7 @@ public final void removeInterestRead(SelectionKey key) {
* @param key the key.
*/
public final void addInterestWrite(SelectionKey key) {
interestOps.add(new AddInterestOps(key, SelectionKey.OP_WRITE));
interestOps.add(new InterestOps(key, SelectionKey.OP_WRITE, 0));
selector.wakeup();
}

Expand All @@ -322,7 +322,7 @@ public final void addInterestWrite(SelectionKey key) {
* @param key the key.
*/
public final void removeInterestWrite(SelectionKey key) {
interestOps.add(new RemoveInterestOps(key, SelectionKey.OP_WRITE));
interestOps.add(new InterestOps(key, 0, SelectionKey.OP_WRITE));
selector.wakeup();
}

Expand Down Expand Up @@ -528,7 +528,7 @@ private boolean processRegistrations() {
*/
private boolean processInterestOps() {
boolean processedSomething = false;
for (AbstractInterestOps ops = interestOps.poll(); ops != null; ops = interestOps.poll()) {
for (InterestOps ops = interestOps.poll(); ops != null; ops = interestOps.poll()) {
try {
if (ops.interestOps()) {
processedSomething = true;
Expand Down Expand Up @@ -703,90 +703,42 @@ public void run() {
/**
* Base class for {@link SelectionKey#interestOps()} modification requests.
*/
private static abstract class AbstractInterestOps {
private static final class InterestOps {
/**
* The {@link SelectionKey}.
*/
protected final SelectionKey key;
private final SelectionKey key;

/**
* Constructor.
*
* @param key the selection key.
* The mask to AND against the ops with.
*/
protected AbstractInterestOps(SelectionKey key) {
this.key = key;
}

private final int opsAnd;
/**
* Returns the desired {@link SelectionKey#interestOps()}.
*
* @return the desired {@link SelectionKey#interestOps()}.
* The mask to OR against the ops with.
*/
public abstract boolean interestOps();
}

/**
* Add operations to the {@link SelectionKey#interestOps()}.
*/
private static class AddInterestOps extends AbstractInterestOps {
/**
* The operations to add to the {@link #key}.
*/
private final int ops;
private final int opsOr;

/**
* Constructor.
*
* @param key the selection key.
* @param ops the operations to add to the {@link #key}.
* @param key the selection key.
* @param add the ops bits to add
* @param remove the ops bits to remove.
*/
AddInterestOps(SelectionKey key, int ops) {
super(key);
this.ops = ops;
}

/**
* {@inheritDoc}
*/
@Override
public boolean interestOps() {
if (key.isValid()) { // may not be valid by the time we run on selector thread
key.interestOps(key.interestOps() | this.ops);
return true;
}
return false;
private InterestOps(SelectionKey key, int add, int remove) {
this.key = key;
this.opsAnd = ~remove;
this.opsOr = add;
}

}

/**
* Remove operations from the {@link SelectionKey#interestOps()}.
*/
private static class RemoveInterestOps extends AbstractInterestOps {
/**
* The operations to remove from the {@link #key}.
*/
private final int ops;

/**
* Constructor.
* Returns the desired {@link SelectionKey#interestOps()}.
*
* @param key the selection key.
* @param ops the operations to remove from the {@link #key}.
*/
RemoveInterestOps(SelectionKey key, int ops) {
super(key);
this.ops = ops;
}

/**
* {@inheritDoc}
* @return the desired {@link SelectionKey#interestOps()}.
*/
@Override
public boolean interestOps() {
if (key.isValid()) { // may not be valid by the time we run on selector thread
key.interestOps(key.interestOps() & ~this.ops);
private boolean interestOps() {
if (key.isValid()) {
key.interestOps((key.interestOps() & opsAnd) | opsOr);
return true;
}
return false;
Expand Down

0 comments on commit 6da1cb4

Please sign in to comment.