./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
34 / 34
100.0%
Branches:
19 / 23
82.6%
Functions:
8 / 8, 1 excl
100.0%

src/layers/craters/generation/cell_cache.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "layers/craters/generation/cell_cache.h"
4
5 #include "terraingen/noise/hash.h"
6 #include "terraingen/util/log.h"
7
8 #include <array>
9 #include <atomic>
10
11 namespace terraingen {
12
13 namespace {
14
15 /// Cells each thread keeps of the caches it last read.
16 constexpr std::size_t kRecentCells = 256;
17
18 /// The source of each cache's `m_id`, from 1 so 0 marks an empty slot.
19 std::atomic<std::uint64_t> nextCacheId{1};
20
21 /// A cell a thread read: its cache, band and index, and its craters.
22 398384 struct RecentCell {
23 std::uint64_t cache = 0;
24 int exponent = 0;
25 std::int32_t i = 0;
26 std::int32_t j = 0;
27 std::shared_ptr<const CellCraters> craters;
28 };
29
30 } // namespace
31
32 std::shared_ptr<const CellCraters>
33 1685559 CraterCellCache::Get(int exponent, const CellIndex& cell,
34 const std::function<CellCraters()>& generate) {
35
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 1685209 times.
1685559 thread_local std::array<RecentCell, kRecentCells> recent;
36 1685560 const std::uint32_t mixed = (static_cast<std::uint32_t>(cell.i) * 0x9E3779B1u) ^
37 1685560 (static_cast<std::uint32_t>(cell.j) * 0x85EBCA77u) ^
38 1685560 (static_cast<std::uint32_t>(exponent) * 0xC2B2AE3Du);
39
2/2
✓ Branch 0 taken 1619187 times.
✓ Branch 1 taken 66373 times.
1685560 RecentCell& slot = recent[(mixed >> 16U) % kRecentCells];
40
7/8
✓ Branch 0 taken 1619187 times.
✓ Branch 1 taken 66373 times.
✓ Branch 2 taken 1587478 times.
✓ Branch 3 taken 31709 times.
✓ Branch 4 taken 1486700 times.
✓ Branch 5 taken 100778 times.
✓ Branch 6 taken 1486868 times.
✗ Branch 7 not taken.
1685560 if (slot.cache == m_id && slot.exponent == exponent && slot.i == cell.i && slot.j == cell.j) {
41
1/2
✓ Branch 0 taken 1486868 times.
✗ Branch 1 not taken.
1486868 return slot.craters;
42 }
43 198692 std::shared_ptr<const CellCraters> craters = m_cache.Get({exponent, cell.i, cell.j}, generate);
44
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 199192 times.
199192 slot = {m_id, exponent, cell.i, cell.j, craters};
45 199192 return craters;
46
1/2
✓ Branch 0 taken 199191 times.
✗ Branch 1 not taken.
597576 }
47
48 605678 std::size_t CraterCellCache::KeyHash::operator()(const Key& key) const {
49 605678 return HashWords({static_cast<std::uint64_t>(key.exponent), static_cast<std::uint64_t>(key.i),
50 605678 static_cast<std::uint64_t>(key.j)});
51 }
52
53 1309 CraterCellCache::CraterCellCache(std::size_t maxCraters)
54 1309 : m_cache(
55 109663 maxCraters, [](const CellCraters& cell) { return cell.craters.Size(); },
56
1/1
✓ Branch 1 taken 1308 times.
1309 [maxCraters](std::size_t evicted) {
57 738 LogDebug("craters", "Crater cache evicted ", evicted, " cells to stay within ",
58 369 maxCraters, " craters");
59 369 }),
60 1308 m_id(nextCacheId.fetch_add(1)) {}
61
62 262738 CellCraterSource CachedCellCraters(CraterCellCache& cache, const SurfaceCells& cells,
63 const SizeFrequencyDistribution& density,
64 const CraterAgeModel& ages, std::uint32_t seed) {
65 2211879 return [&cache, &cells, &density, &ages, seed](const CraterBand& band, const CellIndex& cell) {
66 5165283 return cache.Get(band.exponent, cell, [&] {
67
1/1
✓ Branch 2 taken 109259 times.
109262 return CellCraters(GenerateCellCraters(cells, band, cell, density, ages, seed));
68
1/1
✓ Branch 2 taken 1687120 times.
3370895 });
69 262738 };
70 }
71
72 263038 CraterList GenerateCachedCraters(CraterCellCache* cache, const SurfaceCells& cells,
73 const Bounds2d& region, const SizeFrequencyDistribution& density,
74 const CraterAgeModel& ages, std::uint32_t seed,
75 CraterGenerationOptions options) {
76
2/2
✓ Branch 0 taken 263034 times.
✓ Branch 1 taken 4 times.
263038 if (cache != nullptr) {
77 263034 options.cellCraters = CachedCellCraters(*cache, cells, density, ages, seed);
78 }
79 263584 return GenerateCraters(cells, region, density, ages, seed, options);
80 }
81
82 } // namespace terraingen
83