Skip to content

Make serve rebuilds 2–5× faster and the search index cheaper - #145

Merged
shenxianpeng merged 3 commits into
mainfrom
claude/project-thread-u40qsb
Nov 2, 2026
Merged

shenxianpeng merged 3 commits into
mainfrom
claude/project-thread-u40qsb

Conversation

@shenxianpeng

@shenxianpeng shenxianpeng commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

After an edit, mkdocs serve now shows the page much sooner. Measured on a 1,001-page site (benchmarks/corpus.py, 4 cores), from saving a file to the server returning the new page:

Theme Before After
mkdocs 5.1 s 2.2 s
Sumi 14.6 s 3.1 s
Material (mkdocs-ng-material 9.8.0) 7.0 s 6.3 s

Three commits:

  1. Sumi and opt-in extensions use the serve Markdown cache. mkdocs_sumi.markdown.extension joins SAFE_EXTENSIONS (it depends only on the page's Markdown and its config; the Sumi plugin reads page features from the HTML, not from the extension). Any extension can opt in with mkdocs_cacheable = True on its module or Extension class; an instance a plugin adds is cached when its class opts in, with its config in the key.
  2. Faster search parsing. The search plugin's html.parser pass is replaced by a small tokenizer for the plain, well-formed tags Markdown produces. Anything else (comments, script/style/textarea/title, a bare <, unusual attributes) goes to ContentParser as before. Tests compare both parsers on hand-written cases and 3,000 random fragments. On 1,258 real pages (MkDocs, Sumi and catalog docs plus the corpus) the output is identical and parsing takes 0.32 s instead of 1.00 s.
  3. Serve keeps the site directory. Serve no longer empties site_dir and rewrites every file. utils.write_file skips a file whose content matches what it last wrote, as long as the file wasn't changed on disk since (so post_build minifiers still work). Files that an earlier build produced (written or copied as a File) but the new one doesn't are removed after post_build. Files that plugins write another way are never removed. --dirty builds and builds nested in another build behave as before. Two smaller changes make unchanged pages come out identical, so they can be skipped: during serve, the build date in the mkdocs/readthedocs themes is the server start time, and the search plugin reuses the entries of unchanged pages.

Checked: I rebuilt a site using Material with blog, tags, mkdocs-redirects and mkdocs-static-i18n the way serve does (edit a page, delete a post, add a page, edit again). The result has the same files and content as a clean build, apart from dates. Unit tests cover skipping, rewriting after an outside change, stale-file and empty-dir removal, files outside site_dir, failed and nested builds, and --dirty.

What's left in a serve rebuild is mostly template rendering (about 1.2 s of the 2 s with the mkdocs theme). Material is still slow because its own search plugin parses every page's HTML.

Release notes and the plugin guide are updated under 1.11.0.

Sumi's Markdown extension only depends on the page's Markdown and its
config, so it joins the list of extensions whose HTML mkdocs serve reuses.
Other extensions can opt in with `mkdocs_cacheable = True` on their
module or Extension class; an extension instance that a plugin adds is
cached when its class opts in, with its config as part of the key.

With 1,000 pages and Sumi, showing an edited page took 14.6 s and now
takes 6.2 s.
The search plugin fed every page's HTML through html.parser to find the
text of each section. A small tokenizer now does the same for the plain,
well-formed tags that Markdown produces, and hands anything else
(comments, script and style elements, a bare "<", unusual attributes) to
the old parser. Tests check that both find exactly the same text,
headings and ids, on hand-written cases and 3,000 random fragments; on
1,258 Markdown pages from the MkDocs, Sumi and catalog docs and the
benchmark corpus, the results are identical and parsing takes 0.32
instead of 1.00 seconds.
mkdocs serve emptied the site directory before every rebuild and wrote
every file again, although after an edit nearly all of them come out the
same. Now utils.write_file skips a file whose content is what it wrote
last time, as long as the file wasn't changed on disk since, and files
that an earlier build wrote or copied but the new one doesn't are
removed after post_build. Files that plugins write another way are left
alone. A build that a plugin runs inside another one, and --dirty
builds, work as before.

The build date that the mkdocs and readthedocs themes print changed every
page on each rebuild; during serve it is now the time the server
started. The search plugin also reuses the
index entries of pages whose HTML, title, URL and TOC didn't change.

With 1,000 pages, showing an edited page took 5.1 s with the mkdocs
theme and now takes 2.2 s; with Sumi, 14.6 s (before the previous
commit) and now 3.1 s. Rebuilding a site with Material, blog, tags,
redirects and static-i18n after edits, deletes and additions gives the
same files as a clean build.
@shenxianpeng shenxianpeng self-assigned this Nov 2, 2026
@codspeed

codspeed Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 48.39%

⚡ 6 improved benchmarks
✅ 23 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_search_index_add_entries 500.3 ms 247.7 ms ×2
⚡ test_serve_rebuild 1,664.7 ms 968.6 ms +71.87%
⚡ test_build_theme_pages 647.5 ms 394.9 ms +63.98%
⚡ test_template_render_large_nav 21.1 ms 13.8 ms +52.75%
⚡ test_full_build_scaling[200] 16 s 14.4 s +11.41%
⚡ test_full_build_scaling[50] 4.3 s 3.9 s +10.22%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing claude/project-thread-u40qsb (abded7b) with main (b36f44e)

Open in CodSpeed

@shenxianpeng
shenxianpeng merged commit 7fdbb53 into main Nov 2, 2026
24 checks passed
@shenxianpeng
shenxianpeng deleted the claude/project-thread-u40qsb branch November 2, 2026 14:38
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