Skip to content

Fix quadratic heading ID generation for duplicate headings - #74

Merged
patakuti merged 1 commit into
mainfrom
fix/duplicate-heading-id-perf
Oct 3, 2026
Merged

patakuti merged 1 commit into
mainfrom
fix/duplicate-heading-id-perf

Conversation

@patakuti

@patakuti patakuti commented Oct 3, 2026

Copy link
Copy Markdown
Owner

Fixes #73

Summary

gfmIDs.Generate rescanned the numeric suffixes from 1 for every repeated heading (O(n^2) for n identical headings). It now remembers the last suffix handed out per base ID. Generated IDs are identical to before (suffixes below the remembered value are always taken, since IDs are never released).

Measurements

Server render time, conversation-like Markdown:

Size Before After
1MB 0.165s 0.108s
5MB 2.0s 0.54s
10MB 7.2s 1.08s

BenchmarkConvert_DuplicateHeadings (20,000 identical headings): 14.3s -> 0.106s.

Tests

  • Sequence of IDs for repeated headings
  • Skipping a suffix that already exists as a literal heading ID (a, a-2, a, a, a -> a, a-2, a-1, a-3, a-4)
  • Uniqueness over 5,000 duplicates
  • Benchmark for duplicate headings

Not included

Browser-side layout cost for very large pages (content-visibility: auto was measured to help ~5x but changes scroll height estimation and anchor positioning).

🤖 Generated with Claude Code

Generate rescanned the numeric suffixes from 1 for every repeated heading, so
a document with n identical headings cost O(n^2). Conversation logs have
hundreds of identical "User" / "Claude" headings. Remember the last suffix
per base ID instead; results are identical.

Server render time for conversation-like Markdown: 1MB 0.165s -> 0.108s,
5MB 2.0s -> 0.54s, 10MB 7.2s -> 1.08s.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@patakuti
patakuti merged commit 888d0df into main Oct 3, 2026
2 checks passed
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.

Rendering is slow for large documents with many identical headings

1 participant