Avoid needing to call uasort so many times when building forms

Created on 31 July 2025, 3 months ago

Problem/Motivation

I noticed uasort was called 687 times when building a node form. Drupal\metatag\MetatagManager::sortedTags was responsible for 413 of those!

Somehow cache the sort and avoid runtime sorting

Steps to reproduce

Proposed resolution

\Drupal\metatag\MetatagManager::sortedTags calls \Drupal\metatag\MetatagManager::tagDefinitions which calls \Drupal\Core\Plugin\DefaultPluginManager::getDefinitions

Use uasort for the definitions during plugin discovery so that the sort is cached.

- \Drupal\metatag\MetatagManager::sortedGroups may be easier
- \Drupal\metatag\MetatagManager::sortedTags may be harder as its sorted within the group

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

2.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States mglaman WI, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @mglaman
  • πŸ‡ΊπŸ‡ΈUnited States damienmckenna NH, USA

    I'm shocked at it being called 413 times because of Metatag, that seems excessive. How many tags were available on the site?

    Would the correct approach be to override getDefinition() in the two plugin discovery classes and sort them there?

  • πŸ‡ΊπŸ‡ΈUnited States damienmckenna NH, USA

    I did some testing locally and from what I can see on a cold cache load of a single node page it only runs uasort() once for each tag group plus once more to sort the groups. So in your example were there 412 tag groups?

  • πŸ‡ΊπŸ‡ΈUnited States mglaman WI, USA

    I'll try to do some more debugging vs just reading calls from the Blackfire graph and report back. We also have several Metatag contrib running.

Production build 0.71.5 2024