Requests for comment/Magic expressions

Background
It is well known that the Magic words are very popular, but often one 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 similar to my solution. 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 achieved excellent results.

It is the Extension:PhpTags (former title is the Extension:Foxway).

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. What do you think? --Pastakhov (talk) 03:53, 12 February 2014 (UTC)
 * 3) Caching. The code compilation is an expensive operation and it should be cached.
 * 4) * Compilation result is an array with references. One can be serialized and it's cheap. How can it be cached? --Pastakhov (talk) 04:54, 12 February 2014 (UTC)
 * 5) Syntax. I stuck with the syntax PHP, but I can also add anything from javascript.
 * 6) * Will it be convenient or may confuse users? --Pastakhov (talk) 04:19, 12 February 2014 (UTC)
 * 7) 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.
 * 8) * 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)
 * 9) * Will the limiting number of loops helps to avoid DoS attacks or need some other measure? --Pastakhov (talk) 04:19, 12 February 2014 (UTC)
 * 10) Isolation No, it should not be like a black box.
 * 11) * The scope of variables is limited only by own wikipage, unless they are declared as static or global. Example (Just like for functions in pure PHP) --Pastakhov (talk) 04:34, 12 February 2014 (UTC)
 * 12) Extensibility. Any extension can be integrated like a constant, function or object. (there is no direct code execution, it is like magic words)
 * 13) * I do Extension:PhpTags Functions that implement various PHP functions. --Pastakhov (talk) 09:40, 12 February 2014 (UTC)
 * 14) Universal parser. PhpTags takes the task of parsing parameters and transmits them to other extensions as an array.
 * 15) * I think it will facilitate the extension developers, especially such as in the Bug 54221.
 * 16) Debugging. PhpTags can provide debugging information by which javascript can restore the sequence of operations and their intermediate values.
 * 17) * I tried do it for Extension:Foxway on the server side, turned expensive and inconvenient. I think it's better to do it on the client side. --Pastakhov (talk) 05:19, 12 February 2014 (UTC)
 * 18) Code editor. Something like EditArea and integration VisualEditor. Any ideas?
 * 19) Portability. It uses pure PHP. Does not require anything else.
 * 20) Reverse. It can be used as a safe javascript on the client side too.
 * 21) * To do this, need to write a runtime class for javascript, I think it is not difficult. --Pastakhov (talk) 09:59, 12 February 2014 (UTC)
 * 1) Reverse. It can be used as a safe javascript on the client side too.
 * 2) * To do this, need to write a runtime class for javascript, I think it is not difficult. --Pastakhov (talk) 09:59, 12 February 2014 (UTC)

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 operation)!!!

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. Did I make a mistake in the calculations?

I do not consider compile time, because it will be done once when the page is saved. Then the compiled code will be taken from the cache. It should be cheap.