#include "evo/core/random.hpp" #include "evo/world/coordinates.hpp" #include #include #include #include #include #include #include #include namespace { using Cell = std::uint32_t; constexpr Cell rock = 0xffff0001U; void check(bool condition) { if (!condition) { throw std::runtime_error("chunk layout contract failed"); } } Cell cell(std::uint16_t material, std::uint32_t fill) { const auto value = evo::world::CellSample::create({material}, fill); check(value.has_value()); return static_cast(value->material().value) | (static_cast(value->fill()) << 16U); } struct Dense { std::vector values; explicit Dense(const std::vector& source, std::uint32_t) : values(source) {} Cell get(std::size_t i) const { return values.at(i); } void set(std::size_t i, Cell value) { values.at(i) = value; } std::size_t bytes() const { return sizeof(*this) + values.capacity() * sizeof(Cell); } std::size_t encoded() const { return 1 + values.size() * 4; } }; struct Palette { std::size_t count; std::vector palette; std::vector indexes; std::vector dense; explicit Palette(const std::vector& source, std::uint32_t) : count(source.size()), palette{source.at(0)} { for (std::size_t i = 0; i < count; ++i) { set(i, source[i]); } } Cell get(std::size_t i) const { if (i >= count) { throw std::out_of_range("cell"); } if (!dense.empty()) { return dense[i]; } return palette[indexes.empty() ? std::size_t{0} : static_cast(indexes[i])]; } void set(std::size_t i, Cell value) { if (i >= count) { throw std::out_of_range("cell"); } if (!dense.empty()) { dense[i] = value; return; } auto at = std::ranges::find(palette, value); if (at == palette.end()) { if (palette.size() == 256) { dense.resize(count); for (std::size_t j = 0; j < count; ++j) { dense[j] = palette[indexes[j]]; } std::vector{}.swap(palette); std::vector{}.swap(indexes); dense[i] = value; return; } palette.push_back(value); at = palette.end() - 1; } const auto slot = static_cast(at - palette.begin()); if (indexes.empty() && slot == 0) { return; } if (indexes.empty()) { indexes.resize(count, 0); } indexes[i] = slot; } std::size_t bytes() const { return sizeof(*this) + palette.capacity() * 4 + indexes.capacity() + dense.capacity() * 4; } std::size_t encoded() const { if (!dense.empty()) { return 1 + count * 4; } return indexes.empty() ? 5 : 3 + palette.size() * 4 + count; } }; struct Pages { struct Page { std::uint32_t key; std::array values; }; Cell base; std::uint32_t edge; std::vector pages; explicit Pages(const std::vector& source, std::uint32_t n) : base(source.at(0)), edge(n) { for (std::size_t i = 0; i < source.size(); ++i) { set(i, source[i]); } } std::pair address(std::size_t i) const { const auto n = static_cast(edge); if (i >= n * n * n) { throw std::out_of_range("cell"); } const auto x = static_cast(i % n); const auto y = static_cast((i / n) % n); const auto z = static_cast(i / (n * n)); const auto p = edge / 4; return {x / 4 + p * (y / 4 + p * (z / 4)), x % 4 + 4 * (y % 4 + 4 * (z % 4))}; } Cell get(std::size_t i) const { const auto [key, offset] = address(i); const auto at = std::ranges::lower_bound(pages, key, {}, &Page::key); return at == pages.end() || at->key != key ? base : at->values[offset]; } void set(std::size_t i, Cell value) { const auto [key, offset] = address(i); auto at = std::ranges::lower_bound(pages, key, {}, &Page::key); if (at == pages.end() || at->key != key) { if (value == base) { return; } Page page{key, {}}; page.values.fill(base); at = pages.insert(at, page); } at->values[offset] = value; if (value == base && std::ranges::all_of(at->values, [&](Cell x) { return x == base; })) { pages.erase(at); } } std::size_t bytes() const { return sizeof(*this) + pages.capacity() * sizeof(Page); } std::size_t encoded() const { return 9 + pages.size() * (4 + 64 * 4); } }; std::vector fixture(std::uint32_t edge, std::string_view name) { const auto count = static_cast(edge) * edge * edge; std::vector values(count, rock); for (std::size_t i = 0; i < count; ++i) { const auto x = i % edge, y = (i / edge) % edge, z = i / (static_cast(edge) * edge); if (name == "sparse" && (i == 1 || i == count / 2 || i == count - 2)) { values[i] = 0; } if (name == "mixed") { values[i] = cell(static_cast(1 + (x + 2 * y + 3 * z) % 8), 65535); } if (name == "excavated" && ((x >= edge / 4 && x < 3 * edge / 4 && y >= edge / 4 && y < 3 * edge / 4) || z == edge / 2)) { values[i] = 0; } if (name == "entropy") { values[i] = cell(static_cast(1 + i % 1024), static_cast(1 + (i / 1024) % 65535)); } } return values; } std::uint64_t hash_cell(std::uint64_t hash, Cell value) { return (hash ^ value) * 1099511628211ULL; } template void same(const T& layout, const std::vector& expected) { for (std::size_t i = 0; i < expected.size(); ++i) { check(layout.get(i) == expected[i]); } } template void bounds(T& layout, std::size_t count) { bool read = false, write = false; try { (void)layout.get(count); } catch (const std::out_of_range&) { read = true; } try { layout.set(count, rock); } catch (const std::out_of_range&) { write = true; } check(read && write); } using Clock = std::chrono::steady_clock; auto ns(Clock::time_point start) { return std::chrono::duration_cast(Clock::now() - start).count(); } template void run(std::uint32_t edge, std::string_view pattern, std::string_view kind, unsigned sample, bool verify) { auto expected = fixture(edge, pattern); auto start = Clock::now(); T layout(expected, edge); const auto build_ns = ns(start); same(layout, expected); const auto initial_bytes = layout.bytes(); const auto initial_encoded = layout.encoded(); auto peak = initial_bytes; std::uint64_t hash = 14695981039346656037ULL; start = Clock::now(); for (std::size_t i = 0; i < expected.size(); ++i) { hash = hash_cell(hash, layout.get(i)); } const auto scan_ns = ns(start); std::vector> edits; edits.reserve(4096); evo::Random random{123456}; for (unsigned i = 0; i < 4096; ++i) { edits.emplace_back(static_cast(random.next() % expected.size()), i % 2 == 0 ? 0 : rock); } start = Clock::now(); for (const auto& [index, value] : edits) { layout.set(index, value); peak = std::max(peak, layout.bytes()); } const auto edit_ns = ns(start); for (const auto& [index, value] : edits) { expected[index] = value; } same(layout, expected); bounds(layout, expected.size()); std::uint64_t final_hash = 14695981039346656037ULL; for (std::size_t i = 0; i < expected.size(); ++i) { final_hash = hash_cell(final_hash, layout.get(i)); } if (!verify && sample > 0) { const auto tiles = static_cast(128 / edge) * (128 / edge) * (128 / edge); std::cout << "{\"edge\":" << edge << ",\"pattern\":\"" << pattern << "\",\"layout\":\"" << kind << "\",\"sample\":" << sample << ",\"build_ns\":" << build_ns << ",\"scan_ns\":" << scan_ns << ",\"edit4096_ns\":" << edit_ns << ",\"initial_bytes\":" << initial_bytes << ",\"final_bytes\":" << layout.bytes() << ",\"sampled_peak_bytes\":" << peak << ",\"encoded_estimate\":" << initial_encoded << ",\"tiled128_initial_bytes\":" << initial_bytes * tiles << ",\"hash\":" << hash << ",\"edited_hash\":" << final_hash << "}\n"; } } void transitions() { std::vector input(512, rock); Palette palette(input, 8); check(palette.indexes.empty()); for (std::size_t i = 0; i < 256; ++i) { palette.set(i, cell(static_cast(i + 2), 65535)); if (i == 254) { check(palette.palette.size() == 256 && palette.dense.empty()); } } check(!palette.dense.empty() && palette.palette.empty() && palette.indexes.empty()); for (std::size_t i = 0; i < 256; ++i) { check(palette.get(i) == cell(static_cast(i + 2), 65535)); } Pages pages(input, 8); check(pages.pages.empty()); pages.set(0, 0); pages.set(511, 0); check(pages.pages.size() == 2); pages.set(0, rock); pages.set(511, rock); check(pages.pages.empty()); same(pages, input); // All representations must recover original logical values after churn; // reclaiming retained palette/capacity is explicitly not claimed. for (const auto edge : {8U, 16U, 32U, 64U}) { auto source = fixture(edge, "uniform"); Palette a(source, edge); Pages b(source, edge); Dense c(source, edge); for (std::size_t i = 0; i < source.size(); i += 17) { a.set(i, 0); b.set(i, 0); c.set(i, 0); } for (std::size_t i = 0; i < source.size(); i += 17) { a.set(i, rock); b.set(i, rock); c.set(i, rock); } same(a, source); same(b, source); same(c, source); } } } // namespace int main(int argc, char** argv) try { if (argc > 2 || (argc == 2 && std::string_view{argv[1]} != "--verify")) { return 2; } const bool verify = argc == 2; transitions(); if (!verify) { std::cout << "{\"experiment\":\"chunk-layout-v1\",\"seed\":123456,\"edits\":4096,\"compiler\":\"" #if defined(__clang__) << "Clang " << __clang_version__ #elif defined(_MSC_VER) << "MSVC " << _MSC_VER #else << "GCC " << __VERSION__ #endif << "\",\"release\":" #ifdef NDEBUG << "true" #else << "false" #endif << "}\n"; } for (const auto edge : {8U, 16U, 32U, 64U}) { for (const auto pattern : {"uniform", "sparse", "mixed", "excavated", "entropy"}) { for (unsigned sample = 0; sample < (verify ? 1U : 4U); ++sample) { run(edge, pattern, "dense", sample, verify); run(edge, pattern, "palette", sample, verify); run(edge, pattern, "pages4", sample, verify); } } } return 0; } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; } catch (...) { return 1; }