Skip to content

⚡ Bolt: [performance improvement] Memoize ReactMarkdown components in Chatbot#454

Merged
Oxygen-Low merged 1 commit into
mainfrom
bolt/optimize-reactmarkdown-memoization-8239583131249175266
Jul 17, 2026
Merged

⚡ Bolt: [performance improvement] Memoize ReactMarkdown components in Chatbot#454
Oxygen-Low merged 1 commit into
mainfrom
bolt/optimize-reactmarkdown-memoization-8239583131249175266

Conversation

@Oxygen-Low

@Oxygen-Low Oxygen-Low commented Jul 16, 2026

Copy link
Copy Markdown
Owner

💡 What: Extracted the components object out of the memoized ChatMessage component in Chatbot.tsx.
🎯 Why: When a chat response streams in, the ChatMessage receives new props and re-renders. Defining the components configuration object inline meant it was recreated on every render. ReactMarkdown uses this prop, and receiving a new object reference caused the entire heavy markdown tree and SyntaxHighlighter instances to unnecessarily tear down and re-mount continuously during generation.
📊 Impact: Massively reduces CPU usage and main thread blocking during AI responses, eliminating O(n) re-rendering of ReactMarkdown per token.
🔬 Measurement: Verify by inspecting React DevTools Profiler while generating a long coding response. The ReactMarkdown and SyntaxHighlighter components will no longer show up as rendering for every single token update.


PR created automatically by Jules for task 8239583131249175266 started by @Oxygen-Low

Summary by CodeRabbit

  • Performance Improvements

    • Improved streaming chat rendering performance by preventing unnecessary Markdown re-renders.
    • Preserved syntax highlighting for fenced code blocks and standard rendering for inline code.
  • Documentation

    • Added guidance on avoiding repeated Markdown configuration during rendering.

Extracted the `components` object from the `ReactMarkdown` definition in `Chatbot.tsx` and placed it outside the `ChatMessage` component. This prevents `ReactMarkdown` and `SyntaxHighlighter` from continuously re-rendering on every new streamed token by preserving reference equality for the `components` prop.

Co-authored-by: Oxygen-Low <95589118+Oxygen-Low@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

ChatMessage now reuses a shared ReactMarkdown code-rendering configuration for syntax-highlighted fenced blocks and inline code. A dated note documents the memoization pattern for streaming chat UIs.

Changes

Markdown rendering

Layer / File(s) Summary
Shared Markdown renderer configuration
client/components/apps/Chatbot.tsx, .jules/bolt.md
The custom code renderer is extracted into memoizedMarkdownComponents, passed to ReactMarkdown, and documented as a memoization pattern.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Poem

A bunny found a renderer bright,
Shared once instead of built each night.
Markdown hops through shaded code,
Inline snippets bear their load.
Fewer redraws along the way—
“Memoised!” the rabbit says.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: moving ReactMarkdown components to a memoized/shared configuration in Chatbot for performance.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In @.jules/bolt.md:
- Around line 1-3: Update the heading in the ReactMarkdown memoization
documentation to use a top-level Markdown heading with blank lines around it,
and revise the wording to remove the redundant “of” after “outside.”
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 83d3e728-23fc-4a77-911f-a83ca49c9c5c

📥 Commits

Reviewing files that changed from the base of the PR and between 353a85c and cb6fd3a.

📒 Files selected for processing (2)
  • .jules/bolt.md
  • client/components/apps/Chatbot.tsx

Comment thread .jules/bolt.md
Comment on lines +1 to +3
## 2024-05-17 - [ReactMarkdown Memoization Pattern]
**Learning:** In streaming chat interfaces (like `Chatbot.tsx`), placing inline object or function definitions (like the `components` prop for `ReactMarkdown`) inside a `React.memo` wrapped child component causes that heavy component to re-render constantly. This happens because the parent `ChatMessage` is memoized and might try to avoid re-renders, but when it does re-render due to prop changes (like new tokens in `m.content`), it recreates the `components` object, which then forces `ReactMarkdown` (a very heavy component with SyntaxHighlighter) to completely re-render from scratch instead of just updating text.
**Action:** Always extract static configuration objects, such as `ReactMarkdown`'s `components` mappings or custom renderers, outside of the React render body. If they depend on props/state, use `useMemo`. This prevents O(n) re-renders during frequent streaming state updates.

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.

📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Fix Markdown linting warnings and style issue.

To comply with Markdown best practices and resolve linting warnings, please use a top-level heading (#), add blank lines around it, and remove the redundant "of" after "outside".

📝 Proposed fixes
-## 2024-05-17 - [ReactMarkdown Memoization Pattern]
-**Learning:** In streaming chat interfaces (like `Chatbot.tsx`), placing inline object or function definitions (like the `components` prop for `ReactMarkdown`) inside a `React.memo` wrapped child component causes that heavy component to re-render constantly. This happens because the parent `ChatMessage` is memoized and might try to avoid re-renders, but when it does re-render due to prop changes (like new tokens in `m.content`), it recreates the `components` object, which then forces `ReactMarkdown` (a very heavy component with SyntaxHighlighter) to completely re-render from scratch instead of just updating text.
-**Action:** Always extract static configuration objects, such as `ReactMarkdown`'s `components` mappings or custom renderers, outside of the React render body. If they depend on props/state, use `useMemo`. This prevents O(n) re-renders during frequent streaming state updates.
+# 2024-05-17 - [ReactMarkdown Memoization Pattern]
+
+**Learning:** In streaming chat interfaces (like `Chatbot.tsx`), placing inline object or function definitions (like the `components` prop for `ReactMarkdown`) inside a `React.memo` wrapped child component causes that heavy component to re-render constantly. This happens because the parent `ChatMessage` is memoized and might try to avoid re-renders, but when it does re-render due to prop changes (like new tokens in `m.content`), it recreates the `components` object, which then forces `ReactMarkdown` (a very heavy component with SyntaxHighlighter) to completely re-render from scratch instead of just updating text.
+
+**Action:** Always extract static configuration objects, such as `ReactMarkdown`'s `components` mappings or custom renderers, outside the React render body. If they depend on props/state, use `useMemo`. This prevents O(n) re-renders during frequent streaming state updates.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
## 2024-05-17 - [ReactMarkdown Memoization Pattern]
**Learning:** In streaming chat interfaces (like `Chatbot.tsx`), placing inline object or function definitions (like the `components` prop for `ReactMarkdown`) inside a `React.memo` wrapped child component causes that heavy component to re-render constantly. This happens because the parent `ChatMessage` is memoized and might try to avoid re-renders, but when it does re-render due to prop changes (like new tokens in `m.content`), it recreates the `components` object, which then forces `ReactMarkdown` (a very heavy component with SyntaxHighlighter) to completely re-render from scratch instead of just updating text.
**Action:** Always extract static configuration objects, such as `ReactMarkdown`'s `components` mappings or custom renderers, outside of the React render body. If they depend on props/state, use `useMemo`. This prevents O(n) re-renders during frequent streaming state updates.
# 2024-05-17 - [ReactMarkdown Memoization Pattern]
**Learning:** In streaming chat interfaces (like `Chatbot.tsx`), placing inline object or function definitions (like the `components` prop for `ReactMarkdown`) inside a `React.memo` wrapped child component causes that heavy component to re-render constantly. This happens because the parent `ChatMessage` is memoized and might try to avoid re-renders, but when it does re-render due to prop changes (like new tokens in `m.content`), it recreates the `components` object, which then forces `ReactMarkdown` (a very heavy component with SyntaxHighlighter) to completely re-render from scratch instead of just updating text.
**Action:** Always extract static configuration objects, such as `ReactMarkdown`'s `components` mappings or custom renderers, outside the React render body. If they depend on props/state, use `useMemo`. This prevents O(n) re-renders during frequent streaming state updates.
🧰 Tools
🪛 LanguageTool

[style] ~3-~3: This phrase is redundant. Consider using “outside”.
Context: ...mponents` mappings or custom renderers, outside of the React render body. If they depend o...

(OUTSIDE_OF)

🪛 markdownlint-cli2 (0.23.0)

[warning] 1-1: Headings should be surrounded by blank lines
Expected: 1; Actual: 0; Below

(MD022, blanks-around-headings)


[warning] 1-1: First line in a file should be a top-level heading

(MD041, first-line-heading, first-line-h1)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In @.jules/bolt.md around lines 1 - 3, Update the heading in the ReactMarkdown
memoization documentation to use a top-level Markdown heading with blank lines
around it, and revise the wording to remove the redundant “of” after “outside.”

Source: Linters/SAST tools

@Oxygen-Low
Oxygen-Low merged commit 352adba into main Jul 17, 2026
6 checks passed
@Oxygen-Low
Oxygen-Low deleted the bolt/optimize-reactmarkdown-memoization-8239583131249175266 branch July 17, 2026 18:19
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.

1 participant