perf: the village payload computes the workforce, HQ staffing and instance indexes once (2.2x faster); reads skip the locking catch-up when nothing is due

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-30 13:07:09 +01:00
parent d0647a8f1e
commit d77c02799e
3 changed files with 68 additions and 3 deletions

View File

@ -19,6 +19,10 @@ final class Engine
/** Process this village's events due by $now. Returns how many ran. */ /** Process this village's events due by $now. Returns how many ran. */
public function catchUp(int $villageId, CarbonImmutable $now): int public function catchUp(int $villageId, CarbonImmutable $now): int
{ {
// Most reads find nothing due: one indexed look instead of a locking transaction.
if (! $this->due($villageId, $now)->exists()) {
return 0;
}
$count = 0; $count = 0;
while ($this->processNext($villageId, $now)) { while ($this->processNext($villageId, $now)) {
$count++; $count++;

View File

@ -35,6 +35,18 @@ final class Simulation
private Production $production; private Production $production;
private ?Workforce $workforce = null;
/** Set while a payload is built: nothing changes, so derived values are computed once. */
private bool $frozen = false;
private ?float $hqStaffing = null;
/** @var array<int, int>|null building id => its index among its type */
private ?array $indexes = null;
private ?string $workforceFor = null;
private function __construct(private readonly Village $village) private function __construct(private readonly Village $village)
{ {
$this->config = $village->world->gameConfig(); $this->config = $village->world->gameConfig();
@ -43,6 +55,14 @@ final class Simulation
$this->production = new Production($this->config); $this->production = new Production($this->config);
} }
/** For reading only (the village payload): workforce, staffing and indexes are computed once. */
public function freeze(): self
{
$this->frozen = true;
return $this;
}
public static function of(Village $village): self public static function of(Village $village): self
{ {
return new self($village->loadMissing(['world', 'buildings', 'orders', 'troops', 'recruits'])); return new self($village->loadMissing(['world', 'buildings', 'orders', 'troops', 'recruits']));
@ -68,7 +88,34 @@ final class Simulation
public function workforce(): Workforce public function workforce(): Workforce
{ {
return $this->population->workforce($this->states(), $this->soldierUpkeep(), (int) $this->village->militia); // One village state, one workforce: a payload asks for it dozens of times.
if ($this->frozen && $this->workforce) {
return $this->workforce;
}
$key = $this->workforceKey();
if ($key !== $this->workforceFor) {
$this->workforce = $this->population->workforce($this->states(), $this->soldierUpkeep(), (int) $this->village->militia);
$this->workforceFor = $key;
}
return $this->workforce;
}
/** What the workforce depends on: buildings with their levels and priorities, troops, training, militia. */
private function workforceKey(): string
{
$parts = [(string) (int) $this->village->militia];
foreach ($this->village->buildings as $b) {
$parts[] = "$b->id:$b->level:$b->priority";
}
foreach ($this->village->troops as $s) {
$parts[] = "t$s->id:$s->unit:$s->count";
}
foreach ($this->village->recruits as $o) {
$parts[] = "r$o->id:".($o->count - $o->done);
}
return implode('|', $parts);
} }
/** /**
@ -167,9 +214,12 @@ final class Simulation
/** Output factor of the headquarters' staffing; it scales build speed. */ /** Output factor of the headquarters' staffing; it scales build speed. */
public function hqStaffing(): float public function hqStaffing(): float
{ {
if ($this->frozen && $this->hqStaffing !== null) {
return $this->hqStaffing;
}
$hq = $this->village->buildings->firstWhere('type', 'headquarters'); $hq = $this->village->buildings->firstWhere('type', 'headquarters');
return $hq ? $this->production->staffingFactor($this->workforce()->ratio($hq->id)) : $this->production->staffingFactor(0); return $this->hqStaffing = $hq ? $this->production->staffingFactor($this->workforce()->ratio($hq->id)) : $this->production->staffingFactor(0);
} }
public function builders(): int public function builders(): int
@ -190,6 +240,17 @@ final class Simulation
if (! $building) { if (! $building) {
return $this->instances($type); return $this->instances($type);
} }
if ($this->frozen) {
if ($this->indexes === null) {
$this->indexes = [];
$seen = [];
foreach ($this->village->buildings->sortBy('id') as $b) {
$this->indexes[$b->id] = $seen[$b->type] = ($seen[$b->type] ?? -1) + 1;
}
}
return $this->indexes[$building->id] ?? 0;
}
return $this->village->buildings->where('type', $type)->where('id', '<', $building->id)->count(); return $this->village->buildings->where('type', $type)->where('id', '<', $building->id)->count();
} }

View File

@ -25,7 +25,7 @@ final class VillagePresenter
{ {
public static function present(Village $village, CarbonImmutable $now): array public static function present(Village $village, CarbonImmutable $now): array
{ {
$sim = Simulation::of($village); $sim = Simulation::of($village)->freeze();
$sim->settle($now); $sim->settle($now);
$military = Military::of($village, $sim); $military = Military::of($village, $sim);
$starvation = Starvation::of($village, $sim); $starvation = Starvation::of($village, $sim);