field_attach_load() does a lot of "call a function, get an array result, and then iterate over the array to merge the results with others." e.g.:

    // Invoke the storage engine's hook_field_storage_load(): the field storage
    // engine loads the rest.
    $additions = module_invoke(variable_get('field_storage_module', 'field_sql_storage'), 'field_storage_load', $obj_type, $queried_objects, $age, $skip_fields);

    // First, merge the additions from the storage engine.
    foreach ($additions as $id => $obj_additions) {
      foreach ($obj_additions as $key => $value) {
        $queried_objects[$id]->$key = $value;
      }
    }

Instead, I suggest that we pass the collection array directly to each hook by reference and let them build it up directly, reducing data copying and iterations. Note that hook_field_attach_pre_load() already works this way.

Yes, this requires more cooperation among modules, but we already have hooks that let anyone stomp all over anything anyway, so...

Comments

moshe weitzman’s picture

Right - drupal_alter() just passes an object by reference and it works well in practice. Perhaps use that.

yched’s picture

Yes, I've been thinking about this and couldn't remember why 'gather data to cache' forced us to have load hooks return the results instead of altering $object like for other ops.
I was thinking of refactoring this along with #362024: Field Attach: Make hook_field_load() be 'multiple' like field_attach_load()

catch’s picture

node, taxonomy and vocabulary _load() hooks also act on (arrays of) objects by reference, so it'd be consistent with that too.

bjaspan’s picture

Status: Active » Postponed

yched is right, this really should be done in conjunction with #362024: Field Attach: Make hook_field_load() be 'multiple' like field_attach_load(). Let's revisit or close this issue when that one is done.

jjkd’s picture

Status: Postponed » Active
yched’s picture

Status: Fixed » Closed (fixed)

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