Skip to content

Soft-hold shared NFA DFA bundles to bound cache retention - #140

Merged
jbachorik merged 2 commits into
mainfrom
fix/softref-dfa-bundles
Oct 2, 2026
Merged

jbachorik merged 2 commits into
mainfrom
fix/softref-dfa-bundles

Conversation

@jbachorik

Copy link
Copy Markdown
Collaborator

What does this PR do?

Bounds the retention of the NFA-derived DFA bundles shared per cached pattern: PikeVMEntry.dfaBundle, BitStateEntry.rejectBundle, and the hybrid nfa-half bundles are now held through SoftReference (deterministic rebuild after eviction) instead of strong fields. Also shares the hybrid nfa-half bundles across compiles (previously one private bundle per compile() call) and pins the reject-bundle negative result (NFAs where RejectDfaFactory.build returns null no longer re-run the O(states) scan per matcher).

Motivation

Review comment on #139: each bundle eagerly allocates fixed-capacity LazyDFACache backing arrays (~40–70 KiB per cache before population, several MiB once populated), and the long-lived PIKEVM_NFA_CACHE/BITSTATE_NFA_CACHE retained one strongly per cached pattern — a high-cardinality pattern workload could exhaust the heap, invisible to cacheSize() (which only counts PATTERN_CACHE). Soft retention keeps the sharing benefit while memory is plentiful and lets the GC evict under pressure.

Related Issue(s)

Follow-up to #139 (review thread on RuntimeCompiler.java).

Change Type

  • Bug fix
  • Performance improvement
  • Test improvement

Checklist

  • I have read the CONTRIBUTING.md guidelines
  • All existing tests pass (./gradlew build)
  • I have added tests for my changes
  • I have updated documentation (if applicable)
  • My commits are signed

Performance Impact

Benchmarks on workspace-jb (x86_64, 16 cores, Zulu 26), HEAD vs this change:

Compile per op (min of 5×50k, ns/op):

path before after Δ
pikevm (PikeVMEntry) 2194 2196 +0.1%
bitstate (BitStateEntry) 799 816 +2%
hybrid-bitstate (HybridEntry) 22442 18197 −19%
hybrid-pikevm (HybridEntry) 10773 2115 −80%

Match time (JMH AllStrategyVsJdkBenchmark, pikevmCapture/lazyDfa, all scales): unchanged within error. Match is unaffected by design — matchers copy bundle fields into their own final state, so eviction never reaches a live matcher.

Additional Notes

  • Eviction/rebuild is pinned by two new tests in SharedDfaBundleTest (\b(a?)+x → PIKEVM_CAPTURE, \b(a)+x → BITSTATE_CAPTURE; both install a cleared SoftReference into the real cache entry and assert correct rebuild + matching).
  • Rebuild safety: bundle builders are pure functions of the NFA (the pre-existing "concurrent first-use race yields equivalent bundles" contract still holds), and volatile publication is unchanged.
  • The reviewer-noted cacheSize() blind spot is documented in the entry comments; extending cacheSize() semantics is deliberately out of scope here.

PikeVMEntry/BitStateEntry/HybridEntry keep their shared DfaBundle /
reject bundle behind a SoftReference: fixed-capacity LazyDFACache arrays
per cached pattern (~40-70 KiB, several MiB populated) previously piled
up unbounded in PIKEVM_NFA_CACHE/BITSTATE_NFA_CACHE. Bundles rebuild
deterministically after eviction; live matchers are unaffected (they
copy bundle fields). Hybrid nfa-half bundles are now shared across
compiles (-80% compile time for hybrid-pikevm patterns); ineligible
reject-DFA NFAs skip the per-matcher rebuild scan.
@jbachorik jbachorik added the AI Generated or assisted by AI label Oct 2, 2026
@jbachorik
jbachorik marked this pull request as ready for review October 2, 2026 10:45
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-02T10:48:35.182537Z 0fd855a 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.

@codecov-commenter

codecov-commenter commented Oct 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.9%. Comparing base (967e525) to head (c6ddd1a).

Additional details and impacted files
@@           Coverage Diff           @@
##              main    #140   +/-   ##
=======================================
  Coverage     84.9%   84.9%           
  Complexity       1       1           
=======================================
  Files          163     163           
  Lines        48981   49002   +21     
  Branches      7000    7009    +9     
=======================================
+ Hits         41594   41613   +19     
- Misses        5378    5379    +1     
- Partials      2009    2010    +1     
Files with missing lines Coverage Δ
.../com/datadoghq/reggie/runtime/BitStateMatcher.java 95.2% <100.0%> (+<0.1%) ⬆️
...va/com/datadoghq/reggie/runtime/PikeVMMatcher.java 89.7% <100.0%> (+<0.1%) ⬆️
...com/datadoghq/reggie/runtime/RejectDfaFactory.java 97.5% <100.0%> (+<0.1%) ⬆️
.../com/datadoghq/reggie/runtime/RuntimeCompiler.java 86.3% <100.0%> (+0.1%) ⬆️

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 967e525...c6ddd1a. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0fd855a62e

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread reggie-runtime/src/main/java/com/datadoghq/reggie/runtime/RuntimeCompiler.java Outdated

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 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

For BITSTATE_CAPTURE patterns whose reject over-approximation accepts empty, the new ineligibility flag does not prevent matcher construction from rebuilding the reject DFA, so each compile still incurs the scan this change intends to eliminate.

Open Bits AI session

🤖 Bits Code Review · Commit 0fd855a · @DataDog review to ask questions

…E sentinel

Matchers retain the shared bundle they were built from, so the entry's
SoftReference cannot be GC-cleared while live matchers still pin its
caches (a cleared ref would allocate a duplicate bundle over caches the
matchers already hold). RejectDfaFactory.NONE marks a known-ineligible
NFA so BitStateMatcher skips its matcher-private build retry - the
O(states) ineligibility scan now runs once per cache entry, not once per
matcher.
@jbachorik
jbachorik merged commit 2ad23b7 into main Oct 2, 2026
9 checks passed
@jbachorik
jbachorik deleted the fix/softref-dfa-bundles branch October 2, 2026 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI Generated or assisted by AI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants