[php-src] PR #18189: PoC: Make type checking constant time (for fast generics & DNF matching)
| From: | withinboredom | Date: | Sun, 30 Mar 2025 08:49:32 +0000 |
| Subject: | [php-src] PR #18189: PoC: Make type checking constant time (for fast generics & DNF matching) | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-31229@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/18189
Author: withinboredom
This PR introduces interned type trees to the engine, enabling canonical representation and
deduplication of complex types such as intersections and unions. The goal is to improve memory
efficiency and runtime performance involving complex type annotations.
## Why
Currently,
zend_type structures are duplicated and compared structurally throughout the
engine. This leads to redundant memory usage and slower runtime checks for unions and intersections.
By introducing interned type trees, we create a shared, normalized representation of each unique
type structure, allowing for memory savings, faster comparisons via pointer equality, and a
foundation for advanced type features in the future; such as pattern matching and generics.
## This Proof-of-Concept
I suspect this can be made more backward compatible by using oparray over changing
arginfo, but I wasn't able to figure out how to make it work. Ideally, we could
replace zend_type in php 9.0, but with both being utilized, about 0.5-1% more memory is
needed when compared to master in my tests.
### unresolved issues
- The way I am storing the tree and freeing it is probably disastrously wrong.
- I changed arginfo and function-info structs -- this possibly breaks many extensions.
## Micro-benchmarks
I ran a small collection of microbenchmarks on type checking to see how it performs compared to
master:
```
comparing [type_trees_local vs. master_local]
\Acme\Tests\Benchmark\TypeCheckBench
bench_native_scalar_type................R2 I9 - [Mo0.094μs vs. Mo0.098μs] -3.86% (±0.21%)
bench_native_class_type.................R4 I9 - [Mo0.104μs vs. Mo0.104μs] +0.82% (±0.22%)
bench_native_union_nullable.............R1 I1 - [Mo0.094μs vs. Mo0.098μs] -4.11% (±0.23%)
bench_native_union_deep.................R1 I4 - [Mo0.090μs vs. Mo0.095μs] -4.74% (±0.26%)
bench_native_deep_union_with_intersecti.R5 I7 - [Mo0.098μs vs. Mo0.104μs] -5.53% (±0.23%)
Subjects: 5, Assertions: 0, Failures: 0, Errors: 0
Storing results ... OK
Run: 134fed951f0415dcd36f841639dc64312b77dcc2
+----------------+--------------------------------------------+-----+--------+-----+----------+----------------+----------------+
| benchmark | subject | set | revs | its | mem_peak | mode
| rstdev |
+----------------+--------------------------------------------+-----+--------+-----+----------+----------------+----------------+
| TypeCheckBench | bench_native_scalar_type | | 100000 | 10 | ERR |
0.094μs -3.86% | ±0.21% -11.18% |
| TypeCheckBench | bench_native_class_type | | 100000 | 10 | ERR |
0.104μs +0.82% | ±0.22% +48.04% |
| TypeCheckBench | bench_native_union_nullable | | 100000 | 10 | ERR |
0.094μs -4.11% | ±0.23% -13.74% |
| TypeCheckBench | bench_native_union_deep | | 100000 | 10 | ERR |
0.090μs -4.74% | ±0.26% -8.85% |
| TypeCheckBench | bench_native_deep_union_with_intersections | | 100000 | 10 | ERR |
0.098μs -5.53% | ±0.23% +6.59% |
+----------------+--------------------------------------------+-----+--------+-----+----------+----------------+----------------+
```
I'm not sure why benchmarking is broken on this PR (probably something I did when trying to fix
a memory leak), but here's a link to [the last successful
run](https://github.com/bottledcode/php-src/actions/runs/14129838338) just before I tried fixing the
memory leaks.
Is this worth pursuing further, or should I take a different approach altogether?
cc: @arnaud-lb