From 5bc558da8552e00e41fa7115fa7dcd361673202d Mon Sep 17 00:00:00 2001 From: Douglas Q Hawkins Date: Fri, 20 Nov 2026 22:40:44 -0500 Subject: [PATCH] Read the composite propagator's carrier cache in place CompositePropagator.CarrierCache now implements a new IndexedCarrier (size/key/value by index) over a String[] sized for the handful of headers left after key filtering, growing on demand, instead of an ArrayList(32). HttpCodec.ExtractionCache reads an IndexedCarrier in place rather than copying every pair into a second ArrayList(32); other carriers are still copied. Co-Authored-By: Claude Opus 5.5 --- .../propagation/CompositePropagator.java | 49 +++++++++++---- .../context/propagation/IndexedCarrier.java | 19 ++++++ .../context/propagation/CarrierCacheTest.java | 61 +++++++++++++++++++ .../trace/core/propagation/HttpCodec.java | 35 ++++++++--- 4 files changed, 145 insertions(+), 19 deletions(-) create mode 100644 components/context/src/main/java/datadog/context/propagation/IndexedCarrier.java create mode 100644 components/context/src/test/java/datadog/context/propagation/CarrierCacheTest.java diff --git a/components/context/src/main/java/datadog/context/propagation/CompositePropagator.java b/components/context/src/main/java/datadog/context/propagation/CompositePropagator.java index b2d37a4148e..42120f2281a 100644 --- a/components/context/src/main/java/datadog/context/propagation/CompositePropagator.java +++ b/components/context/src/main/java/datadog/context/propagation/CompositePropagator.java @@ -1,8 +1,7 @@ package datadog.context.propagation; import datadog.context.Context; -import java.util.ArrayList; -import java.util.List; +import java.util.Arrays; import java.util.function.BiConsumer; class CompositePropagator implements Propagator { @@ -74,25 +73,51 @@ public Context extract(Context context, C carrier, CarrierVisitor visitor return context; } - static class CarrierCache implements BiConsumer, CarrierVisitor { - /** Cached key/values from carrier (even indexes are keys, odd indexes are values). */ - private final List keysAndValues; + /** + * The carrier key/value pairs the propagators read, cached so each propagator reads them without + * walking the carrier again, and readable in place as an {@link IndexedCarrier}. + */ + static final class CarrierCache + implements BiConsumer, CarrierVisitor, IndexedCarrier { + /** Enough for the trace headers a carrier usually holds once unread keys are skipped. */ + private static final int INITIAL_PAIRS = 8; - public CarrierCache() { - this.keysAndValues = new ArrayList<>(32); - } + /** Keys at even indexes, values at odd ones. */ + private String[] keysAndValues = new String[2 * INITIAL_PAIRS]; + + private int size; @Override public void accept(String key, String value) { - this.keysAndValues.add(key); - this.keysAndValues.add(value); + int index = 2 * this.size; + if (index == this.keysAndValues.length) { + this.keysAndValues = Arrays.copyOf(this.keysAndValues, 2 * index); + } + this.keysAndValues[index] = key; + this.keysAndValues[index + 1] = value; + this.size++; } @Override public void forEachKeyValue(CarrierCache carrier, BiConsumer visitor) { - for (int i = 0; i < carrier.keysAndValues.size() - 1; i += 2) { - visitor.accept(carrier.keysAndValues.get(i), carrier.keysAndValues.get(i + 1)); + for (int i = 0; i < carrier.size; i++) { + visitor.accept(carrier.keysAndValues[2 * i], carrier.keysAndValues[2 * i + 1]); } } + + @Override + public int size() { + return this.size; + } + + @Override + public String key(int index) { + return this.keysAndValues[2 * index]; + } + + @Override + public String value(int index) { + return this.keysAndValues[2 * index + 1]; + } } } diff --git a/components/context/src/main/java/datadog/context/propagation/IndexedCarrier.java b/components/context/src/main/java/datadog/context/propagation/IndexedCarrier.java new file mode 100644 index 00000000000..9e811762ba6 --- /dev/null +++ b/components/context/src/main/java/datadog/context/propagation/IndexedCarrier.java @@ -0,0 +1,19 @@ +package datadog.context.propagation; + +/** + * A carrier whose key/value pairs are already in memory and can be read by index, so a propagator + * that reads them more than once can do so in place instead of copying them first. The composite + * propagator hands its propagators one, holding the keys any of them reads. + * + *

