-
-
Notifications
You must be signed in to change notification settings - Fork 161
perf(regex): content-keyed construction cache, header-authoritative program lookups, find-only global test #9764
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
Closed
proggeramlug
wants to merge
1
commit into
PerryTS:main
from
proggeramlug:perf/keystroke-regex-site-cache
Closed
Changes from all commits
Commits
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
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,43 @@ | ||
| ### Performance | ||
|
|
||
| - **RegExp construction and exec no longer hash or copy the pattern text.** | ||
| On the claude-code TUI a keystroke re-runs ink's layout, whose text | ||
| measurement (`string-width` / `emoji-regex` / `ansi-regex`) evaluates a | ||
| regex literal per text segment — `emojiRegex()` is a fresh ~12 KB `/…/g` | ||
| per call. Each `js_regexp_new` copied that pattern three times and | ||
| SipHashed it once (the `VALIDATED_PATTERNS` probe key, `owned_pattern`, | ||
| the `REGEX_SOURCE_TABLE` entry); the first operation on each header did | ||
| the same three more times in `build_and_install_programs`; and, for the | ||
| common pattern with no fancy fallback, `lookup_fancy_regex` and | ||
| `lookup_repeat_matcher` fell through to a full clone + hash of the pattern | ||
| on EVERY exec. SipHash over pattern text was 31 % of the main thread in | ||
| the 20 s after a 400-char reply had rendered (regex 38 % inclusive). | ||
|
|
||
| - `crates/perry-runtime/src/regex/site_cache.rs` (new) — a thread-local, | ||
| content-keyed construction cache: a cheap fingerprint (length, three | ||
| 8-byte windows, canonical flags) plus a full byte compare, so identity | ||
| never depends on an address. A hit skips validation (validity is a pure | ||
| function of the pair), shares the owned pattern/flags as `Arc<str>`, and | ||
| installs the programs the first executed header compiled — the new | ||
| header is born built and never touches the `(pattern, flags)` caches. | ||
| Kill switch `PERRY_REGEX_SITE_CACHE=0`. | ||
| - `regex.rs` — `lookup_fancy_regex` / `lookup_repeat_matcher` treat a | ||
| built header as authoritative (a null program pointer after the build | ||
| IS the answer; every install path publishes all three together), so no | ||
| per-exec cache probe remains. `REGEX_SOURCE_TABLE` holds `Arc<str>` | ||
| pairs; the two address-keyed regex tables use the pointer hasher. | ||
| - `regex/exec.rs` — `test` on a global/sticky receiver runs | ||
| `regexp_find_advancing`, the find-only twin of `exec`'s engine phase | ||
| (same engine order, `lastIndex` advance/reset and sticky anchoring), | ||
| instead of materializing a captures array plus one string per capture | ||
| that it then discarded. | ||
| - `hot_diag.rs` (new) — `PERRY_REGEX_DIAG=<path>` (constructions, | ||
| validated/site hits, pattern bytes, compiles, cache clears, lazy builds, | ||
| exec/test/match/replace counts, capture bytes, per-pattern table) and | ||
| `PERRY_IC_DIAG=<path>` (property-read IC misses by reason and by site). | ||
| Snapshots every ~1 s of activity; diagnostic only. | ||
|
|
||
| Tests: `site_cache_reconstruction_is_born_built` (fails without the | ||
| cache) and `global_test_advances_and_resets_last_index` (every | ||
| `lastIndex` branch of the find-only path, all three engines, UTF-16 | ||
| units) in `regex/tests.rs`. | ||
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Limit the no-hash/no-copy claim to cache hits.
js_regexp_compile_valuestill creates and hashes(pattern_str.to_string(), flags_str.to_string())keys atcrates/perry-runtime/src/regex/compile.rs, Lines 153-171. The first-use builder also creates those keys atcrates/perry-runtime/src/regex/lazy.rs, Lines 233-257. This statement is too broad for allRegExpconstruction. State that the site-cache hit path avoids this work.🤖 Prompt for AI Agents