Skip to content

Avoid traversing identical constant arrays in isSuperTypeOf - #7019

Closed
keulinho wants to merge 2 commits into
phpstan:2.3.xfrom
keulinho:codex/constant-array-identity-fast-path
Closed

keulinho wants to merge 2 commits into
phpstan:2.3.xfrom
keulinho:codex/constant-array-identity-fast-path

Conversation

@keulinho

@keulinho keulinho commented Nov 10, 2026 •

Copy link
Copy Markdown

ConstantArrayType::isSuperTypeOf() recursively compares keys and values even when its argument is the exact same type object.
Return yes immediately for that case in both PHP and PHPStan Turbo.
This avoids repeated traversal of large nested shapes without changing the result for distinct operands.

The existing standalone bug-7903.php benchmark exercises this during loop-scope merges of shared nested array templates.
Its expected metrics are refreshed from the benchmark-measured CI artifact.
The implementation-specific mock test is removed.

The committed instruction baseline was 7,793,234,919; the optimized CI measurement is 6,358,641,712, a reduction of 18.4%.
Against the refreshed expectation, the previous count is 22.6% slower and exceeds the existing 20% regression tolerance.
All 93 measured CI benchmarks pass against the updated expectations.

Controlled full-CLI measurements on PHP 8.4.15 alternate before/after order, with only the identity shortcut changed in the backend being measured.
OPcache settings are identical, JIT is disabled, result-cache reuse is disabled with --debug, and both Turbo variants are built with the same compiler flags.
Each run verifies normal Turbo activation, including its enabled type-combination cache.

Implementation Pairs Before median After median Pairs faster after
PHP 12 1.8582 s 1.7299 s 11/12
Turbo 36 1.1410 s 1.1435 s 19/36

The median paired reduction is 6.8% for PHP and 0.4% for Turbo.
This fixture demonstrates a repeatable elapsed-time reduction in PHP; the Turbo difference is within observed timing variation and does not demonstrate a meaningful full-CLI speedup.
The earlier analysis-only Turbo timing claim is withdrawn: direct native-class activation bypassed the normal type-combination cache, so that measurement did not represent normal Turbo operation.

All 96 measured CLI runs report zero diagnostics.
The command shape used for each variant is:

php bin/phpstan analyse -c tests/bench/phpstan.neon --debug --no-progress --error-format=json tests/bench/data/bug-7903.php

Measurement configuration adds a runtime-verification bootstrap and gives each variant a separate temporary directory.
The complete ConstantArrayTypeTest passes in PHP and Turbo (196 tests, 254 assertions each).
The native baseline builds with strict compiler warnings, and the changed test passes upstream's coding standard.

This optimization is independent of array-shape preservation and does not change the complexity budget.

Related analysis and independently scoped changes: twigstan/twigstan#317.

);
}

public function testIdenticalArrayDoesNotCompareItsValues(): void

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.

instead of a mock based test which relies on implementation details, it would be great to have a file when analysed before/after this change shows a meaningful speed improvement

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The tests/bench/data/bug-7903.php case already works as a good example for this improvement, for now i decided to use that case as benchmark and decrease the accepted perf threshholds on that case

happy to create a new dedicated benchmark case when you prefer that

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 can see instruction counts beeing reduced in the benchmarks for some cases. I am not able to reproduce a time-wise performance improvement when running these benchmark on its own so...

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.

3 participants