drupal_find_theme_functions called on every page - is our theme registry being rebuilt? Our new D7 site was running very slowly so I did some profiling with devel/xhprof and found that D7 was doing millions of preg_grep function calls within drupal_find_theme_functions() in theme.inc. This happened on every page load.

It's my understanding that the drupal_find_theme_functions function should only be called when the theme registry is being rebuilt - am I correct in that?

I made sure that "rebuild theme registry" was off in devel, and then disabled devel entirely. However, I'm still seeing those millions of function calls to preg_grep in drupal_find_theme_functions on every page load (I added a piece of code to theme.inc to log when the function was called).

We're using a theme based on Omega and if we are rebuilding the theme registry each time I'm not seeing how/why it's happening. We're turning off as many contrib modules as we can in the meantime. Any help/advice would be MUCH appreciated!!
Nice job on using XHProf to debug what's going on here. drupal_find_theme_functions() should get called once per registry rebuild for the core and installed modules, then once again as each installed theme is utilized.  Are you noticing it once per page load, many times per page load, etc?  I'm going to send you down a slightly different path on this as you should be more interested on what's causing the rebuild, not what happens when the rebuild occurs.

I'm assuming that you're working with a recent release of D7.  Use the devel module and put code below into drupal_theme_rebuild() at line 350 of includes/theme.inc to see what functions are being called on the way to the registry rebuild and that should give some clue as to what is triggering it.

dsm(debug_backtrace());


A quick note on what you should expect to see.  I've done some testing and with a mostly empty Drupal install with only a few popular modules installed.  The registry rebuild is only happening when I clear the theme cache or perform some action that causes it to be cleared.  If you look at the functions being called prior to the rebuild, you should see some indication as to what was responsible for causing it.

When I clear caches from the normal admin screen (admin/config/development/performance) my call stack is 9 functions deep and it passes through a form submission handler that triggers the process.  When I clear the cache from the admin_menu contributed module, my call stack is 5 functions deep and it passes through a page callback that initiates the cache clear.  Your case should be showing something a bit out of the ordinary.