Skip to content

perf(platform): Reduce container lookup overhead in DI resolution#44

Draft
TomA-R wants to merge 1 commit intomasterfrom
perf/optimized-container-lookup
Draft

perf(platform): Reduce container lookup overhead in DI resolution#44
TomA-R wants to merge 1 commit intomasterfrom
perf/optimized-container-lookup

Conversation

@TomA-R
Copy link
Copy Markdown
Member

@TomA-R TomA-R commented Apr 27, 2026

Summary

  • Eliminate redundant container lookups: Added getIfDefined() to ArrayContainerAdapter that combines has()+get() into a single container operation. When the underlying container supports getIfDefined, this reduces per-parameter lookups from 3 redundant offsetExists/offsetGet calls to 1.
  • Remove redundant isset guard from get(): When using an optimized container, get() now uses try/catch instead of a redundant isset + $arr[$id] double-access.
  • Add in-memory method signature cache: CachingClassInspector now caches method signatures in memory (matching the existing constructorCache pattern), avoiding repeated serviceCache lookups for invoke() calls within the same request.

Context

When buildParameterArrayFromContainer resolves dependencies, it calls has() then get() for each constructor parameter. Both trigger ArrayAccess on the underlying container, and get() additionally has an isset guard before the actual $arr[$id] access. For containers with JIT provider loading (where offsetExists/offsetGet trigger provider resolution), this means 3 redundant resolution calls per parameter on the happy path. This PR reduces it to 1 via the new getIfDefined() method.

Test plan

  • All 88 injector tests pass (148 assertions)
  • Verify no regression in service resolution behavior

🤖 Generated with Claude Code

Eliminate redundant loadProviderForService calls during dependency
resolution by adding a single-operation getIfDefined() path that
combines has()+get() into one container access.

- ArrayContainerAdapter: add getIfDefined() that delegates to the
  underlying container's optimized method when available. Remove
  redundant isset guard from get() for optimized containers.
- Injector: use getIfDefined() in buildParameterArrayFromContainer
  when the container supports it, reducing per-parameter lookups
  from 3 loadProviderForService calls to 1.
- CachingClassInspector: add in-memory cache for method signatures
  (matching existing constructorCache pattern) to avoid repeated
  serviceCache lookups for invoke() calls.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant