Problem/Motivation

Spotted this when profiling a cold cache request on a site with token and metatag module installed.

For date formats and field tokens, token checks TokenModuleProvider::getTokenModule before defining a token (probably to avoid overwriting tokens from core?). On this site at least, that call triggers discovery and takes about 800ms.

If we remove the check from those two places, it doesn't get called at all.

I didn't yet look into the full history of why this check exists in the first place, but if it's safe to remove it would presumably defer that discovery until the token browser is viewed rather than blocking all HTML requests after a cache clear.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#8 3621357 after.png679.35 KBcsakiistvan
#8 3621357 before.png406.46 KBcsakiistvan

Issue fork token-3621357

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

catch created an issue. See original summary.

catch’s picture

Status: Active » Needs review
catch’s picture

Issue tags: +Performance

berdir’s picture

Yes, token alters in a lot of tokens, to figure out if tokens are defined by itself and not others, it checks that. Otherwise it might also try to act on core tokens such as node:title.

It's already optimized to use a cache collector, this only happens on the first request, afterwards it read from a minimal cache the in the bootstrap bin since the most recent release or so.

csakiistvan’s picture

Assigned: Unassigned » csakiistvan
csakiistvan’s picture

Assigned: csakiistvan » Unassigned
Status: Needs review » Needs work

❌ Does not fix the issue — applied MR !137, ran ddev drush cr and loaded /node/4 as anonymous with metatag enabled: the call still arrives from TokenTokensHooks.php:651, so TokenModuleProvider::getTokenModule() keeps token info discovery on the critical path of every HTML request after a cache clear.

Notes

  • The two checks the MR removes are not the only ones: extending the same reasoning to lines 651, 659, 667, 673 and 679 would take the discovery off the request path entirely, which is what the issue summary aims for.
  • If the ownership check has to stay for the entity tokens, the token names token.module claims for entity types are a fixed set, so comparing against a hardcoded list would avoid getTokenInfo() altogether.
  • At line 651 the expensive call is also evaluated before the cheap isNew() and hasLinkTemplate() guards; reordering helps when the entity has no canonical link, but not on a node page where [node:url] is requested.
csakiistvan’s picture

StatusFileSize
new406.46 KB
new679.35 KB

Upload network usage with always cold cache