Skip to content

perf(runtime): size the canonical-handle address index and release what it retires (-7.63% command CPU) - #1

Open
proggeramlug wants to merge 1 commit into
campaign/cc-native-recv-0909-basefrom
perf/registry-addr-index-20260912
Open

proggeramlug wants to merge 1 commit into
campaign/cc-native-recv-0909-basefrom
perf/registry-addr-index-20260912

Conversation

@proggeramlug

Copy link
Copy Markdown
Owner

Summary

Three of the cheapest address predicates in value::addr_class embed is_canonical_handle_addr, so on this stack every ordinary heap pointer whose numeric arm cannot decide pays an exact malloc-registry lookup. Measured on one offline claude-code command: 65,133,719 classifications, 43,150,678 reaching the registry (66.25 %), 9,225 finding a real wrapper (0.021 %) — 43,141,453 queries per command with nothing in them, which is the gc_malloc_header_is_tracked symbol the profiles rank at 4.35 % of command cycles.

This sizes the guard to its measured population and makes it release what it retires. Minimum command CPU 1.18 → 1.09 s (−7.63 %), node gap 2.81× → 2.60×.

Base

This PR targets the campaign base, not main. Its subject does not exist on main: is_above_handle_band there is still a bare addr >= HANDLE_BAND_MAX and native_handle/canonical.rs is absent. The canonical-handle wrapper subsystem (9fcd96d1c) is one of 15 commits on this base, which diverged from main 290 commits ago. The fix should travel with that subsystem; see the linked issue.

The defect

The guard is a 1,024-bit monotone Bloom filter and the population does not fit: 643–648 live wrapper addresses, 712–721 admissions and 69–76 retirements per command. Selectivity collapses inside a single process — 0.021–0.154 % of addresses pass after startup, 57.7–74.6 % after the command — a 430–3,500× loss with no configuration involved. Sizing dominates monotonicity: clearing retired bits would still leave ~640 live addresses in a filter that cannot hold 60. The type's own sizing note anticipated the cliff and prescribed "raise WORDS", but WORDS was one shared constant, and the same note records that bits accrue per admission, so a larger fixed filter alone only postpones saturation.

The change

  • WORDS becomes a per-owner const-generic parameter, so one owner can be sized without moving the other three.
  • RegistryAddrIndex keeps an exact set of the addresses the owner currently holds — mutated only on wrapper creation and retirement, 712–721 and 69–76 times per command against tens of millions of probes — and recomputes the bit array from it once enough has changed. Refresh is O(live) per O(live) changes: amortized O(1), with no interval, schedule or workload-specific setting.
  • The hot side is untouched: the same three bit tests on a plain array of atomics, no lock, no allocation, no pointer chase.
  • The canonical owner takes 256 words (16,384 bits, 2 KiB, L1-resident) and retires at all three sites that end a registration — each after the authoritative table, and in retire after finalize_once, so no window exists in which the runtime recognises an address the index denies.

Why overwriting the bits is sound

RegistryAddrFilter deliberately has no clear, because clearing could make a live registered address read as unregistered. The refresh is not a clear, and the argument is written out on the type:

  • Every admit and every refresh for one index is serialized by the same guard, so no admission's bit-set can be lost to a refresh's store — which is why admit takes the guard before setting the bits.
  • The new words are computed from the complete live set, so a live address's three bits are set in the new value, and also in the old (it was admitted under this guard, and every refresh published while it was live included it). A reader may observe any mix of old and new words, and in every such mix all three bits of every live address are set. No reader can observe a false negative at any instant.
  • A non-live address may start reading false — the point — and a stale true costs only the authoritative lookup that was always there. The authoritative path is unchanged, so a missed retirement can cost performance but never correctness.

Measurement

Five interleaved pairs plus three node rows on the quiet Linux box, campaign lock held, load1 1.27–1.62. Control is the retained base binary reused by exact digest; compiler, object cache and bundle unchanged.

Pair control candidate CPU control RSS candidate RSS
1 1.24 s 1.24 s 0.00 % 631.121 MiB 632.707 MiB
2 1.27 s 1.19 s −6.30 % 629.797 MiB 632.074 MiB
3 1.27 s 1.09 s −14.17 % 647.191 MiB 632.270 MiB
4 1.20 s 1.16 s −3.33 % 632.906 MiB 656.840 MiB
5 1.18 s 1.12 s −5.08 % 632.141 MiB 634.508 MiB

Minima −7.63 %, medians 1.24 → 1.16 s, paired median −5.08 %; startup minima unchanged at 0.95 s; startup-plus-command minima −4.65 % with every pair improving; command wall minima −5.93 %, every pair improving. Largest per-pair peak-RSS increase +3.78 %, which is larger than the change's own footprint (2 KiB of bits plus a 643–648-entry set), so its cause is not the structure and was not measured — pair 3's control likewise sits 14–17 MiB above the other four controls. Node anchor in the same session: command 0.44 / 0.43 / 0.42 s. Parity is still unmet; this removes one named cost.

Mechanism

A counter arm of the same change, same workload:

Command phase, 3 rows before after
Classifications 65,133,719 64,809,993
Registry-bound passes 43,150,678 144,246
Passes resolving to a wrapper 9,225 9,225

Resolutions are 3,075 in every row of both arms: the index changes which addresses reach the authoritative lookup, not what it answers.

Tests

test result: ok. 33 passed; 0 failed for registry_latch; native_handle (15) and addr_class (8) unchanged. Three tests are new and each fails without the change:

  • the measured 644-address population at both widths — a 1,024-bit witness of the same population admits 80.4 % of non-members, an independent confirmation of the measured 57.7–74.6 % pass rate;
  • retirement narrowing the index while keeping every live address;
  • 20,000 churned addresses against 64 resident ones not walking back to saturation.

Disabling the refresh fails two of them; reverting the width fails the third. This is a focused set, not the repository's full gap gate, and no broad CI verdict is claimed.

Not in scope

The 232 call sites of the three range predicates are untouched; separating the numeric range test from semantic receiver classification is the larger, independent change an earlier caller census recommended.

Measurement arm without the census counters: the same change as the census
branch's b76dc1c, applied to the retained own-read-cache candidate source so
this binary is comparable with the retained control.

The 1,024-bit RegistryAddrFilter stopped discriminating for the canonical-handle
owner: 643-648 live wrapper addresses admitted 57.7-74.6% of 21.6-21.8 M
classifications per command, 0.021% of them genuine. WORDS becomes a per-owner
parameter; RegistryAddrIndex keeps an exact live-address set, mutated only on
wrapper creation and retirement, and recomputes the bit array from it when
enough has changed, which releases retired addresses. The hot side is unchanged.
The canonical owner moves to 256 words and retires at all three sites that end a
registration, each after the authoritative table.
proggeramlug pushed a commit to PerryTS/perry that referenced this pull request Sep 14, 2026
Refs #10110. Adapt the reusable index and population/churn witnesses from proggeramlug#1 for main. Scale all three hash slices with the per-owner width and cover concurrent refreshes and address reuse. The canonical-wrapper owner is still on the campaign branch; document its required integration without closing the issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant