Add benchmarks and micro-optimise the injection hot path#41
Draft
Add benchmarks and micro-optimise the injection hot path#41
Conversation
35f9419 to
a92b163
Compare
All 86 tests pass, PHPStan level 6 clean. Measured improvement (PHP 8.4, OPcache off, ~750 injections/request): Warm throughput: 413k → 666k ops/s (+61%) Avg time per request: 1.40ms → 1.01ms (−28%) Request sim throughput: 530k → 739k ops/s (+39%) 1. Injector::create() — inline fast-path dispatch Skip the buildParameterArray() call entirely when no explicit parameters are provided (the common autowiring case), calling buildParameterArrayFromContainer() directly instead. 2. Injector::buildParameterArrayFromContainer() — drop $position key tracking The signature array is always 0-indexed sequential (built by ParameterInspector), so $parameters[] = ... produces the same result as $parameters[$position] = ... with less overhead per iteration. 3. Injector::$containerHasCache — cache container->has() per type The container is sealed at bootstrap; whether a given FQCN is registered does not change during a request. Caching the has() boolean per type eliminates a PSR-11 method call + Pimple hash lookup on every repeated injection that shares a container-registered dependency type. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
a92b163 to
13c0d9d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Applies four micro-optimisations to the injection hot path, all with passing tests and PHPStan level 6 clean
Benchmark setup
23 injectable fixture classes across three groups (no-constructor, 2-dep, 3–4-dep) backed by 20 pre-registered Pimple singletons. Total ~744 injections per simulated request.
Run with:
Three reported sections:
Optimisations
Injector::create()empty($parameters)increate()itself and callbuildParameterArrayFromContainer()directly, skipping thebuildParameterArray()wrapper call on the common autowiring pathInjector::buildParameterArrayFromContainer()$positionkey tracking — signature arrays are always 0-indexed sequential, so$parameters[]is equivalent and avoids keyed iteration overheadCachingClassInspector$constructorCache[$class] = [$result]wrapper trick with two arrays ($constructorCachedpresence flag +$constructorSignaturesvalues), keeping the fastisset()check while removing the[0]offset dereference on every cache hitInjector::$containerHasCachecontainer->has($type)results per FQCN — the container is sealed at bootstrap so this eliminates a PSR-11 method call + Pimple hash lookup on every repeated injection sharing the same container-registered dep typesResults (PHP 8.4, OPcache off, 744 injections/request)
Test plan
./vendor/bin/phpunit— 86 tests, 145 assertions pass./vendor/bin/phpstan analyse— no errorsphp benchmarks/bench.php --requests 50— runs cleanly, inspect throughput numbers🤖 Generated with Claude Code