Principle:AGenUI AGenUI Batch Coalescing Flush System
| Knowledge Sources | |
|---|---|
| Domains | Performance, UI_Framework |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
A batch coalescing mechanism that groups multiple component update operations into a single flush to the native renderer, reducing redundant layout passes and render callbacks during rapid streaming updates.
Description
In a streaming UI pipeline, each text chunk from an LLM may produce multiple component updates. Without coalescing, each update triggers a separate layout calculation and native render callback, causing excessive work and janky rendering.
The BatchGuard / BatchScope system solves this by implementing a coalescing window: when a `BatchScope` is active on a surface's `BatchGuard`, all dirty component marks are accumulated. When the scope exits (RAII), a single flush is triggered, emitting all accumulated diffs in one pass. This is analogous to `requestAnimationFrame` batching in web frameworks.
The streaming pipeline uses this at multiple levels:
- `StreamingContentParser::dispatchParseResultsBatched()` merges contiguous `ComponentUpdate` results with the same `surfaceId` into a single `updateComponents` JSON call.
- `SurfaceCoordinator::invalidateFunctionCallValues()` wraps each surface in a `BatchScope`.
- `SurfaceCoordinator::handleRenderFinish()` and `handleSyncUIToData()` both use `BatchScope` to coalesce their respective operations.
Usage
Use this principle when:
- A streaming UI pipeline produces many small updates that should be batched for performance
- Multiple component changes should result in a single native render callback
- Layout recalculation should be minimized during rapid data model changes
Theoretical Basis
The coalescing window pattern (RAII-based):
// When a BatchScope is active, dirty marks accumulate.
// On scope exit, a single flush emits all diffs.
{
BatchScope batchScope(surface->batchGuard());
// ... multiple component attribute changes ...
surface->invalidateFunctionCallValues();
// ... more changes ...
} // ← scope exits: single flush to native renderer
The streaming batch merge:
Parse results: [Comp_A, Comp_B, Comp_C (same surfaceId)]
→ merged into one updateComponents JSON:
{"updateComponents": {"surfaceId": "s1", "components": [A, B, C]}}
→ single coordinator->updateComponents(json) call
→ single layout + render callback
Practical Guide
- The batch system is internal to the SDK; host code benefits automatically.
- When implementing custom surface operations, wrap them in a `BatchScope` to coalesce.
- The `BatchGuard` is per-surface; batching is independent across surfaces.