Requests for comment/Magic expressions

Background
It is well known that the Magic words are very popular, but one often is not easy to use and have performance problems. This issue was discussed here and it was decided in favour of the Scribunto extension. I'm sure it can not be the sole true solution.

Another way
It seems that it was the first way. I just now saw the extension WikiScripts. I have not seen the code, but the description is very similar to what I did. I call it magic expression.

The problem with magic occurs only when one trying to be like an expression. Magic expression is the magic words in a scripting expression. This makes the magic words user-friendly and easier for the extension developers.

Several times I did refactoring the code, and has achieved excellent results.

It is the Extension:PhpTags.

Scope and issues

 * 1) Performance. somewhere between 10-500 times slower pure PHP. Weakness: the variables in the loop.
 * 2) * I think it's more than enough. I am sure that it is possible not to use any loops on the wiki pages. --Pastakhov (talk) 03:53, 12 February 2014 (UTC)
 * 3) Syntax. I stuck with the syntax PHP, but I can also add anything from javascript.
 * 4) * Will it be convenient or may confuse users? --Pastakhov (talk) 04:19, 12 February 2014 (UTC)
 * 5) Sandboxing. Users must not be allowed to abuse the server resources using the scripting language. The solution must be protected from DoS attacks and security exploits.
 * 6) * PhpTags sanitizes the returned data if it is not raw html from another extension. PhpTags never executes any code directly. --Pastakhov (talk) 04:19, 12 February 2014 (UTC)
 * 7) * Will the limiting number of loops helps to avoid DoS attacks or need some other measure? --Pastakhov (talk) 04:19, 12 February 2014 (UTC)
 * 8) Isolation No, it should not be like a black box.
 * 9) * The scope of variables is limited only by own wikipage, unless they are declared as static or global. (Just like for functions in pure PHP) --Pastakhov (talk) 04:34, 12 February 2014 (UTC)
 * 10) Portability. It uses pure PHP. Does not require anything else.

Benchmark 1. Brute force ( 100 000 loops )
Source of Benchmark 1:

I did it on my notebook HP ProBook 4530s, core i5, 4G ram, openSUSE 12.3. Pure PHP is PHP 5.3.17, and PhpTags is Extension:PhpTags 1.0.7 Pure PHP took: 0.036 sec PhpTags took: 18 sec PhpTags/PHP: 500

Yes, it is 500 times slower than PHP, but it's just 0.00018 sec per loop (0.000015 sec for one operator)!!!

Benchmark 2. Big script
See link.

My HP ProBook 4530s: Pure PHP: 0.007 PhpTags time usage: 0.634 sec Compiler: 0.517 sec Runtime: 0.117 sec PhpTags / PHP = 0.634/0.007 = 90 PhpTags\Runtime / PHP = 0.117 / 0.007 = 16.7

test.foxway.org: (Xen VM on HP DL160G6) Pure PHP: 0.005 PhpTags time usage: 0.283 sec Compiler: 0.236 sec Runtime: 0.047 sec PhpTags / PHP = 0.283/0.005 = 57 PhpTags\Runtime / PHP = 0.047 / 0.005 = 9.4

Wow, PhpTags can run for large scripts only about 10 times slower than pure PHP. It seems incredible. Maybe, did I make a mistake in the calculations?