Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ private D1(State<Entry<K>> state) {
/**
* Creates a fixed-size table holding at most {@code maxCapacity} entries. {@code entryClass} is
* used only to infer {@code TEntry}; entries are created by the {@code creator} functions
* passed to the insertion methods. The table does not resize.
* passed to the insertion methods. The table does not resize; its bucket count is {@link
* ConcurrentHashtable#bucketCountFor(int)}.
*/
@Nonnull
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -526,7 +527,8 @@ private D2(State<Entry<K1, K2>> state) {
/**
* Creates a fixed-size table holding at most {@code maxCapacity} entries. {@code entryClass} is
* used only to infer {@code TEntry}; entries are created by the {@code creator} functions
* passed to the insertion methods. The table does not resize.
* passed to the insertion methods. The table does not resize; its bucket count is {@link
* ConcurrentHashtable#bucketCountFor(int)}.
*/
@Nonnull
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -1027,11 +1029,61 @@ private State(AtomicReferenceArray<TEntry> buckets, int maxCapacity) {
/**
* Creates a bucket array for {@code maxCapacity} entries and pairs it with a manager enforcing
* that cap. {@code entryClass} is used only to infer {@code TEntry}.
*
* <p>The array has {@link #bucketCountFor(int) bucketCountFor(maxCapacity)} buckets: one per
* entry for small tables, but bounded by the heap for large ones (see {@link
* #bucketCountFor(int)}).
*/
@Nonnull
public static <TEntry extends Entry<TEntry>> State<TEntry> createBounded(
@Nonnull Class<TEntry> entryClass, int maxCapacity) {
return new State<>(createFixedBuckets(entryClass, maxCapacity), maxCapacity);
return createBounded(entryClass, maxCapacity, bucketCountFor(maxCapacity));
}

/**
* Creates a table of {@code bucketCount} buckets holding at most {@code maxCapacity} entries.
* Chains bear any excess of entries over buckets, so a table sized for its expected population
* still accepts up to its cap, with longer chains. {@code bucketCount} is rounded up to a power
* of two.
*/
@Nonnull
public static <TEntry extends Entry<TEntry>> State<TEntry> createBounded(
@Nonnull Class<TEntry> entryClass, int maxCapacity, int bucketCount) {
return new State<>(createFixedBuckets(entryClass, bucketCount), maxCapacity);
}

/** The fewest buckets {@link #bucketCountFor(int)} limits a table to. */
static final int MIN_HEAP_BOUNDED_BUCKETS = 1 << 10;

/**
* Returns the bucket count {@link #createBounded(Class, int)} uses for {@code maxCapacity}
* entries: {@link #sizeFor(int) sizeFor(maxCapacity)}, but no more than the heap warrants.
*
* <p>The table never resizes, so its bucket array is allocated at full size up front and
* retained, even if the table stays nearly empty. For a large cap on a small heap that is both a
* fixed cost out of proportion to the heap and, under G1, a humongous allocation. So the array is
* kept to at most 1/1024 of the maximum heap and below G1's humongous threshold, and never cut
* below {@value #MIN_HEAP_BOUNDED_BUCKETS} buckets. A table filled past its bucket count still
* works, with chains longer than one.
*/
public static int bucketCountFor(int maxCapacity) {
return bucketCountFor(maxCapacity, Runtime.getRuntime().maxMemory());
}

static int bucketCountFor(int maxCapacity, long maxHeapBytes) {
return Math.min(sizeFor(maxCapacity), maxBucketsForHeap(maxHeapBytes));
}

static int maxBucketsForHeap(long maxHeapBytes) {
// G1 aims for about 2,048 regions, a power of two between 1 and 32 MiB, and allocates any
// object of half a region or more as humongous. Leave room for the array header.
final long region =
Math.max(1L << 20, Math.min(1L << 25, Long.highestOneBit(maxHeapBytes >>> 11)));
final long arrayBytes = Math.min(region / 2 - 64, maxHeapBytes >>> 10);
// Compressed references, the default below 32 GiB, take 4 bytes; otherwise 8.
final long referenceBytes = maxHeapBytes < (32L << 30) ? 4 : 8;
final long buckets = Long.highestOneBit(Math.max(1, arrayBytes / referenceBytes));
return (int) Math.max(MIN_HEAP_BOUNDED_BUCKETS, Math.min(MAX_BUCKETS, buckets));
}

/** Live entries in {@code state}; see {@link SizeManager#estimateSize()}. Lock-free. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package datadog.trace.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import javax.annotation.Nonnull;
import org.junit.jupiter.api.Test;
import org.tabletest.junit.TableTest;

/** Exercises how {@link ConcurrentHashtable#createBounded} sizes its bucket array. */
class ConcurrentHashtableSizingTest {
private static final long MIB = 1L << 20;

private static final class TestEntry extends ConcurrentHashtable.Entry<TestEntry> {
final int value;

TestEntry(int value) {
super(value);
this.value = value;
}

@Override
public boolean matches(@Nonnull TestEntry other) {
return value == other.value;
}
}

@TableTest({
"scenario | heapMiB | buckets",
"1/1024 of a small heap | 16 | 4096 ",
"1/1024 of a 128 MiB heap | 128 | 32768 ",
"below the 1 MiB-region threshold | 1024 | 65536 ",
"below the 2 MiB-region threshold | 4096 | 131072 ",
"8-byte references from 32 GiB | 65536 | 1048576",
"below the 32 MiB-region cap | 1048576 | 1048576",
"never below the floor | 1 | 1024 "
})
void boundsTheBucketArrayByTheHeap(final long heapMiB, final int buckets) {
assertEquals(buckets, ConcurrentHashtable.maxBucketsForHeap(heapMiB * MIB));
}

@Test
void anUnboundedHeapStillGetsAFiniteBound() {
assertEquals(1 << 20, ConcurrentHashtable.maxBucketsForHeap(Long.MAX_VALUE));
}

@Test
void smallTablesGetABucketPerEntry() {
assertEquals(8, ConcurrentHashtable.bucketCountFor(8, 128 * MIB));
assertEquals(1024, ConcurrentHashtable.bucketCountFor(1000, 128 * MIB));
}

@Test
void largeTablesAreBoundedByTheHeap() {
assertEquals(32768, ConcurrentHashtable.bucketCountFor(131_072, 128 * MIB));
assertEquals(131_072, ConcurrentHashtable.bucketCountFor(131_072, 4096 * MIB));
}

@Test
void theDefaultSizingUsesThisHeap() {
final int expected =
ConcurrentHashtable.bucketCountFor(1 << 20, Runtime.getRuntime().maxMemory());

assertEquals(expected, ConcurrentHashtable.bucketCountFor(1 << 20));
assertEquals(
expected, ConcurrentHashtable.createBounded(TestEntry.class, 1 << 20).buckets.length());
}

@Test
void aTableWithFewerBucketsThanEntriesFillsToItsCap() {
final ConcurrentHashtable.State<TestEntry> state =
ConcurrentHashtable.createBounded(TestEntry.class, 100, 5);
assertEquals(8, state.buckets.length());

for (int key = 0; key < 100; key++) {
try (ConcurrentHashtable.Reservation<TestEntry> r =
ConcurrentHashtable.tryReserve(state, key)) {
assertNotNull(r.tryGetOrInsertOrNull(key, TestEntry::new), "key " + key);
}
}
assertTrue(ConcurrentHashtable.isFull(state));
assertEquals(100, ConcurrentHashtable.estimateSize(state));

for (int key = 0; key < 100; key++) {
TestEntry found = null;
for (TestEntry e = ConcurrentHashtable.bucketFor(state.buckets, key);
e != null;
e = e.next()) {
if (e.value == key) {
found = e;
}
}
assertNotNull(found, "key " + key);
try (ConcurrentHashtable.Reservation<TestEntry> r =
ConcurrentHashtable.tryReserve(state, key)) {
assertSame(found, r.tryGetOrInsertOrNull(key, TestEntry::new));
}
}
}
}
3 changes: 0 additions & 3 deletions dd-openfeature/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ extra["excludedClassesBranchCoverage"] = listOf(
"com.datadog.openfeature.internal.delivery.Retries",
"com.datadog.openfeature.internal.exposure.ExposureDeduplicationCache.Key",
"com.datadog.openfeature.internal.exposure.ExposurePipeline.ExposureSerializingHandler",
"com.datadog.openfeature.internal.flagevaluation.FlagEvalEvent",
"com.datadog.openfeature.internal.flagevaluation.FlagEvaluationPipeline",
)
extra["excludedClassesInstructionCoverage"] = listOf(
Expand All @@ -61,8 +60,6 @@ extra["excludedClassesInstructionCoverage"] = listOf(
"com.datadog.openfeature.internal.delivery.HttpCall",
"com.datadog.openfeature.internal.delivery.HttpCall.CancellableBodySubscriber",
"com.datadog.openfeature.internal.exposure.ExposureDeduplicationCache.Sent",
"com.datadog.openfeature.internal.flagevaluation.FlagEvalEvent",
"com.datadog.openfeature.internal.flagevaluation.FlagEvaluationPipeline.SerializingHandlerForTest",
"datadog.trace.api.openfeature.Provider",
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;

import com.datadog.openfeature.internal.flagevaluation.FlagEvalEvent;
import com.datadog.openfeature.internal.flagevaluation.FlagEvaluationWriter;
import dev.openfeature.sdk.FlagEvaluationDetails;
import dev.openfeature.sdk.FlagValueType;
Expand Down Expand Up @@ -37,15 +36,15 @@
* Evaluation-thread benchmark for the OpenFeature hook: the inline cost a caller actually pays
* inside FlagEvalLoggingHook.finallyAfter, across context shapes.
*
* <p>This is the counterpart to FlagEvaluationHotPathBenchmark, which measures the writer queue and
* the worker-thread aggregation.
* <p>This is the counterpart to FlagEvaluationHotPathBenchmark, which measures recording into the
* writer's aggregate.
*
* <p>The dominant inline cost under consent-on is DDEvaluator.copyPrunedContext: one bounded walk
* of the caller-owned EvaluationContext. contextCopy isolates that walk so its share of
* hookFinallyAfter is directly readable. Under consent-off the hook skips the copy entirely, which
* hookFinallyAfterConsentOff measures as the protected-path floor.
*
* <p>The writer used here is a no-op that discards events, so no queue or worker cost is included.
* <p>The writer used here is a no-op that discards evaluations, so no aggregation cost is included.
*
* <p>Run: {@code ./gradlew :dd-openfeature:jmh -Pjmh.includes=FlagEvalHookHotPathBenchmark}.
*/
Expand Down Expand Up @@ -95,13 +94,13 @@ public void setUp() {
hook = new FlagEvalLoggingHook<>(() -> writer);
}

/** Total inline cost under consent-on: scalar extraction, bounded context copy, and enqueue. */
/** Total inline cost under consent-on: scalar extraction, bounded context copy, and record. */
@Benchmark
public void hookFinallyAfter() {
hook.finallyAfter(hookContext, consentOnDetails, Collections.emptyMap());
}

/** Protected-path floor: consent-off skips the context copy, leaving scalar work plus enqueue. */
/** Protected-path floor: consent-off skips the context copy, leaving scalar work plus record. */
@Benchmark
public void hookFinallyAfterConsentOff() {
hook.finallyAfter(hookContext, consentOffDetails, Collections.emptyMap());
Expand Down Expand Up @@ -169,18 +168,19 @@ private static MutableContext addFlat(final MutableContext ctx, final int count)
return ctx;
}

/** Discards events so only hook-inline work is measured. */
/** Discards evaluations so only hook-inline work is measured. */
private static final class NoOpWriter implements FlagEvaluationWriter {
@Override
public void enqueue(final FlagEvalEvent event) {}

@Override
public boolean hasCapacityForEnqueue() {
return true;
}

@Override
public void countPreQueueOverflow() {}
public void record(
final String flagKey,
final String variant,
final String allocationKey,
final String targetingKey,
final String errorMessage,
final long evalTimeMs,
final boolean observeFullEvaluationData,
final Map<String, Object> attrs,
final Map<String, Object> features) {}

@Override
public void countContextTruncated(final String reason) {}
Expand Down
Loading
Loading