1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 02:21:16 +02:00
Commit graph

3295 commits

Author SHA1 Message Date
Sergei Zimmerman
ddcfc81ff1
libexpr: Document requirements for comparator passed to builtins.sort 2025-06-15 16:52:05 +00:00
Sergei Zimmerman
351d898c43
libexpr: Switch builtins.sort primop to use peeksort
This prevents C++ level undefined behavior from affecting
the evaluator. Stdlib implementation details should not affect
eval, regardless of the build platform. Even erroneous usage
of `builtins.sort` should not make it possible to crash the
evaluator or produce results that depend on the host platform.
2025-06-15 16:52:03 +00:00
Jörg Thalheim
a1fe09d2c8
Merge pull request #13350 from xokdvium/meson-format
flake: Add meson formatter
2025-06-13 11:37:32 +02:00
Sergei Zimmerman
7b46eb9958
libexpr: Remove non-const overload of listElems
This overload isn't actually necessary anywhere and
doesn't make much sense. The pointers to `Value`s are
themselves const, but the `Value`s are mutable.
A non-const member function implies that the object itself
can be modified but this doesn't make much sense considering
the return type: `Value * const * `, which is a pointer
to a constant array of pointers to mutable values.
2025-06-12 22:29:05 +00:00
Sergei Zimmerman
bc6b52aff0
libexpr: Add and use pathAccessor getter 2025-06-12 20:01:38 +00:00
Sergei Zimmerman
e4df189123
libexpr: Add and use pathStr getter 2025-06-12 19:57:46 +00:00
Sergei Zimmerman
c041d71406
libexpr: Add and use app getter 2025-06-12 19:53:44 +00:00
Sergei Zimmerman
f07a9f863e
libexpr: Add and use primOpApp getter 2025-06-12 19:51:44 +00:00
Sergei Zimmerman
441fa86e82
libexpr: Add and use thunk getter 2025-06-12 19:48:42 +00:00
Sergei Zimmerman
6587e7bcff
libexpr: Add and use lambda getter 2025-06-12 19:42:50 +00:00
Sergei Zimmerman
93a42a5971
flake: Add meson formatter
This adds a meson.format file that mostly mirrors the projects
meson style and a pre-commit hook to enforce this style.
Some low-diff files are formatted.
2025-06-11 22:08:03 +00:00
Sergei Zimmerman
408873c2f7
libexpr: Use c_str getter 2025-06-10 13:37:06 +00:00
Sergei Zimmerman
77f5f50ec2
libexpr: Use context getter 2025-06-10 13:37:04 +00:00
Sergei Zimmerman
c2aaa68c2c
libexpr: Use primOp getter 2025-06-10 13:37:01 +00:00
Robert Hensing
102259898c
Merge pull request #13258 from NaN-git/opt-symbol-table
Optimize symbol table
2025-06-07 13:16:14 +02:00
Eelco Dolstra
97e3c3fff8
Merge pull request #13309 from NaN-git/fix-substring
libexpr: fix various overflows and type mismatches
2025-06-02 14:01:48 +02:00
Philipp Otterbein
afd9c78508 libexpr: fix various overflows and type mismatches 2025-06-02 02:06:48 +02:00
Sergei Zimmerman
9563b509ff
libexpr: Deduplicate Value::primOpAppPrimOp
`getPrimOp` function was basically identical to existing
`Value::primOpAppPrimOp` modulo some trivial differences.
Makes sense to reuse existing code for that.
2025-06-01 21:16:01 +00:00
Philipp Otterbein
ed4e512dcd symbol-table: reference entries instead of allocating Values 2025-06-01 21:00:01 +02:00
Philipp Otterbein
94bbaddb93 symbol-table: reduce memory usage and use boost::unordered_flat_set 2025-06-01 19:20:11 +02:00
John Ericson
20226c85bc
Merge pull request #13273 from NixOS/deprecate-structured-attrs-hack
Deprecate hacky way of making structured attrs
2025-05-28 13:40:23 -04:00
John Ericson
7577d2d3ae Deprecate hacky way of making structured attrs
The method tested for in the previous commit is now deprecated.

Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
2025-05-28 12:59:04 -04:00
Eelco Dolstra
6686b54077 Fix mingw build
https://hydra.nixos.org/build/298331457
2025-05-28 00:48:10 +02:00
gustavderdrache
ce89c8c114 Log warnings on IFD with new option
Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
2025-05-27 09:18:51 -04:00
Sergei Zimmerman
114de63d88
Fix various typos in source code
This only touches code comments, class names, documentation,
enumeration names and tests.
2025-05-25 20:14:11 +00:00
Sergei Zimmerman
9e97ecabb6
libexpr: Include derivation names in the call stack profile
This makes the profiler much more useful by actually distiguishing
different derivations being evaluated. This does make the implementation
a bit more convoluted, but I think it's worth it.
2025-05-25 15:53:01 +00:00
Sergei Zimmerman
a76c76a9d5
libexpr: Make getAttr a member function of EvalState 2025-05-25 15:52:58 +00:00
Sergei Zimmerman
128750225d
libexpr: Pass mutable EvalState to EvalProfiler
Sometimes the profiler might want to do evaluation (e.g. for getting
derivation names). This is not ideal, but is really necessary
to make the profiler stack traces useful for end users.
2025-05-25 15:52:56 +00:00
Sergei Zimmerman
5e74c0e4d6
libexpr: Add SampleStack stack-sampling profiler
This patch adds support for a native stack sampling
profiler to the evaluator, which saves a collapsed stack
profile information to a configurable location.

