Skip to content

Commit

Permalink
Unify the object pools, specialization for pool types
Browse files Browse the repository at this point in the history
  • Loading branch information
ZehMatt committed Sep 5, 2024
1 parent b3504c9 commit 73b9e9c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
9 changes: 6 additions & 3 deletions benchmark/src/benchmarks/benchmark.assembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,13 @@ namespace zasm::benchmarks
assembler.setCursor(nullptr);
state.ResumeTiming();

for (const auto& instr : zasm::tests::data::Instructions)
for (int i = 0; i < 100; i++)
{
instr.emitter(assembler);
numInstructions++;
for (const auto& instr : zasm::tests::data::Instructions)
{
instr.emitter(assembler);
numInstructions++;
}
}
}

Expand Down
20 changes: 11 additions & 9 deletions zasm/src/zasm/src/program/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,24 +376,24 @@ namespace zasm
auto* nodeToDestroy = detail::toInternal(node);

node->visit([&](auto& ptr) {

using T = std::decay_t<decltype(ptr)>;

auto& objectPool = state.nodePools.getPool<T>();
auto& objectPool = state.objectPools.get<T>();
objectPool.destroy(&ptr);

if (!quickDestroy)
{
objectPool.deallocate(&ptr, 1);
}
});

state.nodePool.destroy(nodeToDestroy);

auto& nodePool = state.objectPools.get<Node>();
nodePool.destroy(nodeToDestroy);

if (!quickDestroy)
{
// Release memory, when quickDestroy is true the entire pool will be cleared at once.
state.nodePool.deallocate(nodeToDestroy, 1);
nodePool.deallocate(nodeToDestroy, 1);

// Remove mapping.
auto& nodeMap = state.nodeMap;
Expand Down Expand Up @@ -438,7 +438,7 @@ namespace zasm
_state->sections.clear();
_state->labels.clear();
_state->symbolNames.clear();
_state->nodePool.reset();
_state->objectPools.reset();
}

void Program::setEntryPoint(const Label& label)
Expand All @@ -456,19 +456,21 @@ namespace zasm
const auto nextId = state.nextNodeId;
state.nextNodeId = static_cast<Node::Id>(static_cast<std::underlying_type_t<Node::Id>>(nextId) + 1U);

auto& pool = state.nodePool;
auto* node = detail::toInternal(pool.allocate(1));
auto& nodePool = state.objectPools.get<Node>();
auto* node = detail::toInternal(nodePool.allocate(1));
if (node == nullptr)
{
return nullptr;
}

// Construct object.
using ObjectType = std::decay_t<T>;
auto& objectPool = state.nodePools.getPool<ObjectType>();
auto& objectPool = state.objectPools.get<ObjectType>();

auto* obj = objectPool.allocate(1);
::new ((void*)obj) ObjectType(std::move(object));

// Construct node.
::new ((void*)node) detail::Node(nextId, obj);

notifyObservers<true>(&Observer::onNodeCreated, state.observer, node);
Expand Down
44 changes: 28 additions & 16 deletions zasm/src/zasm/src/program/program.state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ namespace zasm

namespace zasm::detail
{
constexpr std::size_t PoolSize = 1U << 10;

struct LabelData
{
Label::Id id{ Label::Id::Invalid };
Expand All @@ -47,25 +45,36 @@ namespace zasm::detail
zasm::Node* node{};
};

template<typename... TTypes> struct ObjectPools
namespace detail
{
std::tuple<ObjectPool<TTypes, PoolSize>...> pools;
template<typename T>
struct PoolSize
{
static constexpr std::size_t kSize = 50'000;
};

template<typename T> auto& getPool()
template<> struct PoolSize<Instruction>
{
return std::get<ObjectPool<T, PoolSize>>(pools);
}
};
static constexpr std::size_t kSize = 30'000;
};

using NodePools = ObjectPools<Sentinel, Instruction, Label, EmbeddedLabel, Data, Section, Align>;
template<typename... TTypes> struct ObjectPools
{
std::tuple<ObjectPool<TTypes, PoolSize<TTypes>::kSize>...> pools;

struct NodeStorage
{
ObjectPool<Node, PoolSize> nodePool;
Node::Id nextNodeId{};
template<typename T> auto& get()
{
return std::get<ObjectPool<T, PoolSize<T>::kSize>>(pools);
}

NodePools nodePools;
};
void reset()
{
std::apply([](auto&... pool) { (pool.reset(), ...); }, pools);
}
};
} // namespace detail

using ObjectPools = detail::ObjectPools<zasm::Node, Sentinel, Instruction, Label, EmbeddedLabel, Data, Section, Align>;

struct NodeList
{
Expand All @@ -79,7 +88,7 @@ namespace zasm::detail
StringPool symbolNames;
};

struct ProgramState : NodeStorage, NodeList, Symbols
struct ProgramState : NodeList, Symbols
{
MachineMode mode{};

Expand All @@ -94,6 +103,9 @@ namespace zasm::detail

Label entryPoint{ Label::Id::Invalid };

ObjectPools objectPools;
Node::Id nextNodeId{};

ProgramState(MachineMode m)
: mode(m)
{
Expand Down

0 comments on commit 73b9e9c

Please sign in to comment.