Skip to content

Polish bundle: perf + testing small items (asyncio import, MessageQueue._size, http2 test, iterator/dataframe bench) #271

Closed

Description

@TexasCoding

Summary

Small perf + testing-coverage items bundled for a single PR.

Item 1 — import asyncio inside AsyncTransport.request hot path

A sys.modules lookup per async request. Negligible but in the per-request hot path and pointless since the module is already imported by KalshiAuth.sign_request_async.

Location: kalshi/_base_client.py:374.

Fix: Move import asyncio to the module's top-level imports.

Item 2 — MessageQueue._size maintained per put/get is redundant with len(deque)

Overflow check uses len(self._buffer) (O(1)), but _size is incremented/decremented on every put and popleft purely so qsize() excludes the single sentinel. Two extra int ops per message on the WS hot path with no functional gain over len(self._buffer) - (1 if self._closed else 0).

Location: kalshi/ws/backpressure.py:71-100,138-140.

Fix: Drop _size and compute qsize() from len(self._buffer) minus a sentinel adjustment when _closed.

Item 3 — No behavioural test or bench for http2=True multiplexing

The advertised h2 multiplexing benefit (#233) is never exercised end-to-end. A regression that silently disables ALPN h2 negotiation, or that mishandles concurrent requests over a single h2 connection, would land without any test failing.

Location: kalshi/_base_client.py:163,347, tests/test_config.py:265-303.

Fix: Add an integration test that hits the demo endpoint with http2=True and asserts response.http_version == "HTTP/2" on a concurrent gather; add a bench variant of bench_request_hot_path.py parameterized on http2.

Item 4 — No bench for high-level Orderbook iterator or DataFrame conversion paths

Both existing orderbook benches stop at the in-place mutation and never measure the mgr.get(ticker) / to_orderbook() materialization that high-level consumers pay. DataFrame conversion (Page.to_dataframe on large pages) and large-list model_validate are completely unbenched.

Location: scripts/bench_orderbook_delta.py, scripts/bench_ws_recv.py, scripts/bench_request_hot_path.py.

Fix: Add a bench that drives _OrderbookIterator.__anext__ end-to-end, and a bench_page_to_dataframe.py that times a 1000-item Page → DataFrame conversion.

Severity & category

low / polish, testing, performance

Activity

  1. added
    polishCode-quality and DX improvements; non-functional
    performancePerformance / hot-path concern
    on May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance / hot-path concernpolishCode-quality and DX improvements; non-functionaltestingTest coverage

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions