Skip to content

Cut the page cache's cost when pages change - #155

Merged
shenxianpeng merged 5 commits into
mainfrom
claude/project-thread-epdfwi
Nov 2, 2026
Merged

shenxianpeng merged 5 commits into
mainfrom
claude/project-thread-epdfwi

Conversation

@shenxianpeng

@shenxianpeng shenxianpeng commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #154. CodSpeed flagged test_serve_rebuild as 21% slower on #154. The benchmark doesn't set up serve's site output, so the build date changed on every build and no page could ever be reused. Every page then paid for the cache key and a zlib-compressed copy of its HTML and got nothing back. That cost also applies in real serve whenever pages miss, for example after a template edit.

  • Page HTML is only cached when serve's site output is active. Without it, the build date changes every build.
  • Without post_page events, only the digest of the written file is kept, not a compressed copy of the HTML. The file on disk is the cached copy, and if it changed the page is rendered again.
  • Page keys are serialized with marshal (version 2, which has no back-references, so equal values give equal bytes) instead of repr(), which is about 7× faster. The types _stable() checks are imported once.
  • test_serve_rebuild now sets up the site output the same way mkdocs serve does, so the benchmark measures real serve.

Measured on 200 benchmark pages, minimum of 5 rebuilds:

main #154 this PR
rebuild without site output (old benchmark) 0.28 s 0.47 s 0.29 s
serve rebuild, every page misses 0.25 s 0.32 s 0.27 s
serve rebuild, one page edited 0.25 s 0.21 s 0.16 s

Related Issue

Follow-up to #154.

Checklist

After an edit, every rebuild rendered the theme template of every page,
which took most of the rebuild time on large sites, since each page holds
the whole navigation. Keep each page's HTML, keyed on a digest of its
template context: every attribute of the page, the navigation with the
titles, URLs and metadata of its pages, the list of pages, the config, the
other context variables, the template files that were loaded and the Jinja
globals. A page whose context holds anything without a stable description
is rendered as before. The page_context and post_page events still run for
every page; when there are no post_page events and the file already holds
the HTML, the page is skipped right away.

The HTML is kept compressed, about 16 KB per page with Material.

With 1,000 pages, an edited page shows up after 0.9 s instead of 2.3 s with
the mkdocs theme, and after about 1.4 s instead of 4.1 s with Material.
- Only cache page HTML when serve's site output is active: without it the
  build date changes every build, so no page could be reused.
- Without post_page events, keep only the digest of the written file
  instead of a compressed copy of the HTML.
- Build page keys with marshal instead of repr(), and import the types
  _stable() checks once.
- Make the serve_rebuild benchmark set up the site output as serve does.
- Only cache page HTML when serve's site output is active: without it the
  build date changes every build, so no page could be reused.
- Without post_page events, keep only the digest of the written file
  instead of a compressed copy of the HTML.
- Build page keys with marshal instead of repr(), and import the types
  _stable() checks once.
- Make the serve_rebuild benchmark set up the site output as serve does.
…to claude/project-thread-epdfwi

# Conflicts:
#	docs/about/release-notes.md
@codspeed

codspeed Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 44.81%

1 warning

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 1 improved benchmark
✅ 28 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_serve_rebuild 1,252.7 ms 865.1 ms +44.81%

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-epdfwi (8477027) with main (503edbe)

Open in CodSpeed

@shenxianpeng
shenxianpeng merged commit 374e55d into main Nov 2, 2026
24 checks passed
@shenxianpeng
shenxianpeng deleted the claude/project-thread-epdfwi branch November 2, 2026 22:59
This was referenced Nov 3, 2026
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