Skip to content

Commit

Permalink
Reduce native memory usage of QuicheQuicConnection (java-native-acces…
Browse files Browse the repository at this point in the history
…s#661)

Motivation:

QuicheQuicConnection does only need native memory for one recipient
addres, while we allocated memory for two.

Modifications:

Only allocate native memory for one address

Result:

Less native memory usage per quic connection
  • Loading branch information
normanmaurer authored Feb 5, 2024
1 parent d29c35d commit 5144215
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,6 @@ private boolean connectionSendSegments(SegmentedDatagramPacketAllocator segmente
int segmentSize = -1;
if (connection.isSendInfoChanged()) {
// Change the cached address and let the user know there was a connection migration.
InetSocketAddress oldRemote = remote;
remote = QuicheSendInfo.getToAddress(sendInfo);
local = QuicheSendInfo.getFromAddress(sendInfo);

Expand Down Expand Up @@ -1508,10 +1507,7 @@ void connectionRecv(InetSocketAddress sender, InetSocketAddress recipient, ByteB
ByteBuffer recvInfo = connection.nextRecvInfo();
QuicheRecvInfo.setRecvInfo(recvInfo, sender, recipient);

if (connection.isRecvInfoChanged()) {
// Update the cached address
remote = sender;
}
remote = sender;
local = recipient;

long connAddr = connection.address();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ final class QuicheQuicConnection {
private final ByteBuf recvInfoBuffer;
private final ByteBuf sendInfoBuffer;

private boolean recvInfoFirst = true;
private boolean sendInfoFirst = true;
private final ByteBuffer recvInfoBuffer1;
private final ByteBuffer recvInfoBuffer2;
private final ByteBuffer sendInfoBuffer1;
private final ByteBuffer sendInfoBuffer2;

Expand All @@ -66,16 +64,14 @@ final class QuicheQuicConnection {
this.engine = engine;
this.refCnt = refCnt;
// TODO: Maybe cache these per thread as we only use them temporary within a limited scope.
recvInfoBuffer = Quiche.allocateNativeOrder(2 * TOTAL_RECV_INFO_SIZE);
recvInfoBuffer = Quiche.allocateNativeOrder(TOTAL_RECV_INFO_SIZE);
sendInfoBuffer = Quiche.allocateNativeOrder(2 * Quiche.SIZEOF_QUICHE_SEND_INFO);

// Let's memset the memory.
recvInfoBuffer.setZero(0, recvInfoBuffer.capacity());
sendInfoBuffer.setZero(0, sendInfoBuffer.capacity());

recvInfoBuffer1 = recvInfoBuffer.nioBuffer(0, TOTAL_RECV_INFO_SIZE);
recvInfoBuffer2 = recvInfoBuffer.nioBuffer(TOTAL_RECV_INFO_SIZE, TOTAL_RECV_INFO_SIZE);

sendInfoBuffer1 = sendInfoBuffer.nioBuffer(0, Quiche.SIZEOF_QUICHE_SEND_INFO);
sendInfoBuffer2 = sendInfoBuffer.nioBuffer(Quiche.SIZEOF_QUICHE_SEND_INFO, Quiche.SIZEOF_QUICHE_SEND_INFO);
this.engine.connection = this;
Expand Down Expand Up @@ -184,20 +180,18 @@ void init(InetSocketAddress local, InetSocketAddress remote, Consumer<String> sn
assert recvInfoBuffer.refCnt() != 0;
assert sendInfoBuffer.refCnt() != 0;

// Fill both quiche_recv_info structs with the same address.
// Fill quiche_recv_info struct with the addresses.
QuicheRecvInfo.setRecvInfo(recvInfoBuffer1, remote, local);
QuicheRecvInfo.setRecvInfo(recvInfoBuffer2, remote, local);

// Fill both quiche_send_info structs with the same address.
// Fill both quiche_send_info structs with the same addresses.
QuicheSendInfo.setSendInfo(sendInfoBuffer1, local, remote);
QuicheSendInfo.setSendInfo(sendInfoBuffer2, local, remote);
engine.sniSelectedCallback = sniSelectedCallback;
}

ByteBuffer nextRecvInfo() {
assert recvInfoBuffer.refCnt() != 0;
recvInfoFirst = !recvInfoFirst;
return recvInfoFirst ? recvInfoBuffer1 : recvInfoBuffer2;
return recvInfoBuffer1;
}

ByteBuffer nextSendInfo() {
Expand All @@ -211,11 +205,6 @@ boolean isSendInfoChanged() {
return !QuicheSendInfo.isSameAddress(sendInfoBuffer1, sendInfoBuffer2);
}

boolean isRecvInfoChanged() {
assert recvInfoBuffer.refCnt() != 0;
return !QuicheRecvInfo.isSameAddress(recvInfoBuffer1, recvInfoBuffer2);
}

boolean isClosed() {
assert connection != -1;
return Quiche.quiche_conn_is_closed(connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,4 @@ private static void setAddress(ByteBuffer memory, int socketAddressOffset, int a
memory.position(position);
}
}

/**
* Returns {@code true} if both {@link ByteBuffer}s have the same {@code sock_addr} stored.
*
* @param memory the first {@link ByteBuffer} which holds a {@code quiche_recv_info}.
* @param memory2 the second {@link ByteBuffer} which holds a {@code quiche_recv_info}.
* @return {@code true} if both {@link ByteBuffer}s have the same {@code sock_addr} stored, {@code false}
* otherwise.
*/
static boolean isSameAddress(ByteBuffer memory, ByteBuffer memory2) {
return Quiche.isSameAddress(memory, memory2, Quiche.SIZEOF_QUICHE_RECV_INFO);
}
}

0 comments on commit 5144215

Please sign in to comment.