From e1749ceb4c57447b14b8c0efde29e285a3c1c5ba Mon Sep 17 00:00:00 2001 From: yamashi Date: Sun, 13 Jun 2021 12:56:13 +0200 Subject: [PATCH] Improve allocator stack --- Code/core/include/Allocator.hpp | 6 +-- Code/core/src/Allocator.cpp | 75 +++++---------------------------- Code/tests/src/core.cpp | 60 +++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 71 deletions(-) diff --git a/Code/core/include/Allocator.hpp b/Code/core/include/Allocator.hpp index 7dba703..e2f64c5 100644 --- a/Code/core/include/Allocator.hpp +++ b/Code/core/include/Allocator.hpp @@ -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; }; @@ -152,7 +150,7 @@ namespace TiltedPhoques TP_NOCOPYMOVE(ScopedAllocator); private: - Allocator* m_pAllocator; + Allocator* m_pOldAllocator; }; template diff --git a/Code/core/src/Allocator.cpp b/Code/core/src/Allocator.cpp index 7682454..4526459 100644 --- a/Code/core/src/Allocator.cpp +++ b/Code/core/src/Allocator.cpp @@ -1,76 +1,21 @@ #include "Allocator.hpp" #include "MimallocAllocator.hpp" -#include 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 @@ -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 @@ -92,6 +37,6 @@ namespace TiltedPhoques ScopedAllocator::~ScopedAllocator() noexcept { - Allocator::Pop(); + Allocator::Set(m_pOldAllocator); } } diff --git a/Code/tests/src/core.cpp b/Code/tests/src/core.cpp index 6ca50bb..ed4a82c 100644 --- a/Code/tests/src/core.cpp +++ b/Code/tests/src/core.cpp @@ -19,6 +19,7 @@ #include #include #include +#include using namespace TiltedPhoques; @@ -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); } @@ -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 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 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 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; + } +}