From 7c77ed7252056006deb0e307e7e8ec9388189e80 Mon Sep 17 00:00:00 2001 From: Alexandre Plateau Date: Wed, 9 Jul 2025 20:28:18 +0200 Subject: [PATCH] wip - bad performances --- include/Ark/VM/ScopeView.hpp | 6 ++++-- include/Ark/VM/VM.inl | 5 ++++- src/arkreactor/VM/ScopeView.cpp | 12 ++++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/Ark/VM/ScopeView.hpp b/include/Ark/VM/ScopeView.hpp index 5941d35e..3b9fb3ff 100644 --- a/include/Ark/VM/ScopeView.hpp +++ b/include/Ark/VM/ScopeView.hpp @@ -46,16 +46,18 @@ namespace Ark::internal * * @param id The symbol id of the variable * @param val The value linked to the symbol + * @return bool true if the allocation succeeded, false otherwise */ - void push_back(uint16_t id, Value&& val) noexcept; + bool push_back(uint16_t id, Value&& val) noexcept; /** * @brief Put a value in the scope * * @param id The symbol id of the variable * @param val The value linked to the symbol + * @return bool true if the allocation succeeded, false otherwise */ - void push_back(uint16_t id, const Value& val) noexcept; + bool push_back(uint16_t id, const Value& val) noexcept; /** * @brief Check if the scope maybe holds a specific symbol in memory diff --git a/include/Ark/VM/VM.inl b/include/Ark/VM/VM.inl index d3cbf1ea..b9cfed8f 100644 --- a/include/Ark/VM/VM.inl +++ b/include/Ark/VM/VM.inl @@ -130,7 +130,10 @@ inline void VM::store(const uint16_t id, const Value* val, internal::ExecutionCo // avoid adding the pair (id, _) multiple times, with different values Value* local = context.locals.back()[id]; if (local == nullptr) [[likely]] - context.locals.back().push_back(id, *val); + { + if (!context.locals.back().push_back(id, *val)) + throw Error(fmt::format("Can not create variable: no more heap space (limit: {}). If you are using a recursive algorithm, try making use of tail call optimization to reduce the number of allocated scopes.", ScopeStackSize)); + } else *local = *val; } diff --git a/src/arkreactor/VM/ScopeView.cpp b/src/arkreactor/VM/ScopeView.cpp index 4ec67f27..97275c5c 100644 --- a/src/arkreactor/VM/ScopeView.cpp +++ b/src/arkreactor/VM/ScopeView.cpp @@ -8,8 +8,11 @@ namespace Ark::internal m_storage(storage), m_start(start), m_size(0), m_min_id(std::numeric_limits::max()), m_max_id(0) {} - void ScopeView::push_back(uint16_t id, Value&& val) noexcept + bool ScopeView::push_back(uint16_t id, Value&& val) noexcept { + if (m_start + m_size >= ScopeStackSize) [[unlikely]] + return false; + if (id < m_min_id) m_min_id = id; if (id > m_max_id) @@ -17,10 +20,14 @@ namespace Ark::internal m_storage[m_start + m_size] = std::make_pair(id, std::move(val)); ++m_size; + return true; } - void ScopeView::push_back(uint16_t id, const Value& val) noexcept + bool ScopeView::push_back(uint16_t id, const Value& val) noexcept { + if (m_start + m_size >= ScopeStackSize) [[unlikely]] + return false; + if (id < m_min_id) m_min_id = id; if (id > m_max_id) @@ -28,6 +35,7 @@ namespace Ark::internal m_storage[m_start + m_size] = std::make_pair(id, val); ++m_size; + return true; } bool ScopeView::maybeHas(const uint16_t id) const noexcept