-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add memoise primop #10280
base: master
Are you sure you want to change the base?
Add memoise primop #10280
Conversation
'builtins.memoise f x' is equal to 'f x', but uses a cache to speed up repeated invocations of 'f' with the same 'x'. A typical application is memoising evaluations of Nixpkgs in NixOps network specifications. For example, with the patch below, the time to evaluate a 10-machine NixOps network is reduced from 17.1s to 9.6s, while memory consumption goes from 4486 MiB to 3089 MiB. (This is with GC_INITIAL_HEAP_SIZE=16G.) Nixpkgs patch: diff --git a/pkgs/top-level/impure.nix b/pkgs/top-level/impure.nix index a9f21e45aed..f641067e022 100644 --- a/pkgs/top-level/impure.nix +++ b/pkgs/top-level/impure.nix @@ -79,7 +79,7 @@ in # not be passed. assert args ? localSystem -> !(args ? system || args ? platform); -import ./. (builtins.removeAttrs args [ "system" "platform" ] // { +builtins.memoise or (x: x) (import ./.) (builtins.removeAttrs args [ "system" "platform" ] // { inherit config overlays crossSystem; # Fallback: Assume we are building packages on the current (build, in GNU # Autotools parlance) system.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimizations, and reduced strictness.
(and needs rebase)
if ((*this)(v2->listElems()[n], v1->listElems()[n])) return false; | ||
} | ||
|
||
return n == v1->listSize() && n < v2->listSize(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ordering established by this comparator doesn't have to be exactly lexicographical.
What if we compare the size first, and only compare lexicographically if the sizes are equal?
if ((*this)(i->value, j->value)) return true; | ||
if ((*this)(j->value, i->value)) return false; | ||
} | ||
return i == v1->attrs->end() && j != v2->attrs->end(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like the list ordering, the size could be compared first.
Also, the set of names should be compared before evaluating anything extra.
Motivation
Written by @edolstra, but PR opened by @roberth: This change comes up every now and then. I'm opening a draft PR for it, to increase visibility and so that a discussion can be had.
'builtins.memoise f x' is equal to 'f x', but uses a cache to speed up repeated invocations of 'f' with the same 'x'. A typical application is memoising evaluations of Nixpkgs in NixOps network specifications. For example, with the patch below, the time to evaluate a 10-machine NixOps network is reduced from 17.1s to 9.6s, while memory consumption goes from 4486 MiB to 3089 MiB. (This is with GC_INITIAL_HEAP_SIZE=16G.)
Nixpkgs patch:
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.