./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
35 / 35
100.0%
Branches:
23 / 40
57.5%
Functions:
7 / 7, 2 excl
100.0%

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
1/1
✓ Branch 1 taken 1373 times.
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
2/4
std::shared_ptr<double const> terraingen::EvaluationCache::Get<double>(void const*, unsigned long, double, std::function<double ()> const&):
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
std::shared_ptr<std::optional<terraingen::Plane> const> terraingen::EvaluationCache::Get<std::optional<terraingen::Plane> >(void const*, unsigned long, double, std::function<std::optional<terraingen::Plane> ()> const&):
✓ Branch 0 taken 159 times.
✗ Branch 1 not taken.
164 const std::shared_ptr<const Holder> held =
39
2/2
terraingen::EvaluationCache::Get<double>(void const*, unsigned long, double, std::function<double ()> const&)::{lambda()#1}::operator()() const:
✓ Branch 1 taken 5 times.
terraingen::EvaluationCache::Get<std::optional<terraingen::Plane> >(void const*, unsigned long, double, std::function<std::optional<terraingen::Plane> ()> const&)::{lambda()#1}::operator()() const:
✓ Branch 1 taken 159 times.
299 m_values.Get(key, [&] { return Holder{std::make_shared<const T>(compute())}; });
40
4/8
terraingen::EvaluationCache::Get<double>(void const*, unsigned long, double, std::function<double ()> const&)::{lambda()#1}::operator()() const:
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
terraingen::EvaluationCache::Get<std::optional<terraingen::Plane> >(void const*, unsigned long, double, std::function<std::optional<terraingen::Plane> ()> const&)::{lambda()#1}::operator()() const:
✓ Branch 0 taken 159 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 159 times.
✗ Branch 3 not taken.
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
5/6
✓ Branch 0 taken 1023 times.
✓ Branch 1 taken 114 times.
✓ Branch 2 taken 1023 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1023 times.
✓ Branch 5 taken 114 times.
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
6/12
std::shared_ptr<std::optional<terraingen::Plane> const> terraingen::EvaluationCache::Find<std::optional<terraingen::Plane> >(void const*, unsigned long, double):
✓ Branch 0 taken 1051 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1051 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1051 times.
terraingen::EvaluationCache::Get<double>(void const*, unsigned long, double, std::function<double ()> const&)::{lambda()#1}::operator()() const:
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
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
3/7
✓ Branch 0 taken 135 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 135 times.
✓ Branch 5 taken 135 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
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