From ebcc31694370ae7afcc1982e5fa9be2e0a8e79bd Mon Sep 17 00:00:00 2001 From: Leo Date: Thu, 27 Aug 2026 11:11:08 +0200 Subject: [PATCH 1/2] Add LazyInit and LazyInitOptional utility classes for lazy initialization --- .../Utility/Coordination/LazyInit.hpp | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 include/Nebulite/Utility/Coordination/LazyInit.hpp diff --git a/include/Nebulite/Utility/Coordination/LazyInit.hpp b/include/Nebulite/Utility/Coordination/LazyInit.hpp new file mode 100644 index 000000000..80c7fd62e --- /dev/null +++ b/include/Nebulite/Utility/Coordination/LazyInit.hpp @@ -0,0 +1,67 @@ +#ifndef NEBULITE_UTILITY_COORDINATION_LAZYINIT_HPP +#define NEBULITE_UTILITY_COORDINATION_LAZYINIT_HPP + +//------------------------------------------ +// Includes + +// Standard library +#include +#include + +//------------------------------------------ +namespace Nebulite::Utility::Coordination { + +/** + * @brief A utility class for lazy initialization of objects. + * @details Small inline size variant using std::unique_ptr instead of std::optional. + * @tparam T The type of the object to be lazily initialized. + * @tparam ConstructorArgs The types of the arguments for the constructor. + */ +template +class LazyInit { + std::unique_ptr ptr; + +public: + LazyInit() = default; + + /** + * @brief Returns a reference to the lazily initialized object, constructing it if it doesn't exist. + * @param args The arguments for the constructor. + * @return A reference to the lazily initialized object. + */ + T& get(ConstructorArgs&&... args) { + if (!ptr) { + ptr = std::make_unique(std::forward(args)...); + } + return *ptr; + } +}; + +/** + * @brief A utility class for lazy initialization of objects. + * @details Full inline size variant using std::optional. + * @tparam T The type of the object to be lazily initialized. + * @tparam ConstructorArgs The types of the arguments for the constructor. + */ +template +class LazyInitOptional { + std::optional opt; + +public: + LazyInitOptional() = default; + + /** + * @brief Returns a reference to the lazily initialized object, constructing it if it doesn't exist. + * @param args The arguments for the constructor. + * @return A reference to the lazily initialized object. + */ + T& get(ConstructorArgs&&... args) { + if (!opt) { + opt.emplace(std::forward(args)...); + } + return *opt; + } +}; + +} // namespace Nebulite::Utility::Coordination +#endif // NEBULITE_UTILITY_COORDINATION_LAZYINIT_HPP From 64e8f7db786b8c7ad41e387c1e5efce3d41d51f9 Mon Sep 17 00:00:00 2001 From: Leo Date: Thu, 27 Aug 2026 11:17:08 +0200 Subject: [PATCH 2/2] Implement lazy-initialization jsonScope caching. --- include/Nebulite/Data/Document/JsonScope.hpp | 8 +++++--- include/Nebulite/Data/Document/JsonScope.tpp | 16 ++++++++++++---- src/Nebulite/Data/Document/JsonScope.cpp | 5 ----- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/include/Nebulite/Data/Document/JsonScope.hpp b/include/Nebulite/Data/Document/JsonScope.hpp index 2d9cc9fdc..4a4d4dffd 100644 --- a/include/Nebulite/Data/Document/JsonScope.hpp +++ b/include/Nebulite/Data/Document/JsonScope.hpp @@ -22,7 +22,6 @@ #include // Nebulite -#include "Nebulite/Constants/Alignment.hpp" #include "Nebulite/Constants/ThreadSettings.hpp" #include "Nebulite/Data/Document/KeyType.hpp" #include "Nebulite/Data/Document/RjDirectAccess.hpp" @@ -30,6 +29,7 @@ #include "Nebulite/Data/Document/ScopedKeyView.hpp" #include "Nebulite/Data/Document/SimpleValueError.hpp" #include "Nebulite/Data/MappedOrderedCacheList.hpp" +#include "Nebulite/Utility/Coordination/LazyInit.hpp" //------------------------------------------ // Forward declarations @@ -73,9 +73,11 @@ class JsonScope final { /** * @brief Mapped ordered double pointers intended for non-locking access - * @todo Is a lazy init possible? This could reduce the memory footprint of JsonScope instances and make copying potentially cheaper. */ - alignas(Constants::Alignment::simdAlign) std::array odpCache; + std::array< + Utility::Coordination::LazyInit, + cacheLookupThreadCount> + odpCache; //------------------------------------------ // Complex number prefixes diff --git a/include/Nebulite/Data/Document/JsonScope.tpp b/include/Nebulite/Data/Document/JsonScope.tpp index 59585de80..2db388ba3 100644 --- a/include/Nebulite/Data/Document/JsonScope.tpp +++ b/include/Nebulite/Data/Document/JsonScope.tpp @@ -5,7 +5,17 @@ // Includes // Standard library +#include +#include +#include +#include +#include // NOLINT #include +#include +#include +#include +#include +#include // Nebulite #include "Nebulite/Data/Document/Json.hpp" @@ -76,10 +86,8 @@ void JsonScope::setArray(ScopedKeyView const& key, R const& range) { template requires std::same_as>,ScopedKeyView> double** JsonScope::ensureOrderedCacheList(std::uint64_t uniqueId, R const& keys) { thread_local std::size_t const threadIndex = assignCacheLookupIndex(); - if (threadIndex >= cacheLookupThreadCount) { - throw std::runtime_error("Thread index exceeds non-locking array size! Too many threads accessing ordered cache lists, increase cacheLookupThreadCount or reduce thread count."); - } - return odpCache[threadIndex].ensureOrderedCacheListNoLock(uniqueId, keys); + assert(threadIndex < cacheLookupThreadCount && "Thread index exceeds non-locking array size! Too many threads accessing ordered cache lists, increase cacheLookupThreadCount or reduce thread count."); + return odpCache[threadIndex].get(*this).ensureOrderedCacheListNoLock(uniqueId, keys); } } // namespace Nebulite::Data diff --git a/src/Nebulite/Data/Document/JsonScope.cpp b/src/Nebulite/Data/Document/JsonScope.cpp index 31c82bae9..4262c535e 100644 --- a/src/Nebulite/Data/Document/JsonScope.cpp +++ b/src/Nebulite/Data/Document/JsonScope.cpp @@ -22,9 +22,7 @@ #include "Nebulite/Data/Document/RjDirectAccess.hpp" #include "Nebulite/Data/Document/ScopedKey.hpp" #include "Nebulite/Data/Document/SimpleValueError.hpp" -#include "Nebulite/Data/MappedOrderedCacheList.hpp" #include "Nebulite/Utility/Coordination/IdGenerator.hpp" -#include "Nebulite/Utility/Generate.hpp" //------------------------------------------ namespace Nebulite::Data { @@ -34,21 +32,18 @@ JsonScope::JsonScope(Json& doc, std::optional const& prefix) // create a non-owning shared_ptr to the provided JSON (no delete on destruction) : baseDocument(std::shared_ptr(&doc, [](Json*){})) , scopePrefix(generateScopePrefix(doc, prefix)) - , odpCache(Utility::Generate::array([this](std::size_t) {return MappedOrderedCacheList(*this);})) {} // Constructing a JsonScope from another JsonScope and a sub-prefix JsonScope::JsonScope(JsonScope const& other, std::optional const& prefix) : baseDocument(other.baseDocument) , scopePrefix(generateScopePrefix(other, prefix)) - , odpCache(Utility::Generate::array([this](std::size_t) {return MappedOrderedCacheList(*this);})) {} // Default constructor, we create a self-owned empty JSON document JsonScope::JsonScope() : baseDocument(std::make_shared()) , scopePrefix("") - , odpCache(Utility::Generate::array([this](std::size_t) {return MappedOrderedCacheList(*this);})) {} JsonScope::~JsonScope() = default;