Skip to content

Optimize identity function calls #14037

@roberth

Description

@roberth

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


Add 👍 to issues you find important.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureFeature request or proposallanguageThe Nix expression language; parser, interpreter, primops, evaluation, etc

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions