Skip to content

Improve laziness of container XML parsing#487

Merged
staabm merged 1 commit intophpstan:2.0.xfrom
xificurk:lazy-dic
May 10, 2026
Merged

Improve laziness of container XML parsing#487
staabm merged 1 commit intophpstan:2.0.xfrom
xificurk:lazy-dic

Conversation

@xificurk
Copy link
Copy Markdown
Contributor

@xificurk xificurk commented May 8, 2026

Parsing the XML into the DTOs is a costly operation on large Kernels. This PR defers the ServiceMap and ParameterMap initialization until the first use of their content.

Comment thread src/Symfony/ServiceMap.php Outdated
public function getService(string $id): ?ServiceDefinition;

public static function getServiceIdFromNode(Expr $node, Scope $scope): ?string;
public function getServiceIdFromNode(Expr $node, Scope $scope): ?string;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this is a BC break, given the fact that the interface has @api. If someone implements the interface his code will break.

Same exist for ParameterMap ; so I'll need @ondrejmirtes review.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Rewritten to keep BC

@ondrejmirtes
Copy link
Copy Markdown
Member

Yeah, removing the static keyword is a BC break. For 3.0 we could add @api-do-not-implement there (many PHPStan core interface already have it) and make the interface nicer. But I think this improvement could still be implemented without changing the interfaces?

@xificurk
Copy link
Copy Markdown
Contributor Author

xificurk commented May 9, 2026

@ondrejmirtes Yes, I'll rework this to maintain BC.

Removing the static would make sense for 3.0 - all the existing calls are on a dynamically resolved instance. No true static calls exist and given the implemented logic, they should not exist.

@xificurk xificurk marked this pull request as draft May 9, 2026 08:40
@staabm
Copy link
Copy Markdown
Contributor

staabm commented May 9, 2026

@xificurk please also provide a profiler screenshot / hyperfine perf numbers so we can see the impact of this PR

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@xificurk xificurk marked this pull request as ready for review May 9, 2026 17:15
@xificurk
Copy link
Copy Markdown
Contributor Author

xificurk commented May 9, 2026

@staabm As discussed in phpstan/phpstan-src#5538 I am targetting few file partial analysis on larger code base.

A test on a very simple file bin/phpstan analyse bin/console.php median of 10 runs: 9.22s -> 8.77s. This is due to a complete elimination of ServiceMap and ParameterMap initialization in the worker process (confirmed via SPX trace).

@staabm
Copy link
Copy Markdown
Contributor

staabm commented May 9, 2026

median of 10 runs: 9.22s -> 8.77s.

Awesome 🤩

Comment on lines +32 to +33
$lazyParameterMap::$parameterMapFactory = $parameterMapFactory;
$lazyParameterMap::$parameterMap = null;
Copy link
Copy Markdown
Contributor

@staabm staabm May 10, 2026

Choose a reason for hiding this comment

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

can we move this into a __construct of the above inline class?

same in LazyServiceMap

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not as long as LazyParameterMap::__construct remains private. The private constructor is intentional - it prevents any 3rd party inheritance.

@staabm staabm requested a review from ondrejmirtes May 10, 2026 08:10
@ondrejmirtes
Copy link
Copy Markdown
Member

median of 10 runs: 9.22s -> 8.77s

What is the remaining 8 seconds taken by?

@staabm staabm merged commit fdd0cb5 into phpstan:2.0.x May 10, 2026
43 checks passed
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.

4 participants