From fcd5e5370320d934f371bb96069a4719e94aafcd Mon Sep 17 00:00:00 2001 From: sterni Date: Tue, 11 Oct 2022 14:34:30 +0200 Subject: fix(tvix/eval/builtins): force acc not list element in foldl' When investigating discrepancies between foldl' in tvix and C++ Nix, I discovered that C++ Nix's foldl' doesn't seem to be strict at all. Since this seemed wrong, I looked into Haskell's foldl' implementation which doesn't force the list elements (`val` in our code), but the accumulation value (`res` in our code). You can look at the code here: https://hackage.haskell.org/package/base-4.17.0.0/docs/src/GHC.List.html#foldl%27 This actually makes a lot of sense: If `res` is not forced after each application of `op`, we'll end up thunks nested as deeply as the list is long, potentially taking up a lot of space. This can be limited by forcing the `res` thunk before applying `op` again (and creating a new thunk). I've also PR-ed an equivalent change for C++ Nix at https://github.com/NixOS/nix/pull/7158. Since this is not merged nor backported to our Nix 2.3 fork, I've not copied the eval fail test yet, since it wouldn't when checking our tests against C++ Nix in depot. Change-Id: I34edf6fc3031fc1485c3e714f2280b4fba8f004b Reviewed-on: https://cl.tvl.fyi/c/depot/+/6947 Autosubmit: sterni Reviewed-by: grfn Tested-by: BuildkiteCI --- .../src/tests/tvix_tests/eval-okay-foldlStrict-lazy-elements.exp | 1 + .../src/tests/tvix_tests/eval-okay-foldlStrict-lazy-elements.nix | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-foldlStrict-lazy-elements.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-foldlStrict-lazy-elements.nix (limited to 'tvix/eval/src/tests') diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-foldlStrict-lazy-elements.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-foldlStrict-lazy-elements.exp new file mode 100644 index 000000000000..d81cc0710eb6 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-foldlStrict-lazy-elements.exp @@ -0,0 +1 @@ +42 diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-foldlStrict-lazy-elements.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-foldlStrict-lazy-elements.nix new file mode 100644 index 000000000000..fc4129a2543a --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-foldlStrict-lazy-elements.nix @@ -0,0 +1,8 @@ +let + lst = builtins.foldl' + (acc: x: acc ++ [ x ]) + [ ] + [ 42 (throw "this shouldn't be evaluated") ]; +in + +builtins.head lst -- cgit 1.4.1