Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions include/Nebulite/Data/Document/JsonScope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
#include <vector>

// Nebulite
#include "Nebulite/Constants/Alignment.hpp"
#include "Nebulite/Constants/ThreadSettings.hpp"
#include "Nebulite/Data/Document/KeyType.hpp"
#include "Nebulite/Data/Document/RjDirectAccess.hpp"
#include "Nebulite/Data/Document/ScopedKey.hpp"
#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
Expand Down Expand Up @@ -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<MappedOrderedCacheList, cacheLookupThreadCount> odpCache;
std::array<
Utility::Coordination::LazyInit<MappedOrderedCacheList, JsonScope&>,
cacheLookupThreadCount>
odpCache;

//------------------------------------------
// Complex number prefixes
Expand Down
16 changes: 12 additions & 4 deletions include/Nebulite/Data/Document/JsonScope.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@
// Includes

// Standard library
#include <cassert>
#include <complex>
#include <concepts>
#include <cstddef>
#include <cstdint> // NOLINT
#include <expected>
#include <optional>
#include <ranges>
#include <string>
#include <tuple>
#include <type_traits>

// Nebulite
#include "Nebulite/Data/Document/Json.hpp"
Expand Down Expand Up @@ -76,10 +86,8 @@ void JsonScope::setArray(ScopedKeyView const& key, R const& range) {
template <std::ranges::input_range R> requires std::same_as<std::remove_cvref_t<std::ranges::range_reference_t<R>>,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
Expand Down
67 changes: 67 additions & 0 deletions include/Nebulite/Utility/Coordination/LazyInit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef NEBULITE_UTILITY_COORDINATION_LAZYINIT_HPP
#define NEBULITE_UTILITY_COORDINATION_LAZYINIT_HPP

//------------------------------------------
// Includes

// Standard library
#include <memory>
#include <optional>

//------------------------------------------
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<typename T, typename... ConstructorArgs>
class LazyInit {
std::unique_ptr<T> 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<T>(std::forward<ConstructorArgs>(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<typename T, typename... ConstructorArgs>
class LazyInitOptional {
std::optional<T> 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<ConstructorArgs>(args)...);
}
return *opt;
}
};

} // namespace Nebulite::Utility::Coordination
#endif // NEBULITE_UTILITY_COORDINATION_LAZYINIT_HPP
5 changes: 0 additions & 5 deletions src/Nebulite/Data/Document/JsonScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,21 +32,18 @@ JsonScope::JsonScope(Json& doc, std::optional<std::string> const& prefix)
// create a non-owning shared_ptr to the provided JSON (no delete on destruction)
: baseDocument(std::shared_ptr<Json>(&doc, [](Json*){}))
, scopePrefix(generateScopePrefix(doc, prefix))
, odpCache(Utility::Generate::array<MappedOrderedCacheList, cacheLookupThreadCount>([this](std::size_t) {return MappedOrderedCacheList(*this);}))
{}

// Constructing a JsonScope from another JsonScope and a sub-prefix
JsonScope::JsonScope(JsonScope const& other, std::optional<std::string> const& prefix)
: baseDocument(other.baseDocument)
, scopePrefix(generateScopePrefix(other, prefix))
, odpCache(Utility::Generate::array<MappedOrderedCacheList, cacheLookupThreadCount>([this](std::size_t) {return MappedOrderedCacheList(*this);}))
{}

// Default constructor, we create a self-owned empty JSON document
JsonScope::JsonScope()
: baseDocument(std::make_shared<Json>())
, scopePrefix("")
, odpCache(Utility::Generate::array<MappedOrderedCacheList, cacheLookupThreadCount>([this](std::size_t) {return MappedOrderedCacheList(*this);}))
{}

JsonScope::~JsonScope() = default;
Expand Down
Loading