AlinsRan opened a new pull request, #13377:
URL: https://github.com/apache/apisix/pull/13377

   ## Summary
   
   `get_json_request_body_table()` is called on every request by plugins that 
need to inspect or modify the JSON request body. Each call previously triggered 
a full `json.decode()`, even when the body had not changed within the same 
request.
   
   ## Changes
   
   ### `apisix/core/request.lua`
   
   Cache the decoded table in `ctx._request_body_tab` on the first call. 
Subsequent calls within the same request return the cached table directly.
   
   ### `apisix/patch.lua`
   
   Extend the existing `set_body_data` patch to also clear `_request_body_tab`. 
This ensures that any plugin that rewrites the body will cause the next 
`get_json_request_body_table()` call to re-decode from the new content.
   
   ## Performance
   
   Benchmarked on a single APISIX worker with a 1 MB JSON request body (mock 
upstream, non-streaming response):
   
   | Scenario | RPS |
   |---|---|
   | Before (4 redundant decodes/request) | ~70 |
   | After (1 decode/request) | ~180 |
   
   ~157% throughput improvement. The gain scales with body size — larger bodies 
benefit more since `json.decode` cost grows linearly with input size.
   
   ## Test
   
   Added TEST 17 in `t/core/request.t`:
   - Patches `json.decode` with a call counter
   - Verifies 3 successive calls produce only 1 decode and return the same 
table object
   - Calls `ngx.req.set_body_data()` to replace the body, then verifies the 
next call re-decodes (counter becomes 2) and returns the new content


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to