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) {