perf(optimizer): inline count for known array variables - #98
Merged
Conversation
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.
Single-argument
count($items)still calls the genericphp::fn::countwhen$itemsalready has a fixed array representation. The runtime checks the type before returning the array's stored element count.Lower that case to
static_cast<php::Int>(items.count()). The signed cast preserves PHP integer arithmetic. The shortcut accepts only known local array variables and retains argument-safety checks; recursive/two-argument calls, global wrappers, dynamic values, objects and compound expressions keep their existing paths.Adds codegen and runtime regression coverage plus a standalone benchmark with raw results and reproduction instructions.
Performance
Linux ARM64 in Docker, PHP 8.5.10 ZTS, PHPX
6a68f38, GCC-O2, no LTO; baseline692841a6. A 30-million-iteration array-update/count workload decreased from a median 319.63 ms to 192.73 ms (39.70% less elapsed time). All nine alternating measured pairs were faster after a discarded warm-up pair; checksums matched native PHP. Array updates keep the count from being a loop-invariant-only measurement.These are isolated workload measurements on a shared development machine, not an application-wide improvement or a comparison of execution speed against native PHP.
Validation
CountLiteralFoldTest|CountGlobalsTest: 6 tests / 10 assertions pass; the added codegen test fails on the original implementation.2999995050).git diff --checkpasses. Full-suite and cross-platform validation were not run.