Optimize ModuleHandler::invokeAll()

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

Looking at ModuleHandler::invokeAll() (or module_invoke_all() in D7), I see a number of possible optimizations.

  1. The recursive result merging is not necessary in many cases. This can be discussed separately, see πŸ“Œ Provide different versions of ModuleHandler::invokeAll() for performance Active
  2. Replace $function = $module . '_' . $hook with $function = $module . $suffix, where $suffix can be built outside the loop.
  3. The result of this string concatenation can be cached in a member variable.
  4. call_user_func_array() can be replaced with $function($x, $y, $z), at the cost of making it more verbose and less DRY. Yes, I am suggesting to copy the main part of the function for different parameter counts.
  5. Replace isset($result) with NULL !== $result.
  6. Provide a version of NestedArray::mergeDeep() which accepts exactly two parameters and uses its own implementation instead of calling self::mergeDeepArray(). There is unnecessary indirection and parameter juggling.

I played with changes equivalent to these on a Drupal 7 site, where module_invoke_all('permission') got measurably faster.
Real benchmarks have yet to happen.

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component

extension system

Created by

πŸ‡©πŸ‡ͺGermany donquixote

Live updates comments and jobs are added and updated live.
  • needs profiling

    It may affect performance, and thus requires in-depth technical reviews and profiling.

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