Skip to content

perf: avoid double-AST allocation for JSON bodies and leverage Pydantic model_validate_json #255

Closed

Description

@ZhuchkaTriplesix

Where: src/dispatch.rs:889-908, 1217-1250 and src/schema.rs:49-53 (json_to_py).

Currently, JSON request bodies follow this path:

  1. serde_json::from_slice::<JsonValue>(&body_bytes) constructs a full Rust AST (serde_json::Value), allocating every map, array, and string on the Rust heap.
  2. json_to_py traverses the Rust AST and allocates a duplicate Python AST (PyDict, PyList, PyString).
  3. If body_model is present, model_validate(pyv) is called on the Python dictionary.

Fix direction:

  1. When a Pydantic model (body_model) is configured, call model_validate_json(body_bytes) directly. Pydantic v2 parses JSON in Rust/C via pydantic-core 5-10x faster with zero intermediate Python dict allocations.
  2. When no Pydantic model is present, deserialize bytes directly to Python objects without the intermediate serde_json::Value Rust AST.

Activity

  1. added
    enhancement
    on Oct 18, 2026
  2. added a commit that references this issue on Oct 18, 2026
  3. ZhuchkaTriplesix commented on Oct 18, 2026

    @ZhuchkaTriplesix
    MemberAuthor

    Resolved in PR #262

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

    P1enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions