Skip to content

Commit cc7ca79

Browse files
committed
libexpr: Reduce the size of Value down to 16 bytes
This shaves off a very significand amount of memory used for evaluation as well as reduces the GC-managed heap. Previously the union discriminator (InternalType) was stored as a separate field in the Value, which takes up whole 8 bytes due to padding needed for member alignment. This effectively wasted 7 whole bytes of memory. Instead of doing that InternalType is instead packed into pointer alignment niches. As it turns out, there's more than enough unused bits there for the bit packing to be effective. See the doxygen comment in the ValueStorage specialization for more details. This does not add any performance overhead, even though we now consistently assert the InternalType in all getters. This can also be made atomic with a double width compare-and-swap instruction on x86_64 (CMPXCHG16B instruction) for parallel evaluation.
1 parent 593e1b8 commit cc7ca79

File tree

2 files changed

+382
-29
lines changed

2 files changed

+382
-29
lines changed

src/libexpr/eval-gc.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "nix/util/config-global.hh"
55
#include "nix/util/serialise.hh"
66
#include "nix/expr/eval-gc.hh"
7+
#include "nix/expr/value.hh"
78

89
#include "expr-config-private.hh"
910

@@ -52,6 +53,13 @@ static inline void initGCReal()
5253

5354
GC_INIT();
5455

56+
/* Register valid displacements in case we are using alignment niches
57+
for storing the type information. This way tagged pointers are considered
58+
to be valid, even when they are not aligned. */
59+
if constexpr (detail::useBitPackedValueStorage<sizeof(void *)>)
60+
for (std::size_t i = 1; i < sizeof(std::uintptr_t); ++i)
61+
GC_register_displacement(i);
62+
5563
GC_set_oom_fn(oomHandler);
5664

5765
/* Set the initial heap size to something fairly big (25% of

0 commit comments

Comments
 (0)