Wikipedia:Transclusion costs and benefits

From Wikipedia, the free encyclopedia

This is an essay. It is not a policy or guideline. Please update the page as needed, or discuss it on the talk page.
Shortcut:
WP:TCB

Some users are concerned that the template transclusion feature of the MediaWiki engine may not always be used wisely. Transclusion is a simple idea, but all of its consequences may not be immediately apparent to users with a less technical background. Although templates make many tasks easier, there are risks associated with their use — as there are with any tool. This is a kind of cost we pay for the benefits of templates. Additionally, there is a direct cost in terms of machine resources — as there is with any computer tool. This cost may be represented in simple dollar terms; if the money is not spent, the cost surfaces as a degradation of engine performance. This article is an explanation of these costs and benefits.

Contents

[edit] How transclusion works

To transclude any source page (within a single MediaWiki project, such as en:Wikipedia) within another target page, include the following code:

{{SOMEPAGE}}

Whenever the target page A with this code is rendered, the engine will include in that place not the code itself, but the entire content of the source page B, SOMEPAGE.

This would be a pretty neat trick if it only meant that you didn't have to copy-and-paste from one page to another. For example, you might decide to copy a welcome message to every newcomer's Talk page; you could do this with transclusion (though it might not be the best thing to do!).

But transclusion creates a "live" link between the "template" page and the target page where it appears. When the template is edited, all the other pages change, too.

For example, you might decide to create a (template) page with your mailing address and include that template on not only your page, but all your friends' pages, too. When you move your television and couch to another apartment, you will change your address template, and automatically, that new information will appear on all your friends' pages.

[edit] Costs and benefits

Transclusion is a tool, and like any tool, provides benefits and incurs costs. The exact values of these costs and benefits vary according to wise or foolish design and use, and is the subject of intense debate. It is not possible to pretend that anyone can give a perfectly neutral statement of these costs and benefits; both are largely subjective and extremely difficult to measure.

Transclusion demands a certain measure of machine resources (storage, memory, bandwidth, bus cycles), as does any other operation. This cost does not come all at one time, but is the sum of many very tiny costs. When the template is written, there is, for example, a one-time cost equivalent to that of any comparably-sized page; as long as it resides on the server, there is a recurring cost, also exceedingly small. The template page itself is rarely viewed directly, but of course when it is, there is a small cost associated with the presentation of the page to the viewer — as for any page. All these costs are trivial.

However, let's say that the template is transcluded within the body of a target page. Depending on who views or edits the target page, and depending on the state of various caches, the engine must fetch the body of the template and insert it into the target before it can be rendered. This may be significantly more expensive than rendering the same amount of wiki markup directly in the target. Obviously, templates are ordinarily transcluded in multiple pages, and one target may contain many templates — so the total amount of transclusion is a concern to some.

Further, when the template itself is edited, every page in which it appears seems to change, too. This invalidates caches and makes the engine work a little "harder". Editing a popular template incurs more costs than editing an unpopular one.

It is very difficult to quantify these costs. How much money is a millisecond worth? How many bytes to the penny? Modern microcomputers move, change, store, and serve mammoth quantities of data at costs that would have seemed absurdly low even a few years ago — and this is true no matter when said, because server prices continue to drop steadily, and their speed, power, and capacity go up, too, according to Moore's Law. Still, there are costs, and in a project of sufficient scale, these tiny costs sum to respectable proportions.

Rapid growth of Wikipedia
Enlarge
Rapid growth of Wikipedia

Waste not, want not.Benjamin Franklin. Unless there is some benefit to be gained from transclusion, there is surely no good reason to expend any cost on it.

There is also a social cost of transclusion, the total expectation over time of the risk that a transcluded template page may be vandalized. If a popular template's content is replaced by gibberish, that same gibberish will momentarily appear on every page in which the template is transcluded — just as would any edit. This also adds to the burden borne by the engine — as does any edit.

Offsetting this cost is the fact that template vandalism is as easy to reverse as to commit. The offending material is removed and the former content restored. There is, again, another imposition on the engine.

If a great deal of text, appearing on many pages, is kept in a template and transcluded in all the targets, then there are some savings in storage, but generally there is no benefit to the machine granted by transclusion.

Humans, however, find transclusion very useful, and in many ways, some of them quite unexpected. Almost any type of text, markup, HTML or CSS code can be transcluded; if that text or code is used in several places, templates make human labor much easier. Not only that, but by reducing errors in otherwise repetitive typing, templates help to lower Wikistress and reduce the amount of janitorial work.

