Make a slow Symfony page fast and prove it stays fast: measure with the profiler and Stopwatch before touching anything, kill N+1 queries with explicit joins and SELECT NEW projections into read DTOs, and lock the result behind an integration test that counts the SQL queries an endpoint executes. Also covers HTTP caching with #[Cache] (expiration vs validation, and why public:true on a page holding personal data leaks it through the shared cache), the application cache with tag invalidation placed in an #[AsDecorator] decorator rather than in the service holding the rule, and Doctrine's query and result caches. Use this skill whenever someone says a page is slow, "this takes forever", "it times out", "why is this taking so long", "too many queries", "the shelf takes four seconds to load", asks to add caching or a cache layer, asks about N+1, lazy loading or eager loading, wants to profile or benchmark an endpoint, wants to assert a query count in a test, needs to iterate over tens of thousands of rows without exhausting memory, or asks why a cached page keeps showing stale data. Not for server tuning — OPcache, preload, autoloader and container settings belong to symfony-yoandev-deployment.
Trending skills
7-day visitors to each skill page
Ships in Symfony Skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Tier: on demand — every rule here follows a measurement. Nothing in this skill is done pre-emptively, including the query-count test, which is written for a list that has been seen to grow.
Measure, fix the query, then write the test that stops it coming back.
Everything in this skill assumes the ordering. A cache added before a measurement does not make an application fast; it makes it fast and wrong, because the slow query is still there and now it also serves stale data. Caching is the last move, not the first.
The profiler is the whole toolbox. Load the page, open the toolbar, click the
Doctrine panel: number of queries, time per query, the SQL itself, and — because
profiling_collect_backtrace is on in debug — the line of code that triggered each one.
Read it in this order, because the answer is almost always in the first line:
For code that is slow without being SQL-bound, symfony/stopwatch narrows it down:
$this->stopwatch->start('isbn_lookup', 'shelf');
$data = $this->isbnClient->fetch($isbn);
$this->stopwatch->stop('isbn_lookup');
Named events appear in the profiler timeline. One caveat worth knowing before you inject
it: symfony/stopwatch is a require-dev package in the Symfony skeleton, and
FrameworkBundle only registers debug.stopwatch when the class exists. A service that
type-hints Stopwatch therefore fails to compile the container on
composer install --no-dev. Instrument, measure, remove — or move the package to
require if the instrumentation is meant to stay.
Every list endpoint gets an integration test that counts the SQL queries it executes.
symfony-yoandev-doctrine makes a relation bidirectional only where the inverse side is
genuinely read — and wherever one exists, N+1 is one dot away: a template that touches
book.reviews is enough to reintroduce it by accident. An untested rule is a wish, so
the rule gets a test.
$this->seed(1);
$small = $this->countQueriesFor($this->client, 'GET', '/livres');
$this->seed(9);
$large = $this->countQueriesFor($this->client, 'GET', '/livres');
self::assertSame(2, $small, 'shelf with one book:'.$this->queryLog());
self::assertSame($small, $large, 'the query count grew with the row count:'.$this->queryLog());
Assert on both an exact number and on the absence of growth. The exact number pins
today's cost; the second assertion is the actual definition of N+1 and it survives
someone legitimately adding a third query later. An upper bound alone
(assertLessThan(10, …)) is the weakest form and has a nasty failure mode described
below.
The counting itself is references/query-counting.md — read it before writing the
test, because two of the four moving parts are not obvious and one of them makes a
broken test pass silently.
Two facts worth carrying, both verified against Symfony 8.1.5 / doctrine-bundle 3.3.1 / DBAL 4.4.4:
DebugStack is gone from DBAL 4. Query collection now goes through a DBAL
Driver\Middleware feeding doctrine.debug_data_holder.$client->getProfile()->getCollector('db')->getQueryCount() does exist and works.
It lives on Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector, which
doctrine-bundle's collector extends.| Rule | Why |
|---|---|
Never iterate an entity collection outside a repository. foreach ($book->getReviews() as …) in a service or a template is a lazy load per book. | The collection is a PersistentCollection; touching it issues a SELECT you never wrote and cannot see from the call site. |
List queries join explicitly. ->leftJoin('b.reviews', 'r')->addSelect('r') in the repository method, not lazy loading at render time. | One query with a join costs one round trip. The same data lazily loaded costs one per row, and the cost is invisible until production has rows. |
Never count($book->getRatings()). Add countForBook(Book $book): int to the repository. | PersistentCollection::count() only issues a COUNT query when the association is mapped fetch: 'EXTRA_LAZY'. Otherwise it falls through to Collection::count(), which initialises the whole collection — you hydrate 4 000 entities to display the number 4 000. |
EXTRA_LAZY is the escape hatch Doctrine offers for the third rule, and it works. It is
rejected here for the same reason the charter puts all DQL in repositories: it hides a
SQL query behind a count() call in a Twig template, where no reader and no static
analyser will find it. The repository method is one line longer and says what it does.
Diagnosing a specific N+1, choosing between leftJoin and WITH, and the paginate-then-
join trick that avoids the duplicated-row problem: references/queries.md.
A list that displays three columns has no business hydrating full entities, each of which is tracked by the UnitOfWork for the rest of the request.
public function findShelfRows(): array
{
return $this->createQueryBuilder('b')
->select(\sprintf('NEW %s(b.id, b.title, b.status, b.lastReadAt)', ShelfRow::class))
->orderBy('b.title', 'ASC')
->getQuery()
->getResult();
}
ShelfRow is a final readonly class in src/Dto/Read/ with promoted constructor
properties. Verified: SELECT NEW runs the DBAL type conversion, so a column mapped
with enumType: arrives as the enum and a date_immutable column as a
\DateTimeImmutable — and the identity map stays empty. The service then normalises
Read → Output; if the projection already matches the API contract, project straight into
the Output DTO and skip the layer.
For result sets too large to fit in memory at all, toIterable() plus a periodic
$em->clear(): references/queries.md.
#[Cache] goes on a page whose slowness has been measured, and only then.
Premature HTTP caching is worse than no caching, for two reasons that compound. It hides the real problem — the page is still slow, you just stopped looking at it — and it introduces stale-content bugs, which are the hardest class of bug to reproduce because the reproduction depends on a timer you cannot see.
When it is justified, the two mechanisms answer different questions:
smaxage, maxage) — "do not ask me again for an hour". No request
reaches PHP. Fastest, and wrong until the TTL expires.etag, lastModified) — "ask me, I will answer 304 if nothing
changed". Verified: the attribute evaluates the ETag on kernel.controller_arguments
and short-circuits before the controller runs, so a 304 costs a routing pass and
nothing else. That is what makes it useful on a page that is slow to render.Two things that bite:
public: true on a response containing personal data is a leak. A shared cache
will serve one user's page to the next visitor. The rule has no exception: if the
response varies per user, it is private._csrf — makes SessionListener (which
runs at priority -1000, after the #[Cache] attribute) force
private, max-age=0, must-revalidate. #[Cache(public: true, smaxage: 3600)] on such
a page emits contradictory headers and caches nothing. Silent, and a common waste of
an afternoon.Full treatment, including Vary, stale-while-revalidate and the reverse-proxy
question: references/caching.md.
Two constraints, and they are architectural rather than technical.
The cache does not go in the service holding the rule. It goes in a decorator. The business service stays unit-testable with no cache in sight, and the cache can be deleted by removing one class — as opposed to being unpicked from the middle of a method nobody wants to touch.
#[AsDecorator(ShelfReading::class)]
final readonly class CachedShelfReader implements ShelfReading
{
public function __construct(
#[AutowireDecorated] private ShelfReading $inner,
private TagAwareCacheInterface $cache,
) {
}
}
TagAwareCacheInterface autowires to cache.app.taggable with no configuration.
Verified consequence of this suite's "services are final" rule: you cannot decorate
a final concrete class. lint:container rejects it outright — argument 1 of
BookController::__construct() accepts ShelfReader, CachedShelfReader passed.
Introducing the cache is therefore also the moment you extract an interface and repoint
consumers at it. That is a real cost; it is also the honest one.
Invalidate by tag, not by TTL. A TTL says "this may be wrong for up to an hour". A
tag says "this is wrong now" — the service that modified the book calls
$this->cache->invalidateTags(['book_'.$book->getId()]) and the entry is gone. Keep a
TTL as well, as a safety net for the invalidation you forgot to write.
Key naming, stampede protection, and what belongs in cache.app versus cache.system:
references/caching.md.
Four distinct caches, worth very different amounts:
| Cache | Verdict |
|---|---|
| Metadata | Already on. Nothing to do. |
| Query cache (DQL → SQL parsing) | Configure it in prod. It costs nothing and removes a parse on every request. Pool: cache.system, because it is invalidated by a deploy, not by data. |
| Result cache | Opt-in per query, ->enableResultCache(3600, 'shelf_rows'). Only for a query that is genuinely expensive and genuinely tolerant of staleness. Pool: cache.app, because it holds data. |
| Second-level cache | Not used. It is opt-in per entity, subtle to invalidate, and every problem it solves is better solved by fixing the query. |
Configuration and the caveat about caching a query whose result you then modify:
references/caching.md.
opcache.preload, --optimize-autoloader, container inlining →
symfony-yoandev-deployment. They are deploy-time settings, not code.symfony-yoandev-doctrine.symfony-yoandev-observability. The profiler and Stopwatch
are development instruments and belong here; finding out in production that something
broke or stalled is that skill's.symfony-yoandev-testing. This skill only adds the
query-count assertion.symfony-yoandev-frontend. HTTP/2 is a prerequisite there, not
an optimisation here.| Symptom | Cause |
|---|---|
The Doctrine panel shows 60 near-identical SELECTs | An entity collection is being iterated outside a repository, usually in a Twig template |
| A page is fine with 10 rows, unusable with 1 000 | N+1. Query count is proportional to rows — the growth assertion catches this and a fixed threshold does not |
| A query-count test passes but the page is obviously slow | APP_DEBUG=0 in the test run. Profiling is off, the collector returns 0, and assertLessThan() passes for the wrong reason. See references/query-counting.md |
| The count is high on the first test in a file and correct afterwards | The kernel is not rebooted before the first request, so the test's own fixture queries are counted. Reset the data holder before measuring |
Cache-Control: max-age=0, must-revalidate, private, s-maxage=3600 | The session was touched. The shared cache is disabled; public: true did nothing |
| A cached page shows old data after an edit | TTL-only invalidation. Tag the entry and invalidate it from the service that writes |
| Out of memory exporting a large table | getResult() hydrates everything. toIterable() + $em->clear() |
PHPStan cannot type $profile->getCollector('db') | It returns DataCollectorInterface. Narrow it with assertInstanceOf |
| File | When to read it |
|---|---|
references/query-counting.md | Writing or fixing a test that asserts a query count — read before, not after |
references/queries.md | Diagnosing an N+1, writing a fetch join, projecting into a read DTO, iterating a large result set |
references/caching.md | Adding any cache: HTTP, application, or Doctrine result cache |
What SKILL.md sets; muted values are Claude Code's defaults
/symfony-yoandev-skills:symfony-yoandev-performancefrom Symfony Skills
npx claudepluginhub yoanbernabeu/symfony-yoandev-skills --plugin symfony-yoandev-skills16 skills in this plugin
/symfony-yoandev-architectureDecide where code goes in a Symfony project and wire it correctly: the Controller / Service / Repository / DTO / Entity contract, the three DTO namespaces (Input, Read, Output), service design and who calls flush(), business exceptions carrying their own HTTP status, direct calls instead of domain events, configuration split between env vars, app. parameters and class constants, and the dependency-injection attributes (#[Autowire], #[AutowireIterator], #[AutowireLocator], #[AsDecorator], #[AsTaggedItem], #[Target], #[When], #[Lazy]). Use this skill whenever someone asks where a piece of logic belongs, wants a new service or DTO, says a controller has grown too big, wants to extract or move a class that has outgrown its place, asks how to make something configurable or add a setting or an env var, asks why a service is not autowired or how to inject one of several implementations, wants a factory, strategy, decorator or object mapping, or asks "is this the right place for this?", "how should I structure this feature?", "where does the rating logic go?", "extract this", "why does my exception return a 500?". Also use it for the list of alternatives this standard deliberately rejected and why. For a feature described in plain language, or a bare "refactor this" with no target, `symfony-yoandev-standards` routes first./symfony-yoandev-asyncMove work out of the request with Messenger, send email asynchronously with Mailer and TemplatedEmail, and run recurring jobs with Scheduler — Doctrine transport, retry and failure policy, prioritised queues, idempotent handlers, and workers under supervisor or systemd. Use this skill whenever someone asks to send an email, do something in the background, queue a job, run something every night or every five minutes, replace a crontab, speed up a slow import or a slow signup, process a big CSV, notify someone after an action, set up Messenger or a message handler or a worker, configure retries, deal with a message stuck in the failure queue, or says "my emails never arrive", "the import times out", "this page is slow because it sends mail", or "how do I schedule this"./symfony-yoandev-consoleWrite Symfony console commands as thin translation layers over a service: one class per command family with method-level #[AsCommand] on Symfony 8.1, invokable commands with #[Argument] and #[Option] before that, SymfonyStyle output, dry run by default with --force on anything destructive, mandatory locking on long or scheduled commands, clean shutdown on SIGTERM, and a smoke test with runCommand or CommandTester. Use this skill whenever someone asks to add a command, write a script, make something runnable from the terminal, build an import or an export or a batch job, clean up or purge or delete old data, backfill a column, send a digest, "run this once", "I need a CLI for this", "make:command", says a command times out or eats all the memory or deleted more than it should, or asks how to test a command. For scheduling that command to run every night, and for Messenger workers, use symfony-yoandev-async instead./symfony-yoandev-deploymentShip a Symfony application to production as a Docker image running FrankenPHP: the build and release sequence and what each step is for, migrations run automatically at deploy, production php.ini and OPcache preloading, environment variables and secrets on the server, Messenger workers under systemd or supervisor, post-deployment checks and rollback. Use this skill whenever someone says deploy this, put it in production, ship it, set up the server, prepare a Dockerfile or a production image, write a deploy script, run migrations on the server, restart the workers, configure OPcache or preloading, set environment variables in production, roll back a release — and equally when they describe a symptom: it works locally but not on the server, the site broke after deploying, my CSS is missing in production, production is slow, the workers are running old code, I get a 500 with no error message, the new code is not being picked up. Also use it before a first deployment, when nobody knows how the app is deployed./symfony-yoandev-doctrineThe persistence layer of a Symfony project: entities in maker format with attribute mapping, auto-increment integer ids, \DateTimeImmutable and native enums; relations and the collection traps they open; repositories as the single place where DQL, QueryBuilder and raw SQL are allowed; SELECT NEW projections into Read DTOs; who calls flush(); and migrations that are generated then read and corrected by hand. Use this skill whenever someone asks to create or change an entity, add a field or a column, store or persist something, link two things together, add a relation or a foreign key, write or fix a repository method, write a query, sort or filter a list from the database, count rows, make a query return less data, create or run a migration, rename a column without losing data, or asks "why is my query wrong", "why is this null", "why does saving not work", "the schema is out of sync", "I lost data after deploying"./symfony-yoandev-frontendBuild and debug the front end of a Symfony application with AssetMapper, Twig, Stimulus, Turbo and Symfony UX — no bundler, no Node build step. Use this skill whenever someone asks to make a page interactive, add a filter or a live search, add an autocomplete or a dropdown, add a JavaScript library, style a page or set up Tailwind, add an icon, build a reusable Twig component or a Live Component, add Turbo Drive, Frames or Streams, write a Stimulus controller, organise templates or translation keys, or set a form theme. Also use it for the failures: "my JavaScript does not run", "my JS stopped working after clicking a link", "the CSS did not update", "the page is slow to load", "the assets 404 in production", "the images are served by PHP", "importmap:audit is failing" — and whenever the question is *where* a piece of behaviour belongs, between a Stimulus controller, a Live Component and a Twig Component.Other plugins' skills for the same job