Unnecessary use of Closure::fromCallable()?

Created on 27 May 2023, over 1 year ago
Updated 14 June 2023, over 1 year ago

Problem/Motivation

I see this code in HuxModuleHandler:

      $hookInvoker = \Closure::fromCallable([$service, $methodName]);

Is this needed?
[$service, $methodName] is already a callable, so do we need to wrap it in a closure?
I would imagine this can have a performance impact, but I would not know how big.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

📌 Task
Status

Closed: works as designed

Version

1.2

Component

Code

Created by

🇩🇪Germany donquixote

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @donquixote
  • 🇩🇪Germany donquixote

    I see this is also done in core.
    📌 Delegate all hook invocations to ModuleHandler Fixed
    https://git.drupalcode.org/project/drupal/-/commit/7e81e515dacdf07c0c6d0...

    In one place, invokeAllWith() in core, the reason might be to encapsulate the callable from other code.
    In the other place, invoke() in core, the reason is not so clear.

    I see "Closure::fromCallable" mentioned in #2616814, but I don't see the explanation why we need it.
    I also see you were involved :)

  • 🇩🇪Germany donquixote

    Btw the variable $hookInvoker in core should rather be something like $implementorCallback or $implementingCallback.

    (sorry to spam this issue with somewhat off-topic remarks)

  • Status changed to Closed: works as designed over 1 year ago
  • 🇦🇺Australia dpi Perth, Australia

    To be honest I think the `callable` typehint is misleading. It should have been Closure from the start. That was the intention. I dont know how callable vs Closure slipped by, maybe \Closure as a typehint just feels weird. Regardless.

    Btw the variable $hookInvoker in core should rather be something like

    The variable makes sense knowing the history (per above). I had intended it to only be a closure. I'm pretty sure its all `$hook` in core though, there are remnants of `$hookInvoker` in Hux

    As such I have no plan to change it.

    Given the test results from https://wiki.php.net/rfc/closurefromcallable I'm not really concerned about performance, it seems to be better in some cases.

    As you may see in Hux particularly, these closures are stored in memory and "cached" just for the request. I imagine there is low overhead in this aspect.

    --

    I think the best way forward would be to progress with replacing `callable` with `Closure`, I think in reality there will be little trouble changing, as this is a narrowing of the type.

Production build 0.71.5 2024