fix: render performance while generating messages #4328
Merged
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.
Describe Your Changes
This PR fixes the issue where the app UI becomes laggy while generating messages with tiny models. The performance issue is caused by poor state handling during message updates. Every time a token is yielded, the code loops through the messages array to check if a message is available before updating. This redundant operation is costly.
Before:
After:
Self Checklist
This pull request includes several changes aimed at improving the handling of thread-related state and message generation in the
ModelHandler
and related components. The most important changes include adding new atoms, updating imports, and modifying various hooks and components to support the new state management.State Management Enhancements:
web/helpers/atoms/ChatMessage.atom.ts
: Added a new atomsubscribedGeneratingMessageAtom
to store the subscribed generating message thread.web/hooks/useSetActiveThread.ts
: Updated to use the newsubscribedGeneratingMessageAtom
and added logic to clear the subscriber when the active thread changes. [1] [2]Component Updates:
web/containers/Providers/ModelHandler.tsx
:useAtom
. [1] [2] [3]web/screens/Thread/ThreadCenterPanel/ChatItem/index.tsx
:useAtom
. [1] [2]ChatItem
component.These changes collectively enhance the state management related to thread handling and message generation, ensuring more robust and reactive behavior in the application.