Lots of unnecessary querying for custom tokens

Created on 23 May 2016, about 8 years ago
Updated 7 December 2023, 7 months ago

Every time a token, of any type, is generated, token_custom tries to load it. In the vast majority of cases, this will be for a token type that there aren't any custom tokens for anyway, so this is rather unnecessary, and can be a performance problem.
Drupal's core path system uses a sort of cache/whitelist of which alias prefixes to cover, so it never bothers looking up aliases under other prefixes - this is a pattern that could be copied. See the attached patch.
A query is made for which token types have custom tokens defined. So then only tokens within those types are actually queried for.

On top of that, unfound tokens are repeatedly queried for if repeatedly generated. On querying for a token that does not have a match, the patch also adds that to the cache so that subsequent lookups don't need a DB hit.

πŸ› Bug report
Status

RTBC

Version

1.0

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom james.williams

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¬πŸ‡§United Kingdom james.williams

    This is still a problem for the 8.x-1.x versions (for Drupal 9/10) too. Patch attached!

    I'm a bit amazed to see this issue never got noticed in the Drupal 7 days; maybe because it wasn't ever set to 'Needs review', sorry about that. I could probably help maintain this module if help is needed?

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.2 & MySQL 5.5
    last update 7 months ago
    Composer require failure
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 7 months ago
    1 pass
  • Status changed to Needs work 7 months ago
  • πŸ‡¬πŸ‡§United Kingdom Steven Jones
    +++ b/token_custom.module
    @@ -43,17 +44,60 @@ function token_custom_token_info() {
    +    drupal_static_reset(__FUNCTION__);
    

    Is this a bit of debugging code that's been left in?

    Why do you need to reset the static variable otherwise?

  • Status changed to Needs review 7 months ago
  • πŸ‡¬πŸ‡§United Kingdom james.williams

    Is this a bit of debugging code that's been left in?

    Ohh no, just a plain mistake. TokenCustomType::postSave() does it where it needed to.

  • Status changed to RTBC 7 months ago
  • πŸ‡¬πŸ‡§United Kingdom Steven Jones

    Seems to work nicely, and saved the queries for me :)

  • πŸ‡¬πŸ‡§United Kingdom Steven Jones

    Here's a graph of the minor performance improvement this change brought on our production systems.

    The average call time for token_custom dropped from 1.1ms to 0.08ms

Production build 0.69.0 2024