Skip to content

Commit

Permalink
Improve allocator stack
Browse files Browse the repository at this point in the history
  • Loading branch information
maximegmd committed Jun 13, 2021
1 parent 2c23324 commit e1749ce
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 71 deletions.
6 changes: 2 additions & 4 deletions Code/core/include/Allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ namespace TiltedPhoques
Free(pSize);
}

static void Push(Allocator* apAllocator) noexcept;
static void Push(Allocator& aAllocator) noexcept;
static Allocator* Pop() noexcept;
static void Set(Allocator* apAllocator = nullptr) noexcept;
[[nodiscard]] static Allocator* Get() noexcept;
[[nodiscard]] static Allocator* GetDefault() noexcept;
};
Expand All @@ -152,7 +150,7 @@ namespace TiltedPhoques
TP_NOCOPYMOVE(ScopedAllocator);

private:
Allocator* m_pAllocator;
Allocator* m_pOldAllocator;
};

template<class T>
Expand Down
75 changes: 10 additions & 65 deletions Code/core/src/Allocator.cpp
Original file line number Diff line number Diff line change
@@ -1,76 +1,21 @@
#include "Allocator.hpp"
#include "MimallocAllocator.hpp"
#include <cassert>

namespace TiltedPhoques
{
struct AllocatorStack
{
enum
{
kMaxAllocatorCount = 1024
};

AllocatorStack() noexcept
: m_index(0)
{
m_stack[0] = Allocator::GetDefault();
}

void Push(Allocator* apAllocator) noexcept
{
assert(m_index + 1 < kMaxAllocatorCount);

m_index++;
m_stack[m_index] = apAllocator;
}

Allocator* Pop() noexcept
{
assert(m_index > 0);

const auto pAllocator = m_stack[m_index];
m_index--;

return pAllocator;
}

[[nodiscard]] Allocator* Get() noexcept
{
return m_stack[m_index];
}

[[nodiscard]] static AllocatorStack& Instance() noexcept
{
static thread_local AllocatorStack s_stack;
return s_stack;
}

private:
static thread_local Allocator* s_pCurrentAllocator = nullptr;

uint32_t m_index;
Allocator* m_stack[kMaxAllocatorCount]{};
};


void Allocator::Push(Allocator* apAllocator) noexcept
{
AllocatorStack::Instance().Push(apAllocator);
}

void Allocator::Push(Allocator& aAllocator) noexcept
void Allocator::Set(Allocator* apAllocator) noexcept
{
Push(&aAllocator);
}

Allocator* Allocator::Pop() noexcept
{
return AllocatorStack::Instance().Pop();
s_pCurrentAllocator = apAllocator;
}

Allocator* Allocator::Get() noexcept
{
return AllocatorStack::Instance().Get();
if (s_pCurrentAllocator)
return s_pCurrentAllocator;

return GetDefault();
}

Allocator* Allocator::GetDefault() noexcept
Expand All @@ -80,9 +25,9 @@ namespace TiltedPhoques
}

ScopedAllocator::ScopedAllocator(Allocator* apAllocator) noexcept
: m_pAllocator(apAllocator)
: m_pOldAllocator(Allocator::Get())
{
Allocator::Push(m_pAllocator);
Allocator::Set(apAllocator);
}

ScopedAllocator::ScopedAllocator(Allocator& aAllocator) noexcept
Expand All @@ -92,6 +37,6 @@ namespace TiltedPhoques

ScopedAllocator::~ScopedAllocator() noexcept
{
Allocator::Pop();
Allocator::Set(m_pOldAllocator);
}
}
60 changes: 58 additions & 2 deletions Code/tests/src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <future>
#include <thread>
#include <cstring>
#include <iostream>

using namespace TiltedPhoques;

Expand Down Expand Up @@ -205,12 +206,12 @@ TEST_CASE("Making sure allocator stacks work corrently", "[core.allocator.stack]
{
BoundedAllocator allocator(1000);

Allocator::Push(&allocator);
Allocator::Set(&allocator);

REQUIRE(Allocator::Get() == &allocator);
auto futureResult = std::async(std::launch::async, []() { return Allocator::Get(); });
REQUIRE(futureResult.get() != &allocator);
REQUIRE(Allocator::Pop() == &allocator);
Allocator::Set(nullptr);
REQUIRE(Allocator::Get() != &allocator);
}

Expand Down Expand Up @@ -761,3 +762,58 @@ TEST_CASE("Intializer")

REQUIRE(globalInited);
}

TEST_CASE("Vector benchmark")
{
WHEN("Not using an allocator")
{
auto now = std::chrono::high_resolution_clock::now();
for (auto j = 0; j < 10; ++j)
{
std::vector<uint64_t> vec;
for (auto i = 0; i < 100000; ++i)
{
vec.push_back(i * 4 + (i / 3));

}
}
const auto stdElapsed = std::chrono::high_resolution_clock::now() - now;

now = std::chrono::high_resolution_clock::now();
for (auto j = 0; j < 10; ++j)
{
Vector<uint64_t> vec;
for (auto i = 0; i < 100000; ++i)
{
vec.push_back(i * 4 + (i / 3));

}
}
const auto tpElapsed = std::chrono::high_resolution_clock::now() - now;

now = std::chrono::high_resolution_clock::now();
{
ScratchAllocator s_allocator(100000 * 6 * 8);

for (auto j = 0; j < 10; ++j)
{
ScopedAllocator _(s_allocator);

Vector<uint64_t> vec;
for (auto i = 0; i < 100000; ++i)
{
vec.push_back(i * 4 + (i / 3));
}

s_allocator.Reset();
}


}
const auto tpScratchElapsed = std::chrono::high_resolution_clock::now() - now;

std::cout << "Std: " << stdElapsed.count() << std::endl;
std::cout << "Tp : " << tpElapsed.count() << std::endl;
std::cout << "Tps: " << tpScratchElapsed.count() << std::endl;
}
}

0 comments on commit e1749ce

Please sign in to comment.