Cache dependency graph in findBreadcrumbEntries to fix O(n²) build scaling#71
Open
gavinmorrison wants to merge 1 commit into
Open
Cache dependency graph in findBreadcrumbEntries to fix O(n²) build scaling#71gavinmorrison wants to merge 1 commit into
gavinmorrison wants to merge 1 commit into
Conversation
findBreadcrumbEntries() rebuilds the full dependency graph from collections.all on every call. Since this filter runs once per page and the nodes array is the same reference throughout a build, this causes O(n²) scaling. Cache the graph using the nodes reference as a key. The cache invalidates automatically between builds when collections.all is recreated. Benchmarks (minimal Eleventy site, navigation v1.0.5): - 1,000 pages: 29.9s → 0.64s (47× faster) - 2,000 pages: 279s → 1.17s (238× faster) Fixes 11ty#64
Author
|
@zachleat are you the maintainer for this project? Is there any chance you would be able to review this PR, please? |
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.
Problem
findBreadcrumbEntries()callsgetDependencyGraph(nodes)on every invocation, rebuilding the full dependency graph fromcollections.alleach time. Since this filter runs once per page and thenodesarray is the same reference throughout a build, the result is O(n²) scaling — doubling the page count roughly quadruples build time.Originally reported in #64.
How to reproduce
@11ty/eleventy-navigationand a base layout that callscollections.all | eleventyNavigationBreadcrumb(...)eleventyNavigationkeysnpx eleventy— the benchmark output will showeleventyNavigationBreadcrumbdominating build timeBenchmarks
Tested with
@11ty/eleventy@3.1.2and@11ty/eleventy-navigation@1.0.5:Approach
Cache the dependency graph using a
WeakMapwith thenodesarray as the key. Sincecollections.allis the same object reference throughout a single Eleventy build, the graph only needs to be built once per build.Using a
WeakMapensures that between builds (or in--servemode) when Eleventy creates a new array, the old cache is automatically garbage collected, preventing memory leaks while naturally invalidating the cache.All 25 existing tests pass.
Fixes #64