Read-only, and valid only for the extraction it was handed to: don't keep a reference. + */ +public interface IndexedCarrier { + /** The number of key/value pairs. */ + int size(); + + /** The key of pair {@code index}, from 0 to {@link #size()} - 1. */ + String key(int index); + + /** The value of pair {@code index}, from 0 to {@link #size()} - 1. */ + String value(int index); +} diff --git a/components/context/src/test/java/datadog/context/propagation/CarrierCacheTest.java b/components/context/src/test/java/datadog/context/propagation/CarrierCacheTest.java new file mode 100644 index 00000000000..fd799e9bd1e --- /dev/null +++ b/components/context/src/test/java/datadog/context/propagation/CarrierCacheTest.java @@ -0,0 +1,61 @@ +package datadog.context.propagation; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import datadog.context.propagation.CompositePropagator.CarrierCache; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Test; + +class CarrierCacheTest { + + @Test + void readsPairsByIndexInOrder() { + CarrierCache cache = new CarrierCache(); + cache.accept("traceparent", "tp"); + cache.accept("baggage", "b"); + + assertEquals(2, cache.size()); + assertEquals("traceparent", cache.key(0)); + assertEquals("tp", cache.value(0)); + assertEquals("baggage", cache.key(1)); + assertEquals("b", cache.value(1)); + } + + @Test + void growsPastItsInitialCapacity() { + CarrierCache cache = new CarrierCache(); + for (int i = 0; i < 40; i++) { + cache.accept("key-" + i, "value-" + i); + } + + assertEquals(40, cache.size()); + for (int i = 0; i < 40; i++) { + assertEquals("key-" + i, cache.key(i)); + assertEquals("value-" + i, cache.value(i)); + } + } + + @Test + void visitsEveryPairInOrder() { + CarrierCache cache = new CarrierCache(); + for (int i = 0; i < 20; i++) { + cache.accept("key-" + i, "value-" + i); + } + List seen = new ArrayList<>(); + cache.forEachKeyValue(cache, (key, value) -> seen.add(key + "=" + value)); + + assertEquals(20, seen.size()); + assertEquals("key-0=value-0", seen.get(0)); + assertEquals("key-19=value-19", seen.get(19)); + } + + @Test + void emptyCache() { + CarrierCache cache = new CarrierCache(); + List seen = new ArrayList<>(); + cache.forEachKeyValue(cache, (key, value) -> seen.add(key)); + assertEquals(0, cache.size()); + assertEquals(0, seen.size()); + } +} diff --git a/dd-trace-core/src/main/java/datadog/trace/core/propagation/HttpCodec.java b/dd-trace-core/src/main/java/datadog/trace/core/propagation/HttpCodec.java index 97303fe12d2..30da4e8b2a9 100644 --- a/dd-trace-core/src/main/java/datadog/trace/core/propagation/HttpCodec.java +++ b/dd-trace-core/src/main/java/datadog/trace/core/propagation/HttpCodec.java @@ -6,6 +6,7 @@ import datadog.context.propagation.CarrierKeys; import datadog.context.propagation.CarrierSetter; +import datadog.context.propagation.IndexedCarrier; import datadog.trace.api.Config; import datadog.trace.api.DD128bTraceId; import datadog.trace.api.DD64bTraceId; @@ -371,21 +372,34 @@ private void applyTraceContextToFirstContext( } } + /** + * The carrier's key/value pairs, read once so each extractor visits them without walking the + * carrier again. A carrier that is already an {@link IndexedCarrier} (the composite propagator's + * cache) is read in place; any other is copied. + */ private static class ExtractionCache implements AgentPropagation.KeyClassifier, AgentPropagation.ContextVisitor> { - /** Cached context key-values (even indexes are header names, odd indexes are header values). */ + /** The pairs, read in place; null when they were copied into {@link #keysAndValues}. */ + private final IndexedCarrier indexed; + + /** The copied pairs, keys at even indexes; null when {@link #indexed} is used. */ private final List keysAndValues; - /** - * The parent span identifier from {@link DatadogHttpCodec#SPAN_ID_KEY} header formatted as 16 - * hexadecimal characters, {@code null} if absent or invalid. - */ private String datadogSpanIdHex; public ExtractionCache(C carrier, AgentPropagation.ContextVisitor getter) { - this.keysAndValues = new ArrayList<>(32); - getter.forEachKey(carrier, this); + if (carrier instanceof IndexedCarrier) { + this.indexed = (IndexedCarrier) carrier; + this.keysAndValues = null; + for (int i = 0, n = this.indexed.size(); i < n; i++) { + cacheDatadogSpanId(this.indexed.key(i), this.indexed.value(i)); + } + } else { + this.indexed = null; + this.keysAndValues = new ArrayList<>(32); + getter.forEachKey(carrier, this); + } } @Override @@ -412,6 +426,13 @@ private String getDatadogSpanIdHex() { @Override public void forEachKey(ExtractionCache carrier, AgentPropagation.KeyClassifier classifier) { + IndexedCarrier indexed = carrier.indexed; + if (indexed != null) { + for (int i = 0, n = indexed.size(); i < n; i++) { + classifier.accept(indexed.key(i), indexed.value(i)); + } + return; + } List keysAndValues = carrier.keysAndValues; for (int i = 0; i < keysAndValues.size(); i += 2) { classifier.accept(keysAndValues.get(i), keysAndValues.get(i + 1));