Skip to content

Conversation

@Radvendii
Copy link
Contributor

@Radvendii Radvendii commented Oct 27, 2025

Motivation

For big-picture motivation, see the tracking issue

This PR moves the ExprLambda data into our allocator

  • saves 32 bytes per ExprLambda which has Formals ({ foo, bar }: rather than x: )
  • before:
    • 8 bytes: Formals *
    • 4 bytes: padding in ExprLambda before formals
    • 24 bytes: std::vector
    • 8 bytes: ellipsis bool + padding
  • after:
    • 2 bytes: nFormals
    • 2 bytes: ellipsis bool + hasFormals bool + padding
    • 8 bytes: Formal *

And everything else stays the same.

Alternative Design

Presumably we were using a Formals * here rather than Formals directly so that we don't need to take up the extra space in the common case that there are no formals. One effect of using Formals * rather than Formals directly has been that we don't need to take up the extra space to store a Formals in the common case that there are no formals.

This PR does not make that case any worse, because we can fit the extra data (ellipses + hasFormals + nFormals) in the space that was padding before.

However, we could also choose to put all formals in one array and index into it, reducing the 8 byte Formal * to a 4 byte index.

IMO this is an optimization to look into in the future, when we start putting things in indexable vectors. (e.g. Exprs)

Context


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@Radvendii Radvendii requested a review from edolstra as a code owner October 27, 2025 20:53
@github-actions github-actions bot added the new-cli Relating to the "nix" command label Oct 27, 2025
@Radvendii Radvendii mentioned this pull request Oct 27, 2025
38 tasks
@github-actions github-actions bot added the with-tests Issues related to testing. PRs with tests have some priority label Oct 27, 2025
@Radvendii Radvendii force-pushed the exprlambda-alloc branch 4 times, most recently from 80068f9 to 5db63f3 Compare October 28, 2025 12:15
coderabbitai[bot]

This comment was marked as off-topic.

@NixOS NixOS deleted a comment from coderabbitai bot Oct 28, 2025
@NixOS NixOS deleted a comment from coderabbitai bot Oct 28, 2025
@NixOS NixOS deleted a comment from coderabbitai bot Oct 28, 2025
@NixOS NixOS deleted a comment from coderabbitai bot Oct 28, 2025
Copy link
Member

@Ericson2314 Ericson2314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the way it looks now, thank you very much @Radvendii!

@Ericson2314 Ericson2314 added this pull request to the merge queue Oct 31, 2025
Merged via the queue into NixOS:master with commit 9e79e83 Oct 31, 2025
17 checks passed
Comment on lines -516 to +551
}
std::ranges::copy(formals.formals, formalsStart);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a check in case there a more than 65k formals. We must fail gracefully.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would overflow the heap buffer otherwise. Let's please not do this.

, formals(formals)
, hasFormals(true)
, ellipsis(formals.ellipsis)
, nFormals(formals.formals.size())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can do a silent truncation from size_t -> uint16_t.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants