Skip to content

refactor(Type): keep one TypeContextFactory per TypeResolver - #2364

Merged
DerManoMann merged 3 commits into
zircote:masterfrom
leo-pansonato:refactor/reuse-type-context-factory
Nov 8, 2026
Merged

DerManoMann merged 3 commits into
zircote:masterfrom
leo-pansonato:refactor/reuse-type-context-factory

Conversation

@leo-pansonato

@leo-pansonato leo-pansonato commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2363

Overview

TypeResolver created a new TypeContextFactory for every reflector it resolved. Symfony versions with a use-statement cache discarded it with each instance, so the same class source file was read again for every property. This adds noticeable overhead on slower filesystems, such as Docker Desktop bind mounts.

Changes

  • TypeResolver keeps separate docblock and native reflection context factories for its lifetime.
  • The cache regression test runs when the installed Symfony factory supports caching.
  • Native self and parent resolution is covered on every supported TypeInfo version.

Validation

  • Full test suite passes on PHP 8.4 with TypeInfo 7.3.8, 8.0.0 and 8.1.11. Only the cache-specific test is skipped on versions without that cache.
  • Static analysis and code style pass; regenerating reference documentation leaves no changes.

Docblock and reflection type contexts now come from two factories created with the resolver.
… source file

Native types arrive fully qualified, so the context only needs the declaring class for self, static and parent.

@DerManoMann DerManoMann left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, and for the numbers.

The two new tests fail with the lowest dependencies, which install symfony/type-info 7.3.8:

  • TypeContextFactory only caches the use statements from 8.1 on, so on 7.x and 8.0 the file is still read once per property. The change is fine there, it just does not help. Can the test be skipped when the factory has no cache?
  • In 7.3 TypeContext::getCalledClass() normalizes the class name against the namespace, so a fully qualified name comes back with the namespace twice and self/parent resolve to a class that does not exist. Building the TypeContext by hand depends on how the installed version stores it.

Can we drop the second commit and keep a second factory for the native types instead, as in the issue? That costs one extra read per class, and it works the same on every version we support.

Restore the native TypeContextFactory and skip the cache regression test when Symfony has no use-statement cache.
@leo-pansonato

leo-pansonato commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for catching this. I've reverted that approach and restored the two separate factories from the issue.

The cache test now skips when TypeContextFactory has no usesCache; the self/parent test keeps the source file available and runs on all versions.

I also retested the revised changes together with #2365 against our API. Median generation time dropped from 20.0 s to 11.1 s on the Docker Desktop bind mount.

@DerManoMann
DerManoMann merged commit a78b362 into zircote:master Nov 8, 2026
20 checks passed
@DerManoMann

Copy link
Copy Markdown
Collaborator

Thanks @leo-pansonato

@DerManoMann DerManoMann added the 6.x Also backport to the 6.x branch label Nov 8, 2026
DerManoMann pushed a commit that referenced this pull request Nov 8, 2026
@DerManoMann DerManoMann removed the 6.x Also backport to the 6.x branch label Nov 8, 2026
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.

[Feature]: Reuse TypeContextFactory in TypeResolver to avoid re-reading class files for every property

2 participants