feat(closure): closure parameter type narrowing from call-site literals - #103
Open
yuan-dian wants to merge 3 commits into
Open
feat(closure): closure parameter type narrowing from call-site literals#103yuan-dian wants to merge 3 commits into
yuan-dian wants to merge 3 commits into
Conversation
…terals Infer closure parameter types from call-site literal arguments to generate native C++ types instead of php::Var. When all call sites pass the same literal type, the lambda signature uses the native type directly. Changes: - LocalClosureAnalyzer: track call sites per candidate, inferParamTypes() detects int/float/string/bool/array literals, unary ops, boolean expressions, string concatenation, cast expressions, and ConstFetch - ClosureGenerator: use inferred types in lambda signatures, skip type checks when effectiveType is native, add newClosureWithParameters for closures with type checks - Translator: wire inferParamTypes() into candidate processing - FunctionContext: document callSites and inferredParamTypes keys Performance (5M iterations): - fn()(42): 10ms -> 5ms (2x faster) - fn()(3.14): 85ms -> 8ms (10x faster) - fn()(true): 14ms -> 4ms (3.5x faster) - fn(int $x)(42): 10ms (no regression, approach A: no declaration narrowing) Tests: 22 new unit tests, 60 total closure tests pass
- Remove stray main() call outside function body (TypePHP prohibits loose code) - Change fn($x) => $x to fn($x) => count($x) to match expected int(2) output
run-tests.php requires the closing PHP tag to properly extract the --FILE-- section. All other PHPT tests in the project have it.
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
Infer closure parameter types from call-site literal arguments to generate native C++ types instead of
php::Var. When all call sites pass the same literal type, the lambda signature uses the native type directly, eliminating runtime type checks.Performance
fn($x)(3.14)floatfn($x)("hello")stringfn($x)(true)boolfn($x)(42)intfn($x)($i)variablefn($x)([1,2,3])arrayChanges
Core Logic:
LocalClosureAnalyzer.php: Track call sites per candidate,inferParamTypes()detects int/float/string/bool/array literals, unary ops, boolean expressions, string concatenation, cast expressions, and ConstFetchClosureGenerator.php: Use inferred types in lambda signatures, skip type checks when effectiveType is native, addnewClosureWithParametersfor closures with type checksTranslator.php: WireinferParamTypes()into candidate processingFunctionContext.php: DocumentcallSitesandinferredParamTypeskeysTests:
ClosureParamTypeTest.php: 22 unit tests covering type declarations, call-site literals, multi-call fallback, unary ops, boolean expressions, string concatenation, cast expressions, goto, nested functions, and class method guardclosure-param-type.php: Test fixture with 20+ scenariosclosure-param-type-class.php: Class method guard testclosure-param-type-inference.phpt: End-to-end integration testHow It Works
LocalClosureAnalyzertracks all call sites for each closure candidate and infers parameter types from literal argumentsphp::Int,php::Float) instead ofphp::Varphp::Var+ runtime type check (unchanged behavior)Scope
Safety
php::Var