Wikipedia:Lua speed

From Wikipedia, the free encyclopedia

This essay explains various issues about the execution speed and performance of Lua script in Wikipedia. The speed of using Lua modules can vary greatly, compared to similar markup-based templates, often ranging from 4x-8x times faster, or 180,000x faster when scanning text strings, which can exceed the 500-character parser-function markup limit as 64,000 characters or more. For specific Lua performance, it really depends on how many parameters are being used, and how deep the nested logic runs. Also, optimization techniques could be used to make Lua run even faster.

For very short operations, running a quick Lua function in February 2013 had been clocked at 300 per second, limited by speed of the #invoke Scribunto interface. However, in March 2013, plans were announced to double the #invoke speed, allowing Lua functions to be invoked at 600/second, with {{#invoke:String|len}} running 625/second. Speed is also limited by passing numerous parameters, beyond 400, where the speed drops exponentially, as no longer a tiny fraction of a second. Instead, passing 2,000 parameters runs 3 seconds, 4,000 parameters runs 13 seconds, and 6,000 parameters runs 27 seconds. The time-delay slowdown is the same whether passing parameters with a Lua #invoke, or to a template, or to a parser function, such as #switch with thousands of "|x=" branches.

Numerous tests of the Lua-based wp:CS1 cite templates, such as {{cite_journal}}, each processing over 230 possible parameter names, have clocked as 6x-13x faster, when formatting 80-200 cite templates per second. However, the maximum speed there seems to show Lua could be ~25x faster than markup to format simple parameters. Meanwhile, tests of Lua-based string-scans, or even extracting a final character by "string.sub(mytext,-1,-1)" can run thousands of times faster, and exceed the markup string limit of 500 length by searching whole sections of wikitext exceeding 64,000 characters in length.

When speed is critical, invoking a small Lua function directly can be 50% faster than using the related shell template:

It is 50% faster to use {{#invoke:String|len|s=xxyyzz}} to invoke the short Lua function "len" in Module:String, and also reduces the expansion depth from 3 levels to 2 levels.

Some related speed figures:

  • Many parser functions run at ~1,250 per second (such as: #if #ifeq #ifexpr)
  • Short templates run at hundreds per second.
  • Character-insertion templates can run at 2,400 per second, such as {{nb5}}.
  • Top infoboxes (for settlements) can format in ~1-2 seconds.
  • Bottom navboxes format at 2-3 per second, but double page size.
  • Markup-based CS1 cites run 14/second (with Template:Citation/core).
  • Template:Convert runs 30/second, despite hundreds of possible unit codes.

In fact, {{Convert}} has been an interesting case with expansion-depth as the major problem, not speed, because articles often have fewer than 7 conversions (a quarter second), but the depth of {{Convert}} has often been 19-29 levels deep. Hence, within double-nested infoboxes, {{Convert}} has often exceeded the wp:expansion depth limit. For Lua-based templates, the expansion depth is typically 3 levels deep.