Skip to content

Add an allocation-free bounded LRU, optionally growable, to internal-api - #13594

Open
dougqh wants to merge 5 commits into
dougqh/toolbox-hashtable-growthfrom
dougqh/toolbox-lru
Open

dougqh wants to merge 5 commits into
dougqh/toolbox-hashtable-growthfrom
dougqh/toolbox-lru

Conversation

@dougqh

@dougqh dougqh commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Restacked: this PR now adds LruTable to internal-api (datadog.trace.util), on top of #13649. It sizes itself exactly as #13649's Hashtable does (fixed by default; growth opt-in through Hashtable.Sizing.initialCapacity, at most two doublings via Hashtable.resize), with LruTable.D1.create(Class, Hashtable.Sizing) and the D2 equivalent. The int constructors give a fixed table. Moving dd-openfeature's ExposureDeduplicationCache onto it is now a separate PR, #13652, on #13533. #13531 later moves LruTable into the toolbox with the rest. Behavior is unchanged, apart from the order of entries within a bucket chain after a resize; lookups and recency-order iteration are unaffected.

What Does This Do

  • Adds datadog.trace.util.LruTable to internal-api, with D1 (one key) and D2 (a two-part key, with no key object per lookup). Both are bounded least-recently-used tables.
    • No allocation per lookup: entries carry their own links in the recency order, so a full table holds one object per key.
    • Optionally growable spine: fixed at Hashtable.capacityFor(bound) by default. With a Sizing that sets an initial capacity, the bucket array starts at a quarter of that at least and doubles past 3/4 full, at most twice, up to the full size.
    • Eviction: at the bound, insert evicts the least recently used entry and returns it, so the caller can release what it holds or count the eviction.
    • API: get reorders, peek doesn't. Also remove, clear, eldest and forEach, with entries visited from eldest to youngest.
  • Adds LruTableBenchmark (internal-api JMH): LruTable.D2 against a LinkedHashMap in access order with removeEldestEntry.

Motivation

Motivating user: #13533 made dd-openfeature's exposure cache allocation-free and one object per subject. Its Hashtable.D2 has a fixed bucket array, though: at the cache's 65,536-subject bound that's 131,072 buckets, a 512 KiB array, which is a humongous allocation on a small G1 heap. Creating it lazily (the review follow-up on #13533) only delays it to the first exposure, so a service with a handful of subjects still holds the whole array. Bits flagged this as a P3: caches under about 12k entries hold more than the LinkedHashMap they replaced.

