Typed input bodies are parsed and copied several times before decoding
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
- Domain
- backend, performance
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:
- 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. - Build the
InputValuetree lazily, only for code that readsrequest.input(..). - 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from laravel/rust-framework
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
laravel/rust-framework#864 ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 64/100
laravel/rust-framework#876 ·
Maintainers usually reply within 1 day
-
Case-sensitive contains, starts_with and ends_with that match % and _ literally on every databaseOpen
Difficulty 4/5 3-5 days Newbie friendliness 58/100
laravel/rust-framework#874 ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
laravel/rust-framework#873 ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 58/100
laravel/rust-framework#872 ·
Maintainers usually reply within 1 day
All issues in laravel/rust-framework
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
neuralmagic/crucible#579 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
eclipse-score/kyron#249 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
seanmonstar/reqwest#3157 ·
Maintainers usually reply within 2 days