Skip to content

perf(ui): convert message pairing to O(n) forward pass in chain-of-thought#846

Merged
MaheshtheDev merged 1 commit intosupermemoryai:mainfrom
ishaanxgupta:ishaan/chain
Apr 12, 2026
Merged

perf(ui): convert message pairing to O(n) forward pass in chain-of-thought#846
MaheshtheDev merged 1 commit intosupermemoryai:mainfrom
ishaanxgupta:ishaan/chain

Conversation

@ishaanxgupta
Copy link
Copy Markdown
Contributor

Summary

Optimizes the message pairing algorithm in the ChainOfThought component.

Previously, for every user message in the chat thread, the component created a new sliced array and iterated forward with .find() to locate the next assistant message. This resulted in an $O(n^2)$ time complexity and repeated garbage collection, which becomes noticeably sluggish on long conversations.

Changes

  • Removed the .slice(i + 1).find(...) nested iteration inside the for loop.
  • Introduced a lastUserPair state tracker to map the first subsequent assistant message directly to the most recent user message in a single $O(n)$ forward pass.

Copy link
Copy Markdown
Member

@MaheshtheDev MaheshtheDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for contritbution

@MaheshtheDev MaheshtheDev merged commit 5051658 into supermemoryai:main Apr 12, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants