[Perf]: ObservedEnvelope sorted-Vec insert shifts ~8.5 MB per observation, pinning the evaluator ~18 s
Summary
In a 20.04 s interactive perf capture of the release editor, the evaluator thread spent roughly 18 s (94.93% of its cpu_core samples, ~43.5 G of 45.8 G cycles) inside __memmove_avx_unaligned_erms, all from one call site in ObservedEnvelope::insert → insert_address (Vec::insert on the sorted address ledger).
At capture time the sorted ledger held ~1,148,900 addresses, and insertions landed around index ~81,000, so every insertion moved ~1.07 M entries ≈ 8.5 MB. This is O(n) per observation and O(n²) over an epoch. It matches the multi-second evaluator freezes reported in #475, but in the current build the hot path is live publication, not the GC rebuild path.
This is an evidence report; no fix is proposed here.
Environment
-
Neomacs 0.0.20, git
93d120d19dc719eb09164ebf8a944444d55ce9a4, releasex86_64-unknown-linux-gnu, rustc 1.96.1 (./target/release/neomacs --version). -
Linux 7.3.1-cachyos-lto, i9-13900H.
-
Capture:
~/.cache/neomacs/perf/neomacs-20261114-005058.perf.data(32 MB, 20.042 s, 0 lost samples), recorded by the user'sneomacs-perf-start:perf record -F 199 -e cycles:u --call-graph dwarf,8192 -p <editor-pid> -o <file>The capture is retained locally. It is not attached because it contains user-stack memory; happy to share after review.
What the capture shows
- Event
cpu_core/cycles/u: 3,518 samples on the evaluator thread (TID 2734257), 103 on the main thread.cpu_atom/cycles/u: 76 on the evaluator. perf report --no-children -g none --sort symbol(cpu_core): 94.93%__memmove_avx_unaligned_erms; next entries are 0.34%publish_observed_owner_in_state, 0.21% renderer text pass, then sub-0.2% symbols.- Per-second evaluator timeline: memmove is 76% at t+0.9 s, then 98–100% from t+1 s through t+18 s, tapering at the end. The evaluator thread was pinned for essentially the whole capture.
- Perf's DWARF callchain stops at memmove (glibc's AVX memmove has no CFI). Recovering the return address from the captured
PERF_SAMPLE_STACK_USER/REGS_USERdata shows 817 of 818 evaluator memmove samples return to the same address; one unrelated sample is a display copy inPresentationComposer::compose.- Runtime return address:
0x5555586fe191 - Neomacs load base from
/proc/<pid>/maps:0x555555400000(mapping inode matches the binary) - Static address:
0x32fe191=neovm_value::tagged::collection_reads::publish_observed_owner_in_state+0x411(symbol range0x32fdd80–0x32fe600)
- Runtime return address:
Call site
Disassembly at the return address:
32fe162: lea 0x40(%rcx),%rdi
32fe166: call RawVec::grow_one
32fe170: mov 0x48(%rcx),%rdx
32fe174: lea (%rdx,%r13,8),%rbp
32fe178: mov %r14,%rdx
32fe17b: sub %r13,%rdx ; len - insert_index
32fe184: shl $0x3,%rdx ; * 8 bytes per element
32fe188: mov %rbp,%rsi
32fe18b: call *memmove@GLIBC ; shift the tail right by one slot
32fe191: ...
That is Vec::insert into a sorted Vec<usize>. The captured registers at the samples confirm the sizes:
r14(vector length): 1,148,888 – 1,148,979r13(insertion index): 80,979 – 81,291- bytes moved per insertion:
(r14 - r13) * 8≈ 8.5 MB
In the sampled window the ledger grew by ~900 addresses/s (1,148,888 → 1,148,979 in ~0.1 s), so the evaluator was continuously shifting ~8.5 MB per new observation.
Source and root cause
-
insert_address—crates/neovm-value/src/tagged/collection_reads/observed_envelope.rs:398-406:fn insert_address(addresses: &mut Vec<usize>, address: usize) -> bool { match addresses.binary_search(&address) { Ok(_) => false, Err(index) => { addresses.insert(index, address); true } } } -
Called for both
addressesandslot_addressesfromObservedEnvelope::insert(observed_envelope.rs:176-177). -
Reached from
publish_observed_owner_in_state(crates/neovm-value/src/tagged/collection_reads.rs:1108, call at line 1144). The function is marked#[cold], but this workload made it the dominant cost. -
The sorted-Vec buffers were introduced by commit
69043a0268"Retain observed collection address buffers across reclamation" (2026-10-24). TheLegacyEnvelopestatic assertion in the same file still records the previousBTreeSet<usize>layout. -
That change removed the rebuild cost from #475:
refresh_reclaimedkeeps the ordered buffers when all owners survive, andrebuildonly filters (retain) — it does not reinsert or sort. But live publication then inserts into a large retained vector, and any address that sorts below the retained bulk costs O(n). -
In this capture the insertion point (~81 k) is far below the length (~1.149 M), so ~93% of the vector is shifted on every observation.
Relation to #475
#475 profiled the rebuild path: BTreeMap::insert 77.52%, ObservedEnvelope::rebuild, refresh_reclaimed, under incremental_sweep_slice. This capture shows the follow-up state after 69043a0268: rebuild/refresh is no longer hot, and the same subsystem now spends its time in live sorted-vector insertion. The user-visible symptom — multi-second evaluator stalls — is the same.
Reproduction status
No configuration-independent minimized reproducer yet. The structural condition is:
- A retained sorted address ledger with ~1.15 M entries.
- Live insertions whose sorted position is near the front (~81 k), each shifting ~1.07 M entries.
The interactive workload that produced 1.15 M observations is not yet isolated; the capture came from the user's configured session and also shows LSP/process activity and git child processes. A deterministic benchmark could seed the ledger via the test-support path with N sorted addresses, then insert addresses below the bulk, and compare append-only vs mid-vector insertion.
Possible direction (not implemented here)
Avoid O(n) insertion into the retained buffers, e.g.:
- restore an ordered set for order/gap queries (
BTreeSet) while keeping the hash set for membership; or - keep an append buffer for new observations and merge/sort during
refresh/rebuild, serving exact predecessor/successor queries as needed; or - use chunked sorted runs.
Invariants to preserve: exact predecessor/successor queries used by exclude_unobserved, the below/above/slot_* window computation, capacity retention across reclamation, and the publication-path guarantees about no Lisp call or safepoint.
Evidence-method note
perf report cannot unwind this leaf because glibc's __memmove_avx_unaligned_erms lacks CFI. Re-recording with --call-graph lbr resolves libc callers natively (verified here on a synthetic memmove loop); the existing capture predates that and was analyzed via the captured user-stack return address.
An implementation is up in PR #787.
Change: the retained sorted address ledger is now a two-run structure. New addresses enter a bounded sorted hot run (1024 entries); membership and exact predecessor/successor queries search both runs, and the hot run merges into the retained bulk in one backward in-place linear pass when full. retain filters both runs, so reclamation still keeps warmed capacity — the existing envelope allocation test still measures zero allocations in steady-state turnover.
This targets the reported call site directly: insert no longer shifts the retained tail, and exclude_unobserved keeps its exact gap semantics by combining each run's partition point.
Moved-slot evidence (deterministic test, 200k retained addresses, 20k insertions below the bulk):
issue #767 ledger: retained=200000 inserted=20000 moved=14262000 \
old_tail_shift=4000000000 reduction=280.5x
At the captured ~1.15M entries the bound is HOT_ADDRESS_LIMIT / 2 + n / HOT_ADDRESS_LIMIT ≈ 1.6k slots (~13 KB) per insertion, instead of ~1.07M slots (~8.5 MB).
Verification: cargo nextest run -p neovm-value — 1270 passed; the new regression test also pins membership, per-run ordering, and predecessor/successor across the hot/bulk seam; cargo fmt and the workspace check passed.
Limitation: I did not re-run your interactive 1.15M-object workload or take a new perf capture. If you can repeat the neomacs-perf-start capture after this lands, that would close the loop on the actual session; the deterministic test and analysis are the evidence for now.
Fixed by #787, merged 2026-11-18 as 5b1c67d854 (perf(value): bound observed-address insertion with a hot run).
ObservedEnvelope's two sorted address ledgers are now SortedAddressLedger: a retained sorted bulk plus a bounded sorted hot run (1024 entries). New observations insert into the hot run, so a live insertion shifts at most the hot run (~13 KB worst case) instead of the ~8.5 MB tail in the report; the hot run merges into the bulk in one linear pass when full. Membership and exact predecessor/successor queries (used by exclude_unobserved) search both runs, and reclamation still filters in place with no per-element allocation.
Measured by inserting_below_the_retained_bulk_is_bounded (200,000 retained addresses, 20,000 insertions below the bulk):
issue #767 ledger: retained=200000 inserted=20000 moved=14262000 old_tail_shift=4000000000 reduction=280.5x
cargo nextest run -p neovm-value: 1270 passed. The zero-allocation steady-state turnover test still passes.
Not re-run: the original 20 s interactive capture on a ~1.15M-entry ledger. The evidence is the deterministic moved-slot bound and the O(n/K + K) analysis rather than a fresh perf capture. If a current build still shows __memmove_avx_unaligned_erms at this call site, please reopen with the capture.