Problem/Motivation

This is a performance enhancement. IncludeResolver::resolveIncludeTree() reads $field_item->entity for each reference item to get the referenced entity's type and ID. If the referenced entities are not already loaded or cached, this causes them to be loaded individually.

The method then calls loadMultiple() with the collected entity IDs. This causes the individually loaded entities to be loaded from the cache.

When the entity property definition specifies the target entity type, we can load in a batch instead of loading individually first and then in a batch.

Steps to reproduce

  1. Create 5 published articles with 2 distinct tags each.
  2. Clear the entity caches.
  3. Enable database query logging.
  4. Request /jsonapi/node/article?include=field_tags.
  5. Check the logs. The taxonomy_term_data base table is queried 10 times, once per term.

Proposed resolution

If the entity property definition specifies the target entity type, get the target ID from the item's main property (without loading the entity). The existing loadMultiple() call then loads all targets of that type in one batch.

Skip items with a NULL target ID. Preserve the existing $field_item->entity path for reference fields whose definitions do not specify a target entity type.

User interface changes

None.

API changes

None.

Data model changes

None.

AI disclosure: I used AI to help prepare the IS and MR (Astra and Opus 5.5).

Issue fork drupal-3626240

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

ptmkenny created an issue. See original summary.

ptmkenny’s picture

Issue summary: View changes
ptmkenny’s picture

Status: Active » Needs review