Commit graph

96 commits

Author SHA1 Message Date
Silvan Mosberger
31d23ba418 lib.attrsets: Remove unneeded polyfills
Nix 2.3 (the minimum version needed to evaluate Nixpkgs) supports these, so no need to keep them around.
2024-02-09 05:46:03 +01:00
Alois Wohlschlager
d33127863e
lib: make deprecation warnings consistent
The deprecation warnings in lib were wildly inconsistent. Different
formulations were used in different places for the same meaning. Some warnings
used builtins.trace instead of lib.warn, which prevents silencing; one even
only had a comment instead. Make everything more uniform.
2024-02-03 19:01:39 +01:00
Robert Hensing
a7aa95db53 lib/attrsets: Document and link Nix language operators 2023-12-13 18:38:41 +01:00
Robert Hensing
7d993b9521 lib.attrsets.hasAttrByPath: Document law and laziness, and test it 2023-12-08 23:19:09 +01:00
Robert Hensing
72bd4bbb58 lib.attrsets.longestValidPathPrefix: init
Allows finding the most specific path that exists.
This is useful for error messages relating to attribute paths.
2023-12-08 23:15:13 +01:00
Robert Hensing
51357572f2
Merge pull request #269552 from adisbladis/lib-matchattrs-list-allocs
lib.attrsets.matchAttrs: Avoid some list allocations when walking structure
2023-11-27 14:44:37 +01:00
adisbladis
544a1d375b lib.attrsets.attrByPath: Don't allocate one extra list per lookup recursion
Using `tail` in a recursive loop like this needlessly allocates.
This changes the loop to look up by list index instead.
2023-11-27 11:37:57 +13:00
adisbladis
7e07b3ecd5 lib.attrsets.hasAttrByPath: Don't allocate one extra list per lookup recursion
Using `tail` in a recursive loop like this needlessly allocates.
This changes the loop to look up by list index instead.
2023-11-27 11:37:56 +13:00
adisbladis
013a0a1357 lib.attrsets.matchAttrs: Avoid some list allocations when walking structure
Benchmarks (`nix-instantiate ./. -A python3`):

- Before:
``` json
{
  "cpuTime": 0.29049500823020935,
  "envs": {
    "bytes": 4484216,
    "elements": 221443,
    "number": 169542
  },
  "gc": {
    "heapSize": 402915328,
    "totalBytes": 53086800
  },
  "list": {
    "bytes": 749424,
    "concats": 4242,
    "elements": 93678
  },
  "nrAvoided": 253991,
  "nrFunctionCalls": 149848,
  "nrLookups": 49612,
  "nrOpUpdateValuesCopied": 1587837,
  "nrOpUpdates": 10104,
  "nrPrimOpCalls": 130356,
  "nrThunks": 358981,
  "sets": {
    "bytes": 30423600,
    "elements": 1859999,
    "number": 41476
  },
  "sizes": {
    "Attr": 16,
    "Bindings": 16,
    "Env": 16,
    "Value": 24
  },
  "symbols": {
    "bytes": 236145,
    "number": 24453
  },
  "values": {
    "bytes": 10502520,
    "number": 437605
  }
}
```

- After:
``` json
{
  "cpuTime": 0.2946169972419739,
  "envs": {
    "bytes": 3315224,
    "elements": 172735,
    "number": 120834
  },
  "gc": {
    "heapSize": 402915328,
    "totalBytes": 48718432
  },
  "list": {
    "bytes": 347568,
    "concats": 4242,
    "elements": 43446
  },
  "nrAvoided": 173252,
  "nrFunctionCalls": 101140,
  "nrLookups": 73595,
  "nrOpUpdateValuesCopied": 1587837,
  "nrOpUpdates": 10104,
  "nrPrimOpCalls": 83067,
  "nrThunks": 304216,
  "sets": {
    "bytes": 29704096,
    "elements": 1831673,
    "number": 24833
  },
  "sizes": {
    "Attr": 16,
    "Bindings": 16,
    "Env": 16,
    "Value": 24
  },
  "symbols": {
    "bytes": 236145,
    "number": 24453
  },
  "values": {
    "bytes": 8961552,
    "number": 373398
  }
}
```
2023-11-27 11:20:50 +13:00
Silvan Mosberger
055ba65fed lib: Take advantage of section descriptions
See https://github.com/nix-community/nixdoc/releases/tag/v2.6.0
2023-11-20 03:02:11 +01:00
Silvan Mosberger
5323fbf703
Merge pull request #254452 from flyingcircusio/lib-attrsToList
lib.attrsets.attrsToList: add function
2023-10-10 19:49:17 +02:00
Oliver Schmidt
d70633f91c lib.attrsets.attrsToList: add function
For transforming back between lists and attrsets, it makes sense to have
a quasi-inverse of `builtins.listToAttrs` available as a library
function.

