Skip to content

perf: prevent markdown rendering remounts and re-parses - #1159

Draft
gmsgowtham wants to merge 1 commit into
mainfrom
fix/prevent-rerenders
Draft

gmsgowtham wants to merge 1 commit into
mainfrom
fix/prevent-rerenders

Conversation

@gmsgowtham

Copy link
Copy Markdown
Owner

Parent re-renders and streaming markdown updates remounted and re-parsed the whole tree on every change. Derive stable, content-based React keys in a pre-parse token walk, stabilize hook options against inline object literals, and reuse parsed top-level elements whose token content is unchanged, so unchanged blocks skip re-rendering entirely.

  • assignStableKeys: content-derived keys (type:raw[@occurrence]) per token instance; avoids the duplicate-key flaw of hashing identical content
  • useStableValue + areHookOptionsEqual: inline styles/theme literals no longer invalidate the parse; colorScheme changes still flow through
  • ElementsCache: persist parsed top-level elements, keyed by occurrence-suffixed content key within a parser epoch (theme/style changes rebuild the epoch); caches hold elements, not untyped tokens
  • markdown.tsx: React.memo on computed data via useMarkdown; itemId via stable keys
  • Memoize the Markdown component and FlatList cells; key cells by stable element keys
  • add optional token param to RendererInterface and thread it from Parser so custom renderers can key nodes; registry components keyed from stable token keys

Tests: 187 passed (+27 new), 93 snapshots unchanged, tsc + biome clean

Parent re-renders and streaming markdown updates remounted and re-parsed
the whole tree on every change. Derive stable, content-based React keys in
a pre-parse token walk, stabilize hook options against inline object
literals, and reuse parsed top-level elements whose token content is
unchanged, so unchanged blocks skip re-rendering entirely.

- assignStableKeys: content-derived keys (type:raw[@occurrence]) per token
  instance; avoids the duplicate-key flaw of hashing identical content
- useStableValue + areHookOptionsEqual: inline styles/theme literals no
  longer invalidate the parse; colorScheme changes still flow through
- ElementsCache: persist parsed top-level elements, keyed by
  occurrence-suffixed content key within a parser epoch (theme/style
  changes rebuild the epoch); caches hold elements, not untyped tokens
- markdown.tsx: React.memo on computed data via useMarkdown; itemId via
  stable keys
- Memoize the Markdown component and FlatList cells; key cells by stable
  element keys
- add optional token param to RendererInterface and thread it from Parser
  so custom renderers can key nodes; registry components keyed from
  stable token keys

Tests: 187 passed (+27 new), 93 snapshots unchanged, tsc + biome clean
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Performance Comparison Report

  • Current: HEAD (0ccd6f6) - 2026-11-06 11:50:51Z
  • Baseline: main (d37d906) - 2026-11-06 11:50:13Z

Significant Changes To Duration

Show entries
Name Type Duration Count
Perf test Renders markdown render 6.8 ms → 7.4 ms (+0.5 ms, +7.8%) 🔴 1 → 1
Show details
Name Type Duration Count
Perf test Renders markdown render Baseline
Mean: 6.8 ms
Stdev: 0.4 ms (5.4%)
Runs: 7.6 6.9 6.8 7.4 6.6 6.5 6.6 6.9 6.5 6.6
Warmup runs: 5023.6
Removed outliers: (none)
Stability: 5.4%

Current
Mean: 7.4 ms
Stdev: 0.2 ms (3.0%)
Runs: 7.9 7.5 7.2 7.5 7.3 7.2 7.3 7.4 7.3 7.2
Warmup runs: 169.2
Removed outliers: (none)
Stability: 3.0%
Baseline
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1
Render issues:

Current
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1
Render issues:

Meaningless Changes To Duration

There are no entries

Render Count Changes

There are no entries

Render Issues

There are no entries

Added Entries

There are no entries

Removed Entries

There are no entries

Stability

Name Baseline Current
Weighted Average 5.4% 3.0%

Generated by 🚫 dangerJS against 2fc6609

This branch has not been deployed

No deployments
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