fix(opencode): Throttle storage writes during streaming to reduce I/O overhead#11328
Open
taetaetae wants to merge 2 commits intoanomalyco:devfrom
Open
fix(opencode): Throttle storage writes during streaming to reduce I/O overhead#11328taetaetae wants to merge 2 commits intoanomalyco:devfrom
taetaetae wants to merge 2 commits intoanomalyco:devfrom
Conversation
…/O overhead
## Problem
During streaming responses, OpenCode writes to storage (JSON files) on
every reasoning-delta event. With fast streaming APIs, this results in
hundreds of file writes per second, causing:
1. High I/O overhead from frequent file system operations
2. Increased latency between receiving content and displaying it
3. Slower perceived response time compared to native CLI tools
## Analysis
Comparing OpenCode with kiro-cli using the same Kiro API:
- Both receive streaming responses with similar TTFB (2-10s)
- kiro-cli renders directly to terminal (memory only)
- OpenCode writes every delta to storage via Session.updatePart()
The bottleneck is in processor.ts:
```typescript
case "reasoning-delta":
if (part.text) await Session.updatePart({ part, delta: value.text })
// This writes to file system on EVERY delta!
```
## Solution
Implement throttled storage writes for reasoning-delta events:
- Accumulate content in memory (already happening via part.text +=)
- Only flush to storage every 50ms (STREAMING_UPDATE_THROTTLE_MS)
- Always flush on reasoning-end to ensure data persistence
- No change to final content or persistence guarantees
## Expected Impact
- ~20x fewer file writes during streaming
- Reduced I/O overhead
- Faster perceived response time
- No change to final message content
Contributor
|
Hey! Your PR title Please update it to start with one of:
Where See CONTRIBUTING.md for details. |
Contributor
|
The following comment was made by an LLM, it may be inaccurate: No duplicate PRs found |
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
Collaborator
|
we are moving to sqlite |
vbuccigrossi
pushed a commit
to vbuccigrossi/opencode
that referenced
this pull request
Mar 7, 2026
Tier 1 bug fixes: - Fix O(n²) bash output concatenation with StreamingOutput class (anomalyco#9693) - Fix memory leaks in Bus.once, Format, Plugin, ShareNext, Bootstrap (anomalyco#13514) - Fix FileTime race condition using actual file mtime instead of JS clock - Free memory on compaction prune: clear output/attachments/metadata (anomalyco#7049) - Throttle reasoning-delta storage writes to 50ms intervals (anomalyco#11328) - Handle SIGHUP/SIGTERM to prevent orphaned processes (anomalyco#12718) - Add process.once("close") handler for bash tool reliability Tier 2 features: - Support 1M context window for Anthropic models via beta header (anomalyco#14375) - Input-only token counting for compaction with limit.input models - MCP lazy loading: on-demand tool discovery via mcp_search tool (anomalyco#8771) - MCP servers listed in system prompt when lazy mode enabled - StreamingOutput: output_filter regex for build diagnostics - LSP server cleanup callback for temp directory removal - Extract formatSize utility from uninstall to shared util/format - GitHub CI: fix Bus subscription leak in session event handler Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
During streaming responses, OpenCode writes to storage (JSON files) on every reasoning-delta event. With fast streaming APIs, this results in hundreds of file writes per second, causing:
Fixes #11329
Analysis
I compared OpenCode with kiro-cli using the same Kiro API:
q.us-east-1.amazonaws.com)The bottleneck is in
processor.ts:Session.updatePartcallsStorage.writewhich does:Solution
Implement throttled storage writes for reasoning-delta events:
part.text +=)STREAMING_UPDATE_THROTTLE_MS)reasoning-endto ensure data persistenceExpected Impact
Changes
STREAMING_UPDATE_THROTTLE_MSconstant (50ms)lastUpdateTime,pendingReasoningUpdates)reasoning-deltahandler to throttle storage writesreasoning-endalways flushes pending updates