Skip to content

Perf: REST has no pluggable JSON loader — orjson cannot speed up list parsing #260

Closed

Description

@TexasCoding

Summary

REST response parsing is locked to httpx's stdlib response.json(). List endpoints (markets.list_all, trades.list_all, fcm.orders) return thousands of rows where parsing dominates wall time; orjson would cut REST list latency 2-3x but there is no opt-in. Inconsistent with the WS hot path which already accepts ws_json_loads (#209).

Location

  • kalshi/config.py:52-56 — ws_json_loads exists
  • kalshi/resources/_base.py:137-141 — REST path uses response.json()
  • kalshi/_base_client.py:67 — transport .json() usage

Evidence

# config.py docstring:
ws_json_loads: Optional callable used to parse WS frames. ``None``
    falls back to :func:`json.loads`. Set to e.g. ``orjson.loads``
    for ~2-3x faster recv-loop parsing on high-volume streams.

# resources/_base.py:
result: dict[str, Any] = response.json()
return result

Recommended fix

Add rest_json_loads: Callable[[bytes], Any] | None = None to KalshiConfig and use it in _get/_post/_list (replace response.json() with loader(response.content) when set). Add a small bench variant of bench_request_hot_path.py parametrized on the loader.

Severity & category

medium / performance

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

    enhancementNew feature or requestperformancePerformance / hot-path concern

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions