8000 minor #60000 [Messenger] Improve return type of `HandlerDescriptor::g… · simPod/symfony@8634b21 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8634b21

Browse files
minor symfony#60000 [Messenger] Improve return type of HandlerDescriptor::getHandler() (TimWolla)
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()`
2 parents c7279c1 + 85d4ad3 commit 8634b21

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

src/Symfony/Component/Messenger/Handler/HandlerDescriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct(
4747
}
4848
}
4949

50-
public function getHandler(): callable
50+
public function getHandler(): \Closure
5151
{
5252
return $this->handler;
5353
}

src/Symfony/Component/Messenger/Middleware/HandleMessageMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private function messageHasAlreadyBeenHandled(Envelope $envelope, HandlerDescrip
139139
return false;
140140
}
141141

142-
private function callHandler(callable $handler, object $messa 50A7 ge, ?Acknowledger $ack, ?HandlerArgumentsStamp $handlerArgumentsStamp): mixed
142+
private function callHandler(\Closure $handler, object $message, ?Acknowledger $ack, ?HandlerArgumentsStamp $handlerArgumentsStamp): mixed
143143
{
144144
$arguments = [$message];
145145
if (null !== $ack) {

0 commit comments

Comments
 (0)
0