Skip to content

perf(ai): O(1) exchange_with_id via exchange_id_index#13111

Open
acarl005 wants to merge 3 commits into
masterfrom
andy/long-convo-latency-exchange-lookup
Open

perf(ai): O(1) exchange_with_id via exchange_id_index#13111
acarl005 wants to merge 3 commits into
masterfrom
andy/long-convo-latency-exchange-lookup

Conversation

@acarl005

@acarl005 acarl005 commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Description

Part of a stack of performance fixes for #9799: opening a large Oz agent conversation (3000+ exchanges) caused a beachball / multi-second load due to several O(n²) hotspots in the restore path.

This PR: AIConversation::exchange_with_id previously performed a linear scan through every exchange on every call. During conversation restoration this function is called once per exchange to look up command-block metadata, making the restore path O(n²) overall.

Adds exchange_id_index: HashMap<AIAgentExchangeId, usize> to TaskStore. The index is rebuilt on all structural mutations (append, insert, remove, exchange removal). exchange_with_id and the new exchange_by_id now return in O(1).

Also fixes remove(): previously it retained entries in linearized_refs but left stale entries in exchange_id_index for the removed task; now it does a full index rebuild.

Linked Issue

Fixes #9799

  • The linked issue is labeled ready-to-spec or ready-to-implement.
  • Where appropriate, screenshots or a short video of the implementation are included below (especially for user-visible or UI changes).

Testing

Manually loaded a conversation with 3014 AI exchanges and confirmed the restore time was significantly reduced.

  • I have manually tested my changes locally with ./script/run

Agent Mode

  • Warp Agent Mode - This PR was created via Warp's AI Agent Mode

@cla-bot cla-bot Bot added the cla-signed label Jun 26, 2026

acarl005 commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@oz-for-oss

This comment was marked as resolved.

oz-for-oss[bot]

This comment was marked as resolved.

@acarl005

Copy link
Copy Markdown
Contributor Author

/oz-review

@oz-for-oss

This comment was marked as resolved.

oz-for-oss[bot]

This comment was marked as resolved.

@acarl005

Copy link
Copy Markdown
Contributor Author

/oz-review

@oz-for-oss

oz-for-oss Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

@acarl005

I'm re-reviewing this pull request in response to a review request.

You can view the conversation on Warp.

I completed the review and no human review was requested for this pull request.

Comment /oz-review on this pull request to retrigger a review (up to 3 times on the same pull request).

Powered by Oz

@oz-for-oss oz-for-oss Bot 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.

Overview

This PR adds an exchange_id_index to TaskStore so AIConversation::exchange_with_id can resolve exchanges by ID through a hash map instead of scanning every task. The index is initialized from restored tasks and rebuilt on the structural mutation paths shown in the diff, including inserts, removals, exchange appends/removals, truncation via modify_task, and explicit exchange ID reassignment after fork/hydration flows.

Concerns

  • No blocking correctness, security, or spec-drift concerns found in the changed lines. spec_context.md contains no approved or repository spec context for this PR.

Verdict

Found: 0 critical, 0 important, 0 suggestions

Approve

Comment /oz-review on this pull request to retrigger a review (up to 3 times on the same pull request).

Powered by Oz

@acarl005 acarl005 force-pushed the andy/long-convo-latency-exchange-lookup branch from 90448a3 to 75061d8 Compare June 27, 2026 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Text rendering performance issues for very long conversations

1 participant