Skip to content

Avoid rebuilding JSON arguments for tool invocation fingerprints - #6219

Closed
markstuart-oai wants to merge 1 commit into
mainfrom
perf/function-tool-arguments
Closed

markstuart-oai wants to merge 1 commit into
mainfrom
perf/function-tool-arguments

Conversation

@markstuart-oai

Copy link
Copy Markdown
Contributor

Summary

In an offline test with eight tool calls and 256 records per call, the full run has been dropping from 37.66 to 18.16 ms. A single call with 64 records has been taking 1.95 ms, down from 2.64 ms.

For 32 concurrent streams, the full run has been getting faster but the second batch has been taking 53 ms longer. The garbage collection measurements below have been showing why.

For each approval fingerprint, the SDK has been walking and rebuilding the full tree of JSON arguments after parsing it. The encoder has then been sorting the keys again.

With this change, the SDK has been passing parsed JSON directly to the same encoder. For values from Python objects, it has been handling plain strings, numbers, booleans and nulls before checking for mappings and sequences. Subclasses and custom objects have been keeping the existing conversion path.

The SDK has been computing each fingerprint from the current input. All approval and execution checks have been running as before. No cache or change to stored fingerprints.

Related work in #6191 on parsed JSON. Additional coverage here for plain values in non-JSON inputs and for matching the same nested arguments across representations.

Benchmarks

Complete request Main, ms PR, ms Change [95% interval]
Eight calls with 256 records each 37.66 18.16 -51.71% [-52.12, -51.28]
Eight calls with 64 records each 17.12 11.7 -31.25% [-31.96, -30.99]
One call with 64 records 2.638 1.945 -26.21% [-26.85, -26.00]
One call with one record 1.744 1.692 -3.26% [-3.88, -2.74]
32 concurrent streams, second batch 384.3 436.9 +14.16% [+11.25, +17.59]
32 concurrent streams, sum of three timed batches 1377 1308 -5.21% [-7.50, -3.27]

The separate 30-block replay of eight calls with 256 records each has been reproducing the gain (37.04 to 18.01 ms, −51.48%).

Slower second batch of streams. The second batch has been taking 53 ms longer. The first and third have been taking about 64 and 49 ms less. In a longer test of 12 batches across 24 pairs of processes, only batch two has been getting slower. Including setup, execution, validation and cleanup, time for all 12 has been dropping from 6.43 to 5.83 seconds (−8.80%, 95% interval −11.35 to −7.54%). The slowest batch per process has been getting 9.25% faster.

In a separate check of garbage collection, the full collection in three of four main processes has been landing in setup for batch three, outside the request timer. All four PR processes have been collecting during batch two. Both sides have been doing 12 full collections. The pause has been adding to request latency for that batch.

SQLite writes. The 64-pair repeats have been showing a slowdown for both the small write (+5.62% [2.11, 12.37]) and the large one (+2.29% [0.29, 7.52]). Both intervals have been crossing +3%.

A separate synchronized test has been rotating two copies of main and two copies of the branch across four writer positions in 128 blocks. With matched starts and full checks of the committed data, the small write has been measuring +0.64% [−1.45, +2.91] and the large write −0.02% [−0.50, +0.41]. Both intervals for the same-code controls have been covering zero for each write size. An untimed trace has been recording zero calls to either changed function. The evidence has been pointing to disk timing. The earlier results have been staying in the table.

Regression checks across all 83 comparisons with Codex off. Offline Python SDK workloads on one Linux host with CPython 3.12.16. This change alone against main at 4e162c79, without network or model latency.

95% interval versus a 3% slowdown Within margin Uncertain Above margin
Initial, all 83 80 3 0
Independent repeat, 4 selected 2 2 0
Latest result per workload 81 2 0
Timings and 95% intervals for all 83 comparisons
  • Initial — 32 matched pairs of fresh processes, balanced by run order.
  • Repeat — 64 new pairs for every initial upper bound above +3%, both SQLite writes and 32 concurrent streams. No pooling.
  • Main / PR — median elapsed time in milliseconds, from the repeat where available. For repeated calls, the median across process medians. For 32 streams, the median of three timed batches.
  • Change — median within-pair percentage change, with 95% bounds from 5,000 resamples of whole pairs. Negative for faster. Calculated from individual pairs before taking the median.
  • Margin — within for an upper bound ≤ +3%, above for a lower bound > +3%, otherwise uncertain. A result within the margin can include a small slowdown. Individual intervals only, without correction for multiple comparisons.
