Skip to main content
  • Agency for web development
  • Content Management with Primer
  • Open Source Leadership with Drupal
    Language
  • Deutsch
  • English
  • Contact
Site logo
Agentur für Webentwicklung
  • References
  • Services
  • News
  • About us
  • Agency for web development
  • Content Management with Primer
  • Open Source Leadership with Drupal
close

Improved memory usage in token module

10. August 2024
Sascha Grossenbacher Porträt
Sascha Grossenbacher

It is a known issue that the metatag module combined with tokens causes considerable performance overhead, which gets worse with any additional token that is used. See https://www.drupal.org/project/metatag/issues/3039650 as for more information and recommendations on how to improve that situation a bit.

Blackfire timeline screenshot with highlighted metatag_entity_view_alter hook
Metatag processing takes up a large chunk of processing time on almost all requests.

Memory usage

That however is not the focus of this article. Instead, what I've noticed a few months ago when profiling a client project with Blackfire is that it's not just time-consuming but also used a lot of memory too. Digging deeper, I tracked this down to the _token_module function. This function is used to check if a certain token type should be handled by the generic field token implementation of the token module.

The problem is that it has to load the full token info to do that, and that information grows with every content entity type and field that a site has. On our sites, we typically saw memory usage of that method between 5MB to 8MB but it on sites with a very large amount of fields, it could also be considerable more than that.

Blackfire profiling screenshot showing the memory usage of _token_module(), around 7.5MB

To improve this, the cache collector pattern was implemented to store only the information which stores only which module provides a given token type, and only for the token types that are actively being used, gradually building up this information, typically on the first few requests after a cache clear. Afterwards, the overhead of this function is so small that it is no longer visible in profile requests.

We tested this on our projects, and then it was committed in https://www.drupal.org/project/token/issues/3440371 and released as part of token 8.x-1.14.

Positive impact on garbage collection

Beside improving memory usage, we also saw a positive impact of this as it reduced garbage collection processing. PHP does that automatically as the number of active objects goes beyond 10'000. In short, this helps to reduce memory usage at the cost of execution time and is meant for long-running processes that do heavy processing. It should be avoided in web requests.

Like many other caches in Drupal, token info contains a large amount of translation objects. Not having to load the token info meant that web requests no longer reached that magic number, resulting in an additional performance improvement.

Hol Dir den Newsletter

Jetzt für unseren Newsletter anmelden und monatlich wichtige Insights aus der Branche und MD Systems erhalten. 

Zur Anmeldung

About MD Systems

MD Systems, headquartered in Zurich, is a unique team of international open source initiative leaders for the Drupal content management system.

With our experts for software architecture and design and our industry solutions, you digitize your organization successfully and efficiently.

MD Systems GmbH

Hermetschloostrasse 77, CH-8048 Zürich

Schweiz

+41 44 500 45 95

[email protected]

  • Contact
  • Impressum
  • Data protection
To top

© Copyright 2023 - 2024 MD Systems GmbH. Alle Rechte vorbehalten. Erstellt mit PRIMER - powered by Drupal.