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

perf: memorize markdown rendering #496

Merged
merged 1 commit into from
Apr 5, 2023

Conversation

quark-zju
Copy link
Contributor

Markdown rendering can take time. Use React.memo for better performance.

The improvement is especially visible if there are complex elements. For example, a <Chat /> with an output of 如何推导三次方程求根方程? (which uses latex) now renders in about 5ms, down from ~140ms.

Related: #302

Markdown rendering can take time. Use `React.memo` for better performance.

The improvement is especially visible if there are complex elements. For
example, a `<Chat />` with an output of `如何推导三次方程求根方程?` (which
uses latex) now renders in about 5ms, down from ~140ms.

Related: ChatGPTNextWeb#302
@vercel
Copy link

vercel bot commented Apr 5, 2023

Someone is attempting to deploy a commit to a Personal Account owned by @Yidadaa on Vercel.

@Yidadaa first needs to authorize it.

@quark-zju
Copy link
Contributor Author

可能需要看一下 React.memo 的实现,如果不小心可能导致 stream 输出时占用 O(N^2) 的内存

@quark-zju
Copy link
Contributor Author

大致看了一下,memo 不会导致 O(N^2) 内存占用。

@Yidadaa
Copy link
Collaborator

Yidadaa commented Apr 5, 2023

我目前已经在代码中提供了一个 lazy load 的 hook,但是还没有实装,建议配合使用,需要把 message 提取成一个单独的组件。

@Yidadaa
Copy link
Collaborator

Yidadaa commented Apr 5, 2023

顺便能否提供一下 memo 前后的 performance 截图对比?

@quark-zju
Copy link
Contributor Author

quark-zju commented Apr 5, 2023

之前:

before.webm

之后:

after.webm

之所以选择 Markdown 是因为它的 props 非常简单,只有一个字符串,容易对比,并且在 profiling 中占用了绝大部分时间。其他组件的 props 有可能直接或间接被各种配置影响。

@vercel
Copy link

vercel bot commented Apr 5, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
chat-gpt-next-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 5, 2023 10:00am

@Yidadaa
Copy link
Collaborator

Yidadaa commented Apr 5, 2023

牛逼,马上合入

@Yidadaa Yidadaa merged commit 17d6f3d into ChatGPTNextWeb:main Apr 5, 2023
1 check passed
yxl pushed a commit to yxl/ChatGPT-Next-Web that referenced this pull request May 4, 2023
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.

None yet

2 participants