Tags abuse

8 February 2014, 12:05

Keywords: fast, slow, speed, performance, php, tag

We generally say Textpattern is lightweight and fast. That’s true… until you feed it too much tags.

Consider a simple task: output the square of integers from 1 to 100. Since Textpattern does not provide tags for iterations, neither for arithmetic operations, we use few nifty plugins here:

<txp:rah_repeat range="1, 100" break=",">
	<txp:variable name="row" value='<txp:rah_repeat_value />' />
	<txp:adi_calc name="row" multiply='<txp:variable name="row" />' />
	<txp:variable name="row" />
</txp:rah_repeat>

It does the job, taking ~0.018 seconds. Not a big deal, but let us compare it to

<txp:etc_query data="[1..100]" break=",">
	{$^2}
</txp:etc_query>

Now it takes only ~0.001 seconds, which is about 20 times faster. What happens here? Inside the first snippet we see five <txp:tags />, each corresponding to some UDF. They are parsed and processed on every iteration, yielding about 2000 UDF calls. In the second case, only 100 UDF calls (etc_query evaluators) are needed, since etc_query does not parse the content when not necessary.

So, UDF seem to be a time hog. What if we get rid of them?

<txp:php>
	$out = array();
	for($i=1; $i<=100; $i++) $out[] = $i*$i;
	echo implode(',', $out);
</txp:php>

Unsurprisingly, now it takes only ~0.0002 seconds, which is almost 100 times better than our initial tag soup solution.

In conclusion, you should reduce as much as possible the number of <txp:tags /> (especially heavy ones, like <txp:article_custom />) in loop structures. We have to admit that Textpattern is not a highly efficient programming language. Having to recur to <txp:php /> is often frustrating, but sometimes this is the only valuable choice.