Files
aevolution/include/evo/biology/cellular_currents.hpp
T
dingoandClaude Opus 5.5 10aba324db
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
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 <noreply@anthropic.com>
2026-10-21 19:36:57 -04:00

84 lines
3.4 KiB
C++

#pragma once
#include "evo/biology/cellular_kinematics.hpp"
#include <compare>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <memory>
#include <span>
#include <vector>
// Procedural currents (Cellular_Region_Changes.md, flows; ADR-0177): a fixed environmental
// condition, not stock. A seeded configuration generates 2-n closed looping routes inside the
// region, each with its own width, speed and direction, rasterized once into a Q10 velocity
// grid. Integer-only, so the client regenerates the identical field from the same
// configuration.
namespace evo::biology {
inline constexpr std::uint32_t max_cellular_current_pitch_um = 10'000;
inline constexpr std::size_t max_cellular_current_nodes = 1'048'576;
inline constexpr std::size_t cellular_current_route_points = 32;
struct CellularCurrentConfig {
std::uint64_t seed{};
std::uint32_t width_um{}, height_um{}, pitch_um{};
std::uint32_t min_count{}, max_count{};
std::uint32_t min_route_width_um{}, max_route_width_um{};
std::uint32_t min_speed_um_per_s{}, max_speed_um_per_s{};
auto operator<=>(const CellularCurrentConfig&) const = default;
};
struct CellularRoutePoint {
std::int64_t x_um{}, y_um{};
auto operator<=>(const CellularRoutePoint&) const = default;
};
// A closed loop; points are in flow order (the last joins the first).
struct CellularCurrentRoute {
std::vector<CellularRoutePoint> points;
std::uint32_t width_um{}, speed_um_per_s{};
bool clockwise{};
auto operator<=>(const CellularCurrentRoute&) const = default;
};
enum class CellularCurrentError : std::uint8_t { invalid_config, region_too_small };
class CellularFlowField {
public:
[[nodiscard]] static std::expected<CellularFlowField, CellularCurrentError>
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.
[[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`.
[[nodiscard]] CellularFlowSample at_footprint(CellularPosition position,
std::uint32_t extent_um) const noexcept;
[[nodiscard]] const CellularCurrentConfig& config() const noexcept {
return config_;
}
[[nodiscard]] std::span<const CellularCurrentRoute> routes() const noexcept {
return routes_;
}
[[nodiscard]] std::span<const CellularFlowSample> nodes() const noexcept {
return nodes_;
}
[[nodiscard]] std::size_t columns() const noexcept {
return columns_;
}
[[nodiscard]] std::size_t rows() const noexcept {
return rows_;
}
// Fingerprint of the generated grid, for comparing server and client fields.
[[nodiscard]] std::uint64_t digest() const noexcept;
private:
CellularFlowField() = default;
CellularCurrentConfig config_{};
std::vector<CellularCurrentRoute> routes_;
std::vector<CellularFlowSample> nodes_;
std::size_t columns_{}, rows_{};
};
} // namespace evo::biology