Introduced options (in `EvalSettings`):

- `eval-profile-file` - path to the collected profile file.
- `eval-profiler-frequency` - sampling frequency.
- `eval-profiler` - enumeration option for enabling the profiler.

  Currently only `flamegraph` is supported, but having this an
  enumeration rather than a boolean switch leaves the door open
  for other profiler variants (e.g. tracy).

Profile includes the following information on best-effort basis (e.g. some lambdas might
have an undefined name). Callstack information contains:

- Call site location (where the function gets called).
- Primop/lambda name of the function being called.
- Functors/partial applications don't have a name attached to them unlike special-cased primops and lambads.

For cases where callsite location isn't available we have to resort to providing
the location where the lambda itself is defined. This removes some of the confusing
`«none»:0` locations in the profile from previous attempts.

Example usage with piping directly into zstd for compression:

```
nix eval --no-eval-cache nixpkgs#nixosTests.gnome \
  --eval-profiler flamegraph \
  --eval-profile-file >(zstd -of nix.profile.zstd)
```

Co-authored-by: Jörg Thalheim <joerg@thalheim.io>
2025-05-21 20:15:19 +00:00
Sergei Zimmerman
a4bfd559f1
libexpr: Use attrs.alreadySorted() in primop_functionArgs
Formals are already sorted as it's an invariant of `Formals`.
2025-05-19 19:53:20 +00:00
Jörg Thalheim
638b7ec6c5
Merge pull request #13219 from xokdvium/eval-profiler
libexpr: Add `EvalProfiler` and use it for `FunctionCallTrace`
2025-05-18 21:35:43 +02:00
Sergei Zimmerman
fa6f69f9c5
libexpr: Use EvalProfiler for FunctionCallTrace
This wires up the {pre,post}FunctionCallHook machinery
in EvalState::callFunction and migrates FunctionCallTrace
to use the new EvalProfiler mechanisms for tracing.

Note that branches when the hook gets called are marked with [[unlikely]]
as a hint to the compiler that this is not a hot path. For non-tracing
evaluation this should be a 100% predictable branch, so the performance
cost is nonexistent.

Some measurements to prove support this point:

```
nix build .#nix-cli
nix build github:nixos/nix/d692729759e4e370361cc5105fbeb0e33137ca9e#nix-cli --out-link before
```

(Before)

