Comments (13)
When will this performance improvements reach master branch? Any ETA? I'm looking into moving from Interpreter-JS to Sval.
PD: Quite a nice job.
I will try to complete in August
from sval.
I just found an available register-based js vm, continuum. it did a great work, but unbelievable, its performance is even worse than sval. it makes me reconsider the performance of stack-based interperter written in js.
test case:
<!DOCTYPE html>
<html>
<body>
<script src="https://unpkg.com/sval"></script>
<script src="https://unpkg.com/[email protected]/continuum.js"></script>
<script>
const code = 'for(var i=0;i<100000;i++){}'
for (let i = 0; i < 5; i++) {
const interpreter = new Sval()
console.time(`sval ${i + 1}`)
interpreter.run(code)
console.timeEnd(`sval ${i + 1}`)
const realm = continuum.createRealm()
console.time(`continuum ${i + 1}`)
realm.evaluate(code)
console.timeEnd(`continuum ${i + 1}`)
console.log('')
}
</script>
</body>
</html>
result in chrome:
sval 1: 102.80810546875ms
continuum 1: 1341.805908203125ms
sval 2: 83.824951171875ms
continuum 2: 1551.010986328125ms
sval 3: 79.5869140625ms
continuum 3: 1698.853759765625ms
sval 4: 82.9951171875ms
continuum 4: 1470.83984375ms
sval 5: 83.164794921875ms
continuum 5: 1300.2099609375ms
from sval.
cool
linear int keys allow us to malloc a linear continuous mem to store, and we can access the item by offsetting the pointer. but string keys need a hashtable to store the relationship between keys and indexes, in order to access the mem block by indexes to get the item. that's why int keys have a better performance.
so, to store a dict, we need a hashtable and an array actually. if we store the data in a array, we still need a map/hashtable/object to maintain the mapping relationship between keys and indexes, which takes the same time as we directly store at a object. to avoid this problem and just use a array to store data, we need a stack-based interpreter instead of register-based or tree-walking interpreter.
as you say, the predefinition of dynamic properties has a similar meaning of hoisting variables and functions. I am working on it now, predefining the variables and functions when creating scope, instead of mounting them on the scope context after creating.
from sval.
fengari did a great job of running lua over js. mark for reference - Fengari
<script src="./fengari-web.js"></script>
<script type="application/lua">
local start = os.time()
for i=1, 10000000 do
end
print(os.time() - start) -- 0~1s cool :)
</script>
<script src="https://unpkg.com/sval"></script>
<script>
const interpreter = new Sval()
interpreter.run(`
var start = Date.now()
for (var i = 0; i < 10000000; i++) {}
console.log((Date.now() - start) / 1000) // around 6s :(
`)
</script>
from sval.
I don't think it's a good idea to create a VM with javascript. It just emits more code, needs more allocations and results in more GC.
Sval is great and easy. Firstly, we should try regular optimization ways such as cache
critical paths, instead of rewriting the whole solution.
Take below case for example, it takes about 145ms
on my Chrome 74.:
const interpreter = new Sval();
interpreter.run(`
var x = 0;
function functionOne() {
x++;
}
var iterations = 100000;
console.time('Function #1');
for(var i = 0; i < iterations; i++ ){
functionOne()
};
console.timeEnd('Function #1')
console.log(x);
`);
When optimizing with inline function, the time cost cuts down half to 75ms
, one time faster!
const interpreter = new Sval();
interpreter.run(`
var x = 0;
function functionOne() {
x++;
}
var iterations = 100000;
console.time('Function #1');
for(var i = 0; i < iterations; i++ ){
x++;
};
console.timeEnd('Function #1')
console.log(x);
`);
By the way, I run the ES6 version of Sval, it's 20% ~ 25% faster than ES5 version
from sval.
agree. I will think it over and try to cache something.
actually I don't mean to implement an entire vm. just to flatten the ast by compiling into ir or bytecode, in order to decrease the performance reduction caused by maintaining scopes.
from sval.
Top cost of the above for loop
test case:
5/6 are about property setter/setter, it seems V8 doesn't have good property accessing performance, it should be faster, right? Let's take a look at below test case:
Accessing property with String key
vs. number key
:
const map = {};
const strKeys = new Array(100000);
const numKeys = new Array(100000);
for(var i=0; i< 100000; i++) {
var key = 'Key' + i;
strKeys[i] = key;
numKeys[i] = i;
map[key] = i;
map[i] = i;
}
console.time('StrKey Lookup #1');
for(var i=0; i< 100000; i++) {
var key = strKeys[i];
var v = map[key];
v = v + 1;
}
console.timeEnd('StrKey Lookup #1');
console.time('NumKey Lookup #1');
for(var i=0; i< 100000; i++) {
var key = numKeys[i];
var v = map[key];
v = v + 1;
}
console.timeEnd('NumKey Lookup #1');
Surprise? Of course not, V8 blog gives us the explanation: named properties and indexed elements
are different in V8.
For sval, we can build a data structure to deal with this, or use array instead of object to store data, and convert all string key(such as type) to number; And for those dynamic property setter, try to predefined the properties to reduce HiddenClass Generation. Say:
var x = {}
x.a = 5;
x.b = 6
=>
var x = {
a: undefined,
b: undefined
}
x.a = 5;
x.b = 6
from sval.
currently a stack-based interpreter has been roughly accomplished. a brenchmark demo can be found in #3b660d4, and the interpreter has a great performance promotion lol.
const interpreter = new Sval()
interpreter.run(`
var start = Date.now()
for (var i = 0; i < 10000000; i++) {}
console.log((Date.now() - start) / 1000) // 3.45s now :D
`)
from sval.
When will this performance improvements reach master branch? Any ETA? I'm looking into moving from Interpreter-JS to Sval.
PD: Quite a nice job.
from sval.
Hi :) sorry for the double post, I would love to use Sval too! Do you confirm end of August as a possible release date for the performance improvements? :)
Thank you and great job!!!
from sval.
Hi :) sorry for the double post, I would love to use Sval too! Do you confirm end of August as a possible release date for the performance improvements? :)
Thank you and great job!!!
Nope. I don't have time to handle it this month, and I'm not sure when I can finish. :(
from sval.
That's fine anyway, thank you! :) Looking forward to seeing it completed!
from sval.
Any update? What's left to do? :)
from sval.
Related Issues (20)
- 循环语句中的switch break会导致出错 HOT 2
- vm里的对象实例, 外部call async method的的时候this指向不对 HOT 5
- Can't access variable inside function HOT 2
- How safe is this interpreter? HOT 1
- Using atob returns TypeError: Illegal invocation HOT 6
- No `replaceAll` method on strings HOT 1
- Module support HOT 1
- Support import and export declarations HOT 2
- Maximum call stack size exceeded HOT 1
- 是否有性能数据可以分享? HOT 2
- better stack traces HOT 5
- How to get the result of expression? HOT 4
- Issues with regex HOT 2
- Support optional chaining HOT 1
- behavior of async code exection HOT 2
- sandbox mode without access to global scope HOT 3
- Why does async require '!' operator HOT 2
- bug: array destructured variable is not defined HOT 1
- [BUG] Strings are not handled as char arrays when spreading
- AwaitExpression not implemented
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sval.