cache entities to avoid allocations
Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
parent
d3e93b6a72
commit
f38ebeab14
6 changed files with 324 additions and 37 deletions
|
|
@ -1,11 +1,148 @@
|
|||
#pragma once
|
||||
|
||||
#include <concepts>
|
||||
#include <filesystem>
|
||||
#include <ranges>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
|
||||
#include <sqlitecpp/connection.hpp>
|
||||
|
||||
#include <ikarus/errors.h>
|
||||
|
||||
constexpr inline size_t MAXIMUM_ERROR_INFOS = 8;
|
||||
constexpr inline size_t MAXIMUM_ERROR_MESSAGE_LENGTH = 256;
|
||||
|
||||
class FunctionContext {
|
||||
public:
|
||||
explicit FunctionContext(struct IkarusProject * project);
|
||||
FunctionContext(FunctionContext const&) noexcept = default;
|
||||
FunctionContext(FunctionContext&&) noexcept = default;
|
||||
|
||||
auto operator=(FunctionContext const&) noexcept -> FunctionContext& = default;
|
||||
auto operator=(FunctionContext&&) noexcept -> FunctionContext& = default;
|
||||
|
||||
~FunctionContext();
|
||||
|
||||
template<typename... Infos>
|
||||
requires(std::is_same_v<IkarusErrorInfo, Infos> && ...) && (sizeof...(Infos) <= MAXIMUM_ERROR_INFOS)
|
||||
auto set_error(std::string_view error_message, bool log_error, Infos... infos) -> void;
|
||||
|
||||
private:
|
||||
struct IkarusProject * _project;
|
||||
};
|
||||
|
||||
/// \private
|
||||
struct IkarusProject {
|
||||
std::string name;
|
||||
std::filesystem::path path;
|
||||
std::unique_ptr<sqlitecpp::Connection> db;
|
||||
public:
|
||||
[[nodiscard]] inline auto name() const -> std::string_view {
|
||||
return _name;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline auto path() const -> std::filesystem::path const& {
|
||||
return _path;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline auto db() -> sqlitecpp::Connection * {
|
||||
return _db.get();
|
||||
}
|
||||
|
||||
inline auto function_context() -> FunctionContext * {
|
||||
return &_function_contexts.emplace(this);
|
||||
}
|
||||
|
||||
[[nodiscard]] IkarusBlueprint * get_blueprint(IkarusId id) {
|
||||
return get_cached_object(id, this->_blueprints);
|
||||
}
|
||||
|
||||
auto remove_blueprint(IkarusBlueprint * blueprint) -> void {
|
||||
remove_cached_object(blueprint, _blueprints);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto get_entity(IkarusId id) -> IkarusEntity * {
|
||||
return get_cached_object(id, this->_entities);
|
||||
}
|
||||
|
||||
auto remove_entity(IkarusEntity * entity) -> void {
|
||||
remove_cached_object(entity, _entities);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto get_property(IkarusId id) -> IkarusProperty * {
|
||||
return get_cached_object(id, this->_properties);
|
||||
}
|
||||
|
||||
auto remove_property(IkarusProperty * property) -> void {
|
||||
remove_cached_object(property, _properties);
|
||||
}
|
||||
|
||||
private:
|
||||
template<typename T>
|
||||
[[nodiscard]] T * get_cached_object(IkarusId id, std::unordered_map<IkarusId, std::unique_ptr<T>>& cache) {
|
||||
if (auto iter = cache.find(id); iter == cache.cend()) {
|
||||
auto [ret_iter, _] = cache.emplace(id, std::make_unique<T>(this, id));
|
||||
|
||||
return ret_iter->second.get();
|
||||
} else {
|
||||
return iter->second.get();
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void remove_cached_object(T * object, std::unordered_map<IkarusId, std::unique_ptr<T>>& cache) {
|
||||
cache.erase(object->id);
|
||||
}
|
||||
|
||||
private:
|
||||
friend class FunctionContext;
|
||||
|
||||
std::string _name;
|
||||
std::filesystem::path _path;
|
||||
std::unique_ptr<sqlitecpp::Connection> _db;
|
||||
|
||||
std::array<IkarusErrorInfo, MAXIMUM_ERROR_INFOS> error_infos;
|
||||
std::string error_message_buffer;
|
||||
|
||||
std::unordered_map<IkarusId, std::unique_ptr<IkarusBlueprint>> _blueprints;
|
||||
std::unordered_map<IkarusId, std::unique_ptr<IkarusProperty>> _properties;
|
||||
std::unordered_map<IkarusId, std::unique_ptr<IkarusEntity>> _entities;
|
||||
|
||||
std::stack<FunctionContext> _function_contexts;
|
||||
};
|
||||
|
||||
FunctionContext::FunctionContext(struct IkarusProject * project):
|
||||
_project{project} {}
|
||||
|
||||
FunctionContext::~FunctionContext() {
|
||||
if (_project->_function_contexts.size() == 1) {
|
||||
if (_project->error_message_buffer.empty()) {
|
||||
_project->error_message_buffer.push_back('\0');
|
||||
} else {
|
||||
_project->error_message_buffer[0] = '\0';
|
||||
}
|
||||
|
||||
_project->error_infos = {};
|
||||
}
|
||||
|
||||
_project->_function_contexts.pop();
|
||||
}
|
||||
|
||||
template<typename... Infos>
|
||||
requires(std::is_same_v<IkarusErrorInfo, Infos> && ...) && (sizeof...(Infos) <= MAXIMUM_ERROR_INFOS)
|
||||
auto FunctionContext::set_error(std::string_view error_message, bool log_error, Infos... infos) {
|
||||
if (error_message.size() > _project->error_message_buffer.size()) {
|
||||
_project->error_message_buffer.resize(error_message.size() + 1);
|
||||
}
|
||||
|
||||
for (int i = 0; i < error_message.size(); ++i) {
|
||||
_project->error_message_buffer[i] = error_message[i];
|
||||
}
|
||||
|
||||
_project->error_message_buffer[error_message.size()] = '\0';
|
||||
_project->error_infos = {infos...};
|
||||
|
||||
if (log_error) {
|
||||
LOG_ERROR(
|
||||
"Error({}): {}", fmt::join(_project->error_infos | std::views::transform(get_error_info_name), ", "), error_message
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue