ocsigen/js_of_ocaml

Code size optimizations

vouillon opened this issue · 6 comments

Some ideas to reduce the size of the generated code...

Turn conditional statements into expressions

More compact code, and may allow further optimizations

if(e1)e2 else e3
===>
e1?e2:e3
if(e1)e2
==>
e1&&e2
if(!e1)e2
==>
e1||e2

Eliminate expression statements

Then, we may be able to coalesce more variable declarations, or omit the return of an arrow function.

e1; var x = e2
===> var x = e1, e2
e1; return e2
==> return e1, e2
e1; if (e2) ...
==>
if (e1, e2) ...
e1; e2
==>
e1, e2

Improve handling of functions

We are currently generating assignments for variables defined before a function instead of inlining them, just in case they are bound in the function. Typically, we have

var x = 0; f(x, (y)=>e)

where we could have

f(0, (y)=>e)
hhugo commented

Turn conditional statements into expressions

We already do some of this in js_simpl.ml

Then, we may be able to coalesce more variable declarations, or omit the return of an arrow function.

Is that something that will happen magically when #1467 is merged, or will it required further work?

hhugo commented

Then, we may be able to coalesce more variable declarations, or omit the return of an arrow function.

Is that something that will happen magically when #1467 is merged, or will it required further work?

Possibly automatic with --enable es6.
See #1398

We tried adding these changes in #1467 and found that they generally increased code size after compression, so we may want to close this issue.

Improve handling of functions

This is fixed by #1568

To summarize, @micahcantor's implementation of the first two ideas has shown no benefits, and the third idea has been implemented. Should we close then?