Skip to content

builtins.groupBy: Drop ValueVectorMap#536

Merged
edolstra merged 1 commit into
mainfrom
improve-groupBy
Jul 6, 2026
Merged

builtins.groupBy: Drop ValueVectorMap#536
edolstra merged 1 commit into
mainfrom
improve-groupBy

Conversation

@edolstra

@edolstra edolstra commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Motivation

Apply the same optimization to builtins.groupBy as to builtins.zipAttrsWith in #535, and factor out some shared code. This removes the need for the ValueVectorMap.

Context

Summary by CodeRabbit

  • Refactor
    • Simplified internal handling for attribute grouping and zipping, while preserving the same results for existing workflows.
    • Removed an exposed type alias from the public interface.

Apply the same optimization to builtins.groupBy as to
builtins.zipAttrsWith (162a34378c): instead of accumulating the groups
in a std::map<Symbol, ValueVector> with traceable_allocator - which
costs a GC_MALLOC_UNCOLLECTABLE() per distinct name plus traceable
growth allocations for every per-group vector, all taking the global
GC allocation lock - collect the (name, value) pairs into a plain
std::vector and group them by stable-sorting on the name. The vector
doesn't need to be visible to the GC since the values it points to
are kept alive by the list in args[1] for the duration of the call.

The shared machinery (the NameValue type, sorting/counting the names,
and iterating over the per-name runs) is factored out into helpers
used by both primops.

This removes the last user of ValueVectorMap, so drop that typedef.

Assisted-by: Claude Fable 5 <noreply@anthropic.com>
@edolstra edolstra changed the title groupBy: Avoid GC-visible temporary storage groupBy: Drop ValueVectorMap Jul 6, 2026
@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This change refactors __zipAttrsWith and __groupBy in primops.cc to use new shared helpers (NameValue, sortByName, forEachByName) for stable grouping by attribute name, replacing inline sorting/map-based logic. The unused ValueVectorMap type alias is removed from value.hh.

Changes

Grouping helper refactor

Layer / File(s) Summary
Shared grouping helpers
src/libexpr/primops.cc
Adds NameValue pair type, sortByName stable sort helper, and forEachByName helper that iterates contiguous equal-name runs and builds per-name ListBuilders.
zipAttrsWith refactor
src/libexpr/primops.cc
Rewrites prim_zipAttrsWith to collect (name, value) pairs and process them via sortByName/forEachByName, removing inline stable sort and manual index/run-walking logic.
groupBy refactor and type cleanup
src/libexpr/primops.cc, src/libexpr/include/nix/expr/value.hh
Rewrites prim_groupBy to accumulate (key, element) pairs and build output attribute lists via sortByName/forEachByName, eliminating the map-based accumulation; removes the now-unused ValueVectorMap type alias.

Estimated code review effort: 2 (Simple) | ~15 minutes

Related PRs: None identified.

Suggested labels: refactor, libexpr

Suggested reviewers: None identified.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately reflects the main change: removing ValueVectorMap from builtins.groupBy.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch improve-groupBy

Comment @coderabbitai help to get the list of available commands.

@edolstra edolstra changed the title groupBy: Drop ValueVectorMap builtins.groupBy: Drop ValueVectorMap Jul 6, 2026
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

@github-actions github-actions Bot temporarily deployed to pull request July 6, 2026 16:10 Inactive
@edolstra edolstra added this pull request to the merge queue Jul 6, 2026
Merged via the queue into main with commit 4a1f801 Jul 6, 2026
33 checks passed
@edolstra edolstra deleted the improve-groupBy branch July 6, 2026 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants