From 7c77ed7252056006deb0e307e7e8ec9388189e80 Mon Sep 17 00:00:00 2001 From: Alexandre Plateau Date: Wed, 9 Jul 2025 20:28:18 +0200 Subject: [PATCH 1/2] 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 From 83740a09f4b923a50243ab6a349f656a65fdeca8 Mon Sep 17 00:00:00 2001 From: Lex Plt Date: Fri, 19 Dec 2025 20:29:08 +0100 Subject: [PATCH 2/2] Update ScopeView.hpp Signed-off-by: Lex Plt --- include/Ark/VM/ScopeView.hpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/Ark/VM/ScopeView.hpp b/include/Ark/VM/ScopeView.hpp index 1c95639a..65f3f3ff 100644 --- a/include/Ark/VM/ScopeView.hpp +++ b/include/Ark/VM/ScopeView.hpp @@ -59,6 +59,14 @@ namespace Ark::internal */ bool pushBack(uint16_t id, const Value& val) noexcept; + /** + * @brief Insert one or more pairs at the beginning of the scope + * @details This can ONLY be called on the last known scope, otherwise it will override the data of the next scope! + * + * @param values + */ + void insertFront(const std::vector& values) noexcept; + /** * @brief Check if the scope maybe holds a specific symbol in memory *