📜 ⬆️ ⬇️

Fenom - yet another PHP template engine

Good day. The first stable version of the new Fenom template engine has been released. For the impatient, Fenom is a lightweight (statistics attached), fast (benchmark attached), flexible (API attached) template engine, which can be a good substitute for Twig or Smarty template engines.


The Fenom template engine is a vivid example of how the question “interesting, and what is the tokenizer extension capable of ?” Could lead to something more than a couple of scripts in the sandbox. Like many projects, the template engine started just-for-fun and was created in one evening. The first version of the template engine occupied only 900 lines of code that parsed primitive templates. To maintain extensibility, we had to add another 2500 lines of code to the template code.

If anyone is interested, I can tell you in a separate article all the interesting points and nuances that I had to face when creating a template.
')

Ideology



When it became clear that the experiment went beyond the usual sandbox game, there was a desire to bring it to a full-fledged OpenSource project that could be used in highload projects and compete with popular PHP template engines. Templating in highload projects is a very complex topic. When you have to deal with hundreds of thousands of user-space templates, all template engines begin to have their own “cockroaches”. By the time Fenom was created, I managed to work with PHP templates such as Smarty2, Smarty3, Quicky, Twig, Latte. My experience with them helped me understand what the “my dream” template should be :)
When developing a template engine, I tried to adhere to the following requirements:


Performance



I borrowed a set of tests from a friend of VEG from the article Smarty vs. Twig: A performance where Twig performed well. Not a little time has passed since then, so I decided to repeat, the tests cited in the article, with an additional rival - Fenom. Characters: Smarty version 3.1.13, Twig version 1.13.0 + extension twig.so, Fenom version 1.0.3. I really wanted to test the Volt from the Phalcon native framework, but, alas, at the first test it went into an endless loop, so it had to be disqualified.

Each test was run in 3 different modes:


Tests were performed at 2.6 GHz Intel Core i7, 8 GB 1600 MHz DDR3, MacOS 10.8.4, PHP 5.4.15 from under the CLI. Ubuntu 12.04 also showed a ratio in the results. PHP was run with the -n flag, which disables all external extensions. With twig.so tested separately. The result was taken from the second test run, when the OS has already cached test files and templating engines. Op-keshery, of course, disabled as all other extensions.

So, the first test is the output of a large number of variables. In fact, this test makes sense since all patterns consist of outputting variables to a pattern.
The output of a large number of variables:
Template enginesCold startRegular launchHot start
Smarty34.3394 sec, 15.2 MiB0.0239 sec, 9.2 MiB0.0015 sec, 9.2 MiB
Twig1.9618 sec, 68.9 MiB0.0341 sec, 17.0 MiB0.0013 sec, 17.0 MiB
Fenom0.3432 sec, 8.9 MiB0.0157 sec, 6.6 MiB0.0011 sec, 6.6 MiB

In the first test, Twig allocated itself - tests dropped due to exceeding the memory limit (by default, 32MB).

The second test is an iteration of a large array, which is also a frequent case in templates.
Iteration of a large array:
Template enginesCold startRegular launchHot start
Smarty30.0223 sec, 5.8 MiB0.0036 sec, 3.1 MiB0.0024 sec, 3.1 MiB
Twig0.0300 sec, 4.0 MiB0.0174 sec, 2.7 MiB0.0166 sec, 2.7 MiB
Twig + extension0.0225 sec, 4.7 MiB0.0064 sec, 3.2 MiB0.0060 sec, 3.5 MiB
Fenom0.0080 sec, 3.1 MiB0.0022 sec, 2.5 MiB0.0017 sec, 2.5 MiB


The third test is the inheritance of a large number of patterns. I consider this test more contrived because more than 2-3 levels of inheritance are enough for the eyes, nevertheless, 100 level inheritance remains in the test. In spite of all artificiality, the test gives a general idea about the performance of template inheritance.
Inheritance of a large number of patterns:

Template enginesCold startRegular launchHot start
Smarty30.4165 sec, 10.1 MiB0.0008 sec, 3.1 MiB0.0001 sec, 3.1 MiB
Twig0.3626 sec, 11.2 MiB0.0252 sec, 6.5 MiB0.0021 sec, 6.5 MiB
Fenom0.0569 sec, 3.2 MiB0.0005 sec, 2.5 MiB0.0000 sec, 2.5 MiB

Twig stood out here, the test fell due to exceeding the allowable nesting, in this case recursion. An error was thrown by the xDebug extension, which later turned off for tests.

Total
The obvious winner was the Fenom, which was largely ahead of Smarty and Twig in terms of both speed and resource saving. In second place is Smarty, Twig gets bronze.

What is under the hood?


Below is a table of the fat content of the code:
Template engineNumber of filesNumber of classesNumber of lines of code
Smarty (3.1.13)32019055095
Twig (1.13.0)16213113908
Fenom (1.0.4)9133967


I will note that:


Where to get?


: github.com/bzick/fenom
Packagist.org: packagist.org/packages/bzick/fenom
Composer: "fenom/fenom": "1.*"
: github.com/bzick/fenom/blob/master/docs/readme.md
: github.com/bzick/fenom/issues

?



Fenom . {parent} , in is, . .

P.S.
, ;)
P.P.S.
, raw . 1.0.7 .

Source: https://habr.com/ru/post/169525/


All Articles