Repository navigation
perf(runtime): inline the single-slot case in the shape key index (populated delete −13.9%) #9001
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
Changes from all commits
87c021c
a8f7b7f
638de0e
3cade1c
45c10c3
40bdb23
3dd168a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| ### Fixed | ||
|
|
||
| - Dynamic indexed reads now inspect `ObjectMeta.elements` only for receivers whose GC kind is an object. Array, string, Error, and other layouts can no longer be interpreted as an object metadata pointer, eliminating the release parity crashes introduced with Array-subclass elements storage. | ||
| - Managed heap cells are classified as closures only when their authoritative GC kind is `GC_TYPE_CLOSURE`. Reused `Error` storage whose padding retained the closure magic marker no longer loses `.message` or custom fields during property lookup. | ||
| - Removed the stale Linux parity allowance for `test_class_field_layout`, which now matches Node, and recorded the timezone-provider dependency added to the runtime in `Cargo.lock`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| Two costs removed from the populated-delete path — perry's worst object-model | ||
| gap against node (`bench_populated_delete.ts`, ~280×). | ||
|
|
||
| **1. The shape key index no longer hashes a hash.** `ShapeIndex::slots` was a | ||
| std `HashMap<u64, Vec<u32>>` whose key is ALREADY an FNV-1a content hash, so | ||
| every probe ran SipHash over it — no added distribution, real time. | ||
| `hash_one::<&usize>` plus `sip::Hasher::write` were **14.7% of self time** in | ||
| that benchmark, second only to `shape_slot_lookup`, which is what performs the | ||
| probes. It now uses the `PtrHasher` the runtime's other id-keyed registries | ||
| already use. | ||
|
|
||
| **2. The keys-array clone is a memcpy.** Each delete allocates a fresh keys | ||
| array and previously copied the surviving keys one `f64` at a time; it is now | ||
| two `copy_nonoverlapping` runs. Safe by the code's own existing argument: the | ||
| destination is a freshly allocated, still-unpublished array whose layout is | ||
| rebuilt before publish — which is why the per-element writes carried no GC | ||
| barrier either — and the two allocations cannot overlap. | ||
|
|
||
| Interleaved A/B, min-of-15 at quiet load: 5938 → 5847 ms (−1.5% min, −2.2% | ||
| mean). Smaller than the profile share suggests, because the delete path is | ||
| dominated by allocator and page churn (`clear_page_erms` 5.6%, `mi_free` 4.2%, | ||
| `RawVecInner::finish_grow` 2.9%) rather than by hashing — the index is rebuilt | ||
| per delete, and each of its ~500 buckets is a separate `Vec<u32>`. That is the | ||
| next target and is untouched here. | ||
|
|
||
| Suite 2787 passed, 0 failed, no warnings. Adversarial property differential | ||
| (delete-then-reread, accessor over a cached slot, prototype fallback, freeze, | ||
| key-order shapes, overflow slots) is byte-identical to node. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| The shape key index stops allocating once per key. | ||
|
|
||
| `ShapeIndex::slots` mapped each content hash to a `Vec<u32>`, so every index | ||
| build made one heap allocation PER KEY — and the index is rebuilt on every | ||
| populated delete, so a 500-key object was making ~500 `Vec` allocations per | ||
| `delete`. Allocator and page churn dominates that benchmark (`clear_page_erms` | ||
| 5.6%, `mi_free` 4.2%, `RawVecInner::finish_grow` 2.9%), well above the lookup | ||
| work the index exists to do. | ||
|
|
||
| A bucket holds more than one slot only on a genuine FNV-1a collision between | ||
| two distinct property names, so the common case is exactly one. Store it | ||
| inline and promote to a `Vec` only when a collision actually occurs. | ||
|
|
||
| Interleaved A/B, min-of-15 at quiet load (~1.0): `bench_populated_delete` | ||
| **5810 → 5004 ms, −13.9%** (mean −13.9%, the two agree). The combined | ||
| overwrite loop and realistic-name read are unchanged, as expected for a change | ||
| confined to index construction. | ||
|
|
||
| Suite 2787 passed, no warnings. Adversarial property differential is | ||
| byte-identical to node. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,7 +30,10 @@ | |
|
|
||
| use crate::array::ArrayHeader; | ||
| use std::cell::RefCell; | ||
| use std::collections::HashMap; | ||
|
|
||
| #[path = "shapes_slot_list.rs"] | ||
| mod shapes_slot_list; | ||
| pub(crate) use shapes_slot_list::{record_shape_scan_outcome, SlotList}; | ||
|
|
||
| pub(crate) struct ShapeIndex { | ||
| /// Key count covered by `slots`. Longer live array ⟹ catch up | ||
|
|
@@ -39,7 +42,14 @@ pub(crate) struct ShapeIndex { | |
| indexed_len: u32, | ||
| /// FNV-1a content hash of key bytes → candidate slots (collisions | ||
| /// resolved by the per-hit content validation). | ||
| slots: HashMap<u64, Vec<u32>>, | ||
| /// | ||
| /// Keyed with [`crate::fast_hash::PtrHasher`], not the std default: the | ||
| /// key is ALREADY a well-distributed FNV-1a hash, so running SipHash over | ||
| /// it again buys no distribution and costs real time. On | ||
| /// `bench_populated_delete.ts` — perry's worst object-model gap against | ||
| /// node — `hash_one::<&usize>` plus `sip::Hasher::write` were **14.7% of | ||
| /// self time**, second only to the lookup that performs them. | ||
| slots: crate::fast_hash::PtrHashMap<u64, SlotList>, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- scoped repository knowledge ---'
find /tmp/coderabbit-repo-knowledge/perryts-perry-d4a878bc -maxdepth 2 -type f -name '*.md' -print \
| sort
printf '%s\n' '--- relevant source files ---'
git ls-files 'crates/perry-runtime/src/object/shapes.rs' \
'crates/perry-runtime/src/object/keys_lookup.rs' \
'crates/perry-runtime/src/fast_hash.rs'
printf '%s\n' '--- fast_hash.rs ---'
cat -n crates/perry-runtime/src/fast_hash.rs | sed -n '1,145p'
printf '%s\n' '--- shapes.rs declarations and index methods ---'
cat -n crates/perry-runtime/src/object/shapes.rs | sed -n '45,105p;1630,1735p'
printf '%s\n' '--- keys_lookup.rs lookup path ---'
cat -n crates/perry-runtime/src/object/keys_lookup.rs | sed -n '1,125p'
printf '%s\n' '--- relevant scoped convention content ---'
for f in /tmp/coderabbit-repo-knowledge/perryts-perry-d4a878bc/*/*.md; do
case "$f" in
*runtime*|*object*|*fast_hash*|*shapes*|*keys*) printf '\n--- %s ---\n' "$f"; cat "$f";;
esac
doneRepository: PerryTS/perry Length of output: 37581 🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- shape lookup callers and thresholds ---'
rg -n -C 5 'shape_slot_lookup|KEYS_INDEX_THRESHOLD|WIDE_KEY_INDEX_MIN_KEYS|shape_note_append|shape_note_hit' \
crates/perry-runtime/src/object
printf '%s\n' '--- shape index creation/removal context ---'
rg -n -C 6 'indices\.(entry|remove|get_mut)|ShapeIndex \{' \
crates/perry-runtime/src/object/shapes.rs
printf '%s\n' '--- bounded FNV/PtrHasher bucket probe ---'
python3 - <<'PY'
from collections import defaultdict
MASK = (1 << 64) - 1
FNV_OFFSET = 0xcbf29ce484222325
FNV_PRIME = 0x100000001b3
PTR_MIX = 0x9E3779B97F4A7C15
def fnv(data):
h = FNV_OFFSET
for b in data:
h ^= b
h = (h * FNV_PRIME) & MASK
return h
def ptr_hash(n):
h = (n * PTR_MIX) & MASK
return h ^ (h >> 32)
# Enumerate a bounded, attacker-selectable ASCII name space. For each
# capacity, report the largest bucket group among the first N names and
# the number of names needed to obtain 64 names in one bucket.
names = []
for i in range(200000):
# Distinct valid property names with a fixed prefix and decimal suffix.
names.append(("p" + str(i)).encode())
for cap in (512, 1024, 2048, 4096):
buckets = defaultdict(list)
for name in names:
b = ptr_hash(fnv(name)) & (cap - 1)
buckets[b].append(name)
largest = max(map(len, buckets.values()))
target = next((len(v) for v in buckets.values() if len(v) >= 64), None)
print(f"capacity={cap} largest_in_200000={largest} names_for_64={target}")
PYRepository: PerryTS/perry Length of output: 28856 🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- shape lookup callers and thresholds ---'
rg -n -C 5 'shape_slot_lookup|KEYS_INDEX_THRESHOLD|WIDE_KEY_INDEX_MIN_KEYS|shape_note_append|shape_note_hit' \
crates/perry-runtime/src/object
printf '%s\n' '--- shape index creation/removal context ---'
rg -n -C 6 'indices\.(entry|remove|get_mut)|ShapeIndex \{' \
crates/perry-runtime/src/object/shapes.rs
printf '%s\n' '--- bounded FNV/PtrHasher bucket probe ---'
python3 - <<'PY'
from collections import defaultdict
MASK = (1 << 64) - 1
FNV_OFFSET = 0xcbf29ce484222325
FNV_PRIME = 0x100000001b3
PTR_MIX = 0x9E3779B97F4A7C15
def fnv(data):
h = FNV_OFFSET
for b in data:
h ^= b
h = (h * FNV_PRIME) & MASK
return h
def ptr_hash(n):
h = (n * PTR_MIX) & MASK
return h ^ (h >> 32)
names = []
for i in range(200000):
names.append(("p" + str(i)).encode())
for cap in (512, 1024, 2048, 4096):
buckets = defaultdict(list)
for name in names:
b = ptr_hash(fnv(name)) & (cap - 1)
buckets[b].append(name)
largest = max(map(len, buckets.values()))
target = next((len(v) for v in buckets.values() if len(v) >= 64), None)
print(f"capacity={cap} largest_in_200000={largest} names_for_64={target}")
PYRepository: PerryTS/perry Length of output: 28856 Denial of Service (CWE-400): Uncontrolled Resource Consumption Reachability: External · Exploitability: Moderate Keep
🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| /// Immutable facts named by one ShapeId. | ||
|
|
@@ -1595,7 +1605,12 @@ unsafe fn index_range(shape: &mut ShapeIndex, keys: *const ArrayHeader, key_coun | |
| let v = crate::JSValue::from_bits((*slots.add(i as usize)).to_bits()); | ||
| if let Some(b) = crate::string::js_string_key_bytes(v, &mut sso) { | ||
| let h = super::key_bytes_hash(b.as_ptr(), b.len()); | ||
| shape.slots.entry(h).or_default().push(i); | ||
| match shape.slots.entry(h) { | ||
| std::collections::hash_map::Entry::Occupied(mut e) => e.get_mut().push(i), | ||
| std::collections::hash_map::Entry::Vacant(e) => { | ||
| e.insert(SlotList::One(i)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| shape.indexed_len = key_count; | ||
|
|
@@ -1633,7 +1648,7 @@ pub(crate) unsafe fn shape_slot_lookup( | |
| } | ||
| inner.indices.entry(keys_id).or_insert(ShapeIndex { | ||
| indexed_len: 0, | ||
| slots: HashMap::with_capacity(key_count as usize), | ||
| slots: crate::fast_hash::new_ptr_hash_map(), | ||
| }) | ||
| } | ||
| }; | ||
|
|
@@ -1643,7 +1658,7 @@ pub(crate) unsafe fn shape_slot_lookup( | |
| let candidates = shape.slots.get(&key_hash)?; | ||
| let mut sso = [0u8; crate::value::SHORT_STRING_MAX_LEN]; | ||
| let (slots, slot_len) = super::keys_array_dense_slots(keys); | ||
| for &i in candidates { | ||
| for &i in candidates.iter() { | ||
| if (i as usize) >= slot_len || i >= key_count { | ||
| continue; | ||
| } | ||
|
|
@@ -1670,7 +1685,12 @@ pub(crate) fn shape_note_append( | |
| if let Some(shape) = inner.indices.get_mut(&(keys as usize)) { | ||
| if shape.indexed_len + 1 == new_count { | ||
| shape.indexed_len = new_count; | ||
| shape.slots.entry(key_hash).or_default().push(slot); | ||
| match shape.slots.entry(key_hash) { | ||
| std::collections::hash_map::Entry::Occupied(mut e) => e.get_mut().push(slot), | ||
| std::collections::hash_map::Entry::Vacant(e) => { | ||
| e.insert(SlotList::One(slot)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -1680,7 +1700,12 @@ pub(crate) fn shape_note_append( | |
| pub(crate) fn shape_note_hit(keys: *const ArrayHeader, key_hash: u64, slot: u32) { | ||
| let mut inner = crate::state::state().shapes.inner.borrow_mut(); | ||
| if let Some(shape) = inner.indices.get_mut(&(keys as usize)) { | ||
| shape.slots.entry(key_hash).or_default().push(slot); | ||
| match shape.slots.entry(key_hash) { | ||
| std::collections::hash_map::Entry::Occupied(mut e) => e.get_mut().push(slot), | ||
| std::collections::hash_map::Entry::Vacant(e) => { | ||
| e.insert(SlotList::One(slot)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1788,37 +1813,6 @@ crate::perry_thread_local! { | |
| std::cell::RefCell::new(crate::fast_hash::new_ptr_hash_map()); | ||
| } | ||
|
|
||
| /// Per-descriptor bookkeeping after its keys address has been probed. | ||
| /// | ||
| /// Lifted out of `scan_shape_table_rekey_mut`'s loop so the memoised path and | ||
| /// the probing path cannot drift apart — the probe is what is deduplicated, | ||
| /// never the bookkeeping, which still runs once per descriptor. | ||
| #[inline] | ||
| fn record_shape_scan_outcome( | ||
| visitor: &mut crate::gc::RuntimeRootVisitor<'_>, | ||
| id: &u32, | ||
| descriptor: &mut ShapeDescriptor, | ||
| addr: usize, | ||
| moved: bool, | ||
| dead_descriptor_ids: &mut Vec<u32>, | ||
| descriptor_rekeys: &mut Vec<u32>, | ||
| ) { | ||
| // Validate the POST-visit address. A stale shape key can follow the | ||
| // forwarding record of the non-array tenant that recycled its address; | ||
| // checking only an unmoved old address misses that case. | ||
| if visitor.is_metadata_rewrite_phase() && shape_keys_address_is_recycled(addr) { | ||
| dead_descriptor_ids.push(*id); | ||
| } else if moved { | ||
| descriptor.keys = addr as u64; | ||
| } | ||
| // A live-object edge can rewrite the boxed `keys` slot before this metadata | ||
| // pass. Comparing against the address represented in the reverse maps | ||
| // catches both that ordering and a move observed here. | ||
| if descriptor.keys != descriptor.indexed_keys { | ||
| descriptor_rekeys.push(*id); | ||
| } | ||
| } | ||
|
|
||
| /// Metadata-only forwarding repair for the weak descriptor table and | ||
| /// pointer-keyed slot indices. Mark/copy mode does not root anything; live | ||
| /// object scans provide descriptor reachability, and post-copy rewrite follows | ||
|
|
||
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
Rewrite the intermediate-state release note.
changelog.d/9001-shape-index-inline-slot.mdchanges the allocation pattern described here. In the shipped result, non-colliding buckets no longer allocate a separateVec<u32>per key. The “next target” and “untouched here” statements would make the assembled release notes contradict the final code.Rewrite this paragraph as final behavior, or label these measurements explicitly as an earlier incremental result.
Proposed wording
Based on learnings,
changelog.d/fragments must describe the final shipped behavior as one coherent release-note entry, not separate development-slice narratives.📝 Committable suggestion
🤖 Prompt for AI Agents
Source: Learnings