fix: reduce hot-path allocation, cloning, and lock contention#524
Open
varex83agent wants to merge 1 commit into
Open
fix: reduce hot-path allocation, cloning, and lock contention#524varex83agent wants to merge 1 commit into
varex83agent wants to merge 1 commit into
Conversation
Implements the eight hot-path optimizations from the "hot-path allocation, cloning, and lock-contention reduction" plan. All changes are perf-only and preserve functional equivalence with Charon v1.7.1. - T01 p2p: add alloc-free `PeerStore::has_connection`; migrate the five emptiness-only `connections_to_peer(..).is_empty()` call sites. - T02 qbft: thread one `Arc<QbftConsensusMsg>` across peers on broadcast instead of an N-1 deep clone; move `msg`/`justification`/`values` out of `pb_msg` on receive; `values_by_hash` consumes its input. - T03 eth2api: `Arc`-wrap the cached validator maps, add a read-lock fast path, and release the write lock across the beacon-node fetch (re-checking on re-acquire). - T04 scheduler: replace the O(N) `to_entries()` status-metric scan with a per-pubkey last-status map, zeroing only the previously-reported series. - T05 scheduler: cache `slots_per_epoch` on the actor at build time instead of a beacon-node round-trip on every duty lookup. - T06 parsigex: acquire the peer-store guard once per broadcast and share the encoded payload via `Arc<[u8]>` instead of a per-target `Vec<u8>` copy. - T07 parsigdb: fold the threshold check into `store` under the lock and return only the matched set, dropping the per-store full-accumulator clone. - T08 ssz: pass the buffer by value into `merkleize_impl` (one copy, not two); reuse one `Sha256` in `default_hash_fn`; drop the `put_bitlist` clone. Co-Authored-By: Bohdan Ohorodnii <35969035+varex83@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Implements the eight hot-path optimizations from the hot-path allocation, cloning, and lock-contention reduction plan. Every change is perf-only and preserves functional equivalence with Charon v1.7.1 — no wire format, validation, ordering, or metric semantics change.
Tasks
p2pPeerStore::has_connection; migrate the 5 emptiness-onlyconnections_to_peer(..).is_empty()callers (qbft, frostp2p, bcast, parsigex, monitoringapi).consensus(qbft)Arc<QbftConsensusMsg>across all peers on broadcast instead of an N-1 deep clone of the multi-MB payload; movemsg/justification/valuesout ofpb_msgon receive;values_by_hashnow consumesVec<Any>.eth2apiArc-wrap the cachedActiveValidators/CompleteValidatorsmaps (cheap clones viaDeref), add a read-lock fast path on a warm cache, and release the write lock across the beacon-node fetch (re-checking on re-acquire).core(scheduler)to_entries()status-metric scan with a per-pubkey last-status map, zeroing only the previously-reported series — mirrors Charon'sstatusGauge.Reset.core(scheduler)slots_per_epochon the actor at build time instead of a beacon-node round-trip on every duty lookup.parsigexhas_connection) and share the encoded payload viaArc<[u8]>instead of a per-targetVec<u8>copy.core(parsigdb)storeunder the lock and return only the matched set via aStoreOutcomeenum, dropping the per-store full-accumulator deep clone.sszmerkleize_impl(one copy instead of two); reuse a singleSha256indefault_hash_fn; drop theput_bitlistscratch clone.Tests
New unit/regression tests added for each task (peer-store connectivity,
Arc::ptr_eqshared-buffer assertions for qbft and parsigex, concurrent valcache misses, per-pubkey status-metric transitions, cached-slots_per_epochepoch math, parsigdb duplicate/mismatch paths, and ssz golden merkleization roots).Quality gates
cargo +nightly fmt --all --check✅cargo clippy --workspace --all-targets --all-features -- -D warnings✅cargo test --workspace --all-features✅ (all suites pass)cargo deny check✅🤖 Generated with Claude Code