-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
Is your feature request related to a problem?
The identity function is useful when you need a more complex function to do something less complex.
For instance, concatLists could be defined as concatMap id.
Unfortunately the latter has a minor performance cost
- 1× extra argument
- n× extra function call
If we could improve the performance of id usages like that, we get to have both good performance and a clean library of builtins.
Proposed solution
The latter cost of function calls could be avoided by making the identity function easy to... identify.
Then functions like concatMap, all and concatMapAttrs can detect it cheaply (more so than reading the AST), and skip the function call in every iteration.
Since attribute-based callbacks require an arity two callback, we'll need either builtins.ignore1 for k: v: v or an easily identifiable composition of functions; perhaps just _k: id, which is still a bit simpler than checking k: v: v
Alternative solutions
Identify id implementations by reading the AST.
Additional context
Nixpkgs lib should define its id as builtins.id or (v: v)
Checklist
- checked latest Nix manual (source)
- checked open feature issues and pull requests for possible duplicates
Add 👍 to issues you find important.