```
$ taskset -c 2,3 hyperfine "GC_INITIAL_HEAP_SIZE=16g before/bin/nix eval nixpkgs#gnome --no-eval-cache" --warmup 4
Benchmark 1: GC_INITIAL_HEAP_SIZE=16g before/bin/nix eval nixpkgs#gnome --no-eval-cache
  Time (mean ± σ):      2.517 s ±  0.032 s    [User: 1.464 s, System: 0.476 s]
  Range (min … max):    2.464 s …  2.557 s    10 runs
```

(After)

```
$ taskset -c 2,3 hyperfine "GC_INITIAL_HEAP_SIZE=16g result/bin/nix eval nixpkgs#gnome --no-eval-cache" --warmup 4
Benchmark 1: GC_INITIAL_HEAP_SIZE=16g result/bin/nix eval nixpkgs#gnome --no-eval-cache
  Time (mean ± σ):      2.499 s ±  0.022 s    [User: 1.448 s, System: 0.478 s]
  Range (min … max):    2.472 s …  2.537 s    10 runs
```
2025-05-18 11:55:39 +00:00
Sergei Zimmerman
6b4a86a9e3
libexpr: Add EvalProfiler
This patch adds an EvalProfiler and MultiEvalProfiler that can be used
to insert hooks into the evaluation for the purposes of function tracing
(what function-trace currently does) or for flamegraph/tracy profilers.

See the following commits for how this is supposed to be integrated into
the evaluator and performance considerations.
2025-05-18 11:55:37 +00:00
Eelco Dolstra
efcb9e36a9 Remove global fetcher cache
The cache is now part of fetchers::Settings.
2025-05-17 19:54:32 +02:00
John Ericson
d972f9e2e2 Split out store-open.hh and store-registration.hh
The existing header is a bit too big. Now the following use-cases are
separated, and get their own headers:

- Using or implementing an arbitrary store: remaining `store-api.hh`

  This is closer to just being about the `Store` (and `StoreConfig`)
  classes, as one would expect.

- Opening a store from a textual description: `store-open.hh`

  Opening an aribtrary store implementation like this requires some sort
  of store registration mechanism to exists, but the caller doesn't need
  to know how it works. This just exposes the functions which use such a
  mechanism, without exposing the mechanism itself

- Registering a store implementation: `store-registration.hh`

  This requires understanding how the mechanism actually works, and the
  mechanism in question involves templated machinery in headers we
  rather not expose to things that don't need it, as it would slow down
  compilation for no reason.
2025-05-14 16:07:57 -04:00
Eelco Dolstra
4de7a986d4 Simplify RegisterPrimOp 2025-05-05 08:26:29 +02:00
Sergei Zimmerman
36c583dae0
libexpr: Use C++20 heterogeneous lookup for RegexCache 2025-05-04 16:03:57 +00:00
Jörg Thalheim
7808aa2eee
Merge pull request #13129 from xokdvium/transparent-comparator
Use transparent comparators for `std::set<std::string>` (NFC)
2025-05-03 09:04:52 +02:00
Sergei Zimmerman
161c5dbf39
libexpr: Remove unused field from SymbolTable::symbols and emplace into the ChunkedVector
Remove outdated and no longer relevant TODO. It's more confusing
now, since symbol table must now be addressed by uint32_t indices
in order to keep Attr size down to 16 bytes on 64 bit machines.
2025-05-02 20:42:47 +00:00
Sergei Zimmerman
d8c97d8073
treewide: Use StringSet alias consistently instead of std::set<std::string>
The intention is to switch to transparent comparators from N3657 for
ordered set containers for strings and using the alias consistently
would simplify things.
2025-05-02 17:40:29 +00:00
Sergei Zimmerman
86a3fad085
libexpr: Improve lexer performance by using full scanner tables (-Cf)
This trades off some executable size for measurable lexer performance
improvements.

Note on the explicitly enabling 8bit scanner.
This is needed due to the default behavior of flex (excerpt from the manual [1]):

> Flex’s default behavior is to generate an 8-bit scanner unless you
> use the ‘-Cf’ or ‘-CF’, in which case flex defaults to generating
> 7-bit scanners unless your site was always configured to generate 8-bit
> scanners.

Some quantifyable metrics:

