Skip to content

Create a shadow DOM tree for faster (10x) navtree generation#939

Open
rnk wants to merge 1 commit into
pradyunsg:mainfrom
rnk:optimize-navbar
Open

Create a shadow DOM tree for faster (10x) navtree generation#939
rnk wants to merge 1 commit into
pradyunsg:mainfrom
rnk:optimize-navbar

Conversation

@rnk

@rnk rnk commented Jul 10, 2026

Copy link
Copy Markdown

We attempted to deploy Furo for the LLVM Sphinx docs, (llvm/llvm-project#184440), but our sphinx build and publish bot timed out. I replicated the timeout locally, and came up with this PR, which speeds up the build by 10x.

The slowdown occurred because LLVM has a very large global navigation tree. I've seen in the discussions that Furo is intended for "small sites", so perhaps our use case is out of scope, but I thought I'd share anyway.

The bottleneck for us was that each page would reparse the global navtree for every render with beautiful soup. This is kind of O(n^2), it's for each page, parse HTML which has at least as many elements as there are pages, and beautiful soup isn't super fast.

The solution in this PR was to build a sort of shadow DOM with the _NavigationTree and _NavigationElement classes. Rendering the final navtree for each page is a matter of walking this tree, replacing the HTML elements that vary (CSS class, href, etc), and concatenating the rest of the HTML which does not vary between pages.

My initial thought was to use a templating engine to substitute in the parts that vary without parsing HTML, but that ended up being slower.

I synthesized a project generation benchmark, with a hidden toctree containing N pages, and got these numbers before and after this change:

N=100: 1.85s -> 0.94s
N=200: 5.41s -> 1.45s
N=400: 18.82s -> 2.96s
N=800: 75.43s -> 7.83s

That is roughly a 10x speedup in project generation time for the large-navigation case. This change allowed me to finish LLVM's 1,233 page doc build in 1:27.47, instead of hitting my local 10min timeout.

The code is LLM-generated and the PR description is not. I'm happy to try to tune it up or reduce scope to make it fit better. I think the general idea of a custom tree of navigation elements does make sense. Thanks for the theme, and I appreciate any time you spend reviewing this PR. 🙏

Build the global sidebar navigation tree once per HTML build, then render page-specific links and current-page state from that cached tree. This avoids asking Sphinx to resolve the full global toctree and avoids reparsing the large navigation blob with BeautifulSoup for every generated page.

Synthetic project generation benchmark, with a hidden toctree containing N pages:

  N=100: 1.85s -> 0.94s

  N=200: 5.41s -> 1.45s

  N=400: 18.82s -> 2.96s

  N=800: 75.43s -> 7.83s

That is roughly a 10x speedup in project generation time for the large-navigation case. LLVM's docs-llvm-html build with local Furo went from timing out after 10 minutes before this change to completing all 1,233 pages in 1:27.47. The 1,091 pages that overlapped with the timed-out baseline compared byte-identical.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant