Provide different versions of ModuleHandler::invokeAll() for performance

Created on 31 March 2017, almost 8 years ago
Updated 14 January 2025, 10 days ago

ModuleHandler::invokeAll() contains some costly logic to recursively merge results from hook implementations.

In some cases this recursive merging is not needed, or even undesirable.
In some cases, the return value is not needed at all.

Alternative methods could be provided for these cases, promising performance improvements and an overall simpler and more predictable behavior.

These alternative methods could be part of ModuleHandler, or they could live in a separate class that depends on ModuleHandlerInterface.
The latter option would allow to introduce this change without changing the interface.

Developers could then decide which version of the method they want to call.

Example implementation:

  public function invokeAllReturnNothing($hook, array $args) {
    foreach ($this->moduleHandler->getImplementations($hook) as $module) {
      $function = $module . '_' . $hook;
      call_user_func_array($function, $args);
    }
  }

  public function invokeAllReturnArraySum($hook, array $args = array()) {
    $return = array();
    foreach ($this->getImplementations($hook) as $module) {
      $function = $module . '_' . $hook;
      $module_result = call_user_func_array($function, $args);
      if (NULL !== $module_result && is_array($module_result)) {
        $return = $module_result + $return;
      }
    }
    return $return;
  }

and maybe this one for completeness..

  public function invokeAllReturnKeyed($hook, array $args = array()) {
    $return = array();
    foreach ($this->getImplementations($hook) as $module) {
      $function = $module . '_' . $hook;
      $return[$module] = call_user_func_array($function, $args);
    }
    return $return;
  }

I am sure that other optimizations could be made to ModuleHandler::invokeAll(). But this should happen in separate issues.

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component

extension system

Created by

πŸ‡©πŸ‡ͺGermany donquixote

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024