Nixpkgs revision: a6e3f45acf4e817532a861ab0eda4ab5485fecc1
Parsing the largest file in nixpkgs: pkgs/development/haskell-modules/hackage-packages.nix.

(Before this patch)

```
$ nix build github:nixos/nix/9fe3077d4#nix-expr
$ du --apparent-size result/lib/libnixexpr.so
2518    result/lib/libnixexpr.so
$ nix build github:nixos/nix/9fe3077d4#nix-cli
$ taskset -c 2,3 hyperfine "GC_INITIAL_HEAP_SIZE=16g \
    result/bin/nix-instantiate --parse               \
    ../nixpkgs/pkgs/development/haskell-modules/hackage-packages.nix > /dev/null"
  Time (mean ± σ):     375.5 ms ±   6.3 ms    [User: 316.9 ms, System: 56.7 ms]
  Range (min … max):   368.5 ms … 388.3 ms    10 runs
```

(After the patch)

```
$ nix build .#nix-expr
$ du --apparent-size result/lib/libnixexpr.so
2685    result/lib/libnixexpr.so
$ nix build .#nix-cli
$ taskset -c 2,3 hyperfine "GC_INITIAL_HEAP_SIZE=16g \
    result/bin/nix-instantiate --parse               \
    ../nixpkgs/pkgs/development/haskell-modules/hackage-packages.nix > /dev/null"
  Time (mean ± σ):     326.8 ms ±   4.9 ms    [User: 269.5 ms, System: 55.3 ms]
  Range (min … max):   319.7 ms … 335.5 ms    10 runs
```

Overall, the change is roughly:

- 2518KiB -> 2685KiB ~ 150 KiB of machine code
- 375ms -> 325ms ~ 50ms

The perf uplift for eval-heavy test cases is obviously less noticeable,
but it doesn't make sense not to take this free perf win.

[1]: https://westes.github.io/flex/manual/Options-Affecting-Scanner-Behavior.html#Options-Affecting-Scanner-Behavior
2025-05-01 23:10:04 +00:00
Eelco Dolstra
68de26d38a lockFlake(): Allow registry lookups for the top-level flake
Fixes #13050.
2025-04-24 18:59:10 +02:00
Jörg Thalheim
009ff8e5a7
Merge pull request #13013 from NaN-git/fix-ceil-floor
libexpr: fix UB in builtins.ceil and builtins.floor
2025-04-15 18:56:46 +02:00
Eelco Dolstra
4966217b6a Move the InputCache to EvalState 2025-04-14 14:29:14 +02:00
Philipp Otterbein
56d37656ac libexpr: fix UB in builtins.ceil and builtins.floor
tighten and fix specification of both builtins
2025-04-13 04:36:09 +02:00
Eelco Dolstra
9d3595646d nix shell: Resolve symlinks in storeFS
`storeFS` is the `MountedSourceAccessor` that wraps `store->getFSAccessor()`.
2025-04-09 17:34:19 -04:00
John Ericson
eb643d034f Store::getFSAccessor: Do not include the store dir
Rather than "mounting" the store inside an empty virtual filesystem,
just return the store as a virtual filesystem. This is more modular.

(FWIW, it also supports two long term hopes of mind:

1. More capability-based Nix language mode. I dream of a "super pure
   eval" where you can only use relative path literals (See #8738), and
   any `fetchTree`-fetched stuff + the store are all disjoint (none is
   mounted in another) file systems.

2. Windows, where the store dir may include drive letters, etc., and is
   thus unsuitable to be the prefix of any `CanonPath`s.

)

Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
2025-04-09 17:34:18 -04:00
Robert Hensing
e76bbe413e
Merge pull request #12877 from roberth/c-api-libflake-override-input
C API: Flake loading, input overriding
2025-04-08 09:01:51 +02:00
John Ericson
6789202af0
Merge pull request #12962 from DeterminateSystems/clang-tidy-warnings
Fix some clang-tidy warnings
2025-04-07 16:37:02 -04:00
Eelco Dolstra
c0ad5d36c4 Fix some clang-tidy warnings 2025-04-07 19:55:33 +02:00