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
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:
@@ -4,6 +4,9 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <mutex>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace evo::biology {
|
||||
namespace {
|
||||
@@ -181,6 +184,33 @@ CellularFlowField::generate(const CellularCurrentConfig& config) {
|
||||
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 {
|
||||
const std::int64_t pitch = config_.pitch_um;
|
||||
const auto x = std::clamp<std::int64_t>(x_um, 0, config_.width_um);
|
||||
|
||||
@@ -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<const biology::CellularFlowField>(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<const std::uint8_t> 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<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
|
||||
: inheritance_version ? cellular_life_v6_bytes + 8
|
||||
|
||||
@@ -239,6 +239,35 @@ std::expected<void, CellularSessionBatchError> 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) {
|
||||
|
||||
Reference in New Issue
Block a user