Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: high performance large convo rendering #4166

Merged

Conversation

louis-menlo
Copy link
Contributor

@louis-menlo louis-menlo commented Nov 30, 2024

Describe Your Changes

Enhance Jan’s convo rendering by virtualizing only the visible DOM nodes.

There was a performance issue on latest release 0.5.9, where it renders only a few tokens / second for a large convo (just around ~100 messages). After investigating, we found a few issues related to app state handling and top-level components re-rendering. We attempted to address these issues to minimize the likelihood of app re-rendering.

This pull request also addressed the issue where the conversation does not follow the generated text, which significantly impacts the user experience.

0.5.9 - Very laggy while generating
CleanShot 2024-11-30 at 16 32 26

0.5.9 Nightly with better state handling
CleanShot 2024-11-30 at 16 20 55

However, that’s not sufficient, we constantly strive for better UX. With this fix, we apply visible DOM node virtualization for enhanced performance and consistency, ensuring a seamless experience from small convo to any size of conversations.

Latest fix:
CleanShot 2024-12-01 at 14 22 12

CleanShot.2024-11-30.at.16.47.15.mp4

A big shoutout to @tanstack/react-virtual for creating such an amazing library.

Changes made

  1. Dependency Update:

    • Added @tanstack/react-virtual (version ^3.10.9) in package.json.
  2. Imports:

    • Removed unused imports (ErrorMessage, ListContainer) in ChatBody index file.
    • Added new imports: useMemo, useRef, and useVirtualizer.
  3. Component Logic:

    • Integrated useVirtualizer to efficiently render a virtualized list of chat messages.
    • Added useRef for the scrollable parent element and use useMemo to calculate the message count.
    • Adjusted the component's rendering logic to use the virtualizer's calculated positions and sizes.
  4. Effects:

    • Updated the effect dependencies to include loadModelError and adjusted scrolling logic upon data changes.
  5. Layout Adjustments:

    • Replaced ListContainer with custom div elements to cater for virtualized rendering.
    • Changed styles for LoadModelError for a flexible layout.
  6. Minor Fixes:

    • Added optional chaining (?.) in conditionals to prevent potential runtime errors when accessing properties of possibly undefined values.

These changes improve performance, particularly in scenarios with large datasets by implementing efficient scrolling and rendering techniques.

@github-actions github-actions bot added the type: bug Something isn't working label Nov 30, 2024
@louis-menlo louis-menlo requested a review from a team November 30, 2024 09:53
Copy link
Contributor

github-actions bot commented Nov 30, 2024

Copy link
Contributor

@namchuai namchuai left a comment

Choose a reason for hiding this comment

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

@louis-menlo louis-menlo merged commit b5d88f9 into dev Dec 2, 2024
9 checks passed
@louis-menlo louis-menlo deleted the fix/improve-convo-render-performance-with-virtualizing branch December 2, 2024 02:36
@github-actions github-actions bot added this to the v0.5.10 milestone Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants