JA
apps/cryo-studio / src / Cryo / PhpCryoDelta.php · commit 76aa68d
<?phpdeclare(strict_types=1);namespace App\Cryo;/** * Result of PhpCryoReader::delta() (same API and results as CryoDelta of ext-cryo). * ArrayAccess / Countable / foreach work on the default view (bySite() unless * delta() was given ['by' => 'class'|'stack']), so delta($files)[0]['site'] still works. */final class PhpCryoDelta implements \IteratorAggregate, \Countable, \ArrayAccess{    /** @internal built by PhpCryoReader::delta() */    public function __construct(        private array $views,        private array $timeline,        private string $by,        private PhpCryoReader $last,        private int $firstEpoch,    ) {}    /** Rows {site, new_per_interval, cohort_alive_at_last, survivors, survival, bytes_last, growing, hooked_by}. */    public function bySite(): array    {        return $this->views['site'];    }    /** Same rows keyed by class. */    public function byClass(): array    {        return $this->views['class'];    }    /** Same rows keyed by allocation stack (frames joined by " <- "; needs cryo.track_alloc=stack). */    public function byStack(): array    {        return $this->views['stack'];    }    /** {snapshots: list<{file, epoch, objects, bytes}>, by_class: [class => counts], by_site: [site => counts]} */    public function timeline(): array    {        return $this->timeline;    }    /**     * A bySite() row (index or site string) with the code around its allocation site and     * its main write site, and the typical paths that keep its survivors alive. null if unknown.     * @return null|array{site:string,row:array,file:?string,line:?int,code:?array,hooked_by:?string,hook_code:?array,paths:list<array{count:int,path:list<array>}>}     */    public function explain(int|string $site): ?array    {        $row = null;        foreach ($this->views['site'] as $i => $r) {            if (is_string($site) ? $r['site'] === $site : $i === $site) {                $row = $r;                break;            }        }        if ($row === null) {            return null;        }        $split = PhpCryoReader::siteSplit($row['site']);        return [            'site' => $row['site'],            'row' => $row,            'file' => $split[0] ?? null,            'line' => $split[1] ?? null,            'code' => PhpCryoReader::code($row['site']),            'hooked_by' => $row['hooked_by'],            'hook_code' => PhpCryoReader::code($row['hooked_by']),            'paths' => $this->last->typicalPaths($row['site'], $this->firstEpoch),        ];    }    public function getIterator(): \Iterator    {        return new \ArrayIterator($this->views[$this->by]);    }    public function count(): int    {        return count($this->views[$this->by]);    }    public function offsetExists(mixed $offset): bool    {        return is_int($offset) && isset($this->views[$this->by][$offset]);    }    public function offsetGet(mixed $offset): mixed    {        return is_int($offset) ? ($this->views[$this->by][$offset] ?? null) : null;    }    public function offsetSet(mixed $offset, mixed $value): void    {        throw new \Error('PhpCryoDelta is read-only');    }    public function offsetUnset(mixed $offset): void    {        throw new \Error('PhpCryoDelta is read-only');    }}