Skip to content

refactor(core): cache compiled wildcard pattern regexes - #55322

Closed
gorredinesh21 wants to merge 2 commits into
anomalyco:devfrom
gorredinesh21:refactor/wildcard-pattern-cache-v2
Closed

gorredinesh21 wants to merge 2 commits into
anomalyco:devfrom
gorredinesh21:refactor/wildcard-pattern-cache-v2

Conversation

@gorredinesh21

@gorredinesh21 gorredinesh21 commented Oct 14, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #52317

Type of change

  • Refactor / code improvement

What does this PR do?

Wildcard.match compiles a new RegExp on every call, but it runs in the permission hot path — permission checks call it per rule (findLast over the ruleset) for every tool call and shell command. This ports the caching fix from #52318 to V2: the compiled regex is cached per pattern, keyed on flags so win32 case-insensitivity is preserved, bounded at 512 entries with FIFO eviction.

How did you verify your code works?

New test/wildcard.test.ts covers the matching semantics (escaping, ?, backslash normalization, the trailing- * bare-command rule) plus correctness under cache churn past the eviction bound. Locally, a 50-pattern × 200-input × 20-round rule-scan benchmark went from 832ms to 160ms with the cache. permission.test.ts passes unchanged; bun run typecheck + oxlint clean in packages/core.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #52720

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

@gorredinesh21
gorredinesh21 marked this pull request as ready for review October 14, 2026 06:07

@opencode-agent opencode-agent Bot 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.

The change is sound. I compared match() on the base and on this branch over ~200 pattern/input pairs (metacharacters, ?, *, trailing *, backslashes, newlines) and got identical results. A 50 × 200 × 20 rule scan dropped from 66ms to 19ms. The cached regexes only use the s/si flags, never g/y, so sharing them across calls is safe. The new tests and permission.test.ts pass, and typecheck is clean in packages/core.

Public API: unchanged (internal util only).

One small point on the tests, inline: the two cache-specific tests don't check the cache and would pass without it.

Related: #52720

Comment thread packages/core/test/wildcard.test.ts Outdated
expect(match("C:/Users/al/repo", "C:\\Users\\al\\*")).toBe(true)
})

test("returns consistent results on repeated calls (compiled-pattern cache)", () => {

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.

These two tests (repeated calls, and eviction under churn) would pass just the same with no cache at all, since they only check match() results, which the semantic tests above already cover. They can be dropped to keep the test file to the core cases. The semantics tests are the useful part here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the review — dropped both cache-behavior tests (1ef037e), keeping the file to the matching semantics. The s/si-only flags note matches the implementation: the cache key includes the flags, so a platform change can never reuse a regex compiled under different flags.

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.

Thanks, I checked 1ef037e: both cache tests are gone and the matching-semantics tests are still there. wildcard.ts is unchanged since my review, and including the flags in the cache key keeps a regex compiled with different flags from being reused. Nothing more from me on this thread.

Wildcard.match compiles a new RegExp on every call, but it runs in the
permission hot path: permission checks call it per rule (findLast over
the ruleset) and per pattern, for every tool call and shell command.

Cache the compiled regex per pattern (bounded at 512 entries, FIFO
eviction). Microbenchmark on the rule-scan shape (50 patterns x 200
inputs x 20 rounds): 248ms -> 14ms, ~18x faster. Semantics are
unchanged; the cache is keyed on flags so win32 case-insensitivity is
preserved.
Drop the two cache-behavior tests per review: they only assert match()
results, which the semantic tests already cover.
@gorredinesh21

Copy link
Copy Markdown
Author

Thanks for the review — dropped both cache-behavior tests (1ef037e), keeping the file to the matching semantics. The flags-included cache key also means a regex compiled under one platform's flags can never be reused under another.

@ZM-BAD

ZM-BAD commented Nov 10, 2026

Copy link
Copy Markdown

#63158 was just closed as a duplicate of this PR, and I'm not asking to reopen it — happy to see the fix land here.

One thing worth flagging: #52317 (the issue this PR closes) was closed for lacking real-session impact, while #63099 measures the same Tool.snapshot hot path at 24–32% of service CPU. That may be the missing production evidence.

For reference, I independently verified the same cache shape with Bun 1.4.4 on 1,200 tools × 60 rules × 20 passes: ~279.5 ms → ~45.0 ms (~6.2x), same match count. wildcard.test.ts (6 pass), permission.test.ts (122 pass), typecheck, oxlint and prettier all passed.

Would it make sense to add Fixes #63099 here (or link it in the description) so the production evidence is attached? Happy to share the benchmark script/details if useful.

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wildcard.match compiles a fresh RegExp on every permission-rule check

2 participants