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

Lazy fetchTree outPath path values #10252

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

roberth
Copy link
Member

@roberth roberth commented Mar 15, 2024

Motivation

Improve performance, and make the fetchTree interface more capable while keeping it clean.

Description

This makes fetchTree return lazy InputAccessor-based SourcePaths instead of "cowardly" fetching them to the store and returning absolute "system" paths.
It stays close to existing path semantics, including support for readFile "${toString p}/..", which some expressions rely on.
It does not go as far as lazy-trees, but judging from the amount of change I could reuse, and how little of my own I had to add, lazy-trees will be a natural extension of this PR.

Done:

  • Packages and NixOS evaluate as usual
    • no hash changes, based on my limited testing
  • Flake is not added to store unless e.g.
    • "${flake.outPath}"
    • uses module system (needs clever lazy source strings, or a change to the module system)
  • Note that the above is already an improvement over the status quo - always fetching to the store
  • iirc 0.1s reduction on nixpkgs#hello, and 1s reduction on nixosTests.simple

Conclusion so far:
Viable

TODO:

  • Check the TODO and FIXMEs
  • Update the test suite
    • probably some intended behavior change, such as removal of narHash in some observable places
    • possibly finds a bug
  • Check performance again
  • Cherry-pick the toString path behavior to lazy-trees

Context

Priorities and Process

Add 👍 to pull requests you find important.

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

@github-actions github-actions bot added new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority fetching Networking with the outside (non-Nix) world labels Mar 15, 2024
Copy link
Member Author

Choose a reason for hiding this comment

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

These changes are close to lazy-trees.

void EvalState::registerAccessor(const ref<InputAccessor> accessor)
{
inputAccessors.push_back(accessor);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This is like lazy-trees, except we only do it so that we don't destroy them when we put a non-smart pointer in Value, which has no finalizer because of GC and performance.

@@ -1973,6 +1978,17 @@ void EvalState::concatLists(Value & v, size_t nrLists, Value * * lists, const Po
}
}

// FIXME limit recursion
Copy link
Member Author

Choose a reason for hiding this comment

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

Known issue, solve later with #10240

Comment on lines +2030 to +2055
Value & vTmp0 = *vTmpP++;
i->eval(state, env, vTmp0);
Value & vTmp = *resolveOutPath(state, &vTmp0, i_pos);

