I've got a bunch of forms using panels and ctools_entity_form_field_content_type_content_types()
was making lots of cache calls through field_info_instances()

This can be avoided by not making direct calls to each entities' bundles' field instances in a loop but instead just ask for them all upfront.

xhprof

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joelpittet created an issue. See original summary.

joelpittet’s picture

FileSize
1.11 KB

Here's the patch for review.

joelpittet’s picture

Status: Active » Needs review
FileSize
1002 bytes

Forgot relative flag on the last patch.

rivimey’s picture

@joelpittet, this all looks great. My only concern is whether

field_info_instances() can be considered equivalent to field_info_instances(e,b) in all relevant cases.

Looking at the code for both I think it's probably true, but I'm not sure... can you elaborate?

Apart from that, good to go!

joelpittet’s picture

Thanks for the review @rivimey! Yes, the first grabs all field instances' info for all entities and their bundles where the other grabs a specific entity and bundle pair's field instances' info.

The way it worked before it grabbed all the entities and bundles then looped through the entities and bundles to grab the instances. The new one grabs all the fields upfront (which may be cached statically from previous calls) and I added the condition in there because one bundles may not have fields as it loops through.

It's the same function call but there is a pattern in Drupal to return all things without arguments and a specific item if there is an argument passed. (see most commerce API functions). The big difference is for performance is the possibility of caching all the fields already is greater than the individual field instance being cached AND multiple function calls in a loop can slow things down some depending on how many things it's looping through (in my case a lot of entities and 250ish field instances).

Sorry if I went a bit long on my explanation hope it helps

rivimey’s picture

Thanks Joel, that helps. I get the thing about the pattern - that is fine and I completely accept the approach you are using - but if you examine the field_info_instances() function itself the implementation of the two cases (no args, with args) is quite different. I guess though that one could call a fundamental change here a bug in Core and so not a reason to reject this.

Ok, I am happy with the change; however it would be great if you could include some simpletests for this code, even something fairly trivial. I know the change you're making is minor, but we need to get the test coverage up above < trivial > percent :-) I think the decision is that tests for plugins live close to the plugin, rather than in the ctools-root tests folder, but anything is good!

joelpittet’s picture

One thing that eased my mind about the patch even more is if you look in the xhprof results the function call change on all the other child functions didn't change. Meaning it did the same volume of build-up. But also I was stepping through with xdebug too in PHPStorm to ensure the results were the same after I was done.

rivimey’s picture

Status: Needs review » Reviewed & tested by the community

Would have liked tests, but... :(

rivimey’s picture

joseph.olstad’s picture

We've applied this change, thanks again @joelpittet for the excellent xhprof illustration of performance results and patch!

joseph.olstad’s picture

This patch is golden for us, would think that it'd be a big win for the #2828925: Plan for CTools 7.x-1.13 release

thanks again joelpittet , really looking forward to this being in a release

japerry credited japerry.

japerry’s picture

Status: Reviewed & tested by the community » Fixed

Nice gain! Committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.