perf(compiler): infer closure parameter types from declarations and call sites - #100
Closed
yuan-dian wants to merge 5 commits into
Closed
perf(compiler): infer closure parameter types from declarations and call sites#100yuan-dian wants to merge 5 commits into
yuan-dian wants to merge 5 commits into
Conversation
…all sites * perf(compiler): use PHP type hints to narrow native local closure parameters to php::Int/Float/Bool/Str/Array * perf(compiler): infer closure parameter types from single call-site literal arguments * perf(compiler): skip redundant runtime type checks when parameter is already a native C++ type * fix(compiler): prevent closure variable names from overwriting function C++ names in Translator
* test(compiler): verify type hint parameters use native C++ types (php::Int/Float/Bool/Str/Array) * test(compiler): verify call-site literal inference narrows closure parameters * test(compiler): verify multi-call closures remain php::Var when types conflict * test(compiler): update LocalClosureCodegenTest for php::Int parameter change
* Handle UnaryMinus/UnaryPlus wrapping numeric literals (-42, +3.14) * Add boolean expression inference (&&, ||, !, ===, !==, ==, !=, <, <=, >, >=, <=>, instanceof) * Add string concatenation inference when both operands are strings
Contributor
Author
|
Close, wait for all problems to be solved and resubmit PR. |
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.
Summary
Add closure parameter type inference to the TypePHP compiler. When closure parameters have PHP type declarations or can be inferred from single call-site literal arguments, the compiler now generates native C++ types (
php::Int,php::Float,php::Bool,php::Str,php::Array) instead ofphp::Var. This eliminates runtime type checks and enables native C++ arithmetic, resulting in significant performance improvements.Performance
fn($x)(42)fn($x)(3.14)fn($x)(true)fn($x)(-42)fn($x)(1===2)fn($x)("a"."b")fn(int $x)(42)What's Changed
Phase 1: Type Declaration Inference
PHP type hints on closure parameters are used to generate native C++ types:
Phase 2: Call-Site Literal Inference
When a closure is called exactly once with literal arguments, the parameter type is inferred:
Edge Cases Handled
-42→php::Int,+3.14→php::Float1 === 2,true || false,$obj instanceof Foo→php::Bool"hello" . "world"→php::Str(both operands must be strings)php::Varwhen types conflict across callsBug Fix
Fixed a variable name collision in
Translator.phpwhere the foreach loop variable$nameoverwrote the function's native C++ name, causing incorrect output for functions with closures.Files Changed
src/Analysis/LocalClosureAnalyzer.phpinferParamTypes(),detectArgType(), call-site trackingsrc/Generator/ClosureGenerator.phpsrc/Translator.php$namecollisionphpunit/code/closure-param-type.phpphpunit/src/ClosureParamTypeTest.phpphpunit/src/LocalClosureCodegenTest.phptests/compiler/closure/closure-param-type-inference.phptTest Coverage
testTypeHintParametersUseNativeCppTypes— Verifies type-hinted params generate native typestestCallSiteInferredParametersUseNativeCppTypes— Verifies literal call-site inferencetestMultiCallClosureRemainsPhpVar— Verifies fallback tophp::Varfor multiple callstestUnaryMinusInfersNativeType— Verifies-42→php::Int,-3.14→php::FloattestBooleanExpressionsInferBoolType— Verifies comparison/logical ops →php::BooltestConcatStringInference— Verifies string concat →php::Str