From f9606770b35b3c684c7802868f668e8272ab484a Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Tue, 9 Mar 2021 17:21:43 +1100 Subject: [PATCH] Use calculated header size for QpackEncoder buffer allocation. Signed-off-by: Lachlan Roberts --- .../eclipse/jetty/http3/qpack/QpackEncoder.java | 12 ++++++++++-- .../eclipse/jetty/http3/qpack/EvictionTest.java | 16 ++++++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/jetty-http3/http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/QpackEncoder.java b/jetty-http3/http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/QpackEncoder.java index 0c35867b744..c709058b892 100644 --- a/jetty-http3/http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/QpackEncoder.java +++ b/jetty-http3/http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/QpackEncoder.java @@ -300,8 +300,16 @@ public class QpackEncoder implements Dumpable deltaBase = signBit ? requiredInsertCount - base - 1 : base - requiredInsertCount; } - // TODO: Calculate the size required. - ByteBuffer buffer = _bufferPool.acquire(1024, false); + // Calculate the size required. TODO: it may be more efficient to just use a buffer of MAX_HEADER_SIZE? + int spaceRequired = 0; + spaceRequired += 1 + NBitInteger.octectsNeeded(8, encodedInsertCount); + spaceRequired += 1 + NBitInteger.octectsNeeded(7, deltaBase); + for (EncodableEntry encodableEntry : encodableEntries) + { + spaceRequired += encodableEntry.getRequiredSize(base); + } + + ByteBuffer buffer = _bufferPool.acquire(spaceRequired, false); int pos = BufferUtil.flipToFill(buffer); // Encode the Field Section Prefix into the ByteBuffer. diff --git a/jetty-http3/http3-qpack/src/test/java/org/eclipse/jetty/http3/qpack/EvictionTest.java b/jetty-http3/http3-qpack/src/test/java/org/eclipse/jetty/http3/qpack/EvictionTest.java index d8479c6519c..43940919848 100644 --- a/jetty-http3/http3-qpack/src/test/java/org/eclipse/jetty/http3/qpack/EvictionTest.java +++ b/jetty-http3/http3-qpack/src/test/java/org/eclipse/jetty/http3/qpack/EvictionTest.java @@ -66,14 +66,14 @@ public class EvictionTest _decoder.decode(streamId, encodedFields); HttpFields result = _decoderHandler.getHttpFields(); - System.err.println("encoder: "); - System.err.println(_encoder.dump()); - System.err.println(); - System.err.println("decoder: "); - System.err.println(_decoder.dump()); - System.err.println(); - System.err.println("===================="); - System.err.println(); +// System.err.println("encoder: "); +// System.err.println(_encoder.dump()); +// System.err.println(); +// System.err.println("decoder: "); +// System.err.println(_decoder.dump()); +// System.err.println(); +// System.err.println("===================="); +// System.err.println(); assertThat(result, is(httpFields)); }