-
-
Notifications
You must be signed in to change notification settings - Fork 158
perf(shapes): direct-mapped cache in front of the shape-descriptor table #8917
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
+342
−136
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,46 @@ | ||
| Added a direct-mapped cache in front of the shape-descriptor table. | ||
|
|
||
| `shape_descriptor_by_id` is on the hot property path. Profiling a dynamic | ||
| string-keyed property loop put it and `shape_descriptor_ensure_with_generation` | ||
| at roughly **13% of main-thread samples between them**, and every call paid a | ||
| TLS fetch, a `RefCell` borrow and a hash probe — to reach a record whose address | ||
| never moves. `Box<ShapeDescriptor>` is stable across rehash, so a 256-way | ||
| direct-mapped cache can hold the record's address and a hit becomes mask, | ||
| compare, deref. 4 KiB per thread, fixed. | ||
|
|
||
| Two decisions that matter for correctness: | ||
|
|
||
| **It caches the record's ADDRESS, not a copy of the descriptor.** Records are | ||
| mutated in place — `old_carrier`, `cache_carrier`, and `keys` after evacuation — | ||
| so a cached copy would go quietly stale. Holding the address means a hit always | ||
| reads current data. | ||
|
|
||
| **Epoch invalidation is selective.** Removal frees the box, and one insert path | ||
| can replace a live id with a fresh box; both bump the epoch. A *fresh-id* insert | ||
| deliberately does not, because it cannot invalidate an existing way, and bumping | ||
| there would flush the cache on every shape creation — exactly the workloads that | ||
| build shapes. | ||
|
|
||
| Measured with `benchmarks/bench_dynamic_property_keys.ts` on an idle host, | ||
| minimum of 7 runs: | ||
|
|
||
| | | delete-heavy | overwrite-only | | ||
| |---|---:|---:| | ||
| | node | 31 ms | 19 ms | | ||
| | before | 1222 ms | 1088 ms | | ||
| | after | **1187 ms** | **955 ms** | | ||
|
|
||
| Baseline dynamic property throughput improves **12.2%**. That is well short of | ||
| the 13% of samples the two shape functions hold, because only | ||
| `shape_descriptor_by_id` is served from the cache; | ||
| `shape_descriptor_ensure_with_generation` is a separate path and still probes. | ||
|
|
||
| This does not close the gap to node — perry is still ~50x on this loop. The | ||
| remaining weight sits in `js_array_get_f64` (324 samples) and | ||
| `value::addr_class::try_read_tracked_gc_header` (307), which are the next | ||
| targets and are not touched here. | ||
|
|
||
| The invalidation test is sabotage-checked: deleting the bump in | ||
| `remove_descriptor_and_reverse_indices` fails it. That check matters more than | ||
| usual here — a stale way is not a wrong answer, it is a dangling pointer to a | ||
| dropped `Box<ShapeDescriptor>` reached from the hot property path. |
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
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.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 22109
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 22109
Other (CWE-416): Use After Free
Exploitability: Difficult
Clear cache ways before
lookup_epochwraps.wrapping_addcan restore a stale way's epoch after2^32invalidations. If the way still contains a removed descriptor address,shape_descriptor_by_idcan dereference the freedBox<ShapeDescriptor>. Clear all ways and restart at a nonzero epoch when the counter reachesu32::MAX.🤖 Prompt for AI Agents