mirror of
https://github.com/NixOS/nix.git
synced 2025-11-25 19:51:00 +01:00
Nix, the purely functional package manager
With maximal laziness, you would expect that a function like this
fib = n:
if n == 0 then 0 else
if n == 1 then 1 else
builtins.add (fib (builtins.sub n 1)) (fib (builtins.sub n 2));
can be evaluated efficiently, because maximal laziness should
implictly memoize the recursive calls to "fib". However, non-strictness
interferes with this: the argument "n" is generally not in a form
that allows the memoization to work (e.g., it will be something like
(20 - 1 - 2 - 2) rather than 15). By the time that "n" is
evaluated (in "if n == 0 ..."), we're already deep in the evaluation
of the call.
(Strictness solves this:
builtins.add (strict fib (builtins.sub n 1)) (strict fib (builtins.sub n 2));
but that's not a very nice approach.)
With short-circuiting, the evaluator will check after evaluating a
term, whether that term is the argument of a function call that
we're currently evaluating. If so, it will check to see if the same
call but with the evaluated argument is in the normal form cache.
For instance, after evaluating (20 - 1 - 2 - 2) to 15, if we see
that "fib (20 - 1 - 2 - 2)" is currently being evaluated, we check
to see if "fib 15" is in the normal form cache. If so, we unwind
the stack (by throwing an exception) up to the evalExpr call
responsible for "fib (20 - 1 - 2 - 2)", which can then immediately
return the normal form for "fib 15". And indeed this makes "fib"
run in O(n) time.
The overhead for checking the active function calls (which isn't
very smart yet) seems to be modest, about 2% for "nix-env -qa
--drv-path --out-path" on Nixpkgs.
|
||
|---|---|---|
| blacklisting | ||
| corepkgs | ||
| doc | ||
| externals | ||
| make | ||
| misc | ||
| scripts | ||
| src | ||
| tests | ||
| aterm-gc.supp | ||
| AUTHORS | ||
| bootstrap.sh | ||
| ChangeLog | ||
| configure.ac | ||
| COPYING | ||
| INSTALL | ||
| Makefile.am | ||
| nix.conf.example | ||
| nix.spec.in | ||
| README | ||
| substitute.mk | ||
For installation and usage instructions, please read the manual, which can be found in `docs/manual/manual.html', and additionally at the Nix website at <http://www.cs.uu.nl/groups/ST/Trace/Nix>. Acknowledgments This product includes software developed by the OpenSSL Project for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)