file_file_download() function loads all reference entities in a loop one by one. I think this can be optimized, loading all entities at once outside of the foreach loop. As follows:

Before:

<?php
function file_file_download($uri, $field_type = 'file') {
    // ...
    foreach ($field_references as $entity_type  => $type_references) {
      foreach ($type_references as $id => $reference) {
        $entity = entity_load($entity_type, array($id));
        $entity = reset($entity);
        // ...
      }
    }
    // ...
}

After:

<?php
function file_file_download($uri, $field_type = 'file') {
    // ...
    foreach ($field_references as $entity_type  => $type_references) {
      $entities = entitiy_load($entity_type, array_keys($type_references));
      foreach ($entities as $entity) {
        // ...
      }
    }
    // ...
}

Patch follows.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Елин Й. created an issue. See original summary.

Elin Yordanov’s picture

And the patch. Please review.