hooks and plugins - why my function is slow

They have: 426 posts

Joined: Feb 2005

I have implemented my first hooks and plugins system. I am using xdebug and Kcachegrind to profile my scripts. It is telling me that the function which actually executes the hooks and functions is really slow and I can see this when I run the scripts.

hooks and functions are stored in an array each hook name is the array index and the functions are stored separated by a |. I am using the call_user_func() and this is really slow. Any alternative?

What is wrong with this:

function execute_hooks($where, $args = '')
  {
if (isset($this->hooks[$where]))
    {
$these_hooks = explode('|', $this->hooks[$where]);
$result = $args;
foreach ($these_hooks as $hook)
      {
if (function_exists($hook)) { $result = call_user_func($hook, $result); }
}  
return $result;
}
else
    {
die("There is no such place ($where) for hooks.");
}
}

Any help or advice appreciated.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I think this is going to be the main issue:

$these_hooks = explode('|', $this->hooks[$where]);

I would store it in an array rather than a string where you have to explode several of them on every page request.

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

I've never used function_exists(), but that seems like it could be a resource hog. According to the docs, call_user_func() "Returns the function result, or FALSE on error" so it looks like you don't need to check that the function actually exists.

They have: 426 posts

Joined: Feb 2005

Actually, I moved the scripts from my shared hosting test environment to the product dedicated server and it runs more than 10 times quicker?

Could my shared hosting on Hostgator really be so slow. The scripts call Kelkoo and another shopping service and get the products back as XML which I convert to PHP object and create a new array which I send the the client as json_encoded?

I am using cURL, simpleXML and json_encoded. Apart from this I load up 3 plugins and use the call_user_func() method.

Are any of these resource intensive?

Or could it be that Hostgator shared hosting is just slow and rubbish?

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.