Wikipedia talk:Avoiding MediaWiki expansion depth limit

Cases
The examples are all hypothetical. Are there any real-life cases where more than 40 levels are required (other than accidental recursions that is). -- Red rose64 (talk) 20:27, 20 December 2010 (UTC)
 * The problem has occurred numerous times where Template:Convert has been used inside multi-nested infoboxes displayed on a conditional /doc subpage. If fact, at one point, {Convert} could not even display its own examples, because sub-parts of the /doc page added too many levels of nesting. In that case, the doc page had been structured as several nested templates for each part of the page. The real-life examples are so utterly huge, most people would be unable to clearly see how the nesting was the cause of problems. Hence, the examples use hypothetical nesting to clearly show the effects of exceeding the limit. So, long-term, we just need to rewrite the templates with reduced-nesting of the logic. Fortunately, Template:Convert will bypass several levels of nested logic (which check precision) when a rounding parameter, such as "0" is appended to the conversion. However, knowing the 40-deep level exists, it would be simple to rewrite other templates such as {str_len} to use fewer levels of if-else-if-else nesting, and reduce combinations of templates to below 40 levels. All over Wikipedia, many people have assumed they could use 7 or 9 levels of if-else when not really needed. This is an easy crisis to avert, by avoiding all the extra if-else nesting. -Wikid77 (talk) 22:13, 20 December 2010 (UTC)

Levels of nested templates
Note that, confusingly, when transcluding a template with more than 20 levels of nested templates like in the examples of the section Checking the current expansion limit already an error occurs (try transcluding the subject page).--Patrick (talk) 00:11, 22 December 2010 (UTC)