A basic principle of graphic design — of almost any design — is to standardize presentation. That is (for example), if on several pages, it is thought desirable to present a certain list of items, then all such lists should look alike. Even if the items vary somewhat from page to page, there is often a clear advantage to making them appear similarly. There is no technical reason to do this; the engine will function just as well if every list is a little bit different from the last. But a standard appearance contributes greatly to a professional impression on human readers. Note (for another example) the Wikipedia logo on each and every page. This not only increases our credibility in the larger world; it helps to reinforce our sense of community and encourages investment in our project. Templates are a vital tool in maintaining a consistent look and feel across our hundreds of thousands of pages.

[edit] Double transclusion


Since almost any page can be transcluded, why can't we transclude one page in another, and then transclude that page in a third? The answer is, we can.

Some master templates are designed to be used with other subtemplates. When master template B is transcluded within target page A, the subtemplate C is transcluded, too.

Since both links are "live", if the subtemplate is edited, the target page is also changed. This could be carried even further; page C could transclude some page D , and the content on page A would be an instance of triple transclusion. When page D is edited, page C changes, so page B changes, so page A changes — right on down the line.

[edit] Geometric effect of multiple transclusion

Costs and benefits of double transclusion increase geometrically (in a loose sense of the term). Say a template X is transcluded on 5 other templates; and say each of those templates is itself transcluded on 5 target pages. The template X is doubly transcluded onto 25 pages. An edit to X appears on a total of 31 pages: X, the 5 templates, and the 25 targets.

There is no limit to the number of targets for a given template, nor to levels of transclusion, so it is possible that X be doubly or triply transcluded hundreds or even thousands of times. Thus double transclusion is extremely powerful and the technique in itself has become an object of dispute.

[edit] How substitution works

MediaWiki provides the subst: command, which simply substitutes one page within another. This is very different from transclusion, although the difference will be invisible on the first edit.

To substitute a page, use this code instead:

{{subst:SOMEPAGE}}

(The same syntax applies as in the case of transcluded pages.)

When you preview a page containing a substitution, you will still see the code in the edit box; you can change it if you like. But when you save the target page, the engine substitutes, or replaces, the code with whatever is on the source page. If you edit the target again, then you will see that the substitution code is gone; the content of the source page has replaced it directly.

The link from the target to the source is not merely broken; it does not exist at all. Now, if the source page changes, the target page will not change. The substituted content is frozen in time, in the form it had at the moment the page was saved.

Substitution and transclusion can be used together, often this is a good idea. Instead of using double transclusion, you might substitute one page in another, then transclude that page in a target page. Or, you might find that a page transcludes another; you choose to substitute the transcluding page in a target, rather than force a double transclusion.

On the other hand, double transclusion can be very useful, and may be justified or even demanded in certain contexts. Each case is different, and can only be decided after careful, informed consideration of technical and social situation.

[edit] Tagging

A very common use of templates is to "tag" a page — to insert on it some snippet of text, often boxed, often seen at the very top or bottom of a page. A tag does not contain content, but metacontent: information about the page itself.

For example, {{FAC}} inserts the following tag:

This article is a current featured article candidate. A featured article should exemplify Wikipedia's very best work, and is therefore expected to meet several criteria. Please feel free to leave comments.

There are hundreds, perhaps thousands of tags, each with a different function, many transcluded onto hundreds of pages. Thus tags represent a significant fraction of server resources devoted to transclusion. Many tags include category references, which automatically include pages on which they are transcluded within a category.

Article pages are rarely transcluded, so tagging them presents no problems — the same goes for Talk pages, Wikipedia namespace pages, and indeed most pages. When a page is so tagged, the tag appears only in that one place; if it refers to "this page", it is clear what is meant.

However, Template namespace pages are intended to be transcluded and substituted, so tagging them — for any reason — is problematical.

  • Whatever tag is applied to a template page is applied to every target where the template is used. If the tagged template is substituted, then the transclusion code of the tag will "fall out loose" into the markup of the target page — including any category reference. This results in such inanity as users being nominated for deletion.
  • No matter whether the tagged template is transcluded or substituted, it will be visible on the target for all to see. This may be embarrassing, destructive, annoying, or merely appear childish. Depending on the text of the template so tagged, it may be quite impossible to see, when looking at the target, what has been tagged. In general, the casual reader will have no idea what is meant.
  • Any text or markup may be transcluded, within very broad bounds; and some templates are highly technical, especially those intended as sub- or master templates, with nested parameter calls, or used to manage other templates. Altering even a single character within such a template may cause not only it to break, but cause a cascade of broken templates and damaged pages. Tagging such a template almost guarantees it will no longer function as intended.
  • Tagging templates is a class of double transclusion, at the least; if either tag or template are already involved in double transclusion, the result — as seen on a target page — may be triple or quadruple transclusion. All the costs of double transclusion apply in these cases, and may be intensified.

