[PR #4686] [Other] make remaining streamed tool argument parsing linear #5136

Open
opened 2026-11-07 16:32:58 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4686
Author: @InB4DevOps
Created: 10/30/2026
Status: 🔄 Open

Base: main ← Head: perf/python-stream-tool-buffer


📝 Commits (1)

  • a235f9a [Other] make streamed tool argument parsing linear

📊 Changes

3 files changed (+117 additions, -9 deletions)

View changed files

📝 serve/frontend.py (+61 -8)
📝 serve/test_native_tool_parser.py (+53 -0)
📝 serve/test_parser_call_scan.py (+3 -1)

📄 Description

Problem and result

A streamed tool call with a large string argument still keeps its whole body in OutputParser.buf. Every small model delta appends to that growing Python string even after _scan() has emitted the text to the client. The work therefore grows quadratically with the argument size.

On this DGX Spark, with four-character deltas pinned to one Cortex-X925 core, the median parser time changed as follows:

String argument current main this PR speedup
100 KB 0.358 s 0.044 s 8.2x
200 KB 1.646 s 0.087 s 18.9x
400 KB 5.083 s 0.174 s 29.2x
800 KB 20.301 s 0.348 s 58.3x

The patched times grow approximately in proportion to input size. The emitted event counts and final parsed arguments matched in every measured run. These are synthetic Python parser timings, not end-to-end inference throughput.

This is a focused follow-up to #1557, incorporated as fc27296d. That change avoids structural walks before a possible closing tag arrives, but its description explicitly leaves repeated tool-body string copying and linear scaling unresolved.

The broader branch in #1017 also described a _CallBoundary parser rewrite. That branch closed after repository history was force-pushed and its parser rewrite is not in current main. This PR keeps the current parser and addresses only its remaining growing-buffer cost.

No new issue or scope agreement: this is a focused performance fix with no new option or default change.

Approach

Move already-scanned call text out of the active string in chunks. Small incoming deltas then append to a bounded buf instead of copying the whole argument. Restore the chunks only when a structural walk or the final call parse needs the complete body.

Closing-tag searches remember how much text they checked, including recovery mode and </function> detection, so a literal closing tag in a string value does not cause a full walk on every later delta. clean counts compacted chunks as held text. Final parsing, recovery behavior, streamed JSON, call IDs, and public events remain unchanged.

The change is limited to the Python server parser and its regression test.

Validation

Baseline: 870aedd9e1ebe1c91f94da93b59af17729f6c5c0 (main at the start of this work). Candidate: d6a3b39f.

Environment: NVIDIA DGX Spark, Linux aarch64, Ubuntu 24.04.5, kernel 7.0.0-1019-nvidia, Strata's CPython 3.12.15. The primary benchmark was pinned with taskset -c 5 to a Cortex-X925 performance core (maximum 3.9 GHz). Each table entry is the median of five runs; baseline and candidate order alternated. The harness validated the final argument and event count after every run.

Passed:

  • /home/admin/.local/share/strata/terminal/0.1.54/python/bin/python3.12 -m unittest discover -s serve -p 'test_*.py' — 1,257 tests passed; 38 skipped; 373.942 s.
  • /home/admin/.local/share/strata/terminal/0.1.54/python/bin/python3.12 /tmp/opencode/check_stream_tool_parser_parity.py — 120 randomized large streamed calls matched the baseline after every feed and at finish(). Inputs included literal </parameter>, </function>, and </tool_call> strings, incomplete calls, random chunk widths, and both recovery settings.
  • taskset -c 5 /home/admin/.local/share/strata/terminal/0.1.54/python/bin/python3.12 /tmp/opencode/bench_stream_tool_parser_dgx_spark.py — five alternating samples at 100/200/400/800 KB produced the table above; all final arguments and event counts matched.
  • The same A/B harness with chunk widths 4, 32, and 256 and three repetitions remained faster in all 12 cases. At 800 KB the median speedups were 62.0x, 57.7x, and 36.5x respectively.
  • git diff --check origin/main...HEAD — no errors.

Failed:

  • The first targeted test run with the system python3 could not import regex in HTTP/server tests (ModuleNotFoundError). There were no parser assertion failures. Re-running the targeted tests and then the complete serve suite with Strata's bundled Python passed as reported above.

Not run:

  • End-to-end model inference benchmarks: this change is isolated to parser CPU work and the existing overnight model benchmarks were not repeated.
  • Windows, macOS, or x86-64 validation: behavior and performance were measured on this aarch64 DGX Spark only.
  • CUDA, HIP, SYCL, and Metal builds: no engine or native source file changed.

Limits

The measurable benefit is for streamed tool calls with large string-valued arguments, such as generated file contents. Normal replies, small calls, non-streamed calls, model inference, network transfer, and the tool's own execution are not made faster.

The complete call is still retained and materialized once for final validation, so retained memory remains linear in argument size. Inputs containing many literal structural closing tags can still require additional structural walks.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4686 **Author:** [@InB4DevOps](https://github.com/InB4DevOps) **Created:** 10/30/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/python-stream-tool-buffer` --- ### 📝 Commits (1) - [`a235f9a`](https://github.com/Niko1221/Strata/commit/a235f9a7d6de494971ee5f0d53feaeb86c9c5111) [Other] make streamed tool argument parsing linear ### 📊 Changes **3 files changed** (+117 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `serve/frontend.py` (+61 -8) 📝 `serve/test_native_tool_parser.py` (+53 -0) 📝 `serve/test_parser_call_scan.py` (+3 -1) </details> ### 📄 Description ## Problem and result A streamed tool call with a large string argument still keeps its whole body in `OutputParser.buf`. Every small model delta appends to that growing Python string even after `_scan()` has emitted the text to the client. The work therefore grows quadratically with the argument size. On this DGX Spark, with four-character deltas pinned to one Cortex-X925 core, the median parser time changed as follows: | String argument | current `main` | this PR | speedup | |---:|---:|---:|---:| | 100 KB | 0.358 s | 0.044 s | 8.2x | | 200 KB | 1.646 s | 0.087 s | 18.9x | | 400 KB | 5.083 s | 0.174 s | 29.2x | | 800 KB | 20.301 s | 0.348 s | 58.3x | The patched times grow approximately in proportion to input size. The emitted event counts and final parsed arguments matched in every measured run. These are synthetic Python parser timings, not end-to-end inference throughput. ## Related discussion This is a focused follow-up to [#1557](https://github.com/Niko1221/Strata/pull/1557), incorporated as `fc27296d`. That change avoids structural walks before a possible closing tag arrives, but its description explicitly leaves repeated tool-body string copying and linear scaling unresolved. The broader branch in [#1017](https://github.com/Niko1221/Strata/pull/1017) also described a `_CallBoundary` parser rewrite. That branch closed after repository history was force-pushed and its parser rewrite is not in current `main`. This PR keeps the current parser and addresses only its remaining growing-buffer cost. No new issue or scope agreement: this is a focused performance fix with no new option or default change. ## Approach Move already-scanned call text out of the active string in chunks. Small incoming deltas then append to a bounded `buf` instead of copying the whole argument. Restore the chunks only when a structural walk or the final call parse needs the complete body. Closing-tag searches remember how much text they checked, including recovery mode and `</function>` detection, so a literal closing tag in a string value does not cause a full walk on every later delta. `clean` counts compacted chunks as held text. Final parsing, recovery behavior, streamed JSON, call IDs, and public events remain unchanged. The change is limited to the Python server parser and its regression test. ## Validation Baseline: `870aedd9e1ebe1c91f94da93b59af17729f6c5c0` (`main` at the start of this work). Candidate: `d6a3b39f`. Environment: NVIDIA DGX Spark, Linux aarch64, Ubuntu 24.04.5, kernel `7.0.0-1019-nvidia`, Strata's CPython 3.12.15. The primary benchmark was pinned with `taskset -c 5` to a Cortex-X925 performance core (maximum 3.9 GHz). Each table entry is the median of five runs; baseline and candidate order alternated. The harness validated the final argument and event count after every run. **Passed:** - `/home/admin/.local/share/strata/terminal/0.1.54/python/bin/python3.12 -m unittest discover -s serve -p 'test_*.py'` — 1,257 tests passed; 38 skipped; 373.942 s. - `/home/admin/.local/share/strata/terminal/0.1.54/python/bin/python3.12 /tmp/opencode/check_stream_tool_parser_parity.py` — 120 randomized large streamed calls matched the baseline after every feed and at `finish()`. Inputs included literal `</parameter>`, `</function>`, and `</tool_call>` strings, incomplete calls, random chunk widths, and both recovery settings. - `taskset -c 5 /home/admin/.local/share/strata/terminal/0.1.54/python/bin/python3.12 /tmp/opencode/bench_stream_tool_parser_dgx_spark.py` — five alternating samples at 100/200/400/800 KB produced the table above; all final arguments and event counts matched. - The same A/B harness with chunk widths 4, 32, and 256 and three repetitions remained faster in all 12 cases. At 800 KB the median speedups were 62.0x, 57.7x, and 36.5x respectively. - `git diff --check origin/main...HEAD` — no errors. **Failed:** - The first targeted test run with the system `python3` could not import `regex` in HTTP/server tests (`ModuleNotFoundError`). There were no parser assertion failures. Re-running the targeted tests and then the complete `serve` suite with Strata's bundled Python passed as reported above. **Not run:** - End-to-end model inference benchmarks: this change is isolated to parser CPU work and the existing overnight model benchmarks were not repeated. - Windows, macOS, or x86-64 validation: behavior and performance were measured on this aarch64 DGX Spark only. - CUDA, HIP, SYCL, and Metal builds: no engine or native source file changed. ## Limits The measurable benefit is for streamed tool calls with large string-valued arguments, such as generated file contents. Normal replies, small calls, non-streamed calls, model inference, network transfer, and the tool's own execution are not made faster. The complete call is still retained and materialized once for final validation, so retained memory remains linear in argument size. Inputs containing many literal structural closing tags can still require additional structural walks. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5136
No description provided.