Skip to content

Speed up the pure-Python json decoder and encoder for small documents #150820

@gaborbernat

Description

@gaborbernat

json.loads() and json.dumps() enter through short Python wrappers before the C accelerator does the heavy lifting, and those wrappers run on every call. Decoding scans for leading and trailing whitespace with a regular expression at both ends of the document; encoding builds a float-formatting helper closure each time. For the small documents that dominate real traffic — an API request body, a single record, a config fragment — that fixed per-call overhead is a noticeable fraction of the total work.

Most JSON crossing a service boundary is small and has no surrounding whitespace to skip. Web frameworks, RPC layers and log shippers decode and encode these short payloads constantly, one per request or record.

Over 20 small documents, json.loads() improves from 17.1 µs to 13.4 µs (28% faster) by skipping the whitespace scan when there is nothing to skip, and json.dumps() improves by 8% by building the float helper only when the slower Python encoding path actually needs it. Larger documents and every other shape are unchanged, and output is byte-identical. Both wins apply to the default build with the C accelerator, since the wrappers that carry this overhead are written in Python.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions