Skip to content

text: Improve markdown code block rendering performance#2400

Merged
madcodelife merged 1 commit into
longbridge:mainfrom
BeratHundurel:codex/markdown-codeblock-performance
May 26, 2026
Merged

text: Improve markdown code block rendering performance#2400
madcodelife merged 1 commit into
longbridge:mainfrom
BeratHundurel:codex/markdown-codeblock-performance

Conversation

@BeratHundurel

Copy link
Copy Markdown
Contributor

Description

This changes code block syntax highlighting to compute styles lazily and reuse SyntaxHighlighter instances through a thread-local language cache. This avoids repeated highlighter construction during markdown parsing/render setup for large files with many code blocks.

It also avoids repeatedly recombining visible input highlight ranges when the ranges are already disjoint.

I also tested #2371 against the same case, but it did not help with the slowdown I was seeing.

I first noticed the issue in my side project, then reproduced the same behavior in the example markdown editor. In local testing, Task Manager showed lower CPU usage while editing with this implementation, memory usage stayed about the same.

Adds regression coverage for code block equality and cached highlighter refresh behavior when a language is registered after an initial fallback.

AI assistance was used to help and prepare this PR. I manually reviewed the resulting code and tested the performance difference before opening the PR.

Test File

I used this AI-generated markdown file to reproduce the issue and test the change:

refactor-opportunities.md

Videos

Before

before.mp4

After

after.mp4

Tests

  • cargo test -p gpui-component code_block_
  • cargo test -p gpui-component input::
  • cargo test -p gpui-component text::

Checklist

  • I have read the CONTRIBUTING document and followed the guidelines.
  • Reviewed the changes in this PR and confirmed AI-generated code, if any, is accurate.
  • Passed cargo run for story tests related to the changes.
  • Tested macOS, Windows, and Linux platform performance, if the change is platform-specific.

Cache code block syntax highlighters per thread and compute styles lazily to avoid repeated highlighter setup for large markdown documents.

Add regression tests for code block equality and language cache refresh behavior.
@BeratHundurel BeratHundurel changed the title Optimize markdown code block highlighting text: Improve markdown code block rendering performance May 25, 2026
@madcodelife

Copy link
Copy Markdown
Member

Thank you, it looks great!

@madcodelife madcodelife merged commit b3391c9 into longbridge:main May 26, 2026
3 checks passed
AzureZee pushed a commit to AzureZee/gpui-component that referenced this pull request May 26, 2026
)

## Description

This changes code block syntax highlighting to compute styles lazily and
reuse `SyntaxHighlighter` instances through a thread-local language
cache. This avoids repeated highlighter construction during markdown
parsing/render setup for large files with many code blocks.

It also avoids repeatedly recombining visible input highlight ranges
when the ranges are already disjoint.

I also tested longbridge#2371 against the same case, but it did not help with the
slowdown I was seeing.

I first noticed the issue in my side project, then reproduced the same
behavior in the example markdown editor. In local testing, Task Manager
showed lower CPU usage while editing with this implementation, memory
usage stayed about the same.

Adds regression coverage for code block equality and cached highlighter
refresh behavior when a language is registered after an initial
fallback.

AI assistance was used to help and prepare this PR. I manually reviewed
the resulting code and tested the performance difference before opening
the PR.

## Test File

I used this AI-generated markdown file to reproduce the issue and test
the change:


[refactor-opportunities.md](https://github.com/user-attachments/files/28234513/refactor-opportunities.md)

## Videos

### Before

<video
src="https://github.com/user-attachments/assets/dbcc45ab-f371-40c2-b6b5-1336fee995fe"
controls></video>

### After

<video
src="https://github.com/user-attachments/assets/4e3c0754-55b5-4d54-9eb6-a4261f940ad9"
controls></video>

## Tests

- `cargo test -p gpui-component code_block_`
- `cargo test -p gpui-component input::`
- `cargo test -p gpui-component text::`

## Checklist

- [x] I have read the [CONTRIBUTING](../CONTRIBUTING.md) document and
followed the guidelines.
- [x] Reviewed the changes in this PR and confirmed AI-generated code,
if any, is accurate.
- [x] Passed `cargo run` for story tests related to the changes.
- [x] Tested macOS, Windows, and Linux platform performance, if the
change is platform-specific.
linyisu pushed a commit to linyisu/gpui-component that referenced this pull request May 29, 2026
)

This changes code block syntax highlighting to compute styles lazily and
reuse `SyntaxHighlighter` instances through a thread-local language
cache. This avoids repeated highlighter construction during markdown
parsing/render setup for large files with many code blocks.

It also avoids repeatedly recombining visible input highlight ranges
when the ranges are already disjoint.

I also tested longbridge#2371 against the same case, but it did not help with the
slowdown I was seeing.

I first noticed the issue in my side project, then reproduced the same
behavior in the example markdown editor. In local testing, Task Manager
showed lower CPU usage while editing with this implementation, memory
usage stayed about the same.

Adds regression coverage for code block equality and cached highlighter
refresh behavior when a language is registered after an initial
fallback.

AI assistance was used to help and prepare this PR. I manually reviewed
the resulting code and tested the performance difference before opening
the PR.

I used this AI-generated markdown file to reproduce the issue and test
the change:

[refactor-opportunities.md](https://github.com/user-attachments/files/28234513/refactor-opportunities.md)

<video
src="https://github.com/user-attachments/assets/dbcc45ab-f371-40c2-b6b5-1336fee995fe"
controls></video>

<video
src="https://github.com/user-attachments/assets/4e3c0754-55b5-4d54-9eb6-a4261f940ad9"
controls></video>

- `cargo test -p gpui-component code_block_`
- `cargo test -p gpui-component input::`
- `cargo test -p gpui-component text::`

- [x] I have read the [CONTRIBUTING](../CONTRIBUTING.md) document and
followed the guidelines.
- [x] Reviewed the changes in this PR and confirmed AI-generated code,
if any, is accurate.
- [x] Passed `cargo run` for story tests related to the changes.
- [x] Tested macOS, Windows, and Linux platform performance, if the
change is platform-specific.
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