There are cases in which tagging a template may be less destructive; however, it is very difficult to isolate them — to ensure that a tag will cause no problems.

Therefore: It is recommended that tags never be placed on Template namespace page bodies. If a certain tag seems to apply to a template, it should be placed on its associated Talk page instead.

[edit] Other kinds of inclusion

Other types of markup and engine function also cause content to be included in a rendered page, similar in effect to transclusion or substitution:

  • Images are transcluded in rendered pages.
  • Signatures and datestamps (generated with multiple tildes) are substituted in pages when saved.
    If the signature markup contains a template call, the template call is substituted and the template contents transcluded on each appearance of the sig. If the signature markup contains an image call, the image is transcluded on each appearance.
  • The main site logo Image:Wiki.png is on every page rendered with some skins. All pages transclude some standard links and text.

[edit] Emergency measures

From time to time, server load outstrips capacity; if pages are still to be served, some features of the engine must be switched off. The first to go is the "search" function; after that, perhaps images. Switching off transclusion might drastically reduce server load, but of course that would seriously degrade appearance, performance, and usability.

There is never a need to abuse system resources; in times of excessive server overload, it may be wise to observe certain emergency measures:

  • Consider substitution, rather than transclusion. Substitution's ongoing cost is zero. If you just want to make use of template contents, use {{subst:sometemplatename}}. Once you save the page, the link to the template is not merely broken; it ceases to exist, and causes no performance issue. The other side of this is that if the template is edited, its substitution will not change. This can be good or bad.
  • Don't edit war, especially over large, complex pages.
  • Don't edit war over templates. It is especially good to be very sure your change is a good one when you edit a popular template.
    • Substituting templates on many pages with a bot loads down the servers even more than edit warring.
  • Don't revert war, move war, tag war.
  • When you have a disagreement with another Wikipedian, at least try to discuss it with him. Don't act out your disagreement.
  • Think before you use a template. It is not really necessary to be careful creating a template; but whenever you decide to transclude, know what you are doing and why.
  • Do not tag templates marked for deletion with {{tfd}}. Tag those pages on their associated Talk pages, and use the form {{subst:tfd}}. Better yet, in the case of newly created and unused templates, just contact the creator and ask the template be moved into user namespace.
  • The same can be said for much tagging. Most tags will work just as well if they are substituted, and many will do just as good a job if they are substituted on the page's Talk.
  • Don't tag work to be done; just do it.
  • Don't use images just to be cute. "An image is worth 1000 words." — well, not only is its value 1000 times that of a word, so is its cost.
  • Avoid ultra-cute signatures. Signatures appear in so many places that lengthy sig markup may become a significant burden. Never use images or templates in your sig.
  • Lay off the Preview button. Think about what you are writing. Use an external editor, spellcheck there, and proof your remarks yourself before pasting them into the edit box and hitting Preview.
  • But use the Preview button before the Save button. Multiple edits to a page make work for both machines and humans.
  • Edit by section. It is a lot less wear and tear on the page, and easier for you, too. When adding a new section, it may be helpful to simply add the section head, save the page, and then reopen only that new section.
  • Archive Talk. Archive Talk to history whenever possible; long pages are hard to render, and if a user's browser times out, he is encouraged to reload the page over and over. There is no need to create a new page; history already preserves everything, anyway. All you need to do is this simple, two-step procedure:
    1. Copy the Permanent link location from the toolbox.
    2. Blank the page entirely and paste the permanent link to the previous version into the new, blank Talk page as an external link (using single brackets). You may also copy the datestamp of the archive.
  • Use the Cologne Blue skin, which loads neither the project logo nor the background image, though the images are only loaded once in most browsers.

These ideas are good at all times, regardless of server overload; but when the rack room is running hot, they become especially wise. Finally, never forget that one expensive solution to some computer problems is to buy more computers and hire more computer workers.

[edit] See also