During 2009-2016?
The article repeatedly says "during 2009-2016" or "during 2009-2017". To me as a reader new to the subject, this would mean that it has been changed now (it doesn't say "since 2009"). So what has it been changed to? Is the limit gone? Is there another way to program the templates to avoid the problem completely? Can Lua be used instead? --Jhertel (talk) 20:32, 14 October 2017 (UTC)


 * I think it means "between 2009 and up to when I [whoever was editing it] last checked".
 * The limit does not appear to have changed, since the errors still appear at Avoiding_MediaWiki expansion depth limit.
 * Coding in Lua is the way to avoid the problem, and templates that used to run into expansion problems have now all(?) been re-coded in Lua. Peter coxhead (talk) 16:45, 15 October 2017 (UTC)
 * Thanks, Peter. That all makes sense. --Jhertel (talk) 21:58, 15 October 2017 (UTC)

I'm not sure the advice on this page is correct
I've been carrying out some tests in an attempt to verify the content on this page experimentally:


 * User:ais523/Template limit test 1
 * User:ais523/Template limit test 2
 * User:ais523/Template limit test 3
 * User:ais523/Template limit test 4

The idea is to attempt to apply the template P1 (which simply returns its first argument) ten times to a constant string, and observe the resulting "highest expansion depth". This is done either directly (simply nesting uses of P1 by hand), or via a helper template,, which contains five nested instances of P1.

As far as I can tell, going indirectly via the helper template always seems to be the worst option, adding another 7 to template depth each time it's used (even though it saves five levels in the calling page, it costs 12 on its own). This is the opposite of the performance properties implied by this page (which suggests using subsidiary templates in order to reduce nesting). You'd expect the nesting depth to be reduced via the helper template if MediaWiki were using call by value for template parameters (i.e. evaluating them first, then passing them as arguments), as the "Optimising "recursive" depth" section suggests. Instead, the rule (as explained on, e.g., m:Help:Expansion depth) is that if a template calls another and uses one of its own parameters to produce the parameter given to the called template, the call adds two to the expansion depth, plus the depth of the parameter itself. This is a huge cost that makes it hard to see how any sort of indirection could give savings.

Perhaps the advice on the page needs updating to a) mention the actual formula for calculating expansion depth, and b) explain how to modify templates in a way that will give the best possible values under this formula. (I can't do the latter myself because I'm not sure how templates should be modified to be more expansion-depth friendly, given these rules.) --ais523 23:20, 30 May 2018 (UTC)


 * I can assure you that the advice given on this page is correct, although it's completely irrelevant now, since deeply nested templates should always be replaced by a Lua module, which is much more efficient and doesn't cause problems. I spent weeks trying to make maximize the depth the WP:Automated taxobox system could handle using the template language before I gave up and rewrote it in Lua.
 * Looking quickly, I don't think you are following the advice correctly.
 * You can only put 20 levels of nesting of P1 inside another template, "Proc1" say.
 * To get round it you replace the content of "Proc1" by, say, . This processes 4 levels and then passes on the 5th level.
 * Then you write "Proc2" that contains, say, . This again processes 4 levels and then passes on the 5th level to a different template.
 * If you continue in this way with the last "ProcN" template just processing 4 levels you can process more than 20 levels.
 * The real example of this working is in templates that have now been deleted because they have been replaced by Lua.
 * This whole page is now obsolete! Peter coxhead (talk) 06:55, 31 May 2018 (UTC)
 * No, it's really not. It is certainly less-often needed (which is not the same as being obsolete), if you believe Lua code is inherently preferable to wiki markup.  --Pi zero (talk) 23:18, 8 June 2018 (UTC)
 * Lua code isn't inherently preferable generally to the template language (which isn't the same as wiki markup), but it is hugely preferable when deep recursion is necessary! Peter coxhead (talk) 20:26, 10 June 2018 (UTC)
 * Though we're likely getting rather off-topic, I don't agree. Deep recursion should, in most cases, be avoided entirely; for that small minority of us who can use Lua at all, it becomes too easy to rely too heavily on it in ways like this.  Anything that isn't avoided should be in wiki markup where it's accessible to the community (rather than locked behind a language barrier where only a high priesthood can access it).  --Pi zero (talk) 00:42, 12 June 2018 (UTC)
 * Last comment from me: the entire automated taxobox system, used on about 190,000 pages now, depends on recursing or iterating through taxonomy templates, sometimes to depths of 40–50, to recover the full classification of a taxon. Peter coxhead (talk) 06:02, 12 June 2018 (UTC)
 * Fwiw. My current solution to a similar problem (though less deep atm) at Wikibooks &mdash; searching through a subject/shelf hierarchy to list all ancestors of a particular element of the hierarchy, also a transitive closure though not of strictly a tree as a node may have multiple parents &mdash; is based on semi-automation and incremental computation.  For each page in the hierarchy there is a secondary page with a list of its ancestors.  Each interlocking page in the system does integrity checks on its neighbors, and if it sees a problem, it flags itself with a hidden category and provides a diagnostic message to help a human operator when considering how to remedy the situation.  These checks include comparing a page's list of ancestors to its parents and their lists of ancestors.  The ancestor lists are sorted alphabetically, to avoid propagating duplicates, which doesn't involve a full sort operation, only a merge of the presumably-already-sorted lists from the parents.  For the marginal bits of computation I use  (thus minimizing syntactic overhead and keeping things embedded in wiki markup).  I hope in future to improve the semi-automated assistance offered when a problem is detected by means of interactive wiki dialog.  --Pi zero (talk) 12:04, 12 June 2018 (UTC)
 * The examples you've given there can't possibly be correct as written because your version of Proc1 is calling your version of Proc2 with a parameter, and yet your version of Proc2 doesn't take a parameter (it's just a hardcoded string). I can see how this would work without parameters involved. However, I can't see any way to make it work when the value we want to apply 20 copies of P1 to is a parameter taken from outside (i.e. a parameter to Proc1); as far as my experiments show, putting P1s either around the template as a whole, or around the parameter, both cost more expansion depth than they say. If you think there's some way to do it, could you set up a demonstration in userspace (either yours or mine)? Either that, or specify the exact code that's needed (including parameters) so that I can try it out myself. --ais523 16:12, 19 June 2018 (UTC)
 * Ok, yes, I was a bit sloppy in what I wrote, but the principle is correct. Put on one of your user pages, by itself, and look at the expansion depth (e.g. via the table at the bottom of the page using Preview). You'll see that although it goes 20 deep in the taxonomy templates starting at Template:Taxonomy/Pteranodon, it only has an expansion depth of 13. If you care to, you can copy the "User:Peter coxhead/ProcN" templates and carry on (Proc5 is as far as I could be bothered to go). Peter coxhead (talk) 14:54, 21 June 2018 (UTC)
 * Aha, I see the difference. I'm putting the expansion in a parameter to the template, i.e. . You're putting it in part of the template name, i.e.  . And the two have rather different performance properties (the names obviously have to be evaluated eagerly, whereas at least the template limits count parameters as though they're evaluated lazily – maybe they are!). In that case, the advice isn't wrong, but it's more specific than it looks. --ais523 19:57, 22 June 2018 (UTC)
 * If and when I have time I'll check it out. I worked intensively on this for several weeks, based on the template code that had written to increase the depth of taxonomic hierarchies that could be handled, and the advice that Wikid77 had written here. I did manage to increase the depth that could be handled by a small amount. Then I realized that it was much, much easier just to convert to Lua than to try to use special tricks to get round the limits placed on expansion depth. I've forgotten the details of these tricks now, and I'm not sure I want to recover them! I can only repeat that I think this page is obsolete: use Lua is the right advice if you run into expansion depth problems. Peter coxhead (talk) 11:41, 24 June 2018 (UTC)

Transduction depth
The way this behaves seems to have changed.

The example on this help page (previously) and the following example at wikimedia (currently) both allow transclusion of 40 nested template calls. However, this example now gives errors here after a transclusion depth of 20.

So

gives

0 0

with many errors instead of only one for the 41st call. While the Wikimedia example works on the page at Wikimedia, when it is previewed it behaves as above. Something has changed.  Jts1882 &#124; talk 11:20, 19 September 2019 (UTC)