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()
{
if (!evalState) {
evalState =
#if HAVE_BOEHMGC
std::allocate_shared<eval_cache::CachingEvalState>(traceable_allocator<EvalState>(),
searchPath, getEvalStore(), getStore())
#else
std::make_shared<eval_cache::CachingEvalState>(
searchPath, getEvalStore(), getStore())
#endif
;
evalState = std::allocate_shared<eval_cache::CachingEvalState>(
TraceableAllocator<EvalState>(), searchPath, getEvalStore(), getStore()
);
evalState->repair = repair;

View file

@ -181,11 +181,7 @@ struct NixRepl
* Note: This is `shared_ptr` to avoid garbage collection.
*/
std::shared_ptr<Value *> replOverlaysEvalFunction =
#if HAVE_BOEHMGC
std::allocate_shared<Value *>(traceable_allocator<Value *>(), nullptr);
#else
std::make_shared<Value *>(nullptr);
#endif
std::allocate_shared<Value *>(TraceableAllocator<Value *>(), nullptr);
/**
* 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)
{
#if HAVE_BOEHMGC
return std::allocate_shared<Value *>(traceable_allocator<Value *>(), v);
#else
return std::make_shared<Value *>(v);
#endif
return std::allocate_shared<Value *>(TraceableAllocator<Value *>(), v);
}
// Pretty print types for assertion errors
@ -254,10 +250,8 @@ StaticSymbols::StaticSymbols(SymbolTable & symbols)
}
EvalMemory::EvalMemory()
#if HAVE_BOEHMGC
: valueAllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr))
, env1AllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr))
#endif
: valueAllocCache(std::allocate_shared<void *>(TraceableAllocator<void *>(), nullptr))
, env1AllocCache(std::allocate_shared<void *>(TraceableAllocator<void *>(), nullptr))
{
assert(libexprInitialised);
}

View file

@ -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<void *> env1AllocCache;
#endif
public:
struct Statistics

View file

@ -13,8 +13,6 @@
#include "lix/libutil/checked-arithmetic.hh"
#if HAVE_BOEHMGC
#include <functional> // std::less
#include <utility> // std::pair
#define GC_INCLUDE_NEW
#include <gc/gc.h>
#include <gc/gc_allocator.h>
@ -32,25 +30,8 @@
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>,
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>>;
template<typename T>
using TraceableAllocator = traceable_allocator<T>;
}
@ -71,20 +52,8 @@ using GcList = std::list<ItemT, traceable_allocator<ItemT>>;
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>;
/// 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>;
template<typename T>
using TraceableAllocator = std::allocator<T>;
}
@ -93,6 +62,26 @@ using GcList = std::list<ItemT>;
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]]
inline void * gcAllocBytes(size_t n)
{

View file

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