Workload Main, ms PR, ms Initial % [95% interval] Repeat % [95% interval]
Fresh-agent control, normal 40.787 40.845 -0.55 [-4.26, +2.15] —
Fresh-agent control, streaming 284.09 278.1 -1.05 [-2.99, +1.01] —
Integer-list output, 17 calls, 32769 initial entries, normal 236.18 239.25 +1.09 [-0.18, +2.69] —
Integer-list output, 17 calls, 32769 initial entries, streaming 265.24 263.81 -1.13 [-1.95, +0.47] —
Integer-list output, 3 calls, 8193 initial entries, normal 13.138 13.173 +0.32 [-1.85, +1.59] —
Integer-list output, 3 calls, 8193 initial entries, streaming 16.521 16.304 -1.70 [-2.57, +1.82] —
Reused-agent control, normal 38.021 37.584 -0.90 [-3.04, +0.92] —
Reused-agent control, streaming 39.585 39.303 -0.83 [-2.18, +1.58] —
Typed text-list output, 3 calls, 257 initial entries, normal 6.6606 6.5605 -2.00 [-4.30, +0.05] —
Typed text-list output, 3 calls, 257 initial entries, streaming 8.1356 8.1318 -0.88 [-1.96, +1.07] —
Dynamic tool addition 1.7123 1.6716 -2.18 [-2.82, -1.98] —
Agent with 12 tools, dictionary settings, construction only 0.051375 0.050962 -0.68 [-1.38, +0.10] —
Agent with 12 tools, default settings, construction only 0.070026 0.069843 -0.44 [-1.26, +0.36] —
Agent with 12 tools, typed settings, construction only 0.038275 0.038223 -0.24 [-0.74, +0.15] —
Concurrent runs, 1 task 0.51189 0.5083 +0.10 [-1.50, +1.68] —
Concurrent runs, 32 tasks 12.226 12.195 +0.53 [-0.48, +1.20] —
Concurrent runs, 8 tasks 2.9235 2.9103 -0.44 [-1.03, -0.03] —
32 concurrent runs with GC observation 12.129 12.105 -1.03 [-1.60, +0.92] —
Stream completion, 128 history messages 1.267 1.2651 -0.62 [-1.20, +0.51] —
0 available handoffs 0.49862 0.4962 -0.86 [-2.56, -0.07] —
12 available handoffs 0.71837 0.71835 -0.04 [-0.83, +0.73] —
Input history, 120 messages 0.93504 0.93979 +0.86 [+0.04, +1.35] —
Input history, 2000 messages 6.2446 6.2366 +0.04 [-1.33, +1.16] —
Input history, 720 messages 2.6376 2.6416 +0.12 [-0.93, +0.82] —
6 handoff routes, mixed agent names 0.72671 0.7292 +0.29 [+0.09, +0.48] —
Structured streamed response, 67 fields, 16 tool turns 73.993 72.95 -1.83 [-2.24, -0.87] —
8 handoff routes, repeated agent names 0.98896 0.994 +0.49 [-0.03, +0.82] —
Retained history, 16384 existing + 1 new message 0.39161 0.39386 +0.43 [-0.95, +2.47] —
Retained history, 2048 existing + 1 new message 0.053411 0.053441 +0.37 [+0.00, +1.06] —
Agent with 6 tools, dictionary settings, construction + run 0.77028 0.77306 +0.51 [-0.30, +0.71] —
Agent with 6 tools, default settings, construction + run 0.82202 0.82513 +0.47 [-0.00, +0.75] —
Agent with 6 tools, typed settings, construction + run 0.74512 0.74293 -0.25 [-0.90, +0.42] —
24 available tool schemas 0.84853 0.85272 +0.80 [+0.06, +1.31] —
Server-managed conversation, 1 history message 1.7972 1.7684 -1.90 [-2.17, -1.52] —
Server-managed conversation, 1024 history messages 34.372 34.631 +0.17 [-1.19, +1.12] —
Server-managed conversation, 128 history messages 4.5865 4.5904 +0.06 [-0.60, +0.84] —
Server-managed conversation, 512 history messages 14.945 14.836 +0.84 [-0.38, +3.01] -0.58 [-1.31, +0.15]
0 handoff routes, distinct agent names 0.50067 0.49808 -0.81 [-1.51, +0.21] —
12 handoff routes, distinct agent names 0.59564 0.59904 +0.40 [-0.23, +1.14] —
Streamed final response 0.79497 0.79089 +0.06 [-0.91, +0.80] —
32 sustained concurrent runs 12.008 12.056 -0.08 [-0.58, +0.66] —
SQLite committed batch, 1024 items × 4096 payload characters 58.59 60.886 +2.37 [-0.92, +5.44] +2.29 [+0.29, +7.52]
Retained history, 16384 existing + 2 new messages 0.80112 0.81173 +0.96 [+0.45, +1.65] —
Retained history, 2048 existing + 2 new messages 0.10503 0.10607 +0.87 [-0.37, +1.49] —
SQLite committed batch, 2 items × 256 payload characters 7.0738 7.4856 -0.71 [-4.22, +5.52] +5.62 [+2.11, +12.37]
Retained history, 4096 existing + 8 new messages 0.83168 0.84383 +1.60 [+0.73, +2.16] —
Tool loop, 1 turn 1.6293 1.6002 -1.84 [-2.23, -1.31] —
Tool loop, 16 turns 19.498 19.018 -2.38 [-2.57, -1.70] —
Tool loop, 4 turns 4.6003 4.4876 -2.92 [-3.55, -2.05] —
Warm final response 0.48395 0.48483 +0.21 [-1.05, +0.87] —
Tool-turn repetition control, 1 call, 1024 history messages 35.779 35.277 -1.20 [-2.10, -0.02] —
Tool-turn repetition control, 2 calls, 32 history messages 3.7543 3.6552 -2.35 [-2.91, -1.95] —
Tool turn, 12 calls, 64 history messages 16.909 16.586 -2.06 [-2.51, -1.54] —
Tool turn, 1 call, 1024 history messages 35.055 35.068 -0.44 [-1.52, +1.17] —
Tool turn, 1 call, 2048 history messages 93.362 93.824 +0.47 [-0.25, +0.86] —
Tool turn, 1 call, 256 history messages 7.8105 7.8364 -0.18 [-1.26, +0.53] —
Structured tool workload, 4 calls, 1024 history messages 59.298 55.497 -6.43 [-7.16, -5.61] —
Structured tool workload, 8 calls, 128 history messages 37.926 29.768 -20.56 [-21.93, -19.86] —
32 concurrent streams, 2 tool calls, 128 history messages 484.75 434.2 -10.46 [-12.16, -9.17] -9.92 [-10.95, -8.77]
First completed run, 1 tool turn, including launch 1125 1119.6 -0.81 [-1.67, +0.39] —
First completed run with empty tool server, 1 tool turn, including launch 1100.6 1107.9 +1.04 [-0.11, +2.12] —
List output, 1 call × 1024 entries 5.7276 5.7156 -0.31 [-1.69, +0.58] —
List output, 8 calls × 8192 entries 57.236 56.701 -0.96 [-2.36, -0.13] —
Provider-response conversion, 1 call, 2048 history messages 318.16 317.6 -0.38 [-1.38, +0.21] —
Provider-response conversion, 1 call, 4096 history messages 740.32 745.81 +0.76 [-1.35, +2.63] —
Line-item calculation, 1 call × 1 record 1.7441 1.6917 -3.26 [-3.88, -2.74] —
Line-item calculation, 1 call × 64 records 2.6382 1.9454 -26.21 [-26.85, -26.00] —
Line-item calculation, 8 calls × 16 records 11.695 9.9963 -14.44 [-15.31, -14.01] —
Line-item calculation, 8 calls × 256 records 37.657 18.159 -51.71 [-52.12, -51.28] —
Line-item calculation, 8 calls × 64 records 17.121 11.703 -31.25 [-31.96, -30.99] —
Structured response, 12 tool calls, 96 fields, normal 35.616 35.418 -1.05 [-3.05, +0.64] —
Structured response, 12 tool calls, 96 fields, stream 57.508 57.169 -1.46 [-2.63, -0.61] —
Structured response, 4 tool calls, 12 fields, stream 11.77 11.652 -0.93 [-2.67, +0.16] —
Structured response, 8 tool calls, 0 fields, stream 14.747 14.528 -1.77 [-2.24, -0.34] —
Sustained structured tool workload, 4 calls, 1024 history messages 59.843 56.13 -6.61 [-7.68, -5.96] —
Integer list, 1 call × 1 entry 1.7121 1.6748 -2.23 [-2.85, -1.76] —
Integer list, 8 calls × 8192 entries 32.869 32.98 -0.76 [-1.31, +0.62] —
Mixed list, 4 calls × 512 entries 10.236 10.196 -0.93 [-1.31, +0.14] —
Record list, 8 calls × 512 entries 12.973 12.775 -2.10 [-2.95, -1.58] —
Typed text list, 16 calls × 1024 entries 80.612 80.406 -0.57 [-1.53, +0.43] —
Typed text list, 4 calls × 128 entries 7.061 6.9823 -1.69 [-2.62, -1.20] —
Warm tool control, 1 call, 0 history messages 1.7468 1.701 -2.62 [-3.11, -2.31] —
Warm tool control, 1 call, 4096 history messages 35.866 36.091 +0.65 [-1.05, +1.16] —

Test plan and checks

  • New regression for matching object and JSON arguments with nested nulls, Unicode text and reordered keys. Different array order, different fingerprint. Existing approval, saved-state and custom tool context tests.
  • Full repository verifier (.agents/skills/code-change-verification/scripts/run.sh) including formatting, lint, mypy, Pyright and both pytest phases. 12,865 passed, 30 skipped.
  • Independent review of the complete diff. Identical Git tree for the local checks and PR head.

@markstuart-oai
markstuart-oai requested review from a team, rm-openai and seratch as code owners November 25, 2026 04:33
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T04:34:55.069027Z 46eb734 PR opened
🔒 Security Review ✅ Completed 2026-11-25T04:35:06.092180Z 46eb734 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@jbeckwith-oai jbeckwith-oai left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved after static review and independent review. Returning parsed JSON directly preserves the fingerprint because the final encoder recursively sorts object keys; array order, nested nulls, Unicode, and invalid-constant handling remain unchanged. The exact-builtin scalar shortcut preserves the existing normalization path for subclasses and custom objects. No actionable correctness or approval-identity compatibility findings.

Hosted CI completed: 24 checks passed and 2 were skipped, with no failures. No candidate code, local tests, or benchmarks were executed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants