perf(container): defer aspect construction with class-name registration - #591
Merged
Merged
Conversation
registerAspect() now also accepts an aspect class-name with an optional factory closure. The aspect is constructed on first use (first advice hit, or aspect enumeration during weaving) instead of on every request during configureAop(), so a hot-cache request no longer pays construction, reflection and resource tracking for aspects it never touches. - Instance registration keeps working unchanged, and remains the right choice when construction side effects are part of the design (see InconsistentlyWeavingAspectKernel). - An aspect with required constructor arguments must pass a factory; default construction is validated when the aspect materializes. - In debug mode the aspect source file is registered as a tracked resource at registration time (via reflection, no instantiation), preserving cache-freshness semantics that eager registration provided. BC note (4.0): AspectContainer::registerAspect() signature widened to Aspect|string plus optional factory closure. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V1Z87HZ2iz23WPTtTYqFxE
…y-aspect-registration
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.
What
registerAspect()now also accepts an aspect class-name with an optional factory closure, deferring aspect construction to first use (first advice hit, or aspect enumeration during weaving). TodayconfigureAop()constructs every aspect on every request — plus aReflectionObject, interface scan andis_readable()stat per aspect inadd()— even when the request never triggers a single advice.Details:
getServicesByInterface(Aspect::class)materializes them, soAspectLoader,LazyAdvisorAccessorand the debug commands observe the same aspects as before.AspectExceptiontelling you to pass a factory. A non-Aspectclass is rejected likewise.getServicesByInterface()/materializeService(): autoloading an aspect class inside the materialization loop goes through the weaving pipeline, which re-enters the same enumeration and consumes pending factories — found by a real cold-boot run, covered bytestInterfaceQuerySurvivesReentrantMaterialization.Why (measured)
Fixture project (9 aspects), warm cache,
debug=false, opcache file cache, median of 15 (kernel init + 6 woven class autoloads + first intercepted call), PHP 8.5:AspectKernel::init()BC note (4.0)
AspectContainer::registerAspect()signature widens toAspect|string $aspectOrClassName, ?Closure $aspectFactory = null. Existing instance-based calls compile and behave identically. CustomAspectContainerimplementations must update the signature.Part of the boot-time series
Second of five sequential PRs (container laziness → lazy aspects → lazy transformers → trusted prebuilt cache → cache-state split).
🤖 Generated with Claude Code
https://claude.ai/code/session_01V1Z87HZ2iz23WPTtTYqFxE
Generated by Claude Code