Implement M5.4 bounded compact chunk ownership

This commit is contained in:
dingo
2026-09-26 14:34:22 -04:00
parent e2f99b780f
commit 25ac646f93
12 changed files with 729 additions and 4 deletions
+233
View File
@@ -0,0 +1,233 @@
#include "evo/world/chunks.hpp"
#include <algorithm>
#include <array>
#include <limits>
#include <optional>
#include <utility>
namespace evo::world {
namespace {
std::uint32_t pack(CellSample cell) noexcept {
return cell.material().value | (static_cast<std::uint32_t>(cell.fill()) << 16U);
}
// 257 signals dense fallback. Caller always supplies exactly 4096 valid words.
std::uint32_t collect(std::span<const std::uint32_t> words,
std::array<std::uint32_t, 256>& keys) noexcept {
std::uint32_t count = 0;
for (const auto word : words) {
const auto end = keys.begin() + count;
if (std::find(keys.begin(), end, word) == end) {
if (count == 256) {
return 257;
}
keys[count++] = word;
}
}
return count;
}
} // namespace
CompactCells::CompactCells(CompactCells&& other) noexcept {
*this = std::move(other);
}
CompactCells& CompactCells::operator=(CompactCells&& other) noexcept {
if (this != &other) {
layout_ = std::exchange(other.layout_, CellLayout::uniform);
uniform_ = std::exchange(other.uniform_, 0);
keys_ = std::exchange(other.keys_, 0);
data_ = std::move(other.data_);
}
return *this;
}
std::size_t CompactCells::required_bytes(std::span<const std::uint32_t> words) noexcept {
std::array<std::uint32_t, 256> keys{};
const auto count = collect(words, keys);
if (count == 1) {
return 0;
}
return (count > 256 ? 4096 : 1024 + static_cast<std::size_t>(count)) * sizeof(std::uint32_t);
}
CompactCells CompactCells::encode(std::span<const std::uint32_t> words) {
std::array<std::uint32_t, 256> keys{};
const auto count = collect(words, keys);
CompactCells result;
if (count == 1) {
result.uniform_ = keys[0];
} else if (count > 256) {
result.layout_ = CellLayout::dense;
result.data_ = std::make_unique<std::uint32_t[]>(4096);
std::copy(words.begin(), words.end(), result.data_.get());
} else {
result.layout_ = CellLayout::palette;
result.keys_ = count;
result.data_ = std::make_unique<std::uint32_t[]>(1024 + static_cast<std::size_t>(count));
std::copy_n(keys.begin(), count, result.data_.get());
for (std::size_t i = 0; i < words.size(); ++i) {
const auto key = static_cast<std::uint32_t>(
std::find(keys.begin(), keys.begin() + count, words[i]) - keys.begin());
result.data_[count + i / 4] |= key << ((i % 4) * 8);
}
}
return result;
}
std::expected<CompactCells, ChunkError> CompactCells::create(std::span<const CellSample> cells) {
if (cells.size() != base_chunk_cells) {
return std::unexpected{ChunkError::invalid_cells};
}
std::array<std::uint32_t, base_chunk_cells> words{};
std::transform(cells.begin(), cells.end(), words.begin(), pack);
return encode(words);
}
std::size_t CompactCells::payload_bytes() const noexcept {
if (layout_ == CellLayout::uniform) {
return 0;
}
return (layout_ == CellLayout::dense ? 4096 : 1024 + static_cast<std::size_t>(keys_)) *
sizeof(std::uint32_t);
}
std::expected<CellSample, ChunkError> CompactCells::read(std::uint32_t index) const noexcept {
if (index >= base_chunk_cells) {
return std::unexpected{ChunkError::invalid_local};
}
auto word = uniform_;
if (layout_ == CellLayout::dense) {
word = data_[index];
}
if (layout_ == CellLayout::palette) {
const auto key = (data_[keys_ + index / 4] >> ((index % 4) * 8)) & 255U;
word = data_[key];
}
return *CellSample::create({static_cast<std::uint16_t>(word & 65535U)}, word >> 16U);
}
struct ChunkStore::Entry {
ChunkAddress address{};
std::optional<CompactCells> cells;
std::uint64_t revision{};
bool dirty{};
};
ChunkStore::ChunkStore(BaseDescriptor descriptor, std::size_t slots, std::size_t budget,
std::size_t bytes)
: descriptor_{descriptor}, slots_{slots}, budget_{budget}, bytes_{bytes},
entries_{std::make_unique<Entry[]>(slots)} {}
ChunkStore::~ChunkStore() = default;
std::expected<std::size_t, ChunkError> ChunkStore::minimum_bytes(std::size_t slots) noexcept {
if (slots == 0 || slots > max_slots) {
return std::unexpected{ChunkError::invalid_config};
}
return sizeof(ChunkStore) + slots * sizeof(Entry) + scratch_bytes;
}
std::expected<std::unique_ptr<ChunkStore>, ChunkError>
ChunkStore::create(BaseDescriptor descriptor, std::size_t slots, std::size_t budget) {
const auto minimum = minimum_bytes(slots);
if (!minimum) {
return std::unexpected{minimum.error()};
}
if (!base_sample(descriptor, {})) {
return std::unexpected{ChunkError::invalid_descriptor};
}
if (budget < *minimum) {
return std::unexpected{ChunkError::memory_budget};
}
return std::unique_ptr<ChunkStore>{new ChunkStore(descriptor, slots, budget, *minimum)};
}
bool ChunkStore::current(ChunkView v) const noexcept {
return v.owner_ == this && v.slot_ < slots_ && entries_[v.slot_].cells &&
entries_[v.slot_].revision == v.revision_;
}
std::expected<ChunkView, ChunkError> ChunkStore::view(ChunkAddress address) const noexcept {
if (!join({address, {}}, base_chunk_edge)) {
return std::unexpected{ChunkError::invalid_address};
}
for (std::size_t i = 0; i < slots_; ++i) {
if (entries_[i].cells && entries_[i].address == address) {
return ChunkView{this, i, entries_[i].revision};
}
}
return std::unexpected{ChunkError::missing};
}
std::expected<ChunkView, ChunkError> ChunkStore::load(ChunkAddress address) {
const auto existing = view(address);
if (existing) {
return existing;
}
if (existing.error() != ChunkError::missing) {
return std::unexpected{existing.error()};
}
if (size_ == slots_) {
return std::unexpected{ChunkError::capacity};
}
if (revision_ == std::numeric_limits<std::uint64_t>::max()) {
return std::unexpected{ChunkError::revision_exhausted};
}
std::array<std::uint32_t, base_chunk_cells> words{};
const auto origin = *join({address, {}}, base_chunk_edge);
for (std::uint32_t i = 0; i < base_chunk_cells; ++i) {
const auto local = *local_from_ordinal(i, base_chunk_edge);
words[i] = pack(*base_sample(descriptor_,
{origin.x + local.x, origin.y + local.y, origin.z + local.z}));
}
const auto payload = CompactCells::required_bytes(words);
if (payload > budget_ - bytes_) {
return std::unexpected{ChunkError::memory_budget};
}
auto cells = CompactCells::encode(words); // All fallible allocation precedes publication.
for (std::size_t i = 0; i < slots_; ++i) {
auto& entry = entries_[i];
if (!entry.cells) {
entry.address = address;
entry.cells.emplace(std::move(cells));
entry.revision = ++revision_;
entry.dirty = false;
bytes_ += payload;
++size_;
return ChunkView{this, i, entry.revision};
}
}
return std::unexpected{ChunkError::capacity};
}
std::expected<CellSample, ChunkError> ChunkStore::read(ChunkView v,
LocalCell local) const noexcept {
if (!current(v)) {
return std::unexpected{ChunkError::stale_view};
}
const auto i = ordinal(local, base_chunk_edge);
if (!i) {
return std::unexpected{ChunkError::invalid_local};
}
const auto& cells = entries_[v.slot_].cells;
if (!cells) {
return std::unexpected{ChunkError::stale_view};
}
return cells->read(*i);
}
std::expected<ChunkView, ChunkError> ChunkStore::mark_dirty(ChunkView v) noexcept {
if (!current(v)) {
return std::unexpected{ChunkError::stale_view};
}
auto& entry = entries_[v.slot_];
if (entry.dirty) {
return v;
}
if (revision_ == std::numeric_limits<std::uint64_t>::max()) {
return std::unexpected{ChunkError::revision_exhausted};
}
entry.dirty = true;
entry.revision = ++revision_;
return ChunkView{this, v.slot_, entry.revision};
}
std::expected<void, ChunkError> ChunkStore::evict(ChunkView v) noexcept {
if (!current(v)) {
return std::unexpected{ChunkError::stale_view};
}
auto& entry = entries_[v.slot_];
if (entry.dirty) {
return std::unexpected{ChunkError::dirty};
}
if (!entry.cells) {
return std::unexpected{ChunkError::stale_view};
}
bytes_ -= entry.cells->payload_bytes();
entry.cells.reset();
--size_;
return {};
}
} // namespace evo::world