Build the dep-graph reverse index lazily, per DepKind#157319
Draft
xmakro wants to merge 1 commit into
Draft
Conversation
When a SerializedDepGraph is decoded, it built a fingerprint to index map for every DepKind covering every node. That inverse index is only consulted by node_to_index_opt, which runs for the nodes a session queries directly; the bulk of the graph is reached as edge targets by index and is never looked up by fingerprint, so most of those maps are never read. Replace the eager build with a counting sort that groups node indices into a contiguous range per DepKind, and build the fingerprint map for a kind only the first time a node of that kind is looked up. Decode no longer pays a hash-map insert per node, and kinds that are never looked up never build a map. The on-disk format is unchanged.
Collaborator
|
The job Click to see the possible cause of the failure (guessed by this bot) |
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.
What this does
When a
SerializedDepGraphis decoded at the start of an incremental session, it eagerly builds a fingerprint to index map for everyDepKind, covering every node in the graph (oneUnhashMapper kind, populated with a hash-map insert per node). That inverse index has a single consumer,node_to_index_opt, which is only reached for nodes the session queries directly (try_mark_greenand a fewDepGraphentry points). The overwhelming majority of the graph is reached bytry_mark_previous_greenwalking edges, which reference nodes by index, not by fingerprint, so most of those per-kind maps are built and never read.This makes the index lazy:
DepKind(using the per-kind counts already stored in the graph tail). This is a single pass of array writes, with no hashing.DepKindis built the first time a node of that kind is looked up, viaOnceLock. Kinds that are never looked up never build a map.The on-disk format is unchanged; this only changes how the decoded graph is held in memory.
Why it is sound
node_to_index_optreturns the same index for the sameDepNodeas before. The map for a kind is built from exactly that kind's nodes, so lookups are identical. The only observable difference is that the duplicate-fingerprint assertion now runs lazily, per kind, the first time that kind is looked up, instead of for every kind up front. Kinds that are never looked up are not checked, which weakens a defensive "this should never happen" check for those kinds but does not change any result a lookup returns.What was measured
Two stage1
librustc_drivershared objects were built from this tree, one at the parent commit (baseline) and one with the change. Only the.sois swapped between runs, so nothing else differs. The metric is callgrind instruction reads (Ir), which is deterministic, attributed to the crate's ownrustc --crate-name <crate>invocation (the rustc child with the largest Ir).This change only affects the incremental decode path, so the relevant scenario is an incremental rebuild that reloads a previous session's graph. The crate is built once to populate the on-disk incremental cache, then source mtimes are bumped with no content change and it is rebuilt under callgrind (
CARGO_INCREMENTAL=1). A clean build (CARGO_INCREMENTAL=0) never decodes a previous graph, so it is unaffected by construction.Incremental, unchanged
The absolute saving is the per-node hash-map inserts no longer paid at decode for never-looked-up kinds, so it scales with the size of the serialized graph (largest on serde, smallest on tokio). It is a small fraction of a full check, so this is a modest win, but it is consistent and never regresses.
These are local measurements (deterministic callgrind Ir); marking the PR as draft so a perf run can confirm on the full suite.
Testing
./x test tests/incrementalpasses (176 tests).cargo checkdiagnostics are byte-identical to baseline on regex, ripgrep, syn and serde.