Skip to content

fix: reduce hot-path allocation, cloning, and lock contention#524

Open
varex83agent wants to merge 1 commit into
mainfrom
perf/hot-path-alloc-clone-reduction
Open

fix: reduce hot-path allocation, cloning, and lock contention#524
varex83agent wants to merge 1 commit into
mainfrom
perf/hot-path-alloc-clone-reduction

Conversation

@varex83agent

Copy link
Copy Markdown
Collaborator

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

Task Crate Change
T01 p2p Add alloc-free PeerStore::has_connection; migrate the 5 emptiness-only connections_to_peer(..).is_empty() callers (qbft, frostp2p, bcast, parsigex, monitoringapi).
T02 consensus (qbft) Thread one Arc<QbftConsensusMsg> across all peers on broadcast instead of an N-1 deep clone of the multi-MB payload; move msg/justification/values out of pb_msg on receive; values_by_hash now consumes Vec<Any>.
T03 eth2api Arc-wrap the cached ActiveValidators/CompleteValidators maps (cheap clones via Deref), 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).
T04 core (scheduler) Replace the O(N) to_entries() status-metric scan with a per-pubkey last-status map, zeroing only the previously-reported series — mirrors Charon's statusGauge.Reset.
T05 core (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 (using T01's has_connection) and share the encoded payload via Arc<[u8]> instead of a per-target Vec<u8> copy.
T07 core (parsigdb) Fold the threshold check into store under the lock and return only the matched set via a StoreOutcome enum, dropping the per-store full-accumulator deep clone.
T08 ssz Pass the buffer by value into merkleize_impl (one copy instead of two); reuse a single Sha256 in default_hash_fn; drop the put_bitlist scratch clone.

Tests

New unit/regression tests added for each task (peer-store connectivity, Arc::ptr_eq shared-buffer assertions for qbft and parsigex, concurrent valcache misses, per-pubkey status-metric transitions, cached-slots_per_epoch epoch 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

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>
@varex83 varex83 changed the title perf: reduce hot-path allocation, cloning, and lock contention fix: reduce hot-path allocation, cloning, and lock contention Jul 6, 2026
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