mdgriffith / elm-optimize-level-2

BSD 3-Clause "New" or "Revised" License
128 stars 14 forks source link

Optimization false negative: inlineWrappedFunctions doesn't work for function aliases #101

Open jfmengels opened 2 years ago

jfmengels commented 2 years ago

The inlineWrappedFunctions transformer transforms

var _String_startsWith = F2(function(sub, str)
{
    return str.indexOf(sub) === 0;
});
-- to
var _String_startsWith_fn = function (sub, str) {
    return str.indexOf(sub) === 0;
}, _String_startsWith = F2(_String_startsWith_fn);

But elm/core's String.startsWith is implemented as startsWith = Elm.Kernel.String.startsWith in Elm (var $elm$core$String$startsWith = _String_startsWith; in JS).

The transformer currently ignores these kinds of functions, meaning that they don't benefit from the same optimization. I think the transformer could try to be smarter by noticing this is an alias to a previously optimized function, and split it up as well.

var $elm$core$String$startsWith = _String_startsWith;
-->
var $elm$core$String$startsWith = _String_startsWith, $elm$core$String$startsWith_fn = _String_startsWith_fn;

and then have the transformer use the most optimized version for the newly optimized functions also.