Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Add Value types, use std::span for list iteration #9370

Merged
merged 7 commits into from
Nov 20, 2023

Conversation

roberth
Copy link
Member

@roberth roberth commented Nov 17, 2023

Motivation

I've done some work on exception safety in the evaluator that's resulting in some refactoring. I'm not done with it yet, but I believe these commits should be uncontroversial and I figured StringWithContext might be semi-useful for @tomberek's work on string performance.

Changes:

  • Add explicit type names for the items of the Value union. They aren't used yet, but can be used to write more type safe helper functions.
  • Use std::span for accessing list values. That's a view of a contiguous memory region a dynamic number of instances of a certain type. string_view is like a span<char>.
    • This changes how const is used. I've explained it in the second to last commit message.
  • The change in nix-env is also basically a no-op, but makes it so that all list initialization happens through EvalState, which will be useful later.

Context

Reasons why I'm looking into exception safety:

Priorities

Add 👍 to pull requests you find important.

**`Value` and `const`**

These two deserve some explanation. We'll get to lists later.

Values can normally be thought of as immutable, except they are
are also the vehicle for call by need, which must be implemented
using mutation.

This circumstance makes a `const Value` a rather useless thing:

 - If it's a thunk, you can't evaluate it, except by copying, but
   that would not be call by need.

 - If it's not a thunk, you know the type, so the method that
   acquired it for you should have returned something more specific,
   such as a `const Bindings &` (which actually does make sense
   because that's an immutable span of pointers to mutable `Value`s.

 - If you don't care about the type yet, you might establish the
   convention that `const Value` means `deepSeq`-ed data, but
   this is hardly useful and not actually as safe as you would
   supposedly want to trust it to be - just convention.

**Lists**

`std::span` is a tuple of pointer and size - just what we need.

We don't return them as `const Value`, because considering the
first bullet point we discussed before, we'd have to force all
the list values, which isn't what we want.

So what we end up with is a nice representation of a list in
weak head normal form: the spine is immutable, but the
items may need some evaluation later.
@roberth roberth added the language The Nix expression language; parser, interpreter, primops, evaluation, etc label Nov 17, 2023
@roberth roberth requested a review from edolstra as a code owner November 17, 2023 09:40
@roberth roberth requested review from tomberek and removed request for edolstra November 17, 2023 09:40
@Ericson2314
Copy link
Member

(But @tomberek is reviewing it too :))

@tomberek tomberek self-assigned this Nov 17, 2023
@edolstra
Copy link
Member

A while ago we had to revert a use of std::span because of GCC 9 (5978ceb), but I guess that's not an issue anymore?

@roberth
Copy link
Member Author

roberth commented Nov 17, 2023

GCC 9

I don't think we need to support that anymore. We have GCC 12.2 for aarch64-linux, in the flake here and in NixOS 23.05.
If we want to support previous versions of GCC, we could add them to CI if reasonable, but not until someone asks us and we decide an EOL for it. Fedora is at 12.3 or 13. Debian stable has 12.2.

@Ericson2314
Copy link
Member

Yeah the 23.05 bump solved that.

Copy link
Contributor

@tomberek tomberek left a comment

Choose a reason for hiding this comment

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

This should help make further improvements easier.

@tomberek tomberek merged commit fb68699 into NixOS:master Nov 20, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language The Nix expression language; parser, interpreter, primops, evaluation, etc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants