From 10aba324db44fa9798fc6675ce203347a15d79b9 Mon Sep 17 00:00:00 2001 From: dingo Date: Wed, 21 Oct 2026 19:36:57 -0400 Subject: [PATCH] perf(cellular): share current fields and advance session batches in place DEV region 10 ran at about one tick per second. Every owner restore regenerated the region's current field (about 19 ms of a 29 ms restore for 60 x 60 mm), and the server restores several times per tick: the session batch builder copies the committed state, copied it again for each appended tick, and publication replays from another copy. On the slower worker node that capped the region far below 20 ticks per second, which also left the client's view stale, so it sent no movement. Generated fields are now shared per process (they are immutable and fully determined by their configuration): a 60 x 60 mm restore with 3,500 deposits drops from 29 ms to 1.5 ms. The builder advances its private working state in place when a tick has no births, after checking the batch and mutation-action encodings first; a refused owner tick leaves it unchanged, so appends stay all-or-nothing. Births keep the copying path. Refreshes the DEV session window. Co-Authored-By: Claude Opus 5.5 --- .../region/dev-session-route/listener.yaml | 2 +- include/evo/biology/cellular_currents.hpp | 6 ++++ src/biology/cellular_currents.cpp | 30 +++++++++++++++++++ src/server/lifecycle/cellular_lives.cpp | 8 ++--- src/server/session/cellular_session_batch.cpp | 29 ++++++++++++++++++ 5 files changed, 70 insertions(+), 5 deletions(-) diff --git a/deploy/kubernetes/region/dev-session-route/listener.yaml b/deploy/kubernetes/region/dev-session-route/listener.yaml index 3b15f8a..02fe569 100644 --- a/deploy/kubernetes/region/dev-session-route/listener.yaml +++ b/deploy/kubernetes/region/dev-session-route/listener.yaml @@ -12,7 +12,7 @@ spec: env: - {name: REGION_SESSION, value: "dev"} - {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"} - - {name: REGION_SESSION_WINDOW, value: "dev-20261021T224013-3b10d1cd:1792636813"} + - {name: REGION_SESSION_WINDOW, value: "dev-20261021T233657-69bd31b7:1792640217"} - {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"} ports: # QUIC (ADR-0090): UDP only; nothing listens on TCP 7400. diff --git a/include/evo/biology/cellular_currents.hpp b/include/evo/biology/cellular_currents.hpp index abc6f30..cc8bff7 100644 --- a/include/evo/biology/cellular_currents.hpp +++ b/include/evo/biology/cellular_currents.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -44,6 +45,11 @@ class CellularFlowField { public: [[nodiscard]] static std::expected generate(const CellularCurrentConfig& config); + // The same field, generated once per process and shared: fields are immutable and fully + // determined by their configuration, and restoring an owner must not regenerate one. + [[nodiscard]] static std::expected, + CellularCurrentError> + shared(const CellularCurrentConfig& config); // Liquid velocity at a point, clamped into the region; bilinear between grid nodes. [[nodiscard]] CellularFlowSample sample(std::int64_t x_um, std::int64_t y_um) const noexcept; // At the centre of a square footprint whose lower-left corner is `position`. diff --git a/src/biology/cellular_currents.cpp b/src/biology/cellular_currents.cpp index fdaf113..6ef7fc1 100644 --- a/src/biology/cellular_currents.cpp +++ b/src/biology/cellular_currents.cpp @@ -4,6 +4,9 @@ #include #include +#include +#include +#include namespace evo::biology { namespace { @@ -181,6 +184,33 @@ CellularFlowField::generate(const CellularCurrentConfig& config) { return field; } +std::expected, CellularCurrentError> +CellularFlowField::shared(const CellularCurrentConfig& config) { + // A few regions per process at most; the oldest entry makes room. + constexpr std::size_t capacity = 8; + static std::mutex guard; + static std::vector>> + cache; + { + const std::scoped_lock lock{guard}; + for (const auto& [key, field] : cache) + if (key == config) + return field; + } + auto generated = generate(config); + if (!generated) + return std::unexpected{generated.error()}; + auto field = std::make_shared(std::move(*generated)); + const std::scoped_lock lock{guard}; + for (const auto& [key, existing] : cache) + if (key == config) + return existing; + if (cache.size() == capacity) + cache.erase(cache.begin()); + cache.emplace_back(config, field); + return field; +} + CellularFlowSample CellularFlowField::sample(std::int64_t x_um, std::int64_t y_um) const noexcept { const std::int64_t pitch = config_.pitch_um; const auto x = std::clamp(x_um, 0, config_.width_um); diff --git a/src/server/lifecycle/cellular_lives.cpp b/src/server/lifecycle/cellular_lives.cpp index 66fb1e2..2c77f54 100644 --- a/src/server/lifecycle/cellular_lives.cpp +++ b/src/server/lifecycle/cellular_lives.cpp @@ -936,7 +936,7 @@ CellularLives::liquid_upgrade_candidate(const CellularLiquidRules& rules) const return std::unexpected{CellularLivesError::version}; if (!cellular_liquid_valid(rules, state_->config.area)) return std::unexpected{CellularLivesError::invalid_config}; - auto field = biology::CellularFlowField::generate(rules.currents); + auto field = biology::CellularFlowField::shared(rules.currents); if (!field) return std::unexpected{CellularLivesError::invalid_config}; auto config = state_->habitat->config(); @@ -957,7 +957,7 @@ CellularLives::liquid_upgrade_candidate(const CellularLiquidRules& rules) const next.format_version = cellular_lives_liquid_format; next.rules_version = cellular_lives_liquid_rules; next.liquid = rules; - next.field = std::make_shared(std::move(*field)); + next.field = std::move(*field); for (auto& life : next.lives) life.motion = {}; return candidate; @@ -1585,10 +1585,10 @@ CellularLives::restore(std::span bytes, std::uint64_t world, if (in.get(4) != 0 || turn > 0xFFFFU || cap > 0xFFFFU || !cellular_liquid_valid(liquid, config.area)) return std::unexpected{CellularLivesError::format}; - auto generated = biology::CellularFlowField::generate(liquid.currents); + auto generated = biology::CellularFlowField::shared(liquid.currents); if (!generated) return std::unexpected{CellularLivesError::format}; - field = std::make_shared(std::move(*generated)); + field = std::move(*generated); } const auto life_bytes = liquid_version ? cellular_life_v6_bytes + 8 + cellular_life_motion_bytes : inheritance_version ? cellular_life_v6_bytes + 8 diff --git a/src/server/session/cellular_session_batch.cpp b/src/server/session/cellular_session_batch.cpp index e626c0d..dfa6314 100644 --- a/src/server/session/cellular_session_batch.cpp +++ b/src/server/session/cellular_session_batch.cpp @@ -239,6 +239,35 @@ std::expected CellularSessionBatchBuilder::appe return std::unexpected{CellularSessionBatchError::duplicate_birth}; } + // Without births the tick alone changes the working state, and a refused owner tick leaves + // it byte-for-byte unchanged: check the encodings first, then advance in place. A full + // copy per tick costs a restore of the whole region (rules-12 regions hold thousands of + // deposits). + if (prepared->intents.births.empty()) { + auto next_batch = batch_; + next_batch.ticks.push_back(prepared->intents); + next_batch.header.end_tick = tick; + if (!encode_cellular_batch(next_batch)) + return std::unexpected{CellularSessionBatchError::invalid_birth}; + auto next_mutation_actions = mutation_actions_; + next_mutation_actions.insert(next_mutation_actions.end(), + prepared->mutation_actions.begin(), + prepared->mutation_actions.end()); + if (!encode_cellular_mutation_action_block( + next_mutation_actions, next_batch.header.begin_tick, next_batch.header.end_tick)) + return std::unexpected{CellularSessionBatchError::mutation}; + if (const auto advanced = working_->advance_tick_with_mutations( + prepared->intents.lives, prepared->intents.mutations); + !advanced) { + last_lives_error_ = advanced.error(); + return std::unexpected{CellularSessionBatchError::biology}; + } + batch_ = std::move(next_batch); + actions_.insert(actions_.end(), prepared->actions.begin(), prepared->actions.end()); + mutation_actions_ = std::move(next_mutation_actions); + return {}; + } + auto next = CellularLives::restore(working_->snapshot(), working_->world(), working_->region(), working_->frame(), working_->tick()); if (!next) {