Commit 8634b21
committed
minor symfony#60000 [Messenger] Improve return type of
This PR was squashed before being merged into the 7.3 branch.
Discussion
----------
[Messenger] Improve return type of `HandlerDescriptor::getHandler()`
| Q | A
| ------------- | ---
| Branch? | 7.3
| Bug fix? | no
| New feature? | no
| Deprecations? | no
| Issues | n/a
| License | MIT
Explanation is given in the individual commit messages. I'm repeating them here for visibility:
A return type of `callable` is not particularly useful, since it does not
actually guarantee that the returned value is callable by the caller. It only
guarantees that it is callable by the callee as seen in this example:
```php
class Foo {
public function bar(): callable
{
return [self::class, 'baz'];
}
private static function baz(): void
{
echo __METHOD__;
}
}
$foo = new Foo();
$cb = $foo->bar();
$cb();
```
Since `HandlerDescriptor::$handler` is already typed `\Closure` and since the
class is `final`, we can simply adjust the return type to `\Closure`.
---------------
Since the previous commit this is guaranteed to be a `\Closure`. A microbenchmark using:
```php
function closure_(\Closure $f) {
$f('abc');
}
function callable_(callable $f) {
$f('abc');
}
for ($i = 0; $i < 10000000; $i++) {
callable_(strrev(...));
}
```
Indicates that using `\Closure` as the parameter type is roughly 10% faster:
Benchmark 1: php callable.php
Time (mean ± σ): 708.4 ms ± 5.8 ms [User: 696.4 ms, System: 11.5 ms]
Range (min … max): 699.5 ms … 715.7 ms 10 runs
Benchmark 2: php closure.php
Time (mean ± σ): 647.1 ms ± 21.0 ms [User: 633.2 ms, System: 13.2 ms]
Range (min … max): 626.7 ms … 683.7 ms 10 runs
Summary
php closure.php ran
1.09 ± 0.04 times faster than php callable.php
Commits
-------
85d4ad3 [Messenger] Improve return type of `HandlerDescriptor::getHandler()`HandlerDescriptor::getHandler() (TimWolla)File tree
2 files changed
+2
-2
lines changed- src/Symfony/Component/Messenger
- Handler
- Middleware
2 files changed
+2
-2
lines changedLines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
| 50 | + | |
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
139 | 139 | | |
140 | 140 | | |
141 | 141 | | |
142 | | - | |
| 142 | + | |
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
| |||
0 commit comments