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
This commit is contained in:
eldritch horrors 2024-11-27 02:09:08 +01:00
parent 1e064e08fa
commit 2e5780ebc8
6 changed files with 33 additions and 75 deletions

View file

@ -105,15 +105,9 @@ ref<Store> EvalCommand::getEvalStore()
ref<eval_cache::CachingEvalState> EvalCommand::getEvalState() ref<eval_cache::CachingEvalState> EvalCommand::getEvalState()
{ {
if (!evalState) { if (!evalState) {
evalState = evalState = std::allocate_shared<eval_cache::CachingEvalState>(
#if HAVE_BOEHMGC TraceableAllocator<EvalState>(), searchPath, getEvalStore(), getStore()
std::allocate_shared<eval_cache::CachingEvalState>(traceable_allocator<EvalState>(), );
searchPath, getEvalStore(), getStore())
#else
std::make_shared<eval_cache::CachingEvalState>(
searchPath, getEvalStore(), getStore())
#endif
;
evalState->repair = repair; evalState->repair = repair;

View file

@ -181,11 +181,7 @@ struct NixRepl
* Note: This is `shared_ptr` to avoid garbage collection. * Note: This is `shared_ptr` to avoid garbage collection.
*/ */
std::shared_ptr<Value *> replOverlaysEvalFunction = std::shared_ptr<Value *> replOverlaysEvalFunction =
#if HAVE_BOEHMGC std::allocate_shared<Value *>(TraceableAllocator<Value *>(), nullptr);
std::allocate_shared<Value *>(traceable_allocator<Value *>(), nullptr);
#else
std::make_shared<Value *>(nullptr);
#endif
/** /**
* Get the `info` AttrSet that's passed as the first argument to each * Get the `info` AttrSet that's passed as the first argument to each

View file

@ -51,11 +51,7 @@ namespace nix {
RootValue allocRootValue(Value * v) RootValue allocRootValue(Value * v)
{ {
#if HAVE_BOEHMGC return std::allocate_shared<Value *>(TraceableAllocator<Value *>(), v);
return std::allocate_shared<Value *>(traceable_allocator<Value *>(), v);
#else
return std::make_shared<Value *>(v);
#endif
} }
// Pretty print types for assertion errors // Pretty print types for assertion errors
@ -254,10 +250,8 @@ StaticSymbols::StaticSymbols(SymbolTable & symbols)
} }
EvalMemory::EvalMemory() EvalMemory::EvalMemory()
#if HAVE_BOEHMGC : valueAllocCache(std::allocate_shared<void *>(TraceableAllocator<void *>(), nullptr))
: valueAllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr)) , env1AllocCache(std::allocate_shared<void *>(TraceableAllocator<void *>(), nullptr))
, env1AllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr))
#endif
{ {
assert(libexprInitialised); assert(libexprInitialised);
} }

View file

@ -216,7 +216,6 @@ struct StaticSymbols
class EvalMemory class EvalMemory
{ {
#if HAVE_BOEHMGC
/** /**
* Allocation cache for GC'd Value objects. * Allocation cache for GC'd Value objects.
*/ */
@ -226,7 +225,6 @@ class EvalMemory
* Allocation cache for size-1 Env objects. * Allocation cache for size-1 Env objects.
*/ */
std::shared_ptr<void *> env1AllocCache; std::shared_ptr<void *> env1AllocCache;
#endif
public: public:
struct Statistics struct Statistics

View file

@ -13,8 +13,6 @@
#include "lix/libutil/checked-arithmetic.hh" #include "lix/libutil/checked-arithmetic.hh"
#if HAVE_BOEHMGC #if HAVE_BOEHMGC
#include <functional> // std::less
#include <utility> // std::pair
#define GC_INCLUDE_NEW #define GC_INCLUDE_NEW
#include <gc/gc.h> #include <gc/gc.h>
#include <gc/gc_allocator.h> #include <gc/gc_allocator.h>
@ -32,25 +30,8 @@
namespace nix namespace nix
{ {
/// Alias for std::map which uses BoehmGC's allocator conditional on this Lix template<typename T>
/// build having GC enabled. using TraceableAllocator = traceable_allocator<T>;
template<typename KeyT, typename ValueT>
using GcMap = std::map<
KeyT,
ValueT,
std::less<KeyT>,
traceable_allocator<std::pair<KeyT const, ValueT>>
>;
/// Alias for std::vector which uses BoehmGC's allocator conditional on this Lix
/// build having GC enabled.
template<typename ItemT>
using GcVector = std::vector<ItemT, traceable_allocator<ItemT>>;
/// Alias for std::list which uses BoehmGC's allocator conditional on this Lix
/// build having GC enabled.
template<typename ItemT>
using GcList = std::list<ItemT, traceable_allocator<ItemT>>;
} }
@ -71,20 +52,8 @@ using GcList = std::list<ItemT, traceable_allocator<ItemT>>;
namespace nix namespace nix
{ {
/// Alias for std::map which uses BoehmGC's allocator conditional on this Lix template<typename T>
/// build having GC enabled. using TraceableAllocator = std::allocator<T>;
template<typename KeyT, typename ValueT>
using GcMap = std::map<KeyT, ValueT>;
/// Alias for std::vector which uses BoehmGC's allocator conditional on this Lix
/// build having GC enabled.
template<typename ItemT>
using GcVector = std::vector<ItemT>;
/// Alias for std::list which uses BoehmGC's allocator conditional on this Lix
/// build having GC enabled.
template<typename ItemT>
using GcList = std::list<ItemT>;
} }
@ -93,6 +62,26 @@ using GcList = std::list<ItemT>;
namespace nix namespace nix
{ {
/// Alias for std::map which uses BoehmGC's allocator conditional on this Lix
/// build having GC enabled.
template<typename KeyT, typename ValueT>
using GcMap = std::map<
KeyT,
ValueT,
std::less<KeyT>,
TraceableAllocator<std::pair<KeyT const, ValueT>>
>;
/// Alias for std::vector which uses BoehmGC's allocator conditional on this Lix
/// build having GC enabled.
template<typename ItemT>
using GcVector = std::vector<ItemT, TraceableAllocator<ItemT>>;
/// Alias for std::list which uses BoehmGC's allocator conditional on this Lix
/// build having GC enabled.
template<typename ItemT>
using GcList = std::list<ItemT, TraceableAllocator<ItemT>>;
[[gnu::always_inline]] [[gnu::always_inline]]
inline void * gcAllocBytes(size_t n) inline void * gcAllocBytes(size_t n)
{ {

View file

@ -1,17 +1,9 @@
#pragma once #pragma once
///@file ///@file
#include "gc-alloc.hh"
#include <boost/container/small_vector.hpp> #include <boost/container/small_vector.hpp>
#if HAVE_BOEHMGC
#define GC_INCLUDE_NEW
#include <gc/gc.h>
#include <gc/gc_cpp.h>
#include <gc/gc_allocator.h>
#endif
namespace nix { namespace nix {
struct Value; 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. * 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 <typename T, size_t nItems> template <typename T, size_t nItems>
using SmallVector = boost::container::small_vector<T, nItems, traceable_allocator<T>>; using SmallVector = boost::container::small_vector<T, nItems, TraceableAllocator<T>>;
#else
template <typename T, size_t nItems>
using SmallVector = boost::container::small_vector<T, nItems>;
#endif
/** /**
* A vector of value pointers. See `SmallVector`. * A vector of value pointers. See `SmallVector`.