feat: Reuse live RequestStack objects during ?include serialization - #30
Draft
stephanschuler wants to merge 1 commit into
Draft
feat: Reuse live RequestStack objects during ?include serialization#30stephanschuler wants to merge 1 commit into
stephanschuler wants to merge 1 commit into
Conversation
When serializing a TopLevel response with "?include=foo.bar", every relationship is mapped from a domain object to its type/id identifier and back to a domain object while the RequestStack is traversed. Each "identifier to domain object" step goes through the property mapper, which re-materializes the entity via Doctrine — adding a database round-trip, or at least requiring the object to be present in the persistence unit of work. This adds a RequestStackRegistry singleton that holds weak references to all active RequestStack objects. Each RequestStack registers itself on creation and indexes the resources it holds by type and id. When an entity has to be reconstructed from an identifier — both in RequestStack::pushIdentifier() and in the schema-based entity type converter — the registry is consulted first and an already-loaded object is reused. This avoids the redundant Doctrine lookup and preserves object identity across (nested) serialization passes. As the registry only keeps weak references, finished RequestStacks are garbage-collected normally.
paxuclus
approved these changes
Jul 1, 2026
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.
When serializing a TopLevel response with "?include=foo.bar", every relationship is mapped from a domain object to its type/id identifier and back to a domain object while the RequestStack is traversed. Each "identifier to domain object" step goes through the property mapper, which re-materializes the entity via Doctrine — adding a database round-trip, or at least requiring the object to be present in the persistence unit of work.
This adds a RequestStackRegistry singleton that holds weak references to all active RequestStack objects. Each RequestStack registers itself on creation and indexes the resources it holds by type and id. When an entity has to be reconstructed from an identifier — both in RequestStack::pushIdentifier() and in the schema-based entity type converter — the registry is consulted first and an already-loaded object is reused.
This avoids the redundant Doctrine lookup and preserves object identity across (nested) serialization passes. As the registry only keeps weak references, finished RequestStacks are garbage-collected normally.