Create a shadow DOM tree for faster (10x) navtree generation#939
Open
rnk wants to merge 1 commit into
Open
Conversation
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.
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.
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. 🙏