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:
parent
d0647a8f1e
commit
d77c02799e
|
|
@ -19,6 +19,10 @@ final class Engine
|
|||
/** Process this village's events due by $now. Returns how many ran. */
|
||||
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;
|
||||
while ($this->processNext($villageId, $now)) {
|
||||
$count++;
|
||||
|
|
|
|||
|
|
@ -35,6 +35,18 @@ final class Simulation
|
|||
|
||||
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)
|
||||
{
|
||||
$this->config = $village->world->gameConfig();
|
||||
|
|
@ -43,6 +55,14 @@ final class Simulation
|
|||
$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
|
||||
{
|
||||
return new self($village->loadMissing(['world', 'buildings', 'orders', 'troops', 'recruits']));
|
||||
|
|
@ -68,7 +88,34 @@ final class Simulation
|
|||
|
||||
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. */
|
||||
public function hqStaffing(): float
|
||||
{
|
||||
if ($this->frozen && $this->hqStaffing !== null) {
|
||||
return $this->hqStaffing;
|
||||
}
|
||||
$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
|
||||
|
|
@ -190,6 +240,17 @@ final class Simulation
|
|||
if (! $building) {
|
||||
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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ final class VillagePresenter
|
|||
{
|
||||
public static function present(Village $village, CarbonImmutable $now): array
|
||||
{
|
||||
$sim = Simulation::of($village);
|
||||
$sim = Simulation::of($village)->freeze();
|
||||
$sim->settle($now);
|
||||
$military = Military::of($village, $sim);
|
||||
$starvation = Starvation::of($village, $sim);
|
||||
|
|
|
|||
Loading…
Reference in New Issue