You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
_apply_snapshot_inplace in kalshi/ws/orderbook.py:128-129 calls dict(msg.msg.yes) and dict(msg.msg.no), which copies the already-validated price/quantity dicts. This silently nullifies the ~5× snapshot-apply speedup CHANGELOG v2.5.0 #263 explicitly promises:
WS snapshot apply collapses to a single dict walk (#263). OrderbookSnapshotPayload.yes / .no validate directly into dict[Decimal, Decimal] via a BeforeValidator; _apply_snapshot_inplaceadopts the dict in identity (no rebuild). ~5× faster on a 200-level book.
The validator _levels_to_dict in kalshi/ws/models/orderbook_delta.py is set up correctly — it returns the same dict instance when keys/values are already Decimal. The orderbook caller then immediately copies it. For a 200-level book that's 400 needless re-hashes + reallocs per snapshot, on the recv hot path.
The validator at kalshi/ws/models/orderbook_delta.py has an explicit fast-path that returns the value by identity if the dict is already Decimal-keyed/-valued, with a comment:
# Field type is ``dict[Decimal, Decimal]`` (not ``dict[DollarDecimal, FixedPointCount]``)# so pydantic-core does not re-run the per-item ``BeforeValidator(_coerce_decimal)``# walk on values our ``BeforeValidator`` has already coerced.PriceCountMap=Annotated[dict[Decimal, Decimal], BeforeValidator(_levels_to_dict)]
Then orderbook.py copies the dict anyway.
Suggested fix
Drop the dict() wrappers — the validated dicts are safe to adopt by identity because:
Each frame is a fresh JSON parse → msg.msg.yes / .no have no external references.
The Pydantic model msg is discarded immediately after _apply_snapshot_inplace returns.
_BookState then owns the dicts and mutates them via _apply_delta_inplace.
Update / add a bench in scripts/bench_orderbook_iterator.py that snapshots a 200-level book in a loop and report the wallclock delta. This is the metric the v2.5 release notes claim.
Discovered by
Post-v2.5 multi-reviewer audit (Performance surface). The CHANGELOG claim is correct in intent; only the code-side adoption regressed.
Activity
fix-up(ws): trim snapshot-adopt comment
release: 2.6.0 — post-v2.5 independent reviewer SDK audit closure (#295-
perf(ws): orderbook materialization + apply_snapshot single-copy + ze…