1

I am using module_invoke() many times in the front page to invoke lots of block. Does this effect the site performance? Thank you.

1 Answer 1

0

module_invoke() is essentially using three functions: function_get_args(), function_exists() and call_user_func_array(). As it doesn't invoke any database API function, it doesn't influence the performance a lot.
Using module_invoke() allows to write code that is compatible with future versions of the module or Drupal. There is a feature report on drupal.org which asks to use a different schema for the hook names; if that would be implemented (it could happen in Drupal 8), then the code that invokes directly the hooks should be changed.

In specific cases, where (for example) you are invoking the same hook implemented by the same module many times in row, it is possible to change the code to do something similar to:

  • verify the module implements the hook you need, once, at the beginning
  • call the hook directly when you need the value returned by the hook

This should be done if the module is really invoking the same hook implemented from the same module many times in row (which doesn't exclude the hook is being invoked in a loop).

To notice that module_invoke() cannot be used when one of the parameters needs to be passed by reference. There are specific functions that invoke hook implementations in that cases (e.g. comment_invoke_comment(), but those functions invoke the hook implemented by all the modules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.