include/terraingen/layers/evaluation_cache.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief `EvaluationCache`, values one evaluation's batches of points share. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/noise/hash.h" | ||
| 8 | #include "terraingen/util/bits.h" | ||
| 9 | #include "terraingen/util/lru_cache.h" | ||
| 10 | |||
| 11 | #include <atomic> | ||
| 12 | #include <cstddef> | ||
| 13 | #include <cstdint> | ||
| 14 | #include <functional> | ||
| 15 | #include <memory> | ||
| 16 | |||
| 17 | namespace terraingen { | ||
| 18 | |||
| 19 | /// Thread-safe values that depend only on the terrain beneath a layer, such as crater datums, | ||
| 20 | /// shared by point queries; use one cache per generator and renew it after changing its layers. | ||
| 21 | 1373 | class EvaluationCache { | |
| 22 | public: | ||
| 23 | /// An empty cache holding at most `capacity` values. | ||
| 24 | 1373 | explicit EvaluationCache(std::size_t capacity = 1 << 20) | |
| 25 | 2746 | : m_values(capacity, [](const Holder&) { return std::size_t{1}; }), m_id(NextId()) {} | |
| 26 | |||
| 27 | /// This cache among every one made, never 0 and never reused, so values a thread keeps | ||
| 28 | /// for it can be told from another's. | ||
| 29 | 73348 | std::uint64_t Id() const { | |
| 30 | 73348 | return m_id; | |
| 31 | } | ||
| 32 | |||
| 33 | /// The value of `owner` for feature `id` at `spacing`, computed by `compute` when not held. | ||
| 34 | template <typename T> | ||
| 35 | 164 | std::shared_ptr<const T> Get(const void* owner, std::uint64_t id, double spacing, | |
| 36 | const std::function<T()>& compute) { | ||
| 37 | 164 | const Key key{reinterpret_cast<std::uintptr_t>(owner), id, BitCast<std::uint64_t>(spacing)}; | |
| 38 | 164 | const std::shared_ptr<const Holder> held = | |
| 39 | 299 | m_values.Get(key, [&] { return Holder{std::make_shared<const T>(compute())}; }); | |
| 40 | 328 | return std::static_pointer_cast<const T>(held->value); | |
| 41 | 164 | } | |
| 42 | |||
| 43 | /// The value of `owner` for feature `id` at `spacing` when held, else null. | ||
| 44 | template <typename T> | ||
| 45 | 1137 | std::shared_ptr<const T> Find(const void* owner, std::uint64_t id, double spacing) { | |
| 46 | 1137 | const Key key{reinterpret_cast<std::uintptr_t>(owner), id, BitCast<std::uint64_t>(spacing)}; | |
| 47 | 1137 | const std::shared_ptr<const Holder> held = m_values.Find(key); | |
| 48 | 2160 | return held ? std::static_pointer_cast<const T>(held->value) : nullptr; | |
| 49 | 1137 | } | |
| 50 | |||
| 51 | private: | ||
| 52 | struct Key { | ||
| 53 | std::uintptr_t owner = 0; | ||
| 54 | std::uint64_t id = 0; | ||
| 55 | std::uint64_t spacing = 0; | ||
| 56 | |||
| 57 | 1052 | bool operator==(const Key& other) const { | |
| 58 | 1052 | return owner == other.owner && id == other.id && spacing == other.spacing; | |
| 59 | } | ||
| 60 | }; | ||
| 61 | struct KeyHash { | ||
| 62 | 2692 | std::size_t operator()(const Key& key) const { | |
| 63 | 2692 | return HashWords({static_cast<std::uint64_t>(key.owner), key.id, key.spacing}); | |
| 64 | } | ||
| 65 | }; | ||
| 66 | 270 | struct Holder { | |
| 67 | std::shared_ptr<const void> value; | ||
| 68 | }; | ||
| 69 | |||
| 70 | 1373 | static std::uint64_t NextId() { | |
| 71 | 1373 | static std::atomic<std::uint64_t> next{1}; | |
| 72 | 1373 | return next.fetch_add(1); | |
| 73 | } | ||
| 74 | |||
| 75 | LruCache<Key, Holder, KeyHash> m_values; | ||
| 76 | std::uint64_t m_id; | ||
| 77 | }; | ||
| 78 | |||
| 79 | } // namespace terraingen | ||
| 80 |