Use std::set<StringContextElem> not PathSet for string contexts#7710
Use std::set<StringContextElem> not PathSet for string contexts#7710thufschmitt merged 1 commit intoNixOS:masterfrom
std::set<StringContextElem> not PathSet for string contexts#7710Conversation
|
The pedant in me notes that your commit and PR title uses |
std::std<StringContextElem> not PathSet for string contextsstd::set<StringContextElem> not PathSet for string contexts
1de3dc7 to
e225eb8
Compare
|
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: |
e225eb8 to
b192abc
Compare
|
Rebased, conflicts fixed. |
thufschmitt
left a comment
There was a problem hiding this comment.
Looks good overal, quite welcome change :)
A few unusual things wrt the for loops that would at least deserve a bit of explanation.
The thing I'm not sure about is whether we need the Exact variant is still needed. But since this PR made the conservative choice here, we might just merge it and see later in the worst case.
| NixStringContext context; | ||
| std::transform( | ||
| std::make_move_iterator(refs.begin()), | ||
| std::make_move_iterator(refs.end()), | ||
| std::inserter(context, context.begin()), | ||
| [](const StorePath && p) { | ||
| return NixStringContextElem::Opaque { | ||
| .path = std::move((StorePath &&)p), | ||
| }; | ||
| }); |
There was a problem hiding this comment.
Is there a reason for not using a plain for loop here? As much as I like good functional structures, C++ makes it painfully verbose and hard to understand compared to the simpler
| NixStringContext context; | |
| std::transform( | |
| std::make_move_iterator(refs.begin()), | |
| std::make_move_iterator(refs.end()), | |
| std::inserter(context, context.begin()), | |
| [](const StorePath && p) { | |
| return NixStringContextElem::Opaque { | |
| .path = std::move((StorePath &&)p), | |
| }; | |
| }); | |
| NixStringContext context; | |
| for (auto & p : refs) { | |
| context.insert(NixStringContextElem::Opaque(std::move(p))); | |
| } |
There was a problem hiding this comment.
I think I was concerned about copying vs moving, but sure we can try to for loop first.
There was a problem hiding this comment.
I accepted the change, but based on trying the thing below I think that auto expands to const _, and thus I am not really sure what is happening.
There was a problem hiding this comment.
I suppose it might leave behind an invalid std::set which nonetheless does still deallocate correctly.
src/libexpr/primops/context.cc
Outdated
| auto i = std::make_move_iterator(context.begin()); | ||
| auto e = std::make_move_iterator(context.end()); | ||
| for (; i != e; ++i) { |
There was a problem hiding this comment.
What's the reason for this rather than directly iterating over context ?
There was a problem hiding this comment.
probably again concerned about moving.
There was a problem hiding this comment.
I tired getting rid of this but it was not obvious how to do so without an incorrect const sneaking in.
There was a problem hiding this comment.
Ah I just did the same cast for this and the other one, to make clang happy. Probably same making invalid set + safe dealloc is going on.
src/libexpr/eval-cache.hh
Outdated
| // The order is not supposed to matter, we but we are precisely tracking | ||
| // evaluation | ||
| typedef std::vector<NixStringContextElem> NixStringContextExact; | ||
| typedef std::pair<std::string, NixStringContextExact> string_t; |
There was a problem hiding this comment.
I'm not sure whether we really need that. The definition of the String type mentions that the context must be sorted any way. So I think it's OK to just store it in an arbitrary order in the db
There was a problem hiding this comment.
Oh nice, I'll do what you say and cite that comment. Thanks!
ab70fa4 to
3607521
Compare
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where NixOS#7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR NixOS#7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <[email protected]>
3607521 to
85f0cdc
Compare
|
OK, I think this is ready now. |
Motivation
PathSetis not correct because string contexts have other forms (BuiltandDrvDeep) that are not rendered as plain store paths. Instead of wrongly usingPathSet, or "stringly typed" usingStringSet, usestd::std<StringContextElem>.In support of this change,
NixStringContextis now defined asstd::std<StringContextElem>notstd:vector<StringContextElem>. The old definition was just used by agetContextmethod which was only used by the eval cache. It can be deleted altogether since the types are now unified and the preexistingcopyContextfunction already suffices.Summarizing the previous paragraph:
Old:
value/context.hh:NixStringContext = std::vector<StringContextElem>value.hh:NixStringContext Value::getContext(...)value.hh:copyContext(...)New:
value/context.hh:NixStringContext = std::set<StringContextElem>value.hh:copyContext(...)This makes
NixStringContextthe common thing, and narrows the scope of the odder and rarely usedNixStringContextExact.The string representation of string context elements no longer contains the store dir. The diff of
src/libexpr/tests/value/context.ccshould make clear what the new representation is, so we recommend reviewing that file first. This was done for two reasons:Less API churn:
Value::mkStringand friends did not take aStorebefore. But ifNixStringContextElem::{parse, to_string}do take a store (as they did before), then we cannot have theValuefunctions use them (in order to work with the fully-structuredNixStringContext) without adding that argument.That would have been a lot of churn of threading the store, and this diff is already large enough, so the easier and less invasive thing to do was simply make the element
parseandto_stringfunctions not take theStorereference, and the easiest way to do that was to simply drop the store dir.Space usage:
Dropping the
/nix/store/(or similar) from the internal representation will safe space in the heap of the Nix programming being interpreted. If the heap contains many strings with non-trivial contexts, the saving could add up to something significant.The eval cache version is bumped.
The eval cache serialization uses
NixStringContextElem::{parse, to_string}, and since those functions are changed per the above, that means the on-disk representation is also changed.This is simply done by changing the name of the used for the eval cache from
eval-cache-v4to eval-cache-v5`.To avoid some duplication
EvalCache::mkPathStringis added to abstract over the simple case of turning a store path to a string with just that string in the context.Context
This PR picks up where #7543 left off. That one introduced the fully structured
NixStringContextElemdata type, but keptPathSet contextas an awkward middle ground between internalchar[][]interpreter heap string contexts andNixStringContextfully parsed string contexts.The infelicity of
PathSet contextwas specifically called out during Nix team group review, but it was agreeing that fixing it could be left as future work. This is that future work.A possible follow-up step would be to get rid of the
char[][]evaluator heap representation, too, but it is not yet clear how to do that. To useNixStringContextElemthere we would need to get the STL containers to GC pointers in the GC build, and I am not sure how to do that.PR #7543 effectively is writing the inverse of a
mkPathString,mkOutputString, and one more such function for theDrvDeepcase. I would like that PR to have property tests ensuring it is actually the inverse as expected.This PR sets things up nicely so that reworking that PR to be in that more elegant and better tested way is possible.
Checklist for maintainers
Maintainers: tick if completed or explain if not relevant
tests/**.shsrc/*/teststests/nixos/*