Co-authored-by: Silvan Mosberger <github@infinisil.com>
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
2023-10-10 16:11:01 +02:00
Silvan Mosberger
dd72ff27f7 lib.attrsets.foldlAttrs: Make stricter
See the parent commit for the same change to lib.lists.foldl'
2023-09-27 02:43:59 +02:00
Silvan Mosberger
3b6169f87b lib.lists.foldl': Make strict in the initial accumulator
To maintain backwards compatibility, this can't be changed in the Nix language.
We can however ensure that the version Nixpkgs has the more intuitive behavior.
2023-09-27 02:43:59 +02:00
Silvan Mosberger
fa503f4b92 lib.attrsets.mergeAttrsList: init
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
2023-07-18 20:27:15 +02:00
figsoda
12e01c677c lib/attrsets: remove unused let bindings 2023-06-23 09:41:04 +00:00
Robert Hensing
5319ddf7dc lib.concatMapAttrs: Simplify stack trace 2023-05-31 14:57:37 +02:00
zimbatm
ad0b21fbbc
lib: add missing removeAttrs builtin
I'm expecting all the builtins.* functions to be available in lib.*
2023-04-19 21:27:38 +02:00
hsjobeki
15a8d05ba5 init: lib.foldlAttrs
- provide comprehensive example
- add unit test
2023-03-11 10:42:00 +01:00
Colin Arnott
6ff66fcbd7
lib: standardise attrset type syntax
There are a number of different syntaxes used for attrset type
signatures in our doc strings, this change standardises upon one that
uses :: for specifying attribute type, and ; terminators to be
consistent with nix syntax. There are no bugs in the functions
themselves, just that different syntaxes may confuse new users.
2023-01-30 23:53:44 +00:00
Adam Joseph
a4e3e16258
Update lib/attrsets.nix
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
2023-01-25 02:52:54 +00:00
Adam Joseph
ee00af7a32 attrsets: clarify that mapAttrs maps over *leaf* attrs 2023-01-22 15:19:09 -08:00
YoshiRulz
7fba83890c
lib: Fix mismatched quotes in lib.* doc comments
caused problems for automated rich text generation such as
https://teu5us.github.io/nix-lib.html#customisation-functions
2023-01-02 08:25:17 +10:00
hsjobeki
5ff21bfc73 attrsets: fix and add some doc types 2022-12-24 14:08:11 +01:00
figsoda
695d4bc76b lib: fix typos 2022-12-17 18:59:29 -05:00
Ryan Mulligan
ac1ae0a58e doc: auto-generate asserts and attrset library docs
If all the docs are auto-generated, it should be easier to convert
them to Commonmark.

Co-Authored-By: Valentin Gagarin <valentin.gagarin@tweag.io>
Co-Authored-By: Silvan Mosberger <contact@infinisil.com>
2022-12-08 22:58:22 +01:00
figsoda
f993f8a186 lib/attrsets: add concatMapAttrs 2022-11-17 10:41:53 -05:00
figsoda
4536ebad69 lib/attrsets: simplify chooseDevOutputs 2022-11-17 09:41:47 -05:00
Adam Joseph
037cf2fad1 unionOfDisjoint: use builtins.intersectAttrs
This brings two benefits:

1. The complete list of collisions is printed in the whenever any colliding
   attribute is accessed.

2. The sets are intersected using a C++ primitive, which runs in O(n) time
   (intersecting pre-sorted lists) with small constants rather than interpreted
   Nix code.

Thanks to @toonn for prompting this improvement.
2022-09-25 00:09:15 -07:00
Adam Joseph
99da193877 note that unionOfDisjoint is commutative, unlike // 2022-09-12 12:59:16 -07:00
Adam Joseph
47de66b1a4 lib/attrsets.nix: add unionOfDisjoint 2022-09-12 12:53:31 -07:00
Robert Hensing
4d2237c841 lib.foldAttrs: Clarify example 2022-05-10 07:01:00 +02:00
Robert Hensing
5ff918bf55
Merge pull request #147077 from Infinisil/updateAttrPaths
Introduce `lib.updateManyAttrsByPath`
2022-03-19 19:00:03 +01:00
Silvan Mosberger
85003ecdbb lib.attrsets: Introduce updateManyAttrsByPath 2022-03-18 00:08:29 +01:00
Silvan Mosberger
71b130c581 lib.attrsets: Introduce showAttrPath 2022-03-18 00:05:08 +01:00
Robert Hensing
3ddac7a41a lib.isDerivation: Simplify 2022-03-09 15:48:30 +01:00
John Rinehart
5688e7ff35 fix: typo in lib/attrsets.nix 2022-02-10 01:16:20 +02:00
pennae
6d44bc5b90
Merge pull request #152392 from polykernel/attrset-optimizations-patch-1
lib/attrset: various function optimizations
2022-01-11 16:01:52 +00:00
polykernel
63ce7d3184 lib/attrset: miscellaneous optimizations
- Eta reduce `mapAttrsRecursiveCond`, `foldAttrs`, `getAttrFromPath`.
- Modify `matchAttrs` to use `elemAt` instead of `head (tail xs)` to access
  elements.
- Modify `matchAttrs` to use `any id` instead of `foldr and true`.
2021-12-27 22:24:28 -05:00
polykernel
a54f2231c9 lib/attrset: optimize element access in recursiveUpdateUntil
- Eta reduce formal arguments of `recursiveUpdate'.
- Access elements in `recursiveUpdateUntil` using `elemAt` and `head`
  directly instead of `head (tail xs)` which copies a singleton unnecessarily.
  (`elemAt` is used instead of `last` to save a primitive call to `length`,
  this is possible because the 2-tuple structure is guranteed)
- Use `length` instead of comparison to empty list to save a copy.
2021-12-27 18:17:52 -05:00
pennae
2dcae7d82f lib/attrsets: use builtins.zipAttrsWith if available 2021-12-25 15:20:26 +01:00
pennae
dc895fb281 lib: make extendDerivation lighter on eval
the fix to extendDerivation in #140051 unwittingly worsened eval performance by
quite a bit. set elements alone needed over 1GB extra after the change, which
seems disproportionate to how small it was. if we flip the logic used to
determine which outputs to install around and keep a "this one exactly" flag in
the specific outputs instead of a "all of them" in the root we can avoid most
of that cost.
2021-10-15 16:39:10 +02:00
polykernel
3f4ce46a47 lib: optimize setAttrByPath and cleaup imports
- Remove inheritance of `lists.fold` as it isn't used anywhere.
- Inherit `foldl'` for consistency as only `cartesianProductOfSets` explicitly
  reference lib.
- Inline `foldr` to generate nested attrs instead of using `listToAttrs` and `tail`.
2021-08-23 18:04:47 -04:00
Ben Siraphob
b63a54f81c
Merge pull request #110742 from siraben/deprecate-fold 2021-07-27 15:13:31 +07:00
Erlend Pedersen
3452a739d1
nixpkgs-manual: lib.attrsets.mapAttrsToList returns a list (#122179) 2021-05-08 13:55:11 +02:00
Jacek Galowicz
123045a570 lib/attrsets: add cartesianProductOfSets function 2021-01-28 23:08:59 +01:00
Ben Siraphob
1c2a2b0a08 treewide: fold -> foldr 2021-01-26 10:57:07 +07:00
rnhmjoj
986079275b
lib/attrsets: add getMan function 2020-06-18 10:16:57 +02:00
Pierre Allix
106be3f6fe lib/attrsets: fix typo in the comment of mapAttrsRecursiveCond 2020-06-16 13:48:57 +02:00
Silvan Mosberger
f75c11cfdf
Merge pull request #83241 from Infinisil/valid-drv-name
lib/strings: Add `sanitizeDerivationName` function
2020-04-02 05:58:13 +02:00