Scheduler Optimization: Pre-allocate vectors during buffering checks#1725
Scheduler Optimization: Pre-allocate vectors during buffering checks#1725apfitzge merged 3 commits intoanza-xyz:masterfrom
Conversation
| @@ -531,9 +531,9 @@ impl SchedulerController { | |||
| let mut num_dropped_on_capacity: usize = 0; | |||
| let mut num_buffered: usize = 0; | |||
| for (((packet, transaction), fee_budget_limits), _) in arc_packets | |||
| let mut arc_packets = Vec::with_capacity(CHUNK_SIZE); | ||
| let mut transactions = Vec::with_capacity(CHUNK_SIZE); | ||
| let mut fee_budget_limits_vec = Vec::with_capacity(CHUNK_SIZE); |
There was a problem hiding this comment.
nit: using smallvec further reduces allocation entirely per each buffer_packets() invocation.
There was a problem hiding this comment.
I opted for arrayvec, since it fully guarantees no heap-allocation. I think it's also slightly faster because there's no need to check for overflow onto the heap when we drain them.
There was a problem hiding this comment.
yeah, by principle, it'll be slightly faster.
unlike smallvec, arrayvec will panic!() if we're about to .push() more than CHUNK_SIZE. However, that seems to be guaranteed not to happen.
There was a problem hiding this comment.
Also, the trio of these arrayvec will consume ~36 kb of stack in total, according to rust-analyzer:
// size = 1032 (0x408), align = 0x8
let mut arc_packets: ArrayVec<Arc<ImmutableDeserializedPacket>, 128>
// size = 31752 (0x7C08), align = 0x8
let mut transactions: ArrayVec<SanitizedTransaction, 128>
// size = 4104 (0x1008), align = 0x8
let mut fee_budget_limits_vec: ArrayVec<FeeBudgetLimits, 128>That again will be okay, considering linux's default thread stack size. (8 MiB)
| @@ -479,14 +480,14 @@ impl SchedulerController { | |||
|
|
|||
| const CHUNK_SIZE: usize = 128; | |||
| let lock_results: [_; CHUNK_SIZE] = core::array::from_fn(|_| Ok(())); | |||
There was a problem hiding this comment.
(off topic) this can be rewritten with inline const once we update to rust 1.79.
Problem
Summary of Changes
drainto avoid lifetime issues with the vecs continuing to live onPulled out of #1663 (thanks @t-nelson)
Fixes #