From 2e5780ebc848f3b021f11dd94533b0b68362d989 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Wed, 27 Nov 2024 02:09:08 +0100 Subject: [PATCH] libexpr: simplify HAVE_BOEHMGC ifdefs if we define a TraceableAllocator at all times and use that in places that want maybe-traceable allocation we can simplify things a lot. we also unconditionally allocate cache root pointers for Value and Env-1 caches, even though we don't need them without gc (they're so cheap). defaulting to `std::allocator` without gc recovers previous behavior. Change-Id: I236da8c3b0669b40cdfe355ec3ec4e764d096074 --- lix/libcmd/command.cc | 12 ++----- lix/libcmd/repl.cc | 6 +--- lix/libexpr/eval.cc | 12 ++----- lix/libexpr/eval.hh | 2 -- lix/libexpr/gc-alloc.hh | 59 ++++++++++++++-------------------- lix/libexpr/gc-small-vector.hh | 17 ++-------- 6 files changed, 33 insertions(+), 75 deletions(-) diff --git a/lix/libcmd/command.cc b/lix/libcmd/command.cc index 914042248..362782fe1 100644 --- a/lix/libcmd/command.cc +++ b/lix/libcmd/command.cc @@ -105,15 +105,9 @@ ref EvalCommand::getEvalStore() ref EvalCommand::getEvalState() { if (!evalState) { - evalState = - #if HAVE_BOEHMGC - std::allocate_shared(traceable_allocator(), - searchPath, getEvalStore(), getStore()) - #else - std::make_shared( - searchPath, getEvalStore(), getStore()) - #endif - ; + evalState = std::allocate_shared( + TraceableAllocator(), searchPath, getEvalStore(), getStore() + ); evalState->repair = repair; diff --git a/lix/libcmd/repl.cc b/lix/libcmd/repl.cc index 92a1bf226..1152cc1a8 100644 --- a/lix/libcmd/repl.cc +++ b/lix/libcmd/repl.cc @@ -181,11 +181,7 @@ struct NixRepl * Note: This is `shared_ptr` to avoid garbage collection. */ std::shared_ptr replOverlaysEvalFunction = - #if HAVE_BOEHMGC - std::allocate_shared(traceable_allocator(), nullptr); - #else - std::make_shared(nullptr); - #endif + std::allocate_shared(TraceableAllocator(), nullptr); /** * Get the `info` AttrSet that's passed as the first argument to each diff --git a/lix/libexpr/eval.cc b/lix/libexpr/eval.cc index 1dd820244..2acbdf97d 100644 --- a/lix/libexpr/eval.cc +++ b/lix/libexpr/eval.cc @@ -51,11 +51,7 @@ namespace nix { RootValue allocRootValue(Value * v) { -#if HAVE_BOEHMGC - return std::allocate_shared(traceable_allocator(), v); -#else - return std::make_shared(v); -#endif + return std::allocate_shared(TraceableAllocator(), v); } // Pretty print types for assertion errors @@ -254,10 +250,8 @@ StaticSymbols::StaticSymbols(SymbolTable & symbols) } EvalMemory::EvalMemory() -#if HAVE_BOEHMGC - : valueAllocCache(std::allocate_shared(traceable_allocator(), nullptr)) - , env1AllocCache(std::allocate_shared(traceable_allocator(), nullptr)) -#endif + : valueAllocCache(std::allocate_shared(TraceableAllocator(), nullptr)) + , env1AllocCache(std::allocate_shared(TraceableAllocator(), nullptr)) { assert(libexprInitialised); } diff --git a/lix/libexpr/eval.hh b/lix/libexpr/eval.hh index def78977d..ffc4015d7 100644 --- a/lix/libexpr/eval.hh +++ b/lix/libexpr/eval.hh @@ -216,7 +216,6 @@ struct StaticSymbols class EvalMemory { -#if HAVE_BOEHMGC /** * Allocation cache for GC'd Value objects. */ @@ -226,7 +225,6 @@ class EvalMemory * Allocation cache for size-1 Env objects. */ std::shared_ptr env1AllocCache; -#endif public: struct Statistics diff --git a/lix/libexpr/gc-alloc.hh b/lix/libexpr/gc-alloc.hh index f8daa6139..9816e2869 100644 --- a/lix/libexpr/gc-alloc.hh +++ b/lix/libexpr/gc-alloc.hh @@ -13,8 +13,6 @@ #include "lix/libutil/checked-arithmetic.hh" #if HAVE_BOEHMGC -#include // std::less -#include // std::pair #define GC_INCLUDE_NEW #include #include @@ -32,25 +30,8 @@ namespace nix { -/// Alias for std::map which uses BoehmGC's allocator conditional on this Lix -/// build having GC enabled. -template -using GcMap = std::map< - KeyT, - ValueT, - std::less, - traceable_allocator> ->; - -/// Alias for std::vector which uses BoehmGC's allocator conditional on this Lix -/// build having GC enabled. -template -using GcVector = std::vector>; - -/// Alias for std::list which uses BoehmGC's allocator conditional on this Lix -/// build having GC enabled. -template -using GcList = std::list>; +template +using TraceableAllocator = traceable_allocator; } @@ -71,20 +52,8 @@ using GcList = std::list>; namespace nix { -/// Alias for std::map which uses BoehmGC's allocator conditional on this Lix -/// build having GC enabled. -template -using GcMap = std::map; - -/// Alias for std::vector which uses BoehmGC's allocator conditional on this Lix -/// build having GC enabled. -template -using GcVector = std::vector; - -/// Alias for std::list which uses BoehmGC's allocator conditional on this Lix -/// build having GC enabled. -template -using GcList = std::list; +template +using TraceableAllocator = std::allocator; } @@ -93,6 +62,26 @@ using GcList = std::list; namespace nix { +/// Alias for std::map which uses BoehmGC's allocator conditional on this Lix +/// build having GC enabled. +template +using GcMap = std::map< + KeyT, + ValueT, + std::less, + TraceableAllocator> +>; + +/// Alias for std::vector which uses BoehmGC's allocator conditional on this Lix +/// build having GC enabled. +template +using GcVector = std::vector>; + +/// Alias for std::list which uses BoehmGC's allocator conditional on this Lix +/// build having GC enabled. +template +using GcList = std::list>; + [[gnu::always_inline]] inline void * gcAllocBytes(size_t n) { diff --git a/lix/libexpr/gc-small-vector.hh b/lix/libexpr/gc-small-vector.hh index c4bd1db26..bbb1aefc3 100644 --- a/lix/libexpr/gc-small-vector.hh +++ b/lix/libexpr/gc-small-vector.hh @@ -1,17 +1,9 @@ #pragma once ///@file +#include "gc-alloc.hh" #include -#if HAVE_BOEHMGC - -#define GC_INCLUDE_NEW -#include -#include -#include - -#endif - namespace nix { struct Value; @@ -19,13 +11,8 @@ struct Value; /** * A GC compatible vector that may used a reserved portion of `nItems` on the stack instead of allocating on the heap. */ -#if HAVE_BOEHMGC template -using SmallVector = boost::container::small_vector>; -#else -template -using SmallVector = boost::container::small_vector; -#endif +using SmallVector = boost::container::small_vector>; /** * A vector of value pointers. See `SmallVector`.