From 8723d60f64ea35963b4c109332b4ad5f8e2ecfb2 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Mon, 28 Sep 2026 14:47:33 +0200 Subject: [PATCH] Optimize HPACK full match lookup Avoid indexed access when traversing dynamic table entries. Use sequential iteration for linked lists. --- .../hc/core5/http2/hpack/HPackEncoder.java | 6 +- .../hpack/HPackEncoderLookupBenchmark.java | 110 ++++++++++++++++++ 2 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 httpcore5-testing/src/test/java/org/apache/hc/core5/http2/hpack/HPackEncoderLookupBenchmark.java diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java index 27ae0302d..7a2361c08 100644 --- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java +++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java @@ -34,6 +34,7 @@ import java.nio.charset.CharsetEncoder; import java.nio.charset.CoderResult; import java.nio.charset.StandardCharsets; +import java.util.Iterator; import java.util.List; import java.util.Objects; @@ -259,8 +260,9 @@ private int findFullMatch(final List entries, final String value) { if (entries == null || entries.isEmpty()) { return 0; } - for (int i = 0; i < entries.size(); i++) { - final HPackEntry entry = entries.get(i); + final Iterator iterator = entries.iterator(); + while (iterator.hasNext()) { + final HPackEntry entry = iterator.next(); if (Objects.equals(value, entry.getHeader().getValue())) { return entry.getIndex(); } diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/http2/hpack/HPackEncoderLookupBenchmark.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/http2/hpack/HPackEncoderLookupBenchmark.java new file mode 100644 index 000000000..3a149d4a7 --- /dev/null +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/http2/hpack/HPackEncoderLookupBenchmark.java @@ -0,0 +1,110 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.hc.core5.http2.hpack; + +import java.util.List; +import java.util.Objects; +import java.util.concurrent.TimeUnit; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Warmup(iterations = 5, time = 1) +@Measurement(iterations = 8, time = 1) +@Fork(3) +@State(Scope.Thread) +public class HPackEncoderLookupBenchmark { + + @Param({"32", "64", "80", "256", "1024", "4096"}) + private int entryCount; + + private List entries; + private String missingValue; + + @Setup + public void setup() { + final OutboundDynamicTable table = new OutboundDynamicTable(Integer.MAX_VALUE); + + for (int i = 0; i < entryCount; i++) { + table.add(new HPackHeader("x-request-id", "req-" + i)); + } + + entries = table.getByName("x-request-id"); + missingValue = "not-present"; + } + + @Benchmark + public int findFullMatchIndexed() { + return findFullMatchIndexed(entries, missingValue); + } + + @Benchmark + public int findFullMatchIterator() { + return findFullMatchIterator(entries, missingValue); + } + + private static int findFullMatchIndexed( + final List entries, + final String value) { + if (entries == null || entries.isEmpty()) { + return 0; + } + for (int i = 0; i < entries.size(); i++) { + final HPackEntry entry = entries.get(i); + if (Objects.equals(value, entry.getHeader().getValue())) { + return entry.getIndex(); + } + } + return 0; + } + + private static int findFullMatchIterator( + final List entries, + final String value) { + if (entries == null || entries.isEmpty()) { + return 0; + } + for (final HPackEntry entry : entries) { + if (Objects.equals(value, entry.getHeader().getValue())) { + return entry.getIndex(); + } + } + return 0; + } + +} \ No newline at end of file