Problem/Motivation
context_admin_entityref_create_node_get_children and context_admin_termref_create_node_get_children call field_info_instances hundreds of times on a large site with many entity types and panel pages. Seeing about >200 calls on cache expired or page loads per function. Simple call count isn't important here, but analyzing the following:
foreach ($entities as $entity_type => $entity) {
foreach ($entity['bundles'] as $bundle_type => $bundle) {
foreach (field_info_instances($entity_type, $bundle_type) as $field_name => $field) {
$field_info = field_info_field($field_name);
We don't check the field type before calling field_info_field or field_info_instances, which is now doable with field_info_field_map, and we could check the field type in the second loop.
This isn't a Major issue because it's still fairly fast with the field cache (development), but it does increase the number of database cache calls, which may have a greater effect when there are many concurrent requests around a cache flush (production).
Additionally it looks like the code in those functions does some weird variable mutation that makes this task a bit more complicated:
$bundle = array_shift($field_info['settings']['handler_settings']['target_bundles']);
noderef_create_node, node_reference_compare and user_create_menu also have some calls, but not to the same extent.
Proposed resolution
It should be possible to call field_info_field_map once at the top of those functions, and further reduce the calls to field_info_field (and maybe a more targeted field_info_instance call) based on entity_reference field types.
Remaining tasks
Write a patch to refactor these functions.
Comments