y21 / dash

Experimental JavaScript implementation in Rust
MIT License
42 stars 3 forks source link

Move most opts to runtime #64

Open y21 opened 1 year ago

y21 commented 1 year ago

Currently dash tries to apply some optimizations at compile time, e.g. given

for (let i = 0; i < 1000; i++);

The comparison and increment get special opcodes that are faster than the generic cmp/inc opcodes as it skips the type check and is optimized specifically to work with integers. However, doing this statically is very limited and leads to many missed optimizations.

It would be better if we applied these at runtime since we have far more information at runtime as it executes the code. We already have some amount of infrastructure required for doing this from the JIT, namely the dash_typed_cfg crate for getting a CFG with type information from bytecode, which should already allow for some really nice optimizations that we couldn't do at compile time. Opts like constant propagation should still run at compile time since there isn't any benefit to not doing it right away.

y21 commented 1 year ago

The current opts that rely on type inference at compile time will also be unsound in presence of dynamic code execution (eval, Function), so we should try to move away from this soon.

let i = 0;
globalThis[k1](k2);
i++;

The i++ is currently an ipostinclocal, exploiting the fact that i must be an integer, but when k1 = 'eval', we end up executing arbitrary code which may well change the type of i, breaking all the assumptions...