-
-
Notifications
You must be signed in to change notification settings - Fork 159
perf(runtime): the receiver own-key probe stops scanning the keys array element by element #9190
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
+345
−14
Merged
Changes from all commits
Commits
Show all changes
2 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,12 @@ | ||
| The `[[Set]]` receiver own-key probe walked the keys array one element at a | ||
| time through `js_array_get_f64`, the JS-facing element accessor. That accessor | ||
| runs the whole gauntlet per element — forward resolution, Map/Set/typed-array/ | ||
| buffer registry probes, the descriptor gate, hole translation — for what is a | ||
| raw slot read, and the probe repeated it for every key on every store. At 400 | ||
| stores that was 163,200 element reads. | ||
|
|
||
| The probe now reads the backing storage directly. A test-only entry counter on | ||
| `js_array_get_f64` lets the regression test assert the walk no longer reaches | ||
| for the element accessor at all (163,200 → 0) rather than timing it, which is | ||
| the difference between a test that pins the property and one that pins the | ||
| machine it was measured on. |
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
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
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
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,101 @@ | ||
| //! The receiver `[[Set]]` own-key probe (#9180), split out of `tests.rs` to | ||
| //! keep it under the 2000-line cap. | ||
|
|
||
| use super::*; | ||
|
|
||
| /// #9180: the receiver-based `[[Set]]` walk's "does the receiver already own | ||
| /// this key" probe must consult the shared key index, not walk the keys array | ||
| /// one `js_array_get` at a time. | ||
| /// | ||
| /// The chain is `js_put_value_set_dyn_ic_miss` -> `ordinary_set_with_receiver` | ||
| /// -> `create_or_update_receiver_property` -> `own_set_descriptor` -> | ||
| /// `obj_value_has_own_key`, reached by every store the #5054 direct-store lane | ||
| /// declines — one `Object.defineProperty` on the receiver is enough, which is | ||
| /// the esbuild CJS-namespace shape. The old loop re-read every already-installed | ||
| /// key through the JS-facing element accessor plus a handle round-trip, so | ||
| /// building an object property-by-property was quadratic: measured 163 200 | ||
| /// element reads for 400 stores, versus 0 now. | ||
| /// | ||
| /// Counted, not timed: `test_element_accessor_calls` is the entry counter on | ||
| /// `js_array_get_f64` itself. The assertions below cover BOTH index tiers — | ||
| /// under `KEYS_INDEX_THRESHOLD` (raw dense-slot compare) and over it (the O(1) | ||
| /// shape index) — and the two ways the index declines to answer: a delete that | ||
| /// shrinks it back to `Unindexed`, and the `Absent` completeness verdict for a | ||
| /// key that was never installed. | ||
| #[test] | ||
| fn has_own_key_probe_never_uses_the_element_accessor() { | ||
| { | ||
| let obj = js_object_alloc(0, 0); | ||
| let obj_value = crate::value::js_nanbox_pointer(obj as i64); | ||
|
|
||
| let key_value = |name: &str| { | ||
| let s = crate::string::js_string_from_bytes(name.as_ptr(), name.len() as u32); | ||
| crate::value::js_nanbox_string(s as i64) | ||
| }; | ||
| let set = |name: &str, v: f64| { | ||
| let s = crate::string::js_string_from_bytes(name.as_ptr(), name.len() as u32); | ||
| js_object_set_field_by_name(obj, s, v); | ||
| }; | ||
|
|
||
| // --- below KEYS_INDEX_THRESHOLD (32): the dense raw-slot compare. | ||
| for i in 0..8u32 { | ||
| set(&format!("k{i}"), f64::from(i)); | ||
| } | ||
| let before = crate::array::test_element_accessor_calls(); | ||
| assert!(obj_value_has_own_key(obj_value, key_value("k0"))); | ||
| assert!(obj_value_has_own_key(obj_value, key_value("k7"))); | ||
| assert!(!obj_value_has_own_key(obj_value, key_value("nope"))); | ||
| assert_eq!( | ||
| crate::array::test_element_accessor_calls(), | ||
| before, | ||
| "the small-object tier must answer from the dense slots, with no \ | ||
| `js_array_get_f64` call at all" | ||
| ); | ||
|
|
||
| // --- across the threshold: the O(1) shape index. | ||
| for i in 8..48u32 { | ||
| set(&format!("k{i}"), f64::from(i)); | ||
| } | ||
| let before = crate::array::test_element_accessor_calls(); | ||
| assert!(obj_value_has_own_key(obj_value, key_value("k0"))); | ||
| assert!(obj_value_has_own_key(obj_value, key_value("k31"))); | ||
| assert!(obj_value_has_own_key(obj_value, key_value("k47"))); | ||
| // The `Absent` verdict — a complete index proves a key is missing | ||
| // without any scan at all. This is the arm a wrong answer here would | ||
| // turn into a silently duplicated property. | ||
| assert!(!obj_value_has_own_key(obj_value, key_value("k48"))); | ||
| assert!(!obj_value_has_own_key(obj_value, key_value(""))); | ||
| assert_eq!( | ||
| crate::array::test_element_accessor_calls(), | ||
| before, | ||
| "the wide tier must answer from the shape index, not a per-element \ | ||
| `js_array_get_f64` walk" | ||
| ); | ||
|
|
||
| // --- the index-declines arm: a delete shrinks the keys array, so the | ||
| // next probe must fall back rather than trust a stale `Absent`, and a | ||
| // re-add must be found again. | ||
| let k20 = crate::string::js_string_from_bytes(b"k20".as_ptr(), 3); | ||
| crate::object::js_object_delete_field(obj, k20); | ||
| assert!( | ||
| !obj_value_has_own_key(obj_value, key_value("k20")), | ||
| "a deleted key is not an own key" | ||
| ); | ||
| assert!( | ||
| obj_value_has_own_key(obj_value, key_value("k21")), | ||
| "a delete must not lose its neighbours" | ||
| ); | ||
| set("k20", 2020.0); | ||
| let before = crate::array::test_element_accessor_calls(); | ||
| assert!( | ||
| obj_value_has_own_key(obj_value, key_value("k20")), | ||
| "a re-added key must be found again after the index was dropped" | ||
| ); | ||
| assert!(!obj_value_has_own_key(obj_value, key_value("k48"))); | ||
| assert_eq!( | ||
| crate::array::test_element_accessor_calls(), | ||
| before, | ||
| "post-delete re-entry must still stay off the element accessor" | ||
| ); | ||
| } | ||
| } | ||
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.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Keep the receiver pointer valid across allocations.
setcaptures rawobjand callsjs_string_from_bytesbefore it uses that pointer. Lines 79-80 repeat this pattern. If allocation evacuates the receiver, these calls use a stale pointer and can crash or corrupt this GC-sensitive test. Store the receiver as its NaN-boxed value, then recover or root the current pointer after each allocation.As per coding guidelines: “Captured string/pointer values must be NaN-boxed before storing, not raw bitcast.”
Also applies to: 80-80
🤖 Prompt for AI Agents
Source: Coding guidelines