You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "AlinsRan (via GitHub)" <gi...@apache.org> on 2026/05/12 02:13:00 UTC
[PR] perf(ctx): cache parsed request body to avoid repeated decode for post_arg.* [apisix]
AlinsRan opened a new pull request, #13356:
URL: https://github.com/apache/apisix/pull/13356
## Summary
When multiple `post_arg.*` variables are evaluated in a single request
(e.g. `vars` expressions in route matching), each access independently
triggers a full body read + `json.decode` cycle. For large request
bodies at high QPS, this causes unnecessary repeated CPU and memory
overhead.
## Changes
Split `get_parsed_request_body` into two layers:
- `_get_parsed_request_body(ctx)` — pure parsing logic, no side effects
- `get_parsed_request_body(ctx)` — wrapper that adds a per-request ctx cache
On the first access, the decoded table is stored in `ctx._parsed_request_body`.
Subsequent accesses to different `post_arg.*` keys within the same request
reuse the cached result, so `json.decode` runs at most once per request.
Errors are intentionally **not cached**: plugins may call
`ngx.req.set_body_data()` in later phases, so a transient parse failure
should not be permanently frozen for the request lifetime.
## Tests
Added TEST 3/4 in `t/core/ctx3.t`: a route with three different
`post_arg.*` keys, verifying all fields are read correctly and that
the `reuse parsed request body from ctx cache` debug log appears
exactly twice (for the second and third key accesses).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] perf(ctx): cache parsed request body to avoid repeated decode for post_arg.* [apisix]
Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan merged PR #13356:
URL: https://github.com/apache/apisix/pull/13356
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] perf(ctx): cache parsed request body to avoid repeated decode for post_arg.* [apisix]
Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on PR #13356:
URL: https://github.com/apache/apisix/pull/13356#issuecomment-4427473004
The body rewrite cache invalidation approach (body version counter + `patch.lua` monkey-patching + `set_body_data` hook) has been reverted from this PR.
The existing variable-level cache in `ctx.__index` has the same limitation — it does not invalidate on `set_body_data` either — so fixing it only for the body-level cache would be inconsistent and add complexity without addressing the general problem. That is a separate concern for a future PR.
This PR now contains only the core optimization: `get_parsed_request_body` caches the parsed body table in `ctx` for the request lifetime, so `json.decode` is called at most once per request regardless of how many different `post_arg.*` keys are accessed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org