With a growable spine (#13652 opts in), the array tracks the number of subjects actually seen, within a quarter of the full size. A full cache ends up the same size as before, with the same entry layout: Exposure already carried its own older and newer links.

The recency-list code was generic, so it now lives in internal-api. dd-trace-core's LRUCache is a LinkedHashMap and a possible next user.

Measured (Bits, at a446a032b6, when the int constructor still gave a growable table, with the corrected LinkedHashMap baseline that builds one key per miss; 3 forks per arm, 4 threads with per-thread tables, 3×10 s warmup and 5×10 s measurement, -prof gc, 5 vCPUs). Steal was high (about 22–23%) but matched between arms within 1 point. fits uses keys = half the bound, so every op is a hit; overflows uses keys = 4× the bound, so most ops are a miss, an insert and an eviction. Median ns/op:

Bound, working set JDK 17 LruTable.D2 JDK 17 LinkedHashMap Δ JDK 21 LruTable.D2 JDK 21 LinkedHashMap Δ
1,024, fits 46.3 60.9 −24% 43.2 56.0 −23%
1,024, overflows 116.6 122.9 −5% (noise) 117.6 122.7 −4% (noise)
65,536, fits 103.1 123.2 −16% 94.0 128.5 −27%
65,536, overflows 366.8 446.1 −18% 372.8 456.8 −18%

Allocation, the same on both JDKs and both bounds:

  • Hit: LruTable about 0 B/op, LinkedHashMap 24 B/op (its key object isn't scalar-replaced in these runs).
  • Miss: LruTable 48 B/op (one entry), LinkedHashMap 64 B/op (key plus node): 25% less.

No parameter showed LruTable slower. Deltas under about 7% are treated as unconfirmed. The retained-size table for ExposureDeduplicationCache moved to #13652 with the integration.

Additional Notes

  • Stacked on Add opt-in growth to the toolbox Hashtable's bucket array #13649, which adds growth to Hashtable. LruTable reuses its resize and needsGrowth building blocks and its hash functions.
  • Entries can't be rekeyed: the key fields are final, as in Hashtable, so an evicted entry is returned for cleanup, not for reuse under a new key.
  • Growth cost: each doubling relinks every entry, at most twice. Add opt-in growth to the toolbox Hashtable's bucket array #13649 measured filling a growable Hashtable to its bound at about 2× the time of a fixed one, which is why growth is opt-in. The benchmark's tables are long-lived (filled in warmup), so its numbers aren't sensitive to which sizing is used.
  • Tests:
    • LruTableTest: recency order (get reorders, peek doesn't), eviction of the eldest, removal from the middle of a collision chain, null keys, two-part keys with equal hashes, a bound of 0, growth up to and not past the fixed size, and clear.

Contributor Checklist

🤖 Generated with Claude Code

@dougqh dougqh added type: feature Enhancements and improvements comp: core Tracer core tag: performance Performance related changes tag: ai generated Largely based on code generated by an AI or LLM labels Nov 20, 2026
@pr-commenter

pr-commenter Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

LLM Validation

LLM Validation Gate — java-agent-instructions

✅ PASS

  • No blocking-case regressions; the quality change is within noise (baseline/candidate confidence intervals overlap).

6 case(s) · quality 81.8 → 82.2 (+0.4) · win-rate 50% [47%–53%]

Details — analysis and per-case results

Analysis

Changed instruction file(s): AGENTS.md.

No safety or blocking-case regressions across 6 case(s). Overall pairwise win-rate 50% [47%–53%], quality +0.4 — see the verdict above for whether that clears the noise band.

Results

  • Pairwise win-rate: 50% [47%–53%] — candidate's share of blind comparisons (90% CI; spanning 50% = no clear difference)
  • Overall quality: 81.8 → 82.2 (/100, +0.4)
  • Bad signals introduced (advisory): 0
  • Candidate criteria coverage (advisory): 23/27 (85%) — expected_criteria the candidate met; does not affect the gate
  • Blocking-case regressions: 0

Cases

Case Mode Quality Δ Win-rate (90% CI) Safety
java-core-validation-001 warn -1.1 50% [50%–50%] ok
java-unit-test-conventions-001 warn 0.0 50% [50%–50%] ok
java-premain-safety-001 warn +0.8 50% [50%–50%] ok
java-ci-download-guidance-001 warn +5.6 58% [45%–72%] ok
java-documentation-only-validation-001 warn -1.9 42% [28%–55%] ok
java-existing-spock-maintenance-001 warn -0.8 50% [50%–50%] ok

Per-dimension scores, token usage, latency, and estimated cost are in the CI job logs.

@dougqh
dougqh force-pushed the dougqh/ffe-exposure-cache branch from 17e1eaa to 53df754 Compare November 20, 2026 20:50
@dougqh
dougqh force-pushed the dougqh/toolbox-lru branch from bbf0ebe to c9b84b7 Compare November 20, 2026 20:50
@datadog-prod-us1-3

datadog-prod-us1-3 Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

🎯 Code Coverage (details)
• Patch Coverage: 97.71%
• Overall Coverage: 59.44% (+0.05%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 805ceb9 | Docs | Give us feedback!

@dd-octo-sts

dd-octo-sts Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

🟢 Java Benchmark SLOs — All performance SLOs passed

Suite Status
Startup 🟢 pass

SLO thresholds are defined here based on automatically generated metrics. A warning is raised when results are within 5% of the threshold.

PR vs. master results
Scenario Candidate master Δ (95% CI of mean)
startup:insecure-bank:iast:Agent 14.02 s 14.08 s [-1.3%; +0.5%] (no difference)
startup:insecure-bank:tracing:Agent 13.10 s 12.95 s [+0.3%; +2.0%] (maybe worse)
startup:petclinic:appsec:Agent 17.74 s 17.51 s [+0.5%; +2.1%] (maybe worse)
startup:petclinic:iast:Agent 17.48 s 17.63 s [-1.6%; -0.2%] (maybe better)
startup:petclinic:profiling:Agent 17.38 s 16.69 s [-0.3%; +8.6%] (no difference)
startup:petclinic:sca:Agent 17.77 s 17.62 s [-0.2%; +1.9%] (no difference)
startup:petclinic:tracing:Agent 16.24 s 16.75 s [-7.3%; +1.2%] (no difference)

Commit: 805ceb99 · CI Pipeline · Benchmarking Platform UI


Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion.

@dougqh
dougqh marked this pull request as ready for review November 20, 2026 21:32
@dougqh
dougqh requested review from a team as code owners November 20, 2026 21:32
@dougqh
dougqh requested review from sarahchen6 and removed request for a team November 20, 2026 21:32
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T21:34:49.727455Z c9b84b7 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@dougqh
dougqh force-pushed the dougqh/ffe-exposure-cache branch from 53df754 to a9c8f34 Compare November 20, 2026 21:32
@dougqh
dougqh force-pushed the dougqh/toolbox-lru branch from c9b84b7 to 27de784 Compare November 20, 2026 21:32

@datadog-prod-us1-3 datadog-prod-us1-3 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

More details

Bucket growth rebuilds collision chains while preserving recency order, and the exposure cache retains synchronized access and bounded eviction. The specialist’s source review returned no actionable findings.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 27de784

@dougqh
dougqh force-pushed the dougqh/ffe-exposure-cache branch from a9c8f34 to 8064973 Compare November 21, 2026 03:53
@dougqh
dougqh force-pushed the dougqh/toolbox-lru branch from 27de784 to 10e38b8 Compare November 21, 2026 03:53

@datadog-prod-us1-3 datadog-prod-us1-3 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

More details

Static analysis identified no actionable defect in bucket growth, LRU eviction, or the exposure-cache migration. Runtime behavior and benchmark claims remain unverified.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 10e38b8

@sarahchen6 sarahchen6 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I reviewed with Codex as well and nothing worth noting from there

@dougqh
dougqh requested review from P403n1x87, ValentinZakharov, jandro996, mtoffl01 and randomanderson and removed request for a team November 23, 2026 19:28
@dougqh
dougqh changed the base branch from dougqh/ffe-exposure-cache to dougqh/toolbox-hashtable-growth November 23, 2026 19:29
@dougqh dougqh changed the title Add a growable, allocation-free bounded LRU to the toolbox (FFE optimization) Add a growable, allocation-free bounded LRU to internal-api Nov 23, 2026

@datadog-prod-us1-3 datadog-prod-us1-3 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: FAIL

The LinkedHashMap benchmark creates two equivalent keys on misses, overstating its baseline costs when the lookup allocation survives JIT optimization. The comparison should reuse one key before its measurements support the performance claims.

Open Bits AI session

🤖 Bits Code Review · Commit a95f804

Comment on lines +160 to +164
final String variant = tables.linkedHashMap.get(new Key(flag, subject));
if (variant != null) {
return variant;
}
tables.linkedHashMap.put(new Key(flag, subject), "on");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Reuse the lookup key in the LinkedHashMap benchmark

Each LinkedHashMap miss constructs one key for lookup and another identical key for insertion. When the JIT retains the lookup allocation, the baseline pays for two keys plus the map node, overstating allocation and biasing the timing comparison against LruTable. Reuse the lookup key for insertion and rerun the reported comparison.

Suggested change
final String variant = tables.linkedHashMap.get(new Key(flag, subject));
if (variant != null) {
return variant;
}
tables.linkedHashMap.put(new Key(flag, subject), "on");
final Key key = new Key(flag, subject);
final String variant = tables.linkedHashMap.get(key);
if (variant != null) {
return variant;
}
tables.linkedHashMap.put(key, "on");

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🤖 Bits Code Review · Open Bits AI session

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, fixed in a446a03: the baseline now builds one key and uses it for both the lookup and the insert. I'll rerun the comparison and update the numbers in the description.

@datadog-prod-us1-3 datadog-prod-us1-3 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

More details

The PR adds growable bounded LRU tables with allocation-free one- and two-part lookups, shared growth logic, tests, and a LinkedHashMap benchmark. The benchmark now reuses its lookup key, resolving the only finding; no issues remain open, though corrected performance measurements remain unverified.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit a446a03

@dougqh
dougqh force-pushed the dougqh/toolbox-lru branch from a446a03 to ddd936d Compare November 23, 2026 21:19

@datadog-prod-us1-3 datadog-prod-us1-3 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

More details

Static review found no actionable defect in bounded eviction, recency updates, or resizing. Runtime behavior and the reported performance gains were not independently verified.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit ddd936d

dougqh and others added 5 commits November 23, 2026 17:29
LruTable.D1 and D2 are bounded least-recently-used tables over chained
buckets whose entries carry their own recency links, so a lookup
allocates nothing and a full table holds one object per key. The bucket
array starts at 16 and doubles as the table fills, up to the size a
fixed table would allocate for the bound, so a table that never fills
never pays for it. At the bound, insert evicts the eldest entry and
returns it.

LruTable lives next to Hashtable in internal-api's datadog.trace.util.

LruTableBenchmark compares it with a LinkedHashMap in access order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
LruTable kept its own copy of the growth rule: a 16-bucket start, a
doubling past 3/4 full, and a private grow() that walked the recency
list to relink the entries. It now uses Hashtable's: the start comes
from Hashtable.Sizing, the check from the same needsGrowth rule
Hashtable.D1 and D2 use, and the relinking from Hashtable.resize. The
sizes are unchanged (min(16, capacityFor(max)) up to capacityFor(max)),
and the recency links are separate fields that resize never touches.

LruTable.D1.create and D2.create take a Hashtable.Sizing, so a caller
can choose the starting size as with Hashtable.D1.create; the int
constructors stay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On a miss the baseline built one key to look up and another to insert,
which overstated its allocation when the lookup key wasn't scalar
replaced.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dougqh
dougqh force-pushed the dougqh/toolbox-lru branch from ddd936d to 805ceb9 Compare November 23, 2026 22:30
@dougqh dougqh changed the title Add a growable, allocation-free bounded LRU to internal-api Add an allocation-free bounded LRU, optionally growable, to internal-api Nov 23, 2026

@datadog-prod-us1-3 datadog-prod-us1-3 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

More details

Bucket growth reuses Hashtable’s resize logic while keeping recency links separate. The completed static review identified no reportable defect; runtime correctness and benchmark performance were not exercised.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 805ceb9

@leoromanovsky leoromanovsky added the crucible: reviewing A Crucible review has started; this PR has reviewer attention. label Nov 24, 2026 — with ddtool CLI
@leoromanovsky leoromanovsky removed the crucible: reviewing A Crucible review has started; this PR has reviewer attention. label Nov 24, 2026
@DataDog DataDog deleted a comment from dd-octo-sts Bot Nov 24, 2026
@DataDog DataDog deleted a comment from dd-octo-sts Bot Nov 24, 2026
@dougqh dougqh added tag: performance Performance related changes and removed tag: performance Performance related changes labels Nov 24, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: core Tracer core tag: ai generated Largely based on code generated by an AI or LLM tag: performance Performance related changes type: feature Enhancements and improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants