Make zipAttrsWith more parallel-eval friendly#16097
Open
edolstra wants to merge 1 commit into
Open
Conversation
The previous implementation used a std::map with traceable_allocator, which costs one GC_MALLOC_UNCOLLECTABLE() and one GC_FREE() per distinct attribute name. Uncollectable allocations always take the global GC allocation lock, making this map a major source of mutex contention during parallel evaluation (~12% of all futex calls in a 16-core 'nix search' run, plus the corresponding GC_free traffic). Instead, collect the (name, value) pairs into a plain std::vector and group them by stable-sorting on the name, which yields the same attribute order and per-attribute value order as the map. The vector doesn't need to be visible to the GC since the values it points to are kept alive by the attrsets in args[1] for the duration of the call. At 16 eval cores (with GC disabled via GC_INITIAL_HEAP_SIZE=40G), this cuts kernel time from ~21s to ~8-9s, context switches from 2.6M to 1.5M, and elapsed time from ~6.2s to ~4.9s for 'nix search nixpkgs --no-eval-cache fizzbuzz'. Single-core performance is unchanged. Assisted-by: Claude Fable 5 <noreply@anthropic.com>
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.
Motivation
The previous implementation used an
std::mapwithtraceable_allocatorto collect the attributes, which is extremely expensive in parallel eval because every uncollectable alloc/free takes the global GC lock. The new implementation collects all the attributes in a vector (without theListBuilderthat was the reason why we neededtraceable_allocator), sorts them by name, and then performs a single sweep through the vector to build one list at a time.While this is mostly beneficial for parallel eval, it also seems to give a modest speedup for single-threaded eval (e.g.
nix search nixpkgs fizzbuzz --no-eval-cachewent from 28.9s to 28.5s).Assisted-by: Claude Fable 5 noreply@anthropic.com
Taken from DeterminateSystems#534.
Context
Add 👍 to pull requests you find important.
The Nix maintainer team uses a GitHub project board to schedule and track reviews.