Skip to content

Avoid per-event allocation in Python market-depth fusion #320

Description

@Magnushst

Problem

The Python market-depth fusion path currently creates a temporary Vec<Event> for each accepted depth or snapshot event and immediately appends it to a persistent output vector.

This causes approximately one short-lived allocation per accepted input event.

Evidence

A deterministic release-mode benchmark covered 10,000, 100,000 and 1,000,000 events across narrow and wide depth ranges.

Platform Baseline ns/event Candidate ns/event Improvement
Windows 11 58.100 32.124 44.69%
Debian under WSL2 41.911 30.116 28.37%

These are independent platform results and were not combined.

For the one-million-event workload, median allocation calls fell from 980,071.5 to 31, and cumulative requested bytes fell by 65.15%.

All 260 benchmark pairs on each platform produced identical output counts and sequence checksums.

Proposed change

  • Add append-style bid and ask update methods that write into caller-owned storage.
  • Retain the existing vector-returning methods as compatible wrappers.
  • Reuse the persistent output vector in the Python binding.
  • Preserve add=false by updating depth state and then restoring the previous output length.
  • Add deterministic differential and binding regression tests.

The change is limited to two files, uses safe Rust, adds no dependencies and does not change Python APIs or file formats.

Correctness

The differential test compares emitted events and complete depth state after every update across 20,000 deterministic events.

The binding test covers accepted events, stale timestamps, add=false state updates and invalid sides.

Event ordering, timestamps, flags, prices and quantities remain unchanged.

Scope

Would a focused pull request implementing this change be acceptable?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions