js_of_ocaml icon indicating copy to clipboard operation
js_of_ocaml copied to clipboard

Code size optimizations

Open vouillon opened this issue 2 years ago • 4 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)

vouillon avatar Feb 03 '23 14:02 vouillon

Turn conditional statements into expressions

We already do some of this in js_simpl.ml

hhugo avatar Feb 03 '23 14:02 hhugo

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?

OlivierNicole avatar Jun 08 '23 09:06 OlivierNicole

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 https://github.com/ocsigen/js_of_ocaml/pull/1398

hhugo avatar Jun 08 '23 11:06 hhugo

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.

micahcantor avatar Jun 21 '23 13:06 micahcantor

Improve handling of functions

This is fixed by #1568

hhugo avatar Feb 28 '24 10:02 hhugo

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?

OlivierNicole avatar Mar 12 '24 10:03 OlivierNicole