/* If the first element is a path, then the result will also
be a path, we don't copy anything (yet - that's done later,
since paths are copied when they are used in a derivation),
and none of the strings are allowed to have contexts. */
if (first) {
firstType = vTmp.type();
if (firstType == nPath) {
accessor = vTmp.path().accessor;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Similar but not equal to lazy-trees.

  • I've kept the diff smaller by keeping vTmp as a reference
  • Not adding the first name to the path, because it is added again later. Probably a bug in lazy-trees.

Comment on lines +2334 to +2348
: v.path().accessor->toStringReturnsStorePath()
? store->printStorePath(copyPathToStore(context, SourcePath(v.path().accessor, CanonPath::root))) + v.path().path.absOrEmpty()
Copy link
Member Author

Choose a reason for hiding this comment

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

This is new, in order for readFile "${toString ./.}/.." to work, just as it did before.
Would not recommend to write that, but similar usages of paths exist in the wild.

Comment on lines +2451 to +2467
auto i = v.attrs->find(sOutPath);
if (i != v.attrs->end()) {
return coerceToPath(pos, *i->value, context, errorCtx);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This rule is not new. Previously, it would have worked by falling through to the coerceToString + rootPath code down below.

v.mkPath(
&*path.accessor,
// TODO: GC_STRDUP
strdup(path.path.abs().c_str()));
Copy link
Member Author

Choose a reason for hiding this comment

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

Like lazy-trees, but I had to add strdup to avoid corruption.

@@ -201,18 +201,31 @@ static void fetchTree(

state.checkURI(input.toURLString());

auto [storePath, input2] = input.fetchToStore(state.store);
if (params.returnPath) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Like #10225, but clang was lagging behind GCC's C++20.

* In both cases, the returned string functionally identifies the path,
* and can still be read.
*/
virtual bool toStringReturnsStorePath() const;
Copy link
Member Author

Choose a reason for hiding this comment

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

The only truly new semantics.

Either way, both kinds of paths are virtual in the sense that they haven't been copied yet.
If you have copied it, it'd be a string. (Not if and only if, although Nix does discourage that.)

In lazy-trees, this could be changed to a virtual store path string without a problem.

Copy link
Member Author

Choose a reason for hiding this comment

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

TODO: forward port this rule to prove it.

Copy link
Member

Choose a reason for hiding this comment

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

#10511 (comment) I think rather contain information with which to construct the path, if I understand what is going on here correctly.

Copy link
Member Author

Choose a reason for hiding this comment

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

@Ericson2314 these are solutions to different problems.

  • Here I am adding a property to distinguish the behavior between "system" path values and virtual path values in the language.
  • Linked comment seems to be about optimizing away a copy operation, which iiuc is internal to a fetcher and not perceptible by users.

Both solutions (to different problems!) are trying to solve concerns in the upper layers though.

I'll check if this one can be moved into the evaluator. We could probably just special case the system (ie posix accessor) paths there.

Copy link
Member Author

Choose a reason for hiding this comment

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

Btw, here I've picked the least breaking semantics:

  • The return value still uniquely identifies the source
  • Still deterministic
  • Still readable when converted to a system path with /. + x or /${x}

Performance is at least as good as the status quo, but not as good as lazy trees, because for toString x to be instant, you need to sacrifice one of the above, or come up with a clever scheme, like opaque placeholders in strings or something.

@ConnorBaker
Copy link
Contributor

@roberth is there anything I (or someone in general) could do to try to help move this forward? Thank you for working on this :)

@tomberek tomberek added idea approved The given proposal has been discussed and approved by the Nix team. An implementation is welcome. and removed idea approved The given proposal has been discussed and approved by the Nix team. An implementation is welcome. labels Apr 8, 2024
@roberth roberth force-pushed the lazy-fetchTree branch 2 times, most recently from a29ade5 to 8cb0fd4 Compare April 16, 2024 13:54
edolstra and others added 8 commits April 16, 2024 15:55
This picks a number of changes from the lazy-trees branch.
As it is hand picked, and does not include some other necessary
changes, it does not build. Subsequent commits will fix that.

I have added a couple of comments of my own as well.

Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
This fixes the double copy problem and improves performance
for expressions that don't force the whole source to be added to the
store.

Rules for fast expressions:

- Use path literals where possible
   - import ./foo.nix
- Use + operator with slash in string
   - src = fetchTree foo + "/src";
- Use source filtering, lib.fileset

- AVOID toString
- If possible, AVOID interpolations ("${./.}")
- If possible, move slashes into the interpolation to add less to the store
   - "${./src}/foo" -> "${./src/foo}"

toString may be improved later as part of lazy-trees, so these
recommendations are a snapshot. Path values are quite nice though.
This allows clever editors/IDEs to discern the path more easily
for Ctrl+Click navigate to functionality, e.g. when building
.?ref=HEAD
This showPath is getting a little too ad hoc, but it works for now.
@roberth
Copy link
Member Author

roberth commented Apr 16, 2024

@roberth is there anything I (or someone in general) could do to try to help move this forward? Thank you for working on this :)

Hi @ConnorBaker,

Sorry for the late response; I had to take another look at this first, and it took a while to get around to it.
I've rebased the branch, but I seem to have broken the search path somehow.

I see two ways forward, either

  • re-do the PR with reduced scope
    • don't remove the narHash, if possible; should keep more flakes code the same
    • carefully look whether other changes were absolutely necessary
  • keep going; just fix the remaining test failures, and also remove the commit delay the flake outPath semantics change for now

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2024-04-29-nix-team-meeting-minutes-142/45020/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fetching Networking with the outside (non-Nix) world new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority
Projects
Status: Defined work
Status: ⚖ To discuss
Development

Successfully merging this pull request may close these issues.

Make builtins.fetchTree return a path as its outPath element
7 participants