-
Notifications
You must be signed in to change notification settings - Fork 345
Overhaul set benchmarks: split Immutable / SingleThreaded, add Set.copyOf #11721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
24a9fdc
Overhaul set benchmarks: split Immutable / SingleThreaded, add Set.co…
dougqh 5a14e62
Add Java 17 results to set benchmark Javadocs
dougqh 78075dc
Rename copyOf -> tracerImmutableSet in ImmutableSetBenchmark
dougqh e1a6de3
Fix clone_synchronizedSet to clone the synchronized set (not the plai…
dougqh 8133d8a
Merge branch 'master' into dougqh/set-benchmark
dougqh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
186 changes: 186 additions & 0 deletions
186
internal-api/src/jmh/java/datadog/trace/util/ImmutableSetBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| package datadog.trace.util; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import java.util.TreeSet; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Threads; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
|
|
||
| /** | ||
| * Membership over a small, fixed, read-only string set shared across threads — split into hit and | ||
| * miss lookups (different cost shapes per structure). | ||
| * | ||
| * <p>The set is built once and only read, so a single shared instance ({@link Scope#Benchmark}) | ||
| * read by all {@code @Threads} is realistic and contention-free. This is the read-mostly | ||
| * counterpart to the per-thread mutable {@link SingleThreadedSetBenchmark}, and mirrors {@link | ||
| * ImmutableMapBenchmark} on the set side. Sets in the tracer skew strongly toward this fixed, | ||
| * read-only shape. | ||
| * | ||
| * <p>Strategies compared: | ||
| * | ||
| * <ul> | ||
| * <li>{@code array} / {@code sortedArray} — linear scan / binary search; slow on miss. | ||
| * <li>{@link HashSet} — idiomatic, fast; node-based, allocates per element. | ||
| * <li>{@link TreeSet} — comparator-ordered; worth it only for a custom comparator, not speed. | ||
| * <li>{@link java.util.Set#copyOf} (via {@link CollectionUtils#tryMakeImmutableSet}) — the JDK's | ||
| * compact, array-backed immutable set ({@code ImmutableCollections.SetN}), which is what the | ||
| * agent actually uses for fixed config sets. Java 10+; falls back to {@code HashSet} pre-10. | ||
| * The realistic baseline for any flat/immutable set comparison. | ||
| * </ul> | ||
| * | ||
| * <p>Lookups are interned (the {@code ==} fast path where a structure has one); misses are short | ||
| * and never present. | ||
| * | ||
| * <p>Java 17 results (Apple M1, {@code @Fork(2)}, {@code @Threads(8)}; M ops/s = millions): | ||
| * | ||
| * <pre>{@code | ||
| * Structure hit miss | ||
| * hashSet 2159 1751 (fastest) | ||
| * copyOf (SetN) 1946 1633 | ||
| * array 926 584 | ||
| * sortedArray 664 588 | ||
| * treeSet 642 593 | ||
| * }</pre> | ||
| * | ||
| * <p>Key findings: | ||
| * | ||
| * <ul> | ||
| * <li>{@code HashSet} is fastest; {@link java.util.Set#copyOf} ({@code SetN}) trails by only ~10% | ||
| * on hit and ~7% on miss — and it's the compact, array-backed form the agent already uses for | ||
| * fixed config sets, so it's a strong default when the set is immutable. | ||
| * <li>{@code array} / {@code sortedArray} / {@code treeSet} cluster at ~0.6–0.9B — they scan, | ||
| * binary-search, or tree-walk per lookup, so they trail the hashed structures, most visibly | ||
| * on the miss path. | ||
| * </ul> | ||
| */ | ||
| @Fork(2) | ||
| @Warmup(iterations = 2) | ||
| @Measurement(iterations = 3) | ||
| @Threads(8) | ||
| @State(Scope.Benchmark) | ||
| public class ImmutableSetBenchmark { | ||
| static final String[] STRINGS = { | ||
| "foo", "bar", "baz", "quux", "hello", "world", | ||
| "service", "queryString", "lorem", "ipsum", "dolem", "sit" | ||
| }; | ||
|
|
||
| /** Distinct String instances that are never present, for the miss path. */ | ||
| static final String[] MISSES = newMisses(); | ||
|
|
||
| static String[] newMisses() { | ||
| String[] misses = new String[STRINGS.length * 4]; | ||
| for (int i = 0; i < misses.length; ++i) { | ||
| misses[i] = "dne-" + i; | ||
| } | ||
| return misses; | ||
| } | ||
|
|
||
| // Built once, never mutated -- safe to share across the reader threads. | ||
| String[] array; | ||
| String[] sortedArray; | ||
| HashSet<String> hashSet; | ||
| TreeSet<String> treeSet; | ||
| Set<String> copyOfSet; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setUp() { | ||
| array = STRINGS; | ||
| sortedArray = Arrays.copyOf(STRINGS, STRINGS.length); | ||
| Arrays.sort(sortedArray); | ||
| hashSet = new HashSet<>(Arrays.asList(STRINGS)); | ||
| treeSet = new TreeSet<>(Arrays.asList(STRINGS)); | ||
| copyOfSet = CollectionUtils.tryMakeImmutableSet(Arrays.asList(STRINGS)); | ||
| } | ||
|
|
||
| /** Per-thread lookup cursor so each reader thread cycles keys independently. */ | ||
| @State(Scope.Thread) | ||
| public static class Cursor { | ||
| int hitIndex = 0; | ||
| int missIndex = 0; | ||
|
|
||
| String nextHit() { | ||
| int i = hitIndex + 1; | ||
| if (i >= STRINGS.length) { | ||
| i = 0; | ||
| } | ||
| hitIndex = i; | ||
| return STRINGS[i]; | ||
| } | ||
|
|
||
| String nextMiss() { | ||
| int i = missIndex + 1; | ||
| if (i >= MISSES.length) { | ||
| i = 0; | ||
| } | ||
| missIndex = i; | ||
| return MISSES[i]; | ||
| } | ||
| } | ||
|
|
||
| static boolean arrayContains(String[] array, String needle) { | ||
| for (String s : array) { | ||
| if (needle.equals(s)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Benchmark | ||
| public boolean array_hit(Cursor cursor) { | ||
| return arrayContains(array, cursor.nextHit()); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public boolean array_miss(Cursor cursor) { | ||
| return arrayContains(array, cursor.nextMiss()); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public boolean sortedArray_hit(Cursor cursor) { | ||
| return Arrays.binarySearch(sortedArray, cursor.nextHit()) >= 0; | ||
| } | ||
|
|
||
| @Benchmark | ||
| public boolean sortedArray_miss(Cursor cursor) { | ||
| return Arrays.binarySearch(sortedArray, cursor.nextMiss()) >= 0; | ||
| } | ||
|
|
||
| @Benchmark | ||
| public boolean hashSet_hit(Cursor cursor) { | ||
| return hashSet.contains(cursor.nextHit()); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public boolean hashSet_miss(Cursor cursor) { | ||
| return hashSet.contains(cursor.nextMiss()); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public boolean treeSet_hit(Cursor cursor) { | ||
| return treeSet.contains(cursor.nextHit()); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public boolean treeSet_miss(Cursor cursor) { | ||
| return treeSet.contains(cursor.nextMiss()); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public boolean copyOf_hit(Cursor cursor) { | ||
| return copyOfSet.contains(cursor.nextHit()); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public boolean copyOf_miss(Cursor cursor) { | ||
| return copyOfSet.contains(cursor.nextMiss()); | ||
| } | ||
| } | ||
128 changes: 0 additions & 128 deletions
128
internal-api/src/jmh/java/datadog/trace/util/SetBenchmark.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Maybe a better name instead of
copyOf, e.g.tracerImmutableSetThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I debated that. It was so named because of the underlying mechanism, but tracerImmutableSet might be better.