For a program language, variable access is a basic operation and the most frequently used operation. To improve the efficiency of beetl access variables and improve the performance of beetl, this article introduces how beetl accesses variables.
First, let's look at a simple example:
var a = "hi";print(a);
The first line defines the variable, and the second line references the variable to print the output. Normally, you can save the variable to the map when defining the variable, when needed, it is retrieved Based on the variable name. Therefore, the Appeal code can be translated into Java code similar to the following:
Context. Put ("A", "Hi ");
Print (context. Get ("");
Although we all know that map access is very fast, there is still no faster way. The answer is: array. The access to arrays is faster. You can see from the following code,
The element storage speed of the array is 10 times that of the map, and the reading speed is faster, which is 100 times
String value1 = "A"; string value2 = "B"; string value3 = "C"; string key1 = "key1"; string key2 = "key2 "; string key3 = "key3"; string [] objects = new string [3]; int loop = 10000*5000; // calculate the time log consumed by array storage. key1start (); For (INT I = 0; I <loop; I ++) {objects [0] = value1; objects [1] = value2; objects [2] = value3;} log. key1end (); Map <string, string> map = new hashmap <string, string> (3); // calculate the time log consumed by MAP storage. key2start (); For (INT I = 0; I <loop; I ++) {map. put (key1, value1); map. put (key2, value2); map. put (key3, value3);} log. key2end (); // calculate the time log consumed by the array. key3start (); For (INT I = 0; I <loop; I ++) {value1 = objects [0]; value2 = objects [1]; value3 = objects [2];} log. key3end (); // calculate the time log consumed by map. key4start (); For (INT I = 0; I <loop; I ++) {value1 = map. get (key1); value2 = map. get (key2); value3 = map. get (key3);} log. key4end (); // print the Performance Statistics log. display ("use array Settings", "Use Map Settings", "Use array to read", "Use Map to read ");
Console output:
======================================
Use array settings = 139 percentage, infinity
Use Map to set = 1020 percentage, infinity
Use array READ = 3 percentage, infinity
Use Map to read = 767 percentage, infinity
(The code references https://github.com/javamonkey/... java
When modifying the 2.0 engine, beetl optimizes variable access and uses a one-dimensional array to save variables, as shown in the example at the beginning of this article.
In engine 2.0, translate it into the following code:
context.vars[varNode.index] = "hi"print(context.vars[varNode.index]);
So how does beetl allocate indexes to template variables? The following code allocates indexes?
var a = 0;{var b = 2; }{var c = 2;} var d =1 ;
Although there are four variables, the maintenance of these variables only requires a one-dimensional array, the array length is 3
The index of node A, D, C, and B is 0, 1, 2, 2, that is, the sub-context (after the sub-context enters the block) will be arranged after the upper-level context: First assign the top-level variables A and D, the assigned index is 0 and 1, and the second-level variable B is assigned a value of 2. For the second-level variable C, the assigned index is 2, because the scope of variable B has been applied.
Performance tests show that the performance of 2.0 has increased the value assignment and reference of variables by 50 times.