Sites which run the Metatag module end up parsing tokens an awful lot each page request. It has been reported that for one site that enabling Metatag (with a number of meta tags being output) causes the page request to jump from a few hundred milliseconds to over eight seconds.
Looking at the stats on the page request, most of the processing is happening while processing the tokens, specifically iconv_substr():

I'd like to suggest some sort of optional static caching be added to the token generation system, so that it doesn't end up processing the same "[node:summary]" token for a single entity five+ times on the same page request, that instead it is executed once and then statically cached.
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | Issue-2955407-Static-caching-for-token-processing-ic.patch | 2.24 KB | sylus |
| #12 | 2955407-sample-token-pressure.png | 317.5 KB | sime |
| #9 | metatag_after.png | 112.57 KB | johnwebdev |
| #9 | metatag_before.png | 111.71 KB | johnwebdev |
| #8 | metatag-token-cache-2955407-8-interdiff.txt | 643 bytes | berdir |
Comments
Comment #2
berdirThe token replacement is in core, node_tokens() is in core too. And static caching is tricky, because based on what/how do you invalidate it exactly? the same node could suddenly have different values?
Maybe it's metatag module that should do the caching here? You're the one calling get_tags_from_node() twice for the same node? :)
Also, it looks like you're relying on the symfony mb_strlen() polyfill, which certainly isn't going to help with performance. You should enable the mbstring extension.
Feel free to raise the issue in core, but I think this is up to the caller, aka you.
Comment #3
damienmckennaThat's fair enough, lets add the static caching to Metatag.
We'll also document that the mbstring extension should be enabled in PHP.
Comment #4
kkohlbrenner commentedHi @DamienMcKenna,
I am curious the status of this issue? I see not much activity has occurred here, too.
Comment #5
damienmckennaThe solution is to enable mbstring in your server's PHP confguration, other things are work-arounds that haven't been worked on yet.
Comment #6
berdirHere's a simple patch that adds static caching for each unique value that is passed to Token::replace().
In my case, that's saving 12 Token::replace() calls, although not all of them actually have tokens.
Savings aren't huge, but if you have more identical tokens they could easily be bigger. It's fairly common to reuse the same tokens many times, e.g. for title, og title, twitter title.
Comment #8
berdirDidn't check for $entity being NULL.
Comment #9
johnwebdev commentedHere's another profiling.
Before:
After:
51ms.
Comment #10
moshe weitzman commentedToken generation leads the mass.gov performance report (and thats not a good place to be). I cant say if this patch is a good idea but I do know the need is large.
Comment #11
berdirThat's not exactly very useful feedback :)
Do you have any numbers on how much "leading" exactly is, how much this patch helps and also, have you seen my comments on e.g. #2935187: Token parsing massively slows down Drupal 8; document that mbstring is recommended and #3039650: Metatag discards cacheability metadata, results in a performance hit on how to speed up token generation? one of the most important things is to use property tokens, so :value and so on, that apparently saved 37% for @johnwebdev.
How much this patch benefits will depend directly on how many duplicated metatag tokens you're using.
Comment #12
simeComment #13
simeI'm not following this issue closely (i'm just trying to reduce the number of fields we have, which impacts this problem), but I'd be happy to follow direction and start testing patches.
Comment #14
sylus commentedJust rerolling the patch.
Comment #15
damienmckennaMoshe: any updates on the performance testing from mass.gov?
Comment #16
moshe weitzman commentedSorry, no time for additional profiling. We plan to get rid of metatag one day. Its not metatag's fault per se. Token processing is slow, and we have way too many fields. Its hard to know what fields are unused, after a site has been active for a few years.
Comment #17
joseph.olstadApparently there's also performance differences depending on how the tokens are used.
#3162152: Document performance gains from property tokens
Comment #18
joseph.olstadpatch no longer applies against the latest release. Is this patch now deprecated? I looked at the conflict it's pretty significant.
Comment #19
andypostComment #20
sylus commentedI could be wrong but i think this isn't needed anymore since metatag is roughly doing same logic now.
Comment #21
berdirYes, #2862747-16: Tokens to access individual meta tag values confirms that it basically took this patch and merged it into that issue over there.