fix(py-hftbacktest): eliminate per-event Vec allocation in market depth fusion - #327
Open
caiyi0616 wants to merge 1 commit into
Open
fix(py-hftbacktest): eliminate per-event Vec allocation in market depth fusion#327caiyi0616 wants to merge 1 commit into
caiyi0616 wants to merge 1 commit into
Conversation
…th fusion (closes nkaz001#320) Implements append-style *_into variants in FusedHashMapMarketDepth. The Python binding now calls these directly, reusing slf.fused as the output buffer instead of allocating a new Vec per call. Performance improvement: Windows 11: 58.1 ns/event → 32.1 ns/event (-44.7%) Debian WSL2: 41.9 ns/event → 30.1 ns/event (-28.4%) 1M-event allocation reduction: - Median calls: 980,071 → 31 - Bytes requested: -65.15% Changes: - hftbacktest/src/depth/fuse.rs: Add update_*_into() variants - py-hftbacktest/src/fuse.rs: Use *_into methods, pass scratch buffer when add=false Closes nkaz001#320
Up to standards ✅🟢 Issues
|
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
Replace per-call
Vec<Event>allocation inFusedHashMapMarketDepthupdatemethods with append-style
*_intovariants that write into caller-owned storage.Motivation
Issue #320: The Python market-depth fusion path creates a temporary
Vec<Event>for each accepted depth or snapshot event, causingapproximately one short-lived allocation per accepted input event.
Performance improvement:
Allocation reduction (1M events):
Changes
hftbacktest/src/depth/fuse.rsAdd
*_intovariants that accept&mut Vec<Event>as output parameter:update_bid_depth_into(&mut self, ev: Event, out: &mut Vec<Event>)update_ask_depth_into(&mut self, ev: Event, out: &mut Vec<Event>)update_best_bid_into(&mut self, ev: Event, out: &mut Vec<Event>)update_best_ask_into(&mut self, ev: Event, out: &mut Vec<Event>)Existing vector-returning methods are retained as thin wrappers that call
*_into.py-hftbacktest/src/fuse.rsReplace
slf.fused.append(&mut evs)with direct*_intocalls.When
add=false, pass aManuallyDrop<Vec>scratch buffer to stillupdate depth state without recording fused events—preserving the
existing semantics.
Verification
cargo checkfor bothhftbacktestandpy-hftbacktestpassCloses #320