From 5144215ffca5b2321e91e601b852bfdc2e9d55b8 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 5 Feb 2024 11:23:57 +0100 Subject: [PATCH] Reduce native memory usage of QuicheQuicConnection (#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 --- .../codec/quic/QuicheQuicChannel.java | 6 +----- .../codec/quic/QuicheQuicConnection.java | 19 ++++--------------- .../incubator/codec/quic/QuicheRecvInfo.java | 12 ------------ 3 files changed, 5 insertions(+), 32 deletions(-) diff --git a/codec-classes-quic/src/main/java/io/netty/incubator/codec/quic/QuicheQuicChannel.java b/codec-classes-quic/src/main/java/io/netty/incubator/codec/quic/QuicheQuicChannel.java index 732d49b03..f5ec9bb1a 100644 --- a/codec-classes-quic/src/main/java/io/netty/incubator/codec/quic/QuicheQuicChannel.java +++ b/codec-classes-quic/src/main/java/io/netty/incubator/codec/quic/QuicheQuicChannel.java @@ -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); @@ -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(); diff --git a/codec-classes-quic/src/main/java/io/netty/incubator/codec/quic/QuicheQuicConnection.java b/codec-classes-quic/src/main/java/io/netty/incubator/codec/quic/QuicheQuicConnection.java index b30df8410..92c0ef809 100644 --- a/codec-classes-quic/src/main/java/io/netty/incubator/codec/quic/QuicheQuicConnection.java +++ b/codec-classes-quic/src/main/java/io/netty/incubator/codec/quic/QuicheQuicConnection.java @@ -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; @@ -66,7 +64,7 @@ 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. @@ -74,8 +72,6 @@ final class QuicheQuicConnection { 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; @@ -184,11 +180,10 @@ void init(InetSocketAddress local, InetSocketAddress remote, Consumer 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; @@ -196,8 +191,7 @@ void init(InetSocketAddress local, InetSocketAddress remote, Consumer sn ByteBuffer nextRecvInfo() { assert recvInfoBuffer.refCnt() != 0; - recvInfoFirst = !recvInfoFirst; - return recvInfoFirst ? recvInfoBuffer1 : recvInfoBuffer2; + return recvInfoBuffer1; } ByteBuffer nextSendInfo() { @@ -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); diff --git a/codec-classes-quic/src/main/java/io/netty/incubator/codec/quic/QuicheRecvInfo.java b/codec-classes-quic/src/main/java/io/netty/incubator/codec/quic/QuicheRecvInfo.java index eccdc00a7..7a682fe3b 100644 --- a/codec-classes-quic/src/main/java/io/netty/incubator/codec/quic/QuicheRecvInfo.java +++ b/codec-classes-quic/src/main/java/io/netty/incubator/codec/quic/QuicheRecvInfo.java @@ -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); - } }