Skip to content

perf(ctx): cache parsed request body to avoid repeated decode for post_arg.*#13356

Open
AlinsRan wants to merge 8 commits into
apache:masterfrom
AlinsRan:fix/post-arg-repeated-body-decode
Open

perf(ctx): cache parsed request body to avoid repeated decode for post_arg.*#13356
AlinsRan wants to merge 8 commits into
apache:masterfrom
AlinsRan:fix/post-arg-repeated-body-decode

Conversation

@AlinsRan
Copy link
Copy Markdown
Contributor

@AlinsRan AlinsRan commented May 12, 2026

Summary

When multiple post_arg.* variables are evaluated in a single request
(e.g. vars expressions in route matching or plugin-level vars conditions
in traffic-split / fault-injection), 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

apisix/core/ctx.lua

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 caches the decoded table in ctx._post_arg_request_body for the lifetime of the request

On the first access, the decoded table is stored in ctx._post_arg_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: a failed parse leaves the cache
empty so that a later access (e.g. after a plugin sets a valid body) can
still succeed.

apisix/patch.lua

Patch ngx.req.set_body_data to automatically invalidate both cache layers
when the request body is rewritten mid-request:

  • Clears api_ctx._post_arg_request_body (body-level cache)
  • Removes all post_arg.* entries from api_ctx.var._cache (variable-level cache)

The invalidation is skipped entirely when _post_arg_request_body is nil,
avoiding any overhead on requests that never access post_arg.* variables.

Tests

  • TEST 3/4 (t/core/ctx3.t): verifies all fields are read correctly and
    that reuse parsed request body from ctx cache appears exactly twice when
    three different post_arg.* keys are accessed in one request.
  • TEST 5/6 (t/core/ctx3.t): verifies that after ngx.req.set_body_data(),
    both the same key and a different key reflect the new body content.

…t_arg.*

When multiple post_arg.* variables are evaluated in a single request
(e.g. vars expressions in route matching), each access triggers a full
body read + json.decode cycle independently.

Introduce a two-layer design: _get_parsed_request_body() handles pure
parsing, while get_parsed_request_body() wraps it with a per-request
ctx cache. Subsequent accesses to different post_arg.* keys reuse the
already-decoded table, reducing CPU and memory overhead for large
request bodies.

Errors are intentionally not cached since plugins may call
ngx.req.set_body_data() in later phases.
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. performance generate flamegraph for the current PR labels May 12, 2026
patch.lua increments ngx.ctx._body_version on each ngx.req.set_body_data
call. get_parsed_request_body compares the cached version against the
current one to detect staleness automatically.

This decouples patch.lua from ctx internals and provides a general
mechanism that any future body-dependent cache can reuse. Storing the
version in ngx.ctx (not api_ctx) is appropriate since set_body_data
is a nginx-level operation.
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels May 12, 2026
Body rewrite cache invalidation (ngx.ctx._body_version + patch.lua) is
out of scope for this PR. The existing variable-level cache in ctx.__index
has the same limitation, so fixing it here inconsistently adds complexity
without solving the general problem.

Keep the simpler body-level cache in get_parsed_request_body without
version tracking.
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels May 12, 2026
Patch ngx.req.set_body_data in patch.lua to clear both the body-level
cache (api_ctx._post_arg_request_body) and any post_arg.* entries in
the variable-level cache (api_ctx._cache) when the request body is
rewritten.

The check api_ctx._post_arg_request_body ~= nil avoids iterating _cache
on requests that never access post_arg.* variables.

Also rename the body-level cache key from _parsed_request_body to
_post_arg_request_body to better reflect its purpose.
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels May 12, 2026
AlinsRan added 4 commits May 13, 2026 04:47
api_ctx._cache does not exist; the variable-level cache lives at
api_ctx.var._cache. Without this fix the cache iteration was a no-op
and post_arg.* keys remained stale after set_body_data.
Add type(key) == "string" check before key:sub() to avoid errors
if non-string keys exist in the variable cache.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance generate flamegraph for the current PR size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants