Typed input bodies are parsed and copied several times before decoding

Open
#742 1 comment 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
rust

Research direction

Start by reading the typed input decoding path, TrimStrings, ConvertEmptyStringsToNull, and the request.input(..) path; the issue names input::walk, from_raw_json, to_json_map, and sync_json_value as relevant entry points. Check how the linked decoder PR affects those paths, then compare typed decoding and lazy input access against current behavior, including error messages and transformed strings. Done means equivalent validation and input behavior with less repeated parsing and copying; the issue asks maintainers to decide on the direction before implementation.

Written by the indexing model from the issue text.

Description

A #[laravel::input] body is parsed and copied several times before the handler sees it. On a 10 KB JSON POST this makes the idiomatic handler about 5x slower than the same handler decoding the raw body with serde_json::from_slice, even after the decoder fix in the linked PR.

Measurement

AWS m7i-flex.large (2 vCPU), separate c7i-flex.large load generator, oha, 64 connections, 3 runs of 20 s each. Fresh laravel new --rust app, default middleware, default release profile. The body is {"items": [{"id": i64, "name": String, "price": i64}, ...229 rows]}, 10,221 bytes.

#[laravel::input]
pub struct Row { id: i64, name: String, price: i64 }

#[laravel::input]
pub struct Payload { items: Vec<Row> }

async fn echo(request: Request) -> laravel::Result<Json<Value>> {
    let payload: Payload = request.json()?;
    // ...
}
Handler Requests/s p99
request.json() on 0.1.5 709 149 ms
request.json() with the linked PR 1,553 64 ms
serde_json::from_slice(request.content()), plain Deserialize, input middleware removed 8,303 n/a

The last row skips validation messages and trimming, so it is a ceiling, not a target.

Where the time goes now

perf record -g on the server under load, with the linked PR applied (share of total CPU, inclusive):

Step Share
TrimStrings walk (input::walk with the trim transform) 9.7%
ConvertEmptyStringsToNull walk 9.6%
Building and editing the serde_json::Value tree (to_json_map, IndexMap insert and swap_remove, InputNumber::to_json, sync_json_value) about 12%
Parsing the body into InputValue (from_raw_json) about 5%
Allocator (mi_malloc, mi_free) about 12%
Typed decoding itself about 3%

So for a typed handler, the body becomes an InputValue tree, both transforms walk the whole tree, the tree is copied into a serde_json::Value tree, and only then is the typed value read.

Proposal

Decode a typed input in one pass:

  1. Read the raw body directly into T, applying trim and empty to null to each string as it is read, instead of walking the tree first.
  2. Build the InputValue tree lazily, only for code that reads request.input(..).
  3. Keep the current error collection and messages: the decoder already knows each value's path when a value fails.

Results would match today's for every input, since the same transforms run on the same strings, only at a different time. A differential test could check this on generated bodies (unicode and whitespace kinds, empty strings, nulls, nesting, Option/Box/Cow wrappers, Password, ULID, decimals, duplicate keys, the error limit).

This changes when request bodies are parsed, so I'd like to know whether you would accept this direction before writing it.

Dominant language
Rust
Stars
847
Forks
174
Avg merge
5h 40m
Merged PRs (30d)
167

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from laravel/rust-framework

All issues in laravel/rust-framework

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.