-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Lazy revCount #15772
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
Merged
+529
−45
Merged
Lazy revCount #15772
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8f40805
feat(libutil): add `memo<T>(f0)` memoization combinator
roberth ee78fe1
feat(libfetchers): add lazy attribute values
roberth a08c14b
feat(libexpr): emit thunks for lazy fetcher attributes
roberth e9336fa
feat(git): make revCount lazy
roberth 0c7b61d
fix(flake): don't force revCount in fingerprint
roberth ad649e3
chore: remove redundant comment
roberth 220ccc7
fix: Restore fingerprint value for untrustworthy revCount values
roberth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "nix/expr/fetch-tree.hh" | ||
| #include "nix/expr/tests/libexpr.hh" | ||
| #include "nix/fetchers/attrs.hh" | ||
| #include "nix/fetchers/fetchers.hh" | ||
| #include "nix/store/path.hh" | ||
|
|
||
| namespace nix { | ||
|
|
||
| class LazyFetcherAttrTest : public LibExprTest | ||
| { | ||
| protected: | ||
| StorePath dummyPath() | ||
| { | ||
| return StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-test"}; | ||
| } | ||
| }; | ||
|
|
||
| TEST_F(LazyFetcherAttrTest, nonLazyAttrProducesImmediateValue) | ||
| { | ||
| fetchers::Input input; | ||
| input.attrs.insert_or_assign("type", std::string("git")); | ||
| input.attrs.insert_or_assign("revCount", uint64_t(5)); | ||
|
|
||
| Value v; | ||
| emitTreeAttrs(state, dummyPath(), input, v, false, false); | ||
| state.forceValue(v, noPos); | ||
|
|
||
| auto * rcAttr = v.attrs()->get(state.symbols.create("revCount")); | ||
| ASSERT_NE(rcAttr, nullptr); | ||
| state.forceValue(*rcAttr->value, noPos); | ||
| EXPECT_EQ(rcAttr->value->integer().value, 5); | ||
| } | ||
|
|
||
| TEST_F(LazyFetcherAttrTest, lazyAttrProducesThunk) | ||
| { | ||
| int calls = 0; | ||
| fetchers::Input input; | ||
| input.attrs.insert_or_assign("type", std::string("git")); | ||
| input.attrs.insert_or_assign( | ||
| "revCount", | ||
| fetchers::LazyAttr( | ||
| make_ref<fetchers::LazyAttrComputation>( | ||
| fetchers::LazyAttrComputation{.compute = [&calls]() -> fetchers::ResolvedAttr { | ||
| calls++; | ||
| return uint64_t(42); | ||
| }}))); | ||
|
|
||
| Value v; | ||
| emitTreeAttrs(state, dummyPath(), input, v, false, false); | ||
| state.forceValue(v, noPos); | ||
|
|
||
| auto * rcAttr = v.attrs()->get(state.symbols.create("revCount")); | ||
| ASSERT_NE(rcAttr, nullptr); | ||
|
|
||
| // Not yet forced, so the lazy function should not have been called | ||
| EXPECT_EQ(calls, 0); | ||
|
|
||
| // Force the thunk | ||
| state.forceValue(*rcAttr->value, noPos); | ||
| EXPECT_EQ(rcAttr->value->integer().value, 42); | ||
| EXPECT_EQ(calls, 1); | ||
| } | ||
|
|
||
| TEST_F(LazyFetcherAttrTest, lazyFunctionOnlyCalledOnAccess) | ||
| { | ||
| int calls = 0; | ||
| fetchers::Input input; | ||
| input.attrs.insert_or_assign("type", std::string("git")); | ||
| input.attrs.insert_or_assign("lastModified", uint64_t(1000)); | ||
| input.attrs.insert_or_assign( | ||
| "revCount", | ||
| fetchers::LazyAttr( | ||
| make_ref<fetchers::LazyAttrComputation>( | ||
| fetchers::LazyAttrComputation{.compute = [&calls]() -> fetchers::ResolvedAttr { | ||
| calls++; | ||
| return uint64_t(99); | ||
| }}))); | ||
|
|
||
| Value v; | ||
| emitTreeAttrs(state, dummyPath(), input, v, false, false); | ||
| state.forceValue(v, noPos); | ||
|
|
||
| // Access lastModified, so should not trigger lazy revCount | ||
| auto * lmAttr = v.attrs()->get(state.symbols.create("lastModified")); | ||
| ASSERT_NE(lmAttr, nullptr); | ||
| state.forceValue(*lmAttr->value, noPos); | ||
| EXPECT_EQ(lmAttr->value->integer().value, 1000); | ||
| EXPECT_EQ(calls, 0); | ||
|
|
||
| // Now access revCount | ||
| auto * rcAttr = v.attrs()->get(state.symbols.create("revCount")); | ||
| ASSERT_NE(rcAttr, nullptr); | ||
| state.forceValue(*rcAttr->value, noPos); | ||
| EXPECT_EQ(rcAttr->value->integer().value, 99); | ||
| EXPECT_EQ(calls, 1); | ||
| } | ||
|
|
||
| } // namespace nix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #pragma once | ||
|
|
||
| #include "nix/expr/eval.hh" | ||
|
|
||
| namespace nix { | ||
|
|
||
| /** | ||
| * Convert a libfetchers `Input` to libexpr `Value`. | ||
| */ | ||
| void emitTreeAttrs( | ||
| EvalState & state, | ||
| const StorePath & storePath, | ||
| const fetchers::Input & input, | ||
| Value & v, | ||
| bool emptyRevFallback = false, | ||
| bool forceDirty = false); | ||
|
|
||
| } // namespace nix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "nix/fetchers/attrs.hh" | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
|
|
||
| namespace nix::fetchers { | ||
|
|
||
| TEST(LazyAttr, resolveToInt) | ||
| { | ||
| Attrs attrs; | ||
| attrs.insert_or_assign( | ||
| "count", LazyAttr(make_ref<LazyAttrComputation>(LazyAttrComputation{.compute = []() -> ResolvedAttr { | ||
| return uint64_t(42); | ||
| }}))); | ||
| EXPECT_EQ(maybeGetIntAttr(attrs, "count"), 42); | ||
| } | ||
|
|
||
| TEST(LazyAttr, resolveToString) | ||
| { | ||
| Attrs attrs; | ||
| attrs.insert_or_assign( | ||
| "name", LazyAttr(make_ref<LazyAttrComputation>(LazyAttrComputation{.compute = []() -> ResolvedAttr { | ||
| return std::string("hello"); | ||
| }}))); | ||
| EXPECT_EQ(maybeGetStrAttr(attrs, "name"), "hello"); | ||
| } | ||
|
|
||
| TEST(LazyAttr, resolveToBool) | ||
| { | ||
| Attrs attrs; | ||
| attrs.insert_or_assign( | ||
| "flag", LazyAttr(make_ref<LazyAttrComputation>(LazyAttrComputation{.compute = []() -> ResolvedAttr { | ||
| return Explicit<bool>{true}; | ||
| }}))); | ||
| EXPECT_EQ(maybeGetBoolAttr(attrs, "flag"), true); | ||
| } | ||
|
|
||
| TEST(LazyAttr, attrsToJSONForcesLazy) | ||
| { | ||
| Attrs attrs; | ||
| attrs.insert_or_assign( | ||
| "x", LazyAttr(make_ref<LazyAttrComputation>(LazyAttrComputation{.compute = []() -> ResolvedAttr { | ||
| return uint64_t(99); | ||
| }}))); | ||
| auto json = attrsToJSON(attrs); | ||
| EXPECT_EQ(json["x"], 99); | ||
| } | ||
|
|
||
| TEST(LazyAttr, attrsToQueryForcesLazy) | ||
| { | ||
| Attrs attrs; | ||
| attrs.insert_or_assign( | ||
| "v", LazyAttr(make_ref<LazyAttrComputation>(LazyAttrComputation{.compute = []() -> ResolvedAttr { | ||
| return std::string("val"); | ||
| }}))); | ||
| auto query = attrsToQuery(attrs); | ||
| EXPECT_EQ(query.at("v"), "val"); | ||
| } | ||
|
|
||
| TEST(LazyAttr, notCalledUntilForced) | ||
| { | ||
| int calls = 0; | ||
| Attrs attrs; | ||
| attrs.insert_or_assign( | ||
| "lazy", LazyAttr(make_ref<LazyAttrComputation>(LazyAttrComputation{.compute = [&calls]() -> ResolvedAttr { | ||
| calls++; | ||
| return uint64_t(1); | ||
| }}))); | ||
| EXPECT_EQ(calls, 0); | ||
| maybeGetIntAttr(attrs, "lazy"); | ||
| EXPECT_EQ(calls, 1); | ||
| } | ||
|
|
||
| } // namespace nix::fetchers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.