Skip to content

Commit

Permalink
Using more arenas in ParamHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinchowdhury18 committed Nov 22, 2024
1 parent 6e17786 commit a60ccf5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ struct STLArenaAllocator
{
using value_type = T;

ArenaType& arena;
ArenaType* arena {};

explicit STLArenaAllocator (ArenaType& a) noexcept : arena (a)
explicit STLArenaAllocator (ArenaType& a) noexcept : arena { &a }
{
}

template <class U>
explicit STLArenaAllocator (const STLArenaAllocator<U, ArenaType>& other) noexcept
: arena (other.arena)
: arena { other.arena }
{
}

Expand All @@ -28,7 +28,7 @@ struct STLArenaAllocator

T* allocate (std::size_t n)
{
return static_cast<T*> (arena.allocate_bytes (n * sizeof (T), alignof (T)));
return static_cast<T*> (arena->allocate_bytes (n * sizeof (T), alignof (T)));
}

void deallocate (T*, std::size_t) const
Expand All @@ -41,7 +41,7 @@ struct STLArenaAllocator
template <class T, class U, typename Arena>
constexpr bool operator== (const STLArenaAllocator<T, Arena>& x, const STLArenaAllocator<U, Arena>& y) noexcept
{
return &x.arena == &y.arena;
return x.arena == y.arena;
}

template <class T, class U, typename Arena>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ struct ChunkList

// The list internals are exposed here in case you need to do anything fancy.
// Please don't touch them unless you know what you are doing!
ArenaType& arena;
ArenaType* arena {};
Chunk head_chunk {};
Chunk* tail_chunk = &head_chunk;

/** Constructs a ChunkList with a backing arena. */
explicit ChunkList (ArenaType& arena_to_use) : arena { arena_to_use }
explicit ChunkList (ArenaType& arena_to_use) : arena { &arena_to_use }
{
}

ChunkList (ChunkList&&) noexcept = default;
ChunkList& operator= (ChunkList&&) noexcept = default;

/**
* This will "reset" the ChunkList. If you actually want to reclaim the memory
* being used by the ChunkList, that should be done at the arena level.
Expand Down Expand Up @@ -129,7 +132,7 @@ struct ChunkList
if (tail_chunk->count == chunk_size)
#endif
{
auto* next_chunk_data = arena.allocate_bytes (sizeof (Chunk), alignof (Chunk));
auto* next_chunk_data = arena->allocate_bytes (sizeof (Chunk), alignof (Chunk));
jassert (next_chunk_data != nullptr);
tail_chunk->next = new (next_chunk_data) Chunk {};
tail_chunk = tail_chunk->next;
Expand Down
50 changes: 32 additions & 18 deletions modules/plugin/chowdsp_plugin_state/Backend/chowdsp_ParamHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,35 @@ template <typename ParamType, typename... OtherParams>
std::enable_if_t<std::is_base_of_v<FloatParameter, ParamType>, void>
ParamHolder::add (OptionalPointer<ParamType>& floatParam, OtherParams&... others)
{
allParamsMap.insert ({ floatParam->paramID.toStdString(), floatParam.get() });
things.insert (ThingPtr { reinterpret_cast<PackedVoid*> (isOwning ? floatParam.release() : floatParam.get()),
getFlags (FloatParam, isOwning) });
const auto paramID = toStringView (floatParam->paramID);
ThingPtr paramPtr { reinterpret_cast<PackedVoid*> (isOwning ? floatParam.release() : floatParam.get()),
getFlags (FloatParam, isOwning) };
allParamsMap.insert ({ paramID, paramPtr });
things.insert (std::move (paramPtr));
add (others...);
}

template <typename ParamType, typename... OtherParams>
std::enable_if_t<std::is_base_of_v<ChoiceParameter, ParamType>, void>
ParamHolder::add (OptionalPointer<ParamType>& choiceParam, OtherParams&... others)
{
allParamsMap.insert ({ choiceParam->paramID.toStdString(), choiceParam.get() });
things.insert (ThingPtr { reinterpret_cast<PackedVoid*> (isOwning ? choiceParam.release() : choiceParam.get()),
getFlags (ChoiceParam, isOwning) });
const auto paramID = toStringView (choiceParam->paramID);
ThingPtr paramPtr { reinterpret_cast<PackedVoid*> (isOwning ? choiceParam.release() : choiceParam.get()),
getFlags (ChoiceParam, isOwning) };
allParamsMap.insert ({ paramID, paramPtr });
things.insert (std::move (paramPtr));
add (others...);
}

template <typename ParamType, typename... OtherParams>
std::enable_if_t<std::is_base_of_v<BoolParameter, ParamType>, void>
ParamHolder::add (OptionalPointer<ParamType>& boolParam, OtherParams&... others)
{
allParamsMap.insert ({ boolParam->paramID.toStdString(), boolParam.get() });
things.insert (ThingPtr { reinterpret_cast<PackedVoid*> (isOwning ? boolParam.release() : boolParam.get()),
getFlags (BoolParam, isOwning) });
const auto paramID = toStringView (boolParam->paramID);
ThingPtr paramPtr { reinterpret_cast<PackedVoid*> (isOwning ? boolParam.release() : boolParam.get()),
getFlags (BoolParam, isOwning) };
allParamsMap.insert ({ paramID, paramPtr });
things.insert (std::move (paramPtr));
add (others...);
}

Expand Down Expand Up @@ -277,21 +283,29 @@ void ParamHolder::deserialize (typename Serializer::DeserializedType deserial, P
continue;

paramIDsThatHaveBeenDeserialized.push_back (paramID);
[&paramDeserial] (const ParamPtrVariant& paramPtr)
[&paramDeserial] (ThingPtr& paramPtr)
{
const auto deserializeParam = [] (auto* param, auto& pd)
{
ParameterTypeHelpers::deserializeParameter<Serializer> (pd, *param);
};

if (auto* floatParamPtr = std::get_if<FloatParameter*> (&paramPtr))
deserializeParam (*floatParamPtr, paramDeserial);
else if (auto* choiceParamPtr = std::get_if<ChoiceParameter*> (&paramPtr))
deserializeParam (*choiceParamPtr, paramDeserial);
else if (auto* boolParamPtr = std::get_if<BoolParameter*> (&paramPtr))
deserializeParam (*boolParamPtr, paramDeserial);
else
jassertfalse; // bad variant access?
const auto type = getType (paramPtr);
switch (type)
{
case FloatParam:
deserializeParam (reinterpret_cast<FloatParameter*> (paramPtr.get_ptr()), paramDeserial);
break;
case ChoiceParam:
deserializeParam (reinterpret_cast<ChoiceParameter*> (paramPtr.get_ptr()), paramDeserial);
break;
case BoolParam:
deserializeParam (reinterpret_cast<BoolParameter*> (paramPtr.get_ptr()), paramDeserial);
break;
default:
jassertfalse;
break;
}
}(paramPtrIter->second);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,11 @@ class ParamHolder
return static_cast<uint8_t> (type | (shouldDelete ? ShouldDelete : 0));
}

using ParamPtrVariant = std::variant<FloatParameter*, ChoiceParameter*, BoolParameter*>;
std::unordered_map<std::string, ParamPtrVariant> allParamsMap {};
using MapKey = std::string_view;
using MapValue = ThingPtr;
using MapAllocator = STLArenaAllocator<std::pair<const MapKey, MapValue>, ChainedArenaAllocator>;
MapAllocator mapAllocator { arena };
std::unordered_map<MapKey, MapValue, std::hash<MapKey>, std::equal_to<MapKey>, MapAllocator> allParamsMap { mapAllocator };

juce::String name;
bool isOwning;
Expand Down

0 comments on commit a60ccf5

Please sign in to comment.