perf(cellular): share current fields and advance session batches in place
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m29s
Foundation / postgres-tools (push) Successful in 1m9s
Foundation / server-image (push) Successful in 4m17s
Foundation / image (push) Successful in 4m41s

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 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-21 19:36:57 -04:00
co-authored by Claude Opus 5.5
parent 4921231311
commit 10aba324db
5 changed files with 70 additions and 5 deletions
@@ -12,7 +12,7 @@ spec:
env: env:
- {name: REGION_SESSION, value: "dev"} - {name: REGION_SESSION, value: "dev"}
- {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"} - {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"} - {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"}
ports: ports:
# QUIC (ADR-0090): UDP only; nothing listens on TCP 7400. # QUIC (ADR-0090): UDP only; nothing listens on TCP 7400.
@@ -6,6 +6,7 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <expected> #include <expected>
#include <memory>
#include <span> #include <span>
#include <vector> #include <vector>
@@ -44,6 +45,11 @@ class CellularFlowField {
public: public:
[[nodiscard]] static std::expected<CellularFlowField, CellularCurrentError> [[nodiscard]] static std::expected<CellularFlowField, CellularCurrentError>
generate(const CellularCurrentConfig& config); 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<std::shared_ptr<const CellularFlowField>,
CellularCurrentError>
shared(const CellularCurrentConfig& config);
// Liquid velocity at a point, clamped into the region; bilinear between grid nodes. // 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; [[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`. // At the centre of a square footprint whose lower-left corner is `position`.
+30
View File
@@ -4,6 +4,9 @@
#include <algorithm> #include <algorithm>
#include <limits> #include <limits>
#include <mutex>
#include <utility>
#include <vector>
namespace evo::biology { namespace evo::biology {
namespace { namespace {
@@ -181,6 +184,33 @@ CellularFlowField::generate(const CellularCurrentConfig& config) {
return field; return field;
} }
std::expected<std::shared_ptr<const CellularFlowField>, 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<std::pair<CellularCurrentConfig, std::shared_ptr<const CellularFlowField>>>
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<const CellularFlowField>(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 { CellularFlowSample CellularFlowField::sample(std::int64_t x_um, std::int64_t y_um) const noexcept {
const std::int64_t pitch = config_.pitch_um; const std::int64_t pitch = config_.pitch_um;
const auto x = std::clamp<std::int64_t>(x_um, 0, config_.width_um); const auto x = std::clamp<std::int64_t>(x_um, 0, config_.width_um);
+4 -4
View File
@@ -936,7 +936,7 @@ CellularLives::liquid_upgrade_candidate(const CellularLiquidRules& rules) const
return std::unexpected{CellularLivesError::version}; return std::unexpected{CellularLivesError::version};
if (!cellular_liquid_valid(rules, state_->config.area)) if (!cellular_liquid_valid(rules, state_->config.area))
return std::unexpected{CellularLivesError::invalid_config}; return std::unexpected{CellularLivesError::invalid_config};
auto field = biology::CellularFlowField::generate(rules.currents); auto field = biology::CellularFlowField::shared(rules.currents);
if (!field) if (!field)
return std::unexpected{CellularLivesError::invalid_config}; return std::unexpected{CellularLivesError::invalid_config};
auto config = state_->habitat->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.format_version = cellular_lives_liquid_format;
next.rules_version = cellular_lives_liquid_rules; next.rules_version = cellular_lives_liquid_rules;
next.liquid = rules; next.liquid = rules;
next.field = std::make_shared<const biology::CellularFlowField>(std::move(*field)); next.field = std::move(*field);
for (auto& life : next.lives) for (auto& life : next.lives)
life.motion = {}; life.motion = {};
return candidate; return candidate;
@@ -1585,10 +1585,10 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
if (in.get(4) != 0 || turn > 0xFFFFU || cap > 0xFFFFU || if (in.get(4) != 0 || turn > 0xFFFFU || cap > 0xFFFFU ||
!cellular_liquid_valid(liquid, config.area)) !cellular_liquid_valid(liquid, config.area))
return std::unexpected{CellularLivesError::format}; return std::unexpected{CellularLivesError::format};
auto generated = biology::CellularFlowField::generate(liquid.currents); auto generated = biology::CellularFlowField::shared(liquid.currents);
if (!generated) if (!generated)
return std::unexpected{CellularLivesError::format}; return std::unexpected{CellularLivesError::format};
field = std::make_shared<const biology::CellularFlowField>(std::move(*generated)); field = std::move(*generated);
} }
const auto life_bytes = liquid_version ? cellular_life_v6_bytes + 8 + cellular_life_motion_bytes const auto life_bytes = liquid_version ? cellular_life_v6_bytes + 8 + cellular_life_motion_bytes
: inheritance_version ? cellular_life_v6_bytes + 8 : inheritance_version ? cellular_life_v6_bytes + 8
@@ -239,6 +239,35 @@ std::expected<void, CellularSessionBatchError> CellularSessionBatchBuilder::appe
return std::unexpected{CellularSessionBatchError::duplicate_birth}; 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(), auto next = CellularLives::restore(working_->snapshot(), working_->world(), working_->region(),
working_->frame(), working_->tick()); working_->frame(), working_->tick());
if (!next) { if (!next) {