Repository navigation
Make serve rebuilds 2–5× faster and the search index cheaper - #145
Merged
Merged
Conversation
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.
Contributor
Merging this PR will improve performance by 48.39%
Performance Changes
Tip Curious why performance improved? Comment Comparing |
This was referenced Nov 2, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
After an edit,
mkdocs servenow 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:Three commits:
mkdocs_sumi.markdown.extensionjoinsSAFE_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 withmkdocs_cacheable = Trueon its module or Extension class; an instance a plugin adds is cached when its class opts in, with its config in the key.html.parserpass 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 toContentParseras 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.site_dirand rewrites every file.utils.write_fileskips a file whose content matches what it last wrote, as long as the file wasn't changed on disk since (sopost_buildminifiers still work). Files that an earlier build produced (written or copied as aFile) but the new one doesn't are removed afterpost_build. Files that plugins write another way are never removed.--dirtybuilds 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.