Tooling

Performance and Octane

Cache the compiled shapes on deploy, and warm them once per worker under Octane.

The shape of a class at a revision (its fields, names, types and conversion plan) is compiled once, then reused. Per item rendered, only the sources of the visible fields run, then the compiled conversions.

Cache the shapes

Terminal
php artisan revision:cache    # writes bootstrap/cache/revisions.php
php artisan revision:clear    # deletes it

php artisan optimize runs revision:cache, and optimize:clear runs revision:clear, like the route cache. Run it again after each deploy that changes declarations. Closures are never cached: they come from toArray() and body() at run time. The file location is cache_path.

Octane

The current revision is reset on every request. Add the configuration and the compiled shapes to warm, so they are built once per worker rather than once per request:

config/octane.php
'warm' => [
    ...Octane::defaultServicesToWarm(),
    \Ark4ne\ApiRevisions\Laravel\RevisionsConfig::class,
    \Ark4ne\ApiRevisions\Compilation\Shapes::class,
],

Custom types and descriptor macros are registered at boot and hold no request data: they are safe under Octane.

Collections

A resource collection renders every item through one projection plan, running toArray() once per render. Keep toArray() free of per-model decisions in plain PHP to benefit from it: see Responses.