Skip to content

perf(core): flatten streamed chunk event generation with concatMapIterable - #4197

Open
wahllllll wants to merge 1 commit into
agentscope-ai:mainfrom
wahllllll:perf/issue-3929-sync-chunk-flatten
Open

wahllllll wants to merge 1 commit into
agentscope-ai:mainfrom
wahllllll:perf/issue-3929-sync-chunk-flatten

Conversation

@wahllllll

Copy link
Copy Markdown
Contributor

Summary

Third and last measured change from #3929: generate streaming chunk events synchronously and flatten with concatMapIterable, instead of subscribing an inner Flux per chunk.

Problem

Both chunk paths (preparedModelCallStream, preparedSummaryModelCallStream) processed every streamed chunk inside Flux.deferContextual(...) and returned Flux.fromIterable(events): one Reactor subscription handshake per chunk (deferContextual + inner fromIterable), serial per chunk. At high stream counts that handoff dominates end-to-end latency — #3929 measured ~2x inflation at 500 concurrent streams, and the reporter's fork attributed −7.2 s per 500-chain mean to this change alone. After the first two changes landed (#3961, #4088), the reporter's re-benchmark on current main (2026-11-14) still attributes the remaining ~24.6 s gap to their fork reference to this third change.

Solution

  • Replace the per-chunk concatMap + deferContextual + fromIterable with concatMapIterable(chunk -> { ... }) in both chunk paths.
  • The subscriber context is constant along the chunk chain (no operator between the subscription point and the mapper writes context), so it is now captured once per stream for hook delivery (.contextWrite(ctx -> ctx.putAll(parentCtx)) on the hook call) instead of once per chunk.
  • Sequential per-chunk processing, ordering, backpressure and completion/error behavior are unchanged (untilInterrupted stays in place; the mapper drains synchronously during concat drain).

Tests

New ReActAgentChunkFlattenTest:

  • 31-chunk scripted stream (thinking + 30 text): deltas arrive in exact model order, none lost; thinking delta precedes first text delta; ModelCallStartEvent/ModelCallEndEvent present.
  • Hook registered + downstream contextWrite: hook still sees context written at subscription time — pins the capture-once context semantics.

Full agentscope-core suite: 3453 tests, 0 failures (2 pre-existing Windows symlink-privilege environment errors in DangerousPathBypassTest, unrelated to this change — they fail in test setup before any agent code runs).

The end-to-end number belongs to @realreus319's harness, same as the first two changes.

Refs #3929

…apIterable

The reasoning and summary chunk paths subscribed an inner Flux per chunk
(deferContextual + fromIterable), costing one Reactor subscription handshake
per streamed chunk; at high stream counts that serial handoff dominates
end-to-end latency (~2x at 500 concurrent streams, measured in agentscope-ai#3929).

Generate events synchronously per chunk and flatten with concatMapIterable
instead. The subscriber context is constant along the chunk chain, so it is
now captured once per stream for hook delivery rather than once per chunk.
Sequential per-chunk processing, ordering, and completion/error behavior
are unchanged; ordering and hook-context inheritance are pinned by tests.

Refs agentscope-ai#3929
@realreus319

Copy link
Copy Markdown

Results for #4197, same method as the #3961/#4088 runs: full Maven builds of parent (30dc84b5, current main tip, already containing both earlier changes) and head (7c678c9f), byte-identical benchmark scaffold, and a class-level jar diff first — 19,072 files each, only 11 differ, all ReActAgent*.class; your test class is not in the production jar. Calibration on head: 61.07 / 61.06 s, exact 10/9/300 counts. All 7 groups: 100% completion, zero errors/timeouts, exact counts.

Timing: reproduced — and larger than the historical figure.
Per-token delivery, 500 chains, two parent/head pairs: −23.58 s and −18.47 s; means parent 146.5 s vs head 125.4 s, so ≈ −21.0 s (−14.4%). The two pairs agree in sign and magnitude, and same-jar drift was only 1.6–3.5 s, so this is not noise. The historical −7.2 s was measured on 2.0.4 stacked on top of the first two changes; on current main, this change alone is worth about three times that. We did not isolate why the magnitude grew — recording it, not speculating.
Under burst delivery: parent 126.8 s vs head 117.4 s, −9.4 s (−7.4%) — the first of the three changes to show a clear gain under burst delivery too; the first two were unresolvable there. TTFT: no directional conclusion (per-token pairs −0.30 s / +0.35 s; burst +1.42 s; this session's TTFTs ran 5.3–6.7 s on both arms, higher than earlier sessions, so only within-session deltas are meaningful).

Summary from our side: independent measurement, no objection. Of the three changes, this one is the largest single gain we measured on current main, with no change in counts, completion, or TTFT direction. All three changes from #3929 are now implemented and independently verified as far as our harness can tell.

@jujn jujn added the P2 重要不紧急 label Nov 16, 2026
@wahllllll

Copy link
Copy Markdown
Contributor Author

Hi @jujn, when you have a chance, could you take a look at this PR? It’s been open for a few weeks, and I’d appreciate any feedback or guidance on what would help move it forward. The existing checks are passing, and the independent benchmark results are in the thread above.

A few other PRs are also awaiting review: #3786, #3889, #4065, and #3457. No need to look at all of them at once. If there’s anything I should change or test before review, I’m happy to follow up. Thanks for your time!

This branch has not been deployed

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

Labels

P2 重要不紧急

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants