diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml index 39758d4..5d89323 100644 --- a/.gitea/workflows/build.yaml +++ b/.gitea/workflows/build.yaml @@ -19,7 +19,7 @@ jobs: strategy: fail-fast: false matrix: - suite: [checks, tsan] + suite: [checks, tsan, ecs] steps: - name: Checkout uses: https://github.com/actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 diff --git a/CMakeLists.txt b/CMakeLists.txt index 9096e0a..ea49edb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ endif() option(EVO_WARNINGS_AS_ERRORS "Treat owned-code warnings as errors" ON) option(EVO_ENABLE_TIDY "Run clang-tidy during compilation" OFF) option(EVO_BUILD_FUZZER "Build the Clang libFuzzer snapshot target" OFF) +option(EVO_BUILD_ECS_EXPERIMENT "Build the isolated EnTT comparison (explicit preparation required)" OFF) set(EVO_SANITIZER "none" CACHE STRING "none, address (ASan/UBSan), or thread") set_property(CACHE EVO_SANITIZER PROPERTY STRINGS none address thread) include(cmake/ProjectOptions.cmake) @@ -71,6 +72,27 @@ if(EVO_BUILD_FUZZER) target_link_options(evo-snapshot-fuzz PRIVATE -fsanitize=fuzzer,address,undefined) endif() +if(EVO_BUILD_ECS_EXPERIMENT) + include(tools/entt-pin.cmake) + set(entt_root "${CMAKE_SOURCE_DIR}/build/external/entt") + if(NOT EXISTS "${entt_root}/include/entt/entt.hpp" OR NOT EXISTS "${entt_root}/LICENSE") + message(FATAL_ERROR "Run cmake -P tools/prepare-entt.cmake explicitly before configuring the experiment") + endif() + file(SHA256 "${entt_root}/include/entt/entt.hpp" entt_header_hash) + file(SHA256 "${entt_root}/LICENSE" entt_license_hash) + if(NOT entt_header_hash STREQUAL EVO_ENTT_HEADER_SHA256 OR NOT entt_license_hash STREQUAL EVO_ENTT_LICENSE_SHA256) + message(FATAL_ERROR "Experimental EnTT dependency failed integrity validation") + endif() + add_executable(evo-ecs-experiment experiments/ecs/comparison.cpp) + target_include_directories(evo-ecs-experiment PRIVATE include) + target_include_directories(evo-ecs-experiment SYSTEM PRIVATE "${entt_root}/include") + evo_owned_target(evo-ecs-experiment) + if(BUILD_TESTING) + add_test(NAME ecs-experiment-contracts COMMAND evo-ecs-experiment --verify) + set_tests_properties(ecs-experiment-contracts PROPERTIES TIMEOUT 120) + endif() +endif() + find_program(EVO_CLANG_FORMAT NAMES clang-format-19 clang-format) add_custom_target(format-check COMMAND "${CMAKE_COMMAND}" "-DFORMATTER=${EVO_CLANG_FORMAT}" "-DSOURCE_ROOT=${CMAKE_CURRENT_SOURCE_DIR}" diff --git a/README.md b/README.md index 786a00c..d545aeb 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ network service. Dev verification manifests are separate from the simulation. - [Open design questions](docs/architecture/OPEN_QUESTIONS.md) - [Actual validation results](docs/engineering/VALIDATION.md) - [Current delivery status and next steps](docs/STATUS.md) +- [Opt-in ECS evaluation and measurements](docs/engineering/ECS_EVALUATION.md) Read [AGENTS.md](AGENTS.md) before changing code. Architecture and contracts are authoritative over implementation. No external runtime library is needed for diff --git a/deploy/docker/Dockerfile b/deploy/docker/Dockerfile index e107f42..c44ac21 100644 --- a/deploy/docker/Dockerfile +++ b/deploy/docker/Dockerfile @@ -12,6 +12,7 @@ COPY include/ include/ COPY src/ src/ COPY apps/ apps/ COPY tests/ tests/ +COPY experiments/ experiments/ COPY tools/ tools/ FROM toolchain AS ci-toolchain @@ -29,6 +30,21 @@ RUN cmake --preset tsan -DCMAKE_CXX_COMPILER=clang++-19 \ && cmake --build --preset tsan --parallel 2 CMD ["ctest", "--preset", "tsan", "--no-tests=error", "--timeout", "120"] +FROM ci-toolchain AS ci-ecs +# Explicit experiment preparation; normal configure/runtime remains dependency-free. +RUN cmake -P tools/prepare-entt.cmake \ + && cmake --preset release -B build/ecs -DCMAKE_CXX_COMPILER=g++-14 -DEVO_BUILD_ECS_EXPERIMENT=ON \ + && cmake --build build/ecs --target evo-ecs-experiment --parallel 2 \ + && ctest --test-dir build/ecs -R ecs-experiment --output-on-failure --no-tests=error \ + && ./build/ecs/evo-ecs-experiment --measure \ + && stat -c 'experiment_executable_bytes=%s' build/ecs/evo-ecs-experiment +RUN cmake --preset asan -B build/ecs-asan -DCMAKE_CXX_COMPILER=clang++-19 -DEVO_BUILD_ECS_EXPERIMENT=ON \ + && cmake --build build/ecs-asan --target evo-ecs-experiment --parallel 2 \ + && ctest --test-dir build/ecs-asan -R ecs-experiment --output-on-failure --no-tests=error +RUN cmake --preset analysis -B build/ecs-analysis -DCMAKE_CXX_COMPILER=clang++-19 -DEVO_BUILD_ECS_EXPERIMENT=ON \ + && cmake --build build/ecs-analysis --target evo-ecs-experiment --parallel 2 \ + && ctest --test-dir build/ecs-analysis -R ecs-experiment --output-on-failure --no-tests=error + FROM toolchain AS build RUN cmake --preset release -B build/container \ -DCMAKE_CXX_COMPILER=g++-14 -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=/opt/evo/bin \ diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 0413946..3666319 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -27,12 +27,14 @@ independent reviews; dependent tasks begin after their contracts are settled. | 1.2 Implement minimal component lifecycle | simulation/world.* | 1.1, ADR-0005 | Stable IDs, bounded creation/removal, read-only views | Duplicate/stale ID, capacity and ordering tests | ID reuse or reference invalidation | | 1.3 Canonical local snapshot and restore | simulation/snapshot.cpp, schemas/proof-snapshot.md | 1.2, ADR-0008 | Bounded versioned byte format; validate before publish | Round trip, corruption, overflow, truncation, version rejection | Unsafe parsing, native padding | | 1.4 Freeze deterministic reference result | tests/simulation_tests.cpp | 1.3 | Continuous and restored runs match agreed fingerprint | Golden fingerprint and seeded property ensembles | Test only comparing same broken implementation | -| 1.5 Evaluate mature ECS adoption | prototype branch, ADR-0005 | 1.2, biological component workload sketch | Measured iteration/lifecycle/serialization cost and justified choice | Benchmark with representative sparse compositions | Premature general framework | +| 1.5 Evaluate mature ECS adoption | experiments/ecs/, ADR-0005, ADR-0021 | 1.2, biological component workload sketch | Measured iteration/lifecycle/serialization cost and scoped choice | Opt-in row/EnTT sparse/dense/churn comparison with canonical equality; production decision needs richer biology | Premature general framework | | 1.6 Establish scheduler and input phases | simulation/scheduler/, core/time/ | 1.4–1.5 | Explicit phase DAG, one writer, immutable jobs and stable merge | Reordered completion gives equal state; command ordering tests | Hidden concurrency nondeterminism | | 1.7 Add spatial contract | simulation/spatial/, units ADR | 1.5–1.6 | Local coordinates and bounded neighborhood queries | Boundary/wrap/precision properties and brute-force comparison | Planet precision leaking into local physics | -The first increment implements 1.1–1.4 as a proof. 1.5–1.7 remain separate work; -no scheduler or generic ECS placeholder is counted as completion. +The first increment implements 1.1–1.4 as a proof. M1.5 now has an isolated +comparison and scoped recommendation; see engineering/ECS_EVALUATION.md for +measured versus still-unmeasured coverage. M1.6–1.7 remain separate work; no +scheduler or generic ECS placeholder is counted as completion. ## M2 — Minimal energy ecosystem diff --git a/docs/STATUS.md b/docs/STATUS.md index 43de929..f3dff69 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -7,7 +7,7 @@ persistent simulation server. The founding vision remains unchanged. ## Completed implementation - Vision, architectural pillars, risks, open-question register, architecture - diagrams, biological contracts, engineering constitution and 20 ADRs. + diagrams, biological contracts, engineering constitution and 21 ADRs. Proposed/deferred choices remain distinct from accepted proof decisions. - M0–M4 task breakdowns with dependencies, acceptance evidence and risks, plus M5–M14 gates in [ROADMAP.md](ROADMAP.md). @@ -49,10 +49,16 @@ material and are not automatically included in commits. ## Next reviewable increments, in order +M1.5 progress: the opt-in row/EnTT4 experiment now checks canonical equality, +restore continuation, retired identities and allocator balance, and records +seven-sample sparse/dense/churn results. Windows Release, analysis and ASan checks +have run; Linux validation is tracked in VALIDATION.md. ADR-0005 retains rows +for the bounded proof; the general production ECS remains a later measured choice. + | Priority | Work | Completion evidence | |---|---|---| | 1 | Verify the existing dev Argo mapping and sync the pinned, validated image | Dev Job Complete with replay_verified=true; no World0 changes | -| 2 | M1.5 ECS evaluation: compare the bounded store with a pinned mature ECS in an isolated benchmark using sparse biological capability workloads | Iteration/lifecycle/memory/serialization measurements, license review and updated ADR-0005; no automatic adoption | +| 2 | Close the isolated M1.5 experiment with Linux CI evidence; retain richer production ECS evaluation gates | Matching canonical results and sanitizer/static checks across supported compiler families; no automatic production adoption | | 3 | M1.6 scheduler/input contract: ratify phase order and command admission before implementing a sequential scheduler | Stable input order, one writer, deterministic merges and replay equivalence | | 4 | M1.7 local spatial contract | Neighborhood queries checked against brute force, wrap/boundary/unit tests and measured workload | | 5 | M2.1 energy/matter ledger | Agreed units/ranges, atomic transfers, nonnegative balances and conservation tests before organisms | diff --git a/docs/adr/0005-ecs-strategy.md b/docs/adr/0005-ecs-strategy.md index 5785246..6f1e433 100644 --- a/docs/adr/0005-ecs-strategy.md +++ b/docs/adr/0005-ecs-strategy.md @@ -32,3 +32,20 @@ Prototype grows into homemade ECS, ID exhaustion, view invalidation, structural- Compare churn/query/memory/order/diagnostics on sparse/dense workloads before adoption. Defer structural edits to stable phases. Reject exhausted/stale IDs; generational reuse needs separate design. [EnTT](https://github.com/skypjack/entt), [Flecs](https://github.com/SanderMertens/flecs). +## M1.5 measurement update (2026-09-19) + +ADR-0021 adds an isolated EnTT4 comparison, with full byte-equivalence, +closed-form arithmetic, retired-ID and restore tests. See the +[protocol and results](../engineering/ECS_EVALUATION.md). + +Preliminary Windows evidence supports **retaining sorted rows for the bounded +proof and upcoming scheduler contract**, not adopting a general ECS yet. At +10000 entities, EnTT substantially reduces individual removal/replacement cost; +the row adapter uses less live container memory and has cheaper updates/export +in these small fixed-component workloads. These are tradeoffs, not a universal +ranking. Linux results and sanitizer evidence are recorded with the experiment. + +The production decision remains deferred until variable component sizes, +capability add/remove, high churn over long lifetimes, interacting systems and +bounded external-ID mapping are measured. Compare EnTT groups and batched row +structural updates then. No public EnTT handles or dependency enter libevo. diff --git a/docs/adr/0021-ecs-evaluation.md b/docs/adr/0021-ecs-evaluation.md new file mode 100644 index 0000000..b9e5e59 --- /dev/null +++ b/docs/adr/0021-ecs-evaluation.md @@ -0,0 +1,71 @@ +# ADR-0021: Isolated EnTT comparison + +## Status + +Accepted for M1.5 measurement only. No production ECS adoption or public API change. + +## Context + +ADR-0005 requires evidence before the bounded sorted-row proof becomes a general +ECS. We need sparse multi-component queries and structural churn measurements, +including deterministic export cost rather than raw iteration alone. + +## Decision + +Compare a sorted composed-row adapter with EnTT 4.0.0 in an opt-in experiment. +Use EnTT commit 85c6bba014049b5de8fad49d25424df2f1f6a8c1, single header SHA256 +eedcb83fedffe640334ef69a0647625a186cf1daeb333b21b0a1445f01e89b6c and MIT license +SHA256 73547f6370ee16ea560665329bb62d4e2d8250f41eaa204b47a6c694ca3286ea. +An explicit preparation command downloads/validates both; configuration is +offline and rejects a mismatched header. Default builds do not use EnTT. + +The experiment owns adapters and monotonic external IDs. EnTT handles never enter +simulation contracts or snapshots. Use a tracking memory resource for container +allocation requests; report its scope and limitations. Compare canonical bytes +and independent closed-form counter results before accepting timing samples. +See [protocol](../engineering/ECS_EVALUATION.md). + +## Alternatives Considered + +Flecs is a maintained MIT C/C++ candidate with relationships and scheduling. +Those features exceed this first storage/query experiment and would confound a +small adoption comparison. It remains an alternative if EnTT's adapter cost or +query model fails later needs; no performance ranking against Flecs is claimed. +Writing an internal sparse-set framework now would prejudge the experiment. + +## Benefits + +EnTT is C++-native, header-only, and documents views, lifetime and allocator +support. Its July 2026 release supplies current maintenance evidence. The +experimental adapter can be removed without migrating game state. + +## Costs + +Template compile time, larger experimental executable, an explicit network +preparation step and retained MIT attribution. This single-header experiment +does not select a general package manager; ADR-0003's compiled-dependency gate +still applies. Only the benchmark includes the dependency. + +## Risks + +Independent updates understate deterministic ordering cost for interacting +organisms. Small synthetic components may misrepresent real genome/phenotype +layouts. Allocator counts exclude process RSS, stack, allocator bookkeeping and +snapshot scratch buffers. Seven samples on a shared host are descriptive, not +statistical capacity promises. EnTT's native handle reuse is not persistent ID +reuse; the adapter must enforce the distinction. + +## Consequences + +Run release correctness checks, sanitizers and both supported compiler families. +Record raw samples, toolchain, binary/build observations, canonical equivalence +and limitations. Make no production adoption from one fast loop. Foundation +maintainers own updates/removal; keep third-party warnings outside owned code. +MIT notices remain in the prepared dependency directory and any redistributed +experimental binary's accompanying materials. No known-vulnerability absence is +asserted merely from version selection. + +Sources: [EnTT release](https://github.com/skypjack/entt/releases/tag/v4.0.0), +[pinned source](https://github.com/skypjack/entt/tree/85c6bba014049b5de8fad49d25424df2f1f6a8c1), +[ECS contracts](https://github.com/skypjack/entt/wiki/Entity-Component-System), +[Flecs](https://github.com/SanderMertens/flecs). diff --git a/docs/adr/README.md b/docs/adr/README.md index 0c6cf3a..78c7244 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -13,6 +13,7 @@ ticks, randomness, serialization, units, errors, threads, logging, PostgreSQL, messaging and metrics. 0016–0019 cover durability/ownership, networking/security, infrastructure and voxels. 0020 scopes Gitea delivery and the stateless dev Job. Each record has the eight required sections. +0021 authorizes the opt-in ECS comparison only, not a production dependency. Every dependency adoption records exact version/commit and integrity hash, transitive license obligations, maintenance/security review, owner and removal diff --git a/docs/engineering/ECS_EVALUATION.md b/docs/engineering/ECS_EVALUATION.md new file mode 100644 index 0000000..d6e503f --- /dev/null +++ b/docs/engineering/ECS_EVALUATION.md @@ -0,0 +1,124 @@ +# M1.5 ECS evaluation protocol + +Scope: synthetic composition storage, not biological rules. Public SimulationWorld, +its golden fingerprint, rules version and snapshot format remain unchanged. + +## Workload and controls + +Both adapters store a stable u64 ID and counter, with optional Motion and Capture +counter components. These are workload labels, not organism classes or energy. +Seed 123456 selects capabilities independently at 10% and 100% prevalence. +Run 1000 and 10000 entities, 100 update passes, one warmup and seven measured +samples. Alternate adapter order between samples to reduce simple ordering bias. + +Both adapters use three ordered system passes per tick. One adds 3 to every +common counter; the next adds Motion.rate then increments that +rate by 1, and adds Capture.rate then increments it by 2. These independent +integer operations have a closed-form reference. No wall clock enters state. +After updates, remove every tenth original entity and create the same number +with new monotonic IDs. Each adapter rejects stale IDs and preserves external +identity despite storage relocation/native handle recycling. + +Measure population creation (excluding the empty store constructor), updates, +churn, canonical encoding and fresh restore +separately. Export sorts by external ID and explicitly encodes fields little +endian. Decode checks exact size, bounded count, ID ordering and capability +flags. This in-memory experimental format is not production persistence. +Compare full bytes after every correctness phase and after restore/continuation; +fingerprints in reports are convenient checks, not substitutes for equality. + +The row adapter extends the proof's sorted-row strategy with optional fields; +it is not a benchmark of the unmodified production binary. EnTT uses sparse +views and an external-ID-to-handle vector. That map's allocation is counted. +Neither adapter reserves ahead to conceal growth; both start empty. No owning +groups or per-tick sorting is applied because updates do not interact across +entities. A production interaction scheduler must specify stable conflict order. + +EnTT receives a small allocator-traits bridge backed by the counting resource. +Direct polymorphic_allocator failed compilation because allocator-aware storage +construction received a second injected allocator. The bridge preserves normal +allocator-traits construction and tracks rebound allocations; it is benchmark +instrumentation, not a new production allocator. Lifetime-balance tests apply +to both adapters. Allocation failures abort the experiment at its host boundary; +these internal fixture adapters do not claim transactional recovery. + +External IDs are capped at 200000 in this experiment. The EnTT lookup vector +retains space for retired IDs until store destruction. This is deliberately +measured, not an acceptable unbounded mapping for a persistent region. A +production EnTT adapter would need a separately evaluated bounded/sparse map. + +## Memory and evidence + +Track allocation calls, live requested bytes and peak requested bytes for store +containers through a dedicated std::pmr resource. Exclude canonical snapshot +scratch/output, stack, allocator metadata and process RSS. Check all tracked +bytes return to zero on adapter destruction. Do not call this total memory. +Report construction/iteration/churn/export/restore timings for every sample, +seed, count, density, compiler and build type; summarize medians and ranges. +Measure cold/warm target build time and executable size separately. Do not +compare wall times between Windows and Linux as if hosts/flags were controlled. + +## Acceptance and decision + +Correctness gates: hand-checkable arithmetic fixture; density/seed ensembles; +empty/singleton stores; stale/duplicate identities; churn; byte equality; +malformed codec inputs; restore then continued equality; allocator balance. +Timing has no CI threshold. Regression checks remain active under NDEBUG. + +Prefer the simpler strategy when it satisfies measured needs. A candidate's +query speed cannot excuse unstable IDs, hidden iteration-order semantics, +unbounded adapter retention or more memory than the region budget supports. +Document the measured recommendation in ADR-0005; any later production adapter +needs a separate contract review and unchanged replay tests. + +## Reproduction + +After initializing the compiler environment (Windows: run the existing check +helper once in the same shell): + +```sh +cmake -P tools/prepare-entt.cmake +cmake --preset release -B build/ecs -DEVO_BUILD_ECS_EXPERIMENT=ON +cmake --build build/ecs --target evo-ecs-experiment --parallel 2 +ctest --test-dir build/ecs -R ecs-experiment --output-on-failure +./build/ecs/evo-ecs-experiment --measure +``` + +Preparation is the only network step. Repeating configure/build uses the verified +local header offline. Use .exe on Windows. Never place EnTT headers in include/evo. + +## Windows observations, 2026-09-19 + +MSVC 19.44.35217.0 (/O2, NDEBUG), CMake 3.31.6, Ninja 1.12.1; Windows +10.0.26200 on Ryzen 7 9800X3D (8 cores / 16 logical processors). Seven samples +after warmup; [raw JSONL](results/ecs-msvc1944.jsonl) includes every sample and +fingerprint. Measurements were recorded after compiler jobs completed. There +is no controlled CPU isolation or portable performance threshold. + +10000 entities, median elapsed milliseconds; update column is **100 ticks**, +churn removes/replaces 1000 individuals. Memory is live requested container bytes. + +| Store / density | Create ms | Update ms | Churn ms | Export ms | Restore ms | Live bytes | +|---|---:|---:|---:|---:|---:|---:| +| Rows / 10% | 0.3366 | 0.7365 | 1.3883 | 0.3019 | 0.3056 | 485520 | +| EnTT4 / 10% | 0.4178 | 0.9868 | 0.0952 | 0.6435 | 0.5067 | 638608 | +| Rows / 100% | 0.2699 | 0.9237 | 1.3829 | 0.2873 | 0.2561 | 485520 | +| EnTT4 / 100% | 0.5777 | 7.0615 | 0.1399 | 0.6269 | 0.6842 | 866632 | + +All paired samples have identical canonical bytes. EnTT wins this individual +churn case by roughly 10–15x; rows win the measured simple updates/export and +use fewer live bytes. EnTT's peak bytes can be lower than the row vector's +temporary reallocation peak: at 10% density, 638608 versus 809200. Do not equate +live bytes, peak requested bytes and process RSS. + +The combined comparison executable is 99328 bytes; it contains both adapters +and verification code, so this is not an isolated EnTT binary-size delta. +Clean target build observed 2.7621 seconds, warm no-op 0.0475 seconds +(configuration excluded; OS file cache not flushed). +No claim of allocator-overhead/RSS measurement is made. + +Recommendation: keep the current bounded row proof while designing the +deterministic scheduler. Preserve a future adapter boundary. Revisit with real +component sizes, dynamic capabilities, interacting systems, EnTT groups, batched +structural changes and a bounded persistent-ID mapping before production adoption. +Do not extrapolate these synthetic counts into organism/player capacity. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index e000ef8..9cb9fbc 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -99,3 +99,22 @@ Registry digest: The dev manifest pins this digest. This proves build/test/image delivery; Argo application mapping and Kubernetes Job completion remain unverified locally. + +## M1.5 native ECS experiment + +Opt-in target evo-ecs-experiment (EnTT4 pinned in ADR-0021), based on 1c6e6e1 plus +this experiment's changes. MSVC Release contract checks passed, including 72 +seed/density/count combinations, hand-calculated arithmetic, canonical equality, +restore continuation, retired-ID rejection, malformed snapshot checks and +allocator balance. The final clang-tidy build/test passed after tightening the +process-boundary diagnostic catch. MSVC ASan passed on the final source. +Baseline Release CTest remained 5/5 +and format-check included the experiment. No simulation rule/golden changed. + +Commands: configure release/analysis/asan with `-B build/ecs[-analysis|-asan] +-DEVO_BUILD_ECS_EXPERIMENT=ON`, build target evo-ecs-experiment, then +`ctest --test-dir -R ecs-experiment --output-on-failure`. +Seven measured samples plus warmup are retained in results/ecs-msvc1944.jsonl; +protocol, build observations and scope limits are in ECS_EVALUATION.md. The +Linux experiment CI job is pending its first pushed run; no Linux experiment +success is claimed yet. diff --git a/docs/engineering/results/ecs-msvc1944.jsonl b/docs/engineering/results/ecs-msvc1944.jsonl new file mode 100644 index 0000000..ba0f4d3 --- /dev/null +++ b/docs/engineering/results/ecs-msvc1944.jsonl @@ -0,0 +1,57 @@ +{"experiment":"ecs-storage-v1","entt":"4.0.0","compiler":"MSVC 1944","build":"release"} +{"store":"rows","entities":1000,"density_percent":10,"seed":123456,"ticks":100,"sample":0,"create_s":2.81e-05,"tick_s":6.63e-05,"churn_s":1.32e-05,"export_s":4.53e-05,"restore_s":4.25e-05,"live_bytes":42640,"peak_bytes":71080,"allocations":18,"hash":"ae6c58eaaffcbb17"} +{"store":"entt4","entities":1000,"density_percent":10,"seed":123456,"ticks":100,"sample":0,"create_s":7.81e-05,"tick_s":9.88e-05,"churn_s":1.1e-05,"export_s":7.13e-05,"restore_s":9.6e-05,"live_bytes":136420,"peak_bytes":140684,"allocations":127,"hash":"ae6c58eaaffcbb17"} +{"store":"entt4","entities":1000,"density_percent":10,"seed":123456,"ticks":100,"sample":1,"create_s":7.7e-05,"tick_s":9.98e-05,"churn_s":1.01e-05,"export_s":5.91e-05,"restore_s":8.8e-05,"live_bytes":136420,"peak_bytes":140684,"allocations":127,"hash":"ae6c58eaaffcbb17"} +{"store":"rows","entities":1000,"density_percent":10,"seed":123456,"ticks":100,"sample":1,"create_s":2.88e-05,"tick_s":6.55e-05,"churn_s":1.29e-05,"export_s":4.55e-05,"restore_s":4.63e-05,"live_bytes":42640,"peak_bytes":71080,"allocations":18,"hash":"ae6c58eaaffcbb17"} +{"store":"rows","entities":1000,"density_percent":10,"seed":123456,"ticks":100,"sample":2,"create_s":2.77e-05,"tick_s":6.49e-05,"churn_s":1.19e-05,"export_s":4.58e-05,"restore_s":5.06e-05,"live_bytes":42640,"peak_bytes":71080,"allocations":18,"hash":"ae6c58eaaffcbb17"} +{"store":"entt4","entities":1000,"density_percent":10,"seed":123456,"ticks":100,"sample":2,"create_s":9.09e-05,"tick_s":9.79e-05,"churn_s":1.07e-05,"export_s":6.93e-05,"restore_s":8.76e-05,"live_bytes":136420,"peak_bytes":140684,"allocations":127,"hash":"ae6c58eaaffcbb17"} +{"store":"entt4","entities":1000,"density_percent":10,"seed":123456,"ticks":100,"sample":3,"create_s":7.86e-05,"tick_s":9.56e-05,"churn_s":9.9e-06,"export_s":5.7e-05,"restore_s":9.21e-05,"live_bytes":136420,"peak_bytes":140684,"allocations":127,"hash":"ae6c58eaaffcbb17"} +{"store":"rows","entities":1000,"density_percent":10,"seed":123456,"ticks":100,"sample":3,"create_s":5.1e-05,"tick_s":6.47e-05,"churn_s":1.25e-05,"export_s":4.22e-05,"restore_s":5.37e-05,"live_bytes":42640,"peak_bytes":71080,"allocations":18,"hash":"ae6c58eaaffcbb17"} +{"store":"rows","entities":1000,"density_percent":10,"seed":123456,"ticks":100,"sample":4,"create_s":4.27e-05,"tick_s":6.41e-05,"churn_s":1.17e-05,"export_s":3.95e-05,"restore_s":4.42e-05,"live_bytes":42640,"peak_bytes":71080,"allocations":18,"hash":"ae6c58eaaffcbb17"} +{"store":"entt4","entities":1000,"density_percent":10,"seed":123456,"ticks":100,"sample":4,"create_s":8.94e-05,"tick_s":9.84e-05,"churn_s":9.8e-06,"export_s":6.44e-05,"restore_s":8.1e-05,"live_bytes":136420,"peak_bytes":140684,"allocations":127,"hash":"ae6c58eaaffcbb17"} +{"store":"entt4","entities":1000,"density_percent":10,"seed":123456,"ticks":100,"sample":5,"create_s":7.87e-05,"tick_s":9.89e-05,"churn_s":9.7e-06,"export_s":5.17e-05,"restore_s":8.32e-05,"live_bytes":136420,"peak_bytes":140684,"allocations":127,"hash":"ae6c58eaaffcbb17"} +{"store":"rows","entities":1000,"density_percent":10,"seed":123456,"ticks":100,"sample":5,"create_s":3.96e-05,"tick_s":6.39e-05,"churn_s":1.15e-05,"export_s":5.08e-05,"restore_s":4.27e-05,"live_bytes":42640,"peak_bytes":71080,"allocations":18,"hash":"ae6c58eaaffcbb17"} +{"store":"rows","entities":1000,"density_percent":10,"seed":123456,"ticks":100,"sample":6,"create_s":4.27e-05,"tick_s":6.36e-05,"churn_s":1.13e-05,"export_s":3.76e-05,"restore_s":3.52e-05,"live_bytes":42640,"peak_bytes":71080,"allocations":18,"hash":"ae6c58eaaffcbb17"} +{"store":"entt4","entities":1000,"density_percent":10,"seed":123456,"ticks":100,"sample":6,"create_s":7.03e-05,"tick_s":9.59e-05,"churn_s":9.7e-06,"export_s":5.69e-05,"restore_s":8.19e-05,"live_bytes":136420,"peak_bytes":140684,"allocations":127,"hash":"ae6c58eaaffcbb17"} +{"store":"rows","entities":1000,"density_percent":100,"seed":123456,"ticks":100,"sample":0,"create_s":2.94e-05,"tick_s":8.8e-05,"churn_s":1.12e-05,"export_s":3.37e-05,"restore_s":3.32e-05,"live_bytes":42640,"peak_bytes":71080,"allocations":18,"hash":"c18e5db8d060af79"} +{"store":"entt4","entities":1000,"density_percent":100,"seed":123456,"ticks":100,"sample":0,"create_s":8.48e-05,"tick_s":0.0006977,"churn_s":1.57e-05,"export_s":5.87e-05,"restore_s":9.88e-05,"live_bytes":143820,"peak_bytes":148084,"allocations":137,"hash":"c18e5db8d060af79"} +{"store":"entt4","entities":1000,"density_percent":100,"seed":123456,"ticks":100,"sample":1,"create_s":8.11e-05,"tick_s":0.0006962,"churn_s":1.45e-05,"export_s":5.14e-05,"restore_s":8.87e-05,"live_bytes":143820,"peak_bytes":148084,"allocations":137,"hash":"c18e5db8d060af79"} +{"store":"rows","entities":1000,"density_percent":100,"seed":123456,"ticks":100,"sample":1,"create_s":2.91e-05,"tick_s":8.79e-05,"churn_s":1.17e-05,"export_s":3.23e-05,"restore_s":2.73e-05,"live_bytes":42640,"peak_bytes":71080,"allocations":18,"hash":"c18e5db8d060af79"} +{"store":"rows","entities":1000,"density_percent":100,"seed":123456,"ticks":100,"sample":2,"create_s":2.87e-05,"tick_s":8.78e-05,"churn_s":1.13e-05,"export_s":3.32e-05,"restore_s":2.68e-05,"live_bytes":42640,"peak_bytes":71080,"allocations":18,"hash":"c18e5db8d060af79"} +{"store":"entt4","entities":1000,"density_percent":100,"seed":123456,"ticks":100,"sample":2,"create_s":8.22e-05,"tick_s":0.0006988,"churn_s":1.56e-05,"export_s":5.53e-05,"restore_s":8.88e-05,"live_bytes":143820,"peak_bytes":148084,"allocations":137,"hash":"c18e5db8d060af79"} +{"store":"entt4","entities":1000,"density_percent":100,"seed":123456,"ticks":100,"sample":3,"create_s":8.08e-05,"tick_s":0.0006977,"churn_s":1.46e-05,"export_s":4.99e-05,"restore_s":8.7e-05,"live_bytes":143820,"peak_bytes":148084,"allocations":137,"hash":"c18e5db8d060af79"} +{"store":"rows","entities":1000,"density_percent":100,"seed":123456,"ticks":100,"sample":3,"create_s":2.95e-05,"tick_s":8.79e-05,"churn_s":1.14e-05,"export_s":3.07e-05,"restore_s":2.69e-05,"live_bytes":42640,"peak_bytes":71080,"allocations":18,"hash":"c18e5db8d060af79"} +{"store":"rows","entities":1000,"density_percent":100,"seed":123456,"ticks":100,"sample":4,"create_s":2.97e-05,"tick_s":8.8e-05,"churn_s":1.12e-05,"export_s":3.29e-05,"restore_s":2.63e-05,"live_bytes":42640,"peak_bytes":71080,"allocations":18,"hash":"c18e5db8d060af79"} +{"store":"entt4","entities":1000,"density_percent":100,"seed":123456,"ticks":100,"sample":4,"create_s":8.1e-05,"tick_s":0.0007039,"churn_s":1.56e-05,"export_s":5.57e-05,"restore_s":8.82e-05,"live_bytes":143820,"peak_bytes":148084,"allocations":137,"hash":"c18e5db8d060af79"} +{"store":"entt4","entities":1000,"density_percent":100,"seed":123456,"ticks":100,"sample":5,"create_s":7.98e-05,"tick_s":0.0007589,"churn_s":1.52e-05,"export_s":5.54e-05,"restore_s":8.72e-05,"live_bytes":143820,"peak_bytes":148084,"allocations":137,"hash":"c18e5db8d060af79"} +{"store":"rows","entities":1000,"density_percent":100,"seed":123456,"ticks":100,"sample":5,"create_s":2.91e-05,"tick_s":9e-05,"churn_s":1.19e-05,"export_s":3.06e-05,"restore_s":2.75e-05,"live_bytes":42640,"peak_bytes":71080,"allocations":18,"hash":"c18e5db8d060af79"} +{"store":"rows","entities":1000,"density_percent":100,"seed":123456,"ticks":100,"sample":6,"create_s":2.93e-05,"tick_s":8.73e-05,"churn_s":1.12e-05,"export_s":3.34e-05,"restore_s":2.73e-05,"live_bytes":42640,"peak_bytes":71080,"allocations":18,"hash":"c18e5db8d060af79"} +{"store":"entt4","entities":1000,"density_percent":100,"seed":123456,"ticks":100,"sample":6,"create_s":7.99e-05,"tick_s":0.0007663,"churn_s":1.54e-05,"export_s":5.61e-05,"restore_s":8.8e-05,"live_bytes":143820,"peak_bytes":148084,"allocations":137,"hash":"c18e5db8d060af79"} +{"store":"rows","entities":10000,"density_percent":10,"seed":123456,"ticks":100,"sample":0,"create_s":0.0003536,"tick_s":0.0007323,"churn_s":0.0013903,"export_s":0.0003548,"restore_s":0.0003743,"live_bytes":485520,"peak_bytes":809200,"allocations":24,"hash":"1e0aaac006ff1a06"} +{"store":"entt4","entities":10000,"density_percent":10,"seed":123456,"ticks":100,"sample":0,"create_s":0.0004459,"tick_s":0.001013,"churn_s":9.47e-05,"export_s":0.0006435,"restore_s":0.0005379,"live_bytes":638608,"peak_bytes":638608,"allocations":212,"hash":"1e0aaac006ff1a06"} +{"store":"entt4","entities":10000,"density_percent":10,"seed":123456,"ticks":100,"sample":1,"create_s":0.0004167,"tick_s":0.0009827,"churn_s":9.84e-05,"export_s":0.0006469,"restore_s":0.0005008,"live_bytes":638608,"peak_bytes":638608,"allocations":212,"hash":"1e0aaac006ff1a06"} +{"store":"rows","entities":10000,"density_percent":10,"seed":123456,"ticks":100,"sample":1,"create_s":0.0003366,"tick_s":0.0007453,"churn_s":0.001388,"export_s":0.0003228,"restore_s":0.000392,"live_bytes":485520,"peak_bytes":809200,"allocations":24,"hash":"1e0aaac006ff1a06"} +{"store":"rows","entities":10000,"density_percent":10,"seed":123456,"ticks":100,"sample":2,"create_s":0.000375,"tick_s":0.0007312,"churn_s":0.0013888,"export_s":0.0002883,"restore_s":0.000289,"live_bytes":485520,"peak_bytes":809200,"allocations":24,"hash":"1e0aaac006ff1a06"} +{"store":"entt4","entities":10000,"density_percent":10,"seed":123456,"ticks":100,"sample":2,"create_s":0.0004178,"tick_s":0.0009963,"churn_s":9.48e-05,"export_s":0.0006384,"restore_s":0.0004966,"live_bytes":638608,"peak_bytes":638608,"allocations":212,"hash":"1e0aaac006ff1a06"} +{"store":"entt4","entities":10000,"density_percent":10,"seed":123456,"ticks":100,"sample":3,"create_s":0.0004159,"tick_s":0.0010396,"churn_s":9.85e-05,"export_s":0.0006736,"restore_s":0.000545,"live_bytes":638608,"peak_bytes":638608,"allocations":212,"hash":"1e0aaac006ff1a06"} +{"store":"rows","entities":10000,"density_percent":10,"seed":123456,"ticks":100,"sample":3,"create_s":0.0003283,"tick_s":0.0008778,"churn_s":0.0013898,"export_s":0.0003027,"restore_s":0.0002713,"live_bytes":485520,"peak_bytes":809200,"allocations":24,"hash":"1e0aaac006ff1a06"} +{"store":"rows","entities":10000,"density_percent":10,"seed":123456,"ticks":100,"sample":4,"create_s":0.0003473,"tick_s":0.0007322,"churn_s":0.0013875,"export_s":0.0002874,"restore_s":0.0003056,"live_bytes":485520,"peak_bytes":809200,"allocations":24,"hash":"1e0aaac006ff1a06"} +{"store":"entt4","entities":10000,"density_percent":10,"seed":123456,"ticks":100,"sample":4,"create_s":0.0004432,"tick_s":0.0009824,"churn_s":9.52e-05,"export_s":0.0006572,"restore_s":0.0004959,"live_bytes":638608,"peak_bytes":638608,"allocations":212,"hash":"1e0aaac006ff1a06"} +{"store":"entt4","entities":10000,"density_percent":10,"seed":123456,"ticks":100,"sample":5,"create_s":0.0004146,"tick_s":0.0009868,"churn_s":9.52e-05,"export_s":0.0006328,"restore_s":0.000528,"live_bytes":638608,"peak_bytes":638608,"allocations":212,"hash":"1e0aaac006ff1a06"} +{"store":"rows","entities":10000,"density_percent":10,"seed":123456,"ticks":100,"sample":5,"create_s":0.0003105,"tick_s":0.0007455,"churn_s":0.0013883,"export_s":0.0003019,"restore_s":0.0002687,"live_bytes":485520,"peak_bytes":809200,"allocations":24,"hash":"1e0aaac006ff1a06"} +{"store":"rows","entities":10000,"density_percent":10,"seed":123456,"ticks":100,"sample":6,"create_s":0.0002835,"tick_s":0.0007365,"churn_s":0.0013849,"export_s":0.0002923,"restore_s":0.0003155,"live_bytes":485520,"peak_bytes":809200,"allocations":24,"hash":"1e0aaac006ff1a06"} +{"store":"entt4","entities":10000,"density_percent":10,"seed":123456,"ticks":100,"sample":6,"create_s":0.000445,"tick_s":0.0009866,"churn_s":9.48e-05,"export_s":0.0006257,"restore_s":0.0005067,"live_bytes":638608,"peak_bytes":638608,"allocations":212,"hash":"1e0aaac006ff1a06"} +{"store":"rows","entities":10000,"density_percent":100,"seed":123456,"ticks":100,"sample":0,"create_s":0.000264,"tick_s":0.0009358,"churn_s":0.0013822,"export_s":0.0002945,"restore_s":0.000296,"live_bytes":485520,"peak_bytes":809200,"allocations":24,"hash":"56e63b67590e4cf9"} +{"store":"entt4","entities":10000,"density_percent":100,"seed":123456,"ticks":100,"sample":0,"create_s":0.0005764,"tick_s":0.0071502,"churn_s":0.0001714,"export_s":0.0006253,"restore_s":0.0006842,"live_bytes":866632,"peak_bytes":866632,"allocations":252,"hash":"56e63b67590e4cf9"} +{"store":"entt4","entities":10000,"density_percent":100,"seed":123456,"ticks":100,"sample":1,"create_s":0.0005531,"tick_s":0.0070694,"churn_s":0.0001399,"export_s":0.0006572,"restore_s":0.0007577,"live_bytes":866632,"peak_bytes":866632,"allocations":252,"hash":"56e63b67590e4cf9"} +{"store":"rows","entities":10000,"density_percent":100,"seed":123456,"ticks":100,"sample":1,"create_s":0.0002631,"tick_s":0.0009242,"churn_s":0.0013859,"export_s":0.0002801,"restore_s":0.0002415,"live_bytes":485520,"peak_bytes":809200,"allocations":24,"hash":"56e63b67590e4cf9"} +{"store":"rows","entities":10000,"density_percent":100,"seed":123456,"ticks":100,"sample":2,"create_s":0.0003084,"tick_s":0.0009203,"churn_s":0.0013826,"export_s":0.0002853,"restore_s":0.0002531,"live_bytes":485520,"peak_bytes":809200,"allocations":24,"hash":"56e63b67590e4cf9"} +{"store":"entt4","entities":10000,"density_percent":100,"seed":123456,"ticks":100,"sample":2,"create_s":0.0005777,"tick_s":0.0071149,"churn_s":0.0001429,"export_s":0.0006525,"restore_s":0.0007687,"live_bytes":866632,"peak_bytes":866632,"allocations":252,"hash":"56e63b67590e4cf9"} +{"store":"entt4","entities":10000,"density_percent":100,"seed":123456,"ticks":100,"sample":3,"create_s":0.0005635,"tick_s":0.0070522,"churn_s":0.0001399,"export_s":0.0006252,"restore_s":0.0006686,"live_bytes":866632,"peak_bytes":866632,"allocations":252,"hash":"56e63b67590e4cf9"} +{"store":"rows","entities":10000,"density_percent":100,"seed":123456,"ticks":100,"sample":3,"create_s":0.0002591,"tick_s":0.0009237,"churn_s":0.0016731,"export_s":0.0002873,"restore_s":0.0002896,"live_bytes":485520,"peak_bytes":809200,"allocations":24,"hash":"56e63b67590e4cf9"} +{"store":"rows","entities":10000,"density_percent":100,"seed":123456,"ticks":100,"sample":4,"create_s":0.0003584,"tick_s":0.0009218,"churn_s":0.0013859,"export_s":0.0003422,"restore_s":0.0002999,"live_bytes":485520,"peak_bytes":809200,"allocations":24,"hash":"56e63b67590e4cf9"} +{"store":"entt4","entities":10000,"density_percent":100,"seed":123456,"ticks":100,"sample":4,"create_s":0.0005855,"tick_s":0.0070491,"churn_s":0.0001392,"export_s":0.0006269,"restore_s":0.0006574,"live_bytes":866632,"peak_bytes":866632,"allocations":252,"hash":"56e63b67590e4cf9"} +{"store":"entt4","entities":10000,"density_percent":100,"seed":123456,"ticks":100,"sample":5,"create_s":0.0005805,"tick_s":0.0070615,"churn_s":0.0001407,"export_s":0.0006198,"restore_s":0.0006525,"live_bytes":866632,"peak_bytes":866632,"allocations":252,"hash":"56e63b67590e4cf9"} +{"store":"rows","entities":10000,"density_percent":100,"seed":123456,"ticks":100,"sample":5,"create_s":0.0002699,"tick_s":0.0009311,"churn_s":0.0013829,"export_s":0.0002835,"restore_s":0.0002378,"live_bytes":485520,"peak_bytes":809200,"allocations":24,"hash":"56e63b67590e4cf9"} +{"store":"rows","entities":10000,"density_percent":100,"seed":123456,"ticks":100,"sample":6,"create_s":0.0003301,"tick_s":0.0009232,"churn_s":0.0013792,"export_s":0.0003064,"restore_s":0.0002561,"live_bytes":485520,"peak_bytes":809200,"allocations":24,"hash":"56e63b67590e4cf9"} +{"store":"entt4","entities":10000,"density_percent":100,"seed":123456,"ticks":100,"sample":6,"create_s":0.0005805,"tick_s":0.0070573,"churn_s":0.0001395,"export_s":0.0006499,"restore_s":0.000742,"live_bytes":866632,"peak_bytes":866632,"allocations":252,"hash":"56e63b67590e4cf9"} diff --git a/experiments/ecs/comparison.cpp b/experiments/ecs/comparison.cpp new file mode 100644 index 0000000..3749c62 --- /dev/null +++ b/experiments/ecs/comparison.cpp @@ -0,0 +1,503 @@ +#include "evo/core/random.hpp" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { +constexpr std::uint64_t max_id = 200'000; +constexpr std::size_t max_count = 100'000; +constexpr std::uint64_t seed = 123456; + +void require(bool condition, const char* message) { + if (!condition) { + throw std::runtime_error(message); + } +} + +struct Record { + std::uint64_t id{}, counter{}, motion{}, capture{}; + std::uint8_t mask{}; + bool operator==(const Record&) const = default; +}; +struct State { + std::uint64_t high_water{}; + std::vector rows; + bool operator==(const State&) const = default; +}; +bool valid(const Record& row) { + return row.id > 0 && row.id <= max_id && row.mask <= 3 && + ((row.mask & 1U) != 0 || row.motion == 0) && ((row.mask & 2U) != 0 || row.capture == 0); +} +Record initial(std::uint64_t id, std::uint64_t world_seed, std::uint32_t density) { + evo::Random rng{world_seed ^ id}; + const bool motion = rng.next() % 100 < density; + const bool capture = rng.next() % 100 < density; + return {id, id * 7, motion ? 2U : 0U, capture ? 5U : 0U, + static_cast((motion ? 1U : 0U) | (capture ? 2U : 0U))}; +} + +// Records requested container bytes, not allocator bookkeeping or process RSS. +class CountingResource final : public std::pmr::memory_resource { + public: + std::size_t live{}, peak{}, calls{}; + + private: + void* do_allocate(std::size_t bytes, std::size_t alignment) override { + auto* result = std::pmr::new_delete_resource()->allocate(bytes, alignment); + live += bytes; + peak = std::max(peak, live); + ++calls; + return result; + } + void do_deallocate(void* pointer, std::size_t bytes, std::size_t alignment) override { + if (bytes > live) { + std::terminate(); + } + live -= bytes; + std::pmr::new_delete_resource()->deallocate(pointer, bytes, alignment); + } + bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override { + return this == &other; + } +}; + +class Rows { + public: + explicit Rows(std::pmr::memory_resource& resource) : rows_{&resource} {} + void create(Record row) { + require(valid(row) && row.id > high_water_ && rows_.size() < max_count, + "Invalid row creation"); + rows_.push_back(row); + high_water_ = row.id; + } + bool remove(std::uint64_t id) { + const auto it = std::ranges::lower_bound(rows_, id, {}, &Record::id); + if (it == rows_.end() || it->id != id) { + return false; + } + rows_.erase(it); + return true; + } + void tick() { + for (auto& row : rows_) { + row.counter += 3; + } + for (auto& row : rows_) { + if ((row.mask & 1U) != 0) { + row.counter += row.motion++; + } + } + for (auto& row : rows_) { + if ((row.mask & 2U) != 0) { + row.counter += row.capture; + row.capture += 2; + } + } + } + State state() const { + return {high_water_, {rows_.begin(), rows_.end()}}; + } + void restore_high_water(std::uint64_t value) { + require(value >= high_water_ && value <= max_id, "Invalid row high-water mark"); + high_water_ = value; + } + + private: + std::pmr::vector rows_; + std::uint64_t high_water_{}; +}; + +struct Identity { + std::uint64_t value; +}; +struct Counter { + std::uint64_t value; +}; +struct Motion { + std::uint64_t rate; +}; +struct Capture { + std::uint64_t rate; +}; +// EnTT passes storage allocators explicitly. A standard allocator-traits bridge +// avoids polymorphic_allocator injecting a second allocator during construction. +template struct ResourceAllocator { + using value_type = T; + std::pmr::memory_resource* resource = std::pmr::get_default_resource(); + ResourceAllocator() = default; + explicit ResourceAllocator(std::pmr::memory_resource& value) : resource{&value} {} + template + ResourceAllocator(const ResourceAllocator& other) noexcept : resource{other.resource} {} + T* allocate(std::size_t count) { + return std::pmr::polymorphic_allocator{resource}.allocate(count); + } + void deallocate(T* pointer, std::size_t count) noexcept { + std::pmr::polymorphic_allocator{resource}.deallocate(pointer, count); + } + template bool operator==(const ResourceAllocator& other) const noexcept { + return resource->is_equal(*other.resource); + } +}; +class EnttStore { + public: + explicit EnttStore(std::pmr::memory_resource& resource) + : registry_{ResourceAllocator{resource}}, handles_{&resource} {} + void create(Record row) { + require(valid(row) && row.id > high_water_ && count_ < max_count, "Invalid EnTT creation"); + const auto entity = registry_.create(); + registry_.emplace(entity, row.id); + registry_.emplace(entity, row.counter); + if ((row.mask & 1U) != 0) { + registry_.emplace(entity, row.motion); + } + if ((row.mask & 2U) != 0) { + registry_.emplace(entity, row.capture); + } + handles_.resize(static_cast(row.id + 1), entt::null); + handles_[static_cast(row.id)] = entity; + high_water_ = row.id; + ++count_; + } + bool remove(std::uint64_t id) { + if (id >= handles_.size()) { + return false; + } + auto& entity = handles_[static_cast(id)]; + if (entity == entt::null) { + return false; + } + registry_.destroy(entity); + entity = entt::null; + --count_; + return true; + } + void tick() { + for (auto&& [entity, counter] : registry_.view().each()) { + static_cast(entity); + counter.value += 3; + } + for (auto&& [entity, counter, motion] : registry_.view().each()) { + static_cast(entity); + counter.value += motion.rate++; + } + for (auto&& [entity, counter, capture] : registry_.view().each()) { + static_cast(entity); + counter.value += capture.rate; + capture.rate += 2; + } + } + State state() const { + State result{high_water_, {}}; + result.rows.reserve(count_); + for (auto&& [entity, identity, counter] : registry_.view().each()) { + const auto* motion = registry_.try_get(entity); + const auto* capture = registry_.try_get(entity); + result.rows.push_back( + {identity.value, counter.value, motion ? motion->rate : 0, + capture ? capture->rate : 0, + static_cast((motion ? 1U : 0U) | (capture ? 2U : 0U))}); + } + std::ranges::sort(result.rows, {}, &Record::id); + return result; + } + void restore_high_water(std::uint64_t value) { + require(value >= high_water_ && value <= max_id, "Invalid EnTT high-water mark"); + high_water_ = value; + } + + private: + entt::basic_registry> registry_; + std::pmr::vector handles_; + std::uint64_t high_water_{}; + std::size_t count_{}; +}; + +using Bytes = std::vector; +constexpr std::array magic{'E', 'C', 'S', 'P', 'R', 'F', '0', '1'}; +void put(Bytes& bytes, std::uint64_t value) { + for (unsigned index = 0; index < 8; ++index) { + bytes.push_back(static_cast(value & 255U)); + value >>= 8U; + } +} +Bytes encode(const State& state) { + Bytes bytes{magic.begin(), magic.end()}; + bytes.reserve(24 + state.rows.size() * 33); + put(bytes, state.high_water); + put(bytes, state.rows.size()); + for (const auto& row : state.rows) { + put(bytes, row.id); + put(bytes, row.counter); + put(bytes, row.motion); + put(bytes, row.capture); + bytes.push_back(row.mask); + } + return bytes; +} +enum class DecodeError : std::uint8_t { invalid }; +std::expected decode(std::span bytes) { + if (bytes.size() < 24 || bytes.size() > 24 + 33 * max_count || + !std::ranges::equal(bytes.first(8), magic)) { + return std::unexpected(DecodeError::invalid); + } + std::size_t offset = 8; + const auto get = [&]() { + std::uint64_t value = 0; + for (unsigned shift = 0; shift < 64; shift += 8) { + value |= static_cast(bytes[offset++]) << shift; + } + return value; + }; + const auto high_water = get(); + const auto count = get(); + if (high_water > max_id || count > max_count || bytes.size() != 24 + count * 33) { + return std::unexpected(DecodeError::invalid); + } + State result{high_water, {}}; + result.rows.reserve(static_cast(count)); + std::uint64_t previous = 0; + for (std::uint64_t index = 0; index < count; ++index) { + Record row{get(), get(), get(), get(), bytes[offset++]}; + if (!valid(row) || row.id <= previous || row.id > high_water) { + return std::unexpected(DecodeError::invalid); + } + previous = row.id; + result.rows.push_back(row); + } + return result; +} +template void restore(Store& store, const Bytes& bytes) { + const auto state = decode(bytes); + require(state.has_value(), "Rejected experimental snapshot"); + for (const auto row : state->rows) { + store.create(row); + } + store.restore_high_water(state->high_water); +} +template +void fill(Store& store, std::uint64_t count, std::uint64_t world_seed, std::uint32_t density) { + for (std::uint64_t id = 1; id <= count; ++id) { + store.create(initial(id, world_seed, density)); + } +} +template void advance(Store& store, std::uint64_t ticks) { + for (std::uint64_t tick = 0; tick < ticks; ++tick) { + store.tick(); + } +} +template +void churn(Store& store, std::uint64_t count, std::uint64_t world_seed, std::uint32_t density) { + for (std::uint64_t id = 10; id <= count; id += 10) { + require(store.remove(id), "Churn removal failed"); + } + for (std::uint64_t id = count + 1; id <= count + count / 10; ++id) { + store.create(initial(id, world_seed, density)); + } +} +Record closed_form(Record row, std::uint64_t ticks) { + row.counter += 3 * ticks; + if ((row.mask & 1U) != 0) { + row.counter += ticks * row.motion + ticks * (ticks - 1) / 2; + row.motion += ticks; + } + if ((row.mask & 2U) != 0) { + row.counter += ticks * row.capture + ticks * (ticks - 1); + row.capture += 2 * ticks; + } + return row; +} +template void verify_store() { + CountingResource memory; + { + Store store{memory}; + store.create({1, 10, 2, 5, 3}); + advance(store, 3); + require(store.state().rows == std::vector{{1, 49, 5, 11, 3}}, + "Hand-computed fixture mismatch"); + bool duplicate_rejected = false; + try { + store.create({1, 0, 0, 0, 0}); + } catch (const std::runtime_error&) { + duplicate_rejected = true; + } + require(duplicate_rejected, "Duplicate ID accepted"); + require(store.remove(1) && !store.remove(1), "Stale identity accepted"); + const auto bytes = encode(store.state()); + Store restored{memory}; + restore(restored, bytes); + require(restored.state().high_water == 1, "Deleted high-water mark lost"); + bool reused = true; + try { + restored.create({1, 0, 0, 0, 0}); + } catch (const std::runtime_error&) { + reused = false; + } + require(!reused, "Restore allowed ID reuse"); + restored.create({2, 0, 0, 0, 0}); + } + require(memory.live == 0, "Store allocations leaked"); +} +void verify() { + verify_store(); + verify_store(); + for (const auto count : {0U, 1U, 103U}) { + for (const auto density : {0U, 10U, 100U}) { + for (std::uint64_t test_seed = 0; test_seed < 8; ++test_seed) { + CountingResource rows_memory, entt_memory; + { + Rows rows{rows_memory}; + EnttStore entt{entt_memory}; + fill(rows, count, test_seed, density); + fill(entt, count, test_seed, density); + advance(rows, 17); + advance(entt, 17); + require(rows.state() == entt.state(), "Store state differs after updates"); + for (const auto row : rows.state().rows) { + require(row == closed_form(initial(row.id, test_seed, density), 17), + "Closed-form result differs"); + } + churn(rows, count, test_seed, density); + churn(entt, count, test_seed, density); + const auto bytes = encode(rows.state()); + require(bytes == encode(entt.state()), "Canonical bytes differ after churn"); + Rows resumed_rows{rows_memory}; + EnttStore resumed_entt{entt_memory}; + restore(resumed_rows, bytes); + restore(resumed_entt, bytes); + advance(rows, 5); + advance(entt, 5); + advance(resumed_rows, 5); + advance(resumed_entt, 5); + require(rows.state() == entt.state() && rows.state() == resumed_rows.state() && + rows.state() == resumed_entt.state(), + "Restored continuation differs"); + } + require(rows_memory.live == 0 && entt_memory.live == 0, "Unbalanced allocations"); + } + } + } + auto bytes = encode(State{1, {{1, 0, 0, 0, 0}}}); + require(decode(bytes).has_value(), "Valid codec fixture rejected"); + bytes.back() = 4; + require(!decode(bytes), "Invalid mask accepted"); + bytes.pop_back(); + require(!decode(bytes), "Truncated input accepted"); + require(!decode(encode(State{2, {{1, 0, 0, 0, 0}, {1, 0, 0, 0, 0}}})), + "Duplicate snapshot IDs accepted"); + require(!decode(encode(State{0, {{1, 0, 0, 0, 0}}})), "Invalid watermark accepted"); + require(!decode(encode(State{1, {{1, 0, 1, 0, 0}}})), "Absent capability payload accepted"); + bytes = encode(State{}); + bytes[0] = 0; + require(!decode(bytes), "Unknown format accepted"); + bytes = encode(State{}); + bytes[16] = 255; + require(!decode(bytes), "Invalid count/length accepted"); +} + +template double timed(Function&& function) { + const auto start = std::chrono::steady_clock::now(); + function(); + return std::chrono::duration(std::chrono::steady_clock::now() - start).count(); +} +std::uint64_t fingerprint(const Bytes& bytes) { + std::uint64_t result = 14695981039346656037ULL; + for (const auto value : bytes) { + result = (result ^ value) * 1099511628211ULL; + } + return result; +} +template +Bytes measure(std::string_view name, std::uint64_t count, std::uint32_t density, int sample) { + CountingResource memory, restore_memory; + Bytes bytes; + { + Store store{memory}; + const auto create_seconds = timed([&] { fill(store, count, seed, density); }); + const auto tick_seconds = timed([&] { advance(store, 100); }); + const auto churn_seconds = timed([&] { churn(store, count, seed, density); }); + const auto export_seconds = timed([&] { bytes = encode(store.state()); }); + Store restored{restore_memory}; + const auto restore_seconds = timed([&] { restore(restored, bytes); }); + require(encode(restored.state()) == bytes, "Measured restore differs"); + if (sample >= 0) { + std::cout << "{\"store\":\"" << name << "\",\"entities\":" << count + << ",\"density_percent\":" << density << ",\"seed\":" << seed + << ",\"ticks\":100,\"sample\":" << sample + << ",\"create_s\":" << create_seconds << ",\"tick_s\":" << tick_seconds + << ",\"churn_s\":" << churn_seconds << ",\"export_s\":" << export_seconds + << ",\"restore_s\":" << restore_seconds << ",\"live_bytes\":" << memory.live + << ",\"peak_bytes\":" << memory.peak << ",\"allocations\":" << memory.calls + << ",\"hash\":\"" << std::hex << fingerprint(bytes) << std::dec << "\"}\n"; + } + } + require(memory.live == 0 && restore_memory.live == 0, "Measured store allocations leaked"); + return bytes; +} +} // namespace + +int main(int argc, char** argv) try { + if (argc != 2 || + (std::string_view{argv[1]} != "--verify" && std::string_view{argv[1]} != "--measure")) { + std::cerr << "Usage: evo-ecs-experiment --verify|--measure\n"; + return 2; + } + verify(); + if (std::string_view{argv[1]} == "--verify") { + std::cout << "ECS experiment contracts passed\n"; + return 0; + } + std::cout << "{\"experiment\":\"ecs-storage-v1\",\"entt\":\"4.0.0\",\"compiler\":\"" +#if defined(__clang__) + << "Clang " << __clang_version__ +#elif defined(_MSC_VER) + << "MSVC " << _MSC_VER +#elif defined(__GNUC__) + << "GCC " << __VERSION__ +#endif + << "\",\"build\":\"" +#if defined(NDEBUG) + << "release" +#else + << "debug" +#endif + << "\"}\n"; + for (const auto count : {1'000U, 10'000U}) { + for (const auto density : {10U, 100U}) { + for (int sample = -1; sample < 7; ++sample) { + Bytes rows, entt; + if (sample % 2 == 0) { + rows = measure("rows", count, density, sample); + entt = measure("entt4", count, density, sample); + } else { + entt = measure("entt4", count, density, sample); + rows = measure("rows", count, density, sample); + } + require(rows == entt, "Measured adapters produced different canonical bytes"); + } + } + } + return 0; +} catch (const std::exception& error) { + try { + std::cerr << error.what() << '\n'; + } catch (...) { + // Reporting failure must not let an exception escape the process boundary. + return 1; + } + return 1; +} catch (...) { + return 1; +} diff --git a/tools/check-format.cmake b/tools/check-format.cmake index 3e8556b..276bf58 100644 --- a/tools/check-format.cmake +++ b/tools/check-format.cmake @@ -7,7 +7,8 @@ if(NOT result EQUAL 0 OR NOT version MATCHES "version 19\\.") endif() file(GLOB_RECURSE sources "${SOURCE_ROOT}/include/*.hpp" "${SOURCE_ROOT}/src/*.cpp" - "${SOURCE_ROOT}/apps/*.cpp" "${SOURCE_ROOT}/tests/*.cpp") + "${SOURCE_ROOT}/apps/*.cpp" "${SOURCE_ROOT}/tests/*.cpp" + "${SOURCE_ROOT}/experiments/*.cpp") execute_process(COMMAND "${FORMATTER}" --dry-run --Werror ${sources} RESULT_VARIABLE result) if(NOT result EQUAL 0) message(FATAL_ERROR "Formatting check failed") diff --git a/tools/entt-pin.cmake b/tools/entt-pin.cmake new file mode 100644 index 0000000..53fa681 --- /dev/null +++ b/tools/entt-pin.cmake @@ -0,0 +1,3 @@ +set(EVO_ENTT_REVISION 85c6bba014049b5de8fad49d25424df2f1f6a8c1) +set(EVO_ENTT_HEADER_SHA256 eedcb83fedffe640334ef69a0647625a186cf1daeb333b21b0a1445f01e89b6c) +set(EVO_ENTT_LICENSE_SHA256 73547f6370ee16ea560665329bb62d4e2d8250f41eaa204b47a6c694ca3286ea) diff --git a/tools/prepare-entt.cmake b/tools/prepare-entt.cmake new file mode 100644 index 0000000..a183703 --- /dev/null +++ b/tools/prepare-entt.cmake @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.28) +include("${CMAKE_CURRENT_LIST_DIR}/entt-pin.cmake") +set(destination "${CMAKE_CURRENT_LIST_DIR}/../build/external/entt") +file(MAKE_DIRECTORY "${destination}/include/entt") +set(upstream "https://raw.githubusercontent.com/skypjack/entt/${EVO_ENTT_REVISION}") +file(DOWNLOAD "${upstream}/single_include/entt/entt.hpp" + "${destination}/include/entt/entt.hpp" TLS_VERIFY ON + EXPECTED_HASH "SHA256=${EVO_ENTT_HEADER_SHA256}") +file(DOWNLOAD "${upstream}/LICENSE" "${destination}/LICENSE" TLS_VERIFY ON + EXPECTED_HASH "SHA256=${EVO_ENTT_LICENSE_SHA256}") +message(STATUS "Prepared verified EnTT 4.0.0 for the opt-in ECS experiment")