Skip to content

Commit

Permalink
Avoid using JVM HashCode on Http 2 DefaultStream (#13760)
Browse files Browse the repository at this point in the history
Motivation:

relying on DefaultStream's hashCode forces calling into JVM's strategy
for identity hash code

Modifications:

Use the stream's id to identify DefaultStream

Result:

Faster active stream insertion, saving producing JVM's hashcode
  • Loading branch information
franz1981 committed Jan 12, 2024
1 parent 3b81f2c commit 4dea3f7
Showing 1 changed file with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,13 @@ private class DefaultStream implements Http2Stream {
private static final byte META_STATE_RECV_HEADERS = 1 << 4;
private static final byte META_STATE_RECV_TRAILERS = 1 << 5;
private final int id;
private final long identity;
private final PropertyMap properties = new PropertyMap();
private State state;
private byte metaState;

DefaultStream(int id, State state) {
DefaultStream(long identity, int id, State state) {
this.identity = identity;
this.id = id;
this.state = state;
}
Expand Down Expand Up @@ -599,14 +601,28 @@ void resizeIfNecessary(int index) {
}
}
}

@Override
public boolean equals(final Object obj) {
return super.equals(obj);
}

@Override
public int hashCode() {
long value = identity;
if (value == 0) {
return System.identityHashCode(this);
}
return (int) (value ^ (value >>> 32));
}
}

/**
* Stream class representing the connection, itself.
*/
private final class ConnectionStream extends DefaultStream {
ConnectionStream() {
super(CONNECTION_STREAM_ID, IDLE);
super(0, CONNECTION_STREAM_ID, IDLE);
}

@Override
Expand Down Expand Up @@ -670,6 +686,10 @@ public boolean isPushPromiseSent() {
*/
private final class DefaultEndpoint<F extends Http2FlowController> implements Endpoint<F> {
private final boolean server;
/**
* This is an always increasing sequence number used to hash {@link DefaultStream} instances.
*/
private long lastCreatedStreamIdentity;
/**
* The minimum stream ID allowed when creating the next stream. This only applies at the time the stream is
* created. If the ID of the stream being created is less than this value, stream creation will fail. Upon
Expand All @@ -694,6 +714,7 @@ private final class DefaultEndpoint<F extends Http2FlowController> implements En
int numStreams;

DefaultEndpoint(boolean server, int maxReservedStreams) {
this.lastCreatedStreamIdentity = 0;
this.server = server;

// Determine the starting stream ID for this endpoint. Client-initiated streams
Expand Down Expand Up @@ -750,8 +771,10 @@ public DefaultStream createStream(int streamId, boolean halfClosed) throws Http2

checkNewStreamAllowed(streamId, state);

lastCreatedStreamIdentity++;

// Create and initialize the stream.
DefaultStream stream = new DefaultStream(streamId, state);
DefaultStream stream = new DefaultStream(lastCreatedStreamIdentity, streamId, state);

incrementExpectedStreamId(streamId);

Expand Down Expand Up @@ -785,8 +808,10 @@ public DefaultStream reservePushStream(int streamId, Http2Stream parent) throws
State state = isLocal() ? RESERVED_LOCAL : RESERVED_REMOTE;
checkNewStreamAllowed(streamId, state);

lastCreatedStreamIdentity++;

// Create and initialize the stream.
DefaultStream stream = new DefaultStream(streamId, state);
DefaultStream stream = new DefaultStream(lastCreatedStreamIdentity, streamId, state);

incrementExpectedStreamId(streamId);

Expand Down

0 comments on commit 4dea3f7

Please sign in to comment.