Description
sablejs 1.x has been running well on YoTest, but because of the private opcode design, we don't open all code for community(preventing decompiling). After discussions, sablejs 2.0 will plan to open all code! To reach this goal, we will make a major upgrade to the current code of 1.x, include:
1. remove the private opcode design and transform for the stack operation directly
2. dynamically import inner object for sandbox
1. Transform Directly
Since sablejs 1.x is fully stack-based vm, we will have a big loop to execute the relevant opcode code. However, this approach causes a significant performance loss due to the failure of branch prediction:
for (;;) {
switch (opcode) {
case 0: {
// ...
}
case 1: {
// ...
}
case 2: {
// ...
}
}
}
After using the latest d8 and perf for profiling, about 30% of the performance consumption is wasted. To solve this problem, and to make V8 better able to help us with JIT, we will perform the equivalent transform operation directly, consider the following example:
function add() {
return 1 + 2 + 3;
}
In 1.x, we will get linear opcode bytes, which will then be executed by vm. But in 2.0, we will compile directly to the following code:
function __C_add(J) {
__pushNumber(J, 1);
__pushNumber(J, 2);
__R_add(J);
__pushNumber(J, 3);
__R_add(J);
}
Here, stack manipulation methods such as __pushNumber
are still provided by runtime. In this way, together with the relevant compilation optimizations in 1.x, there will be a relatively large performance improvment for frequent execution(benchmark can be followed by DoppioJVM Web JIT implement).
(function(){
for(var i = 0; i < 10000000; i++);
}());
// sablejs 2.0: 276.279ms --- baseline
// sablejs 1.0.6 878ms --- slower: 218.11%
// quickjs-wasm: 228ms --- faster: 17.39%
At the same time, this brings the benefit of not having to rely on opcode, so we can directly open all of sablejs's code.
2. Dynamically Import Inner Object
In most of the usage of the current feedback, it is basically using sablejs for JSVMP. But sablejs 1.x is mainly designed with sandbox as the core, after 2.0 we will mainly aim at JSVMP while taking into account the functionality of sandbox,(the Inner Object will be import on demand according to your needs).
Also, when you are using JSVMP only, sablejs 2.0 will get a very big performance improvment thanks to V8's object optimization!
I considered for releasing sablejs 2.0 in mid-2022, please look forward to it! 😁