2020-08-16 17:01:18 +03:00
# Overview of Transformations
2020-07-28 16:16:18 +03:00
2020-08-24 14:56:01 +03:00
This is an overview of the transformations for `elm-optimize-level-2` .
2020-07-28 16:16:18 +03:00
2020-08-16 17:01:18 +03:00
Not all of them made the cut, but seeing that a transformation is not as effective as initially thought is really good information.
2020-08-13 02:55:03 +03:00
We got a huge head start because of [Robin's article ](https://dev.to/skinney/improving-elm-s-compiler-output-5e1h ).
2020-07-28 16:16:18 +03:00
2020-08-16 17:01:18 +03:00
Each transformation also has a rough summary of impact.
2020-07-28 16:16:18 +03:00
2020-08-13 02:55:03 +03:00
# Applying Functions Directly
2020-08-16 21:25:20 +03:00
Elm wraps functions in an object that tracks how many arguments the function takes(also known as 'arity').
2020-08-13 02:55:03 +03:00
2020-08-16 21:25:20 +03:00
This is so that functions can be partially applied, meaning you can apply a few arguments and get a new function that has those arguments "built in".
2020-08-13 02:55:03 +03:00
2020-08-19 15:45:30 +03:00
The most significant speedups we've seen is in finding places where we can skip the wrapper and call the actual function directly. This happens when you call a function with exactly the number of arguments it needs.
2020-08-13 02:55:03 +03:00
2020-08-23 22:05:08 +03:00
In order to do this, we need to adjust function declarations so that the original function can be called either in the standard 'wrapped' way, or directly.
2020-08-13 02:55:03 +03:00
before
```js
var MyFunction = F2(function (tag, value) {
2020-08-19 15:45:30 +03:00
return value;
2020-08-13 02:55:03 +03:00
});
```
after
```js
2020-08-16 21:25:20 +03:00
var MyFunction_fn = function (tag, value) {
2020-08-19 15:45:30 +03:00
return value;
},
MyFunction = F2(MyFunction_fn);
2020-08-13 02:55:03 +03:00
```
2020-08-23 22:05:08 +03:00
Then, if this function is called with `A2` , we can unwrap the wrapper and call the function directly.
2020-08-13 02:55:03 +03:00
before
2020-08-19 15:45:30 +03:00
2020-08-13 02:55:03 +03:00
```js
A2(MyFunction, one two)
```
after
2020-08-19 15:45:30 +03:00
2020-08-13 02:55:03 +03:00
```js
MyFunction_fn(one two)
```
2020-08-16 21:25:20 +03:00
## Results Summary
2020-08-24 14:56:01 +03:00
- Included in `elm-optimize-level-2` tool\*\*
2020-08-16 21:25:20 +03:00
- Potentially large positive effect on speed
- Likley small but positive effect on asset size
This has lead to dramatic speedups in some cases, especially when a large number of smaller functions are called and the overhead of calling twice as many functions is significant.
As well, it has a really interesting characteristic in that it makes the initial size of the generated JS **larger** , but usually results in a **smaller** minified asset size.
We generate two definitions for a function, but in most cases a function is either always partially applied, or always called with the full number of arguments.
2020-08-19 15:45:30 +03:00
If a function is always called with the full number of arguments, the minifier can eliminate our wrapped version (`F2(MyFunction_fn)`) and _also_ eliminate the `A2` call, which is explicitly smaller than before.
2020-08-13 02:55:03 +03:00
2020-08-17 07:15:04 +03:00
# Passing unwrapped functions and calling them directly
2020-08-17 00:38:39 +03:00
Let's say we have some elm code that produces the following js.
```js
2020-08-19 15:45:30 +03:00
var f = function (func, a, b) {
return A2(func, a, b);
2020-08-17 00:38:39 +03:00
};
2020-08-19 15:45:30 +03:00
f(
F2(function (a, b) {
return a + b;
}),
1,
2
);
2020-08-17 00:38:39 +03:00
```
2020-08-19 15:45:30 +03:00
we can transform it to
2020-08-17 00:38:39 +03:00
```js
2020-08-19 15:45:30 +03:00
var f = function (func, a, b) {
return A2(func, a, b);
},
f_unwrapped = function (func, a, b) {
return func(a, b); // < -- direct function call !
};
2020-08-17 00:38:39 +03:00
2020-08-17 07:15:04 +03:00
// note that the lambda is unwrapped as well
2020-08-19 15:45:30 +03:00
f_unwrapped(
function (a, b) {
return a + b;
},
1,
2
);
2020-08-17 00:38:39 +03:00
```
2020-08-17 07:15:04 +03:00
This transformation works with separately defined functions too.
2020-08-13 02:55:03 +03:00
# Passing in Unwrappable Functions to Higher Order Functions
2020-08-17 00:38:39 +03:00
**Future Work**
2020-08-16 21:25:20 +03:00
Higher order functions like `List.map` have a hard time taking advantage of the direct function calls because we don't know the arity of the function within the `List.map` call.
2020-08-19 16:25:25 +03:00
This is a challenging case, but worth exploring!
2020-08-13 02:55:03 +03:00
# Making type representation isomorphic
2020-07-28 16:16:18 +03:00
Currently the Elm compiler will generate objects that match the shape of a given type.
2020-08-13 02:55:03 +03:00
`Maybe` looks like this:
2020-07-28 16:16:18 +03:00
2020-08-01 19:56:23 +03:00
```js
2020-08-19 15:45:30 +03:00
var elm$core$Maybe$Just = function (a) {
2020-08-01 19:56:23 +03:00
return { $: 0, a: a };
2020-07-28 16:16:18 +03:00
};
2020-08-01 19:56:23 +03:00
var elm$core$Maybe$Nothing = { $: 1 };
2020-07-28 16:16:18 +03:00
```
However, the V8 engine is likely better able to optimize these objects if they have the same shape.
So, this transformation fills out the rest of the variants with `field: null` so that they have the same shape.
2020-08-01 19:56:23 +03:00
```js
2020-08-19 15:45:30 +03:00
var elm$core$Maybe$Just = function (a) {
2020-08-01 19:56:23 +03:00
return { $: 0, a: a };
2020-07-28 16:16:18 +03:00
};
2020-08-01 19:56:23 +03:00
var elm$core$Maybe$Nothing = { $: 1, a: null };
2020-07-28 16:16:18 +03:00
```
This does require information from the Elm code itself, which we're currently getting through `elm-tree-sitter` .
2020-08-16 21:25:20 +03:00
## Results Summary
2020-08-21 15:46:31 +03:00
- Partially Included - We stub in shapes for `Maybe` and `List` , but that's it.
2020-08-19 15:45:30 +03:00
- Has an effect in certain circumstances in browsers using V8(Chrome and Edge). Nothing observable otherwise.
- Most prominently observed in the `Elm Core - sum 300 list of custom types` benchmark. Otherwise I didn't notice it.
2020-08-16 21:25:20 +03:00
- No noticable effect on asset size.
2020-08-21 15:46:31 +03:00
- More work is needed to make parsing an existing project more robust and also to discover what approach for representing the shapes actually produces the best benefit.
2020-08-16 21:25:20 +03:00
2020-08-13 02:55:03 +03:00
# Inlining literal list constructors
2020-07-28 16:16:18 +03:00
2020-08-13 02:55:03 +03:00
Before
2020-07-28 16:16:18 +03:00
2020-07-29 16:55:57 +03:00
```js
_List_fromArray(['a', 'b', 'c']);
```
2020-07-28 16:16:18 +03:00
2020-08-13 02:55:03 +03:00
After, using `InlineMode.UsingConsFunc`
2020-07-28 16:16:18 +03:00
2020-07-29 16:55:57 +03:00
```js
2020-08-01 19:56:23 +03:00
_List_cons('a', _List_cons('b', _List_cons('c', _List_Nil)));
```
2020-07-28 16:16:18 +03:00
2020-08-13 02:55:03 +03:00
with `InlineMode.UsingLiteralObjects`
2020-08-01 19:56:23 +03:00
```js
2020-08-13 02:55:03 +03:00
({ $: 1, a: 'a', b: { $: 1, a: 'b', b: { $: 1, a: 'c', b: _List_Nil } } });
2020-07-29 16:55:57 +03:00
```
2020-08-23 22:05:08 +03:00
_Note_ - Elm actually had this originally (the literal objects version)! But there's an issue in Chrome with more than 1000 elements.
2020-07-29 16:55:57 +03:00
2020-08-13 02:55:03 +03:00
There's also tradeoff between asset size and speed.
2020-07-28 16:16:18 +03:00
2020-08-23 22:05:08 +03:00
Also of note, because `_List_fromArray` is used for lists of _anything_ , that it's likely being deoptimized by the JavaScript compiler.
2020-07-29 16:55:57 +03:00
2020-08-23 22:05:08 +03:00
There may be a nice trade-off here of using `InlineMode.UsingConsFunc` , but only inlining at most 20 elements or something, and then using `List_fromArray` after that.
2020-07-29 16:55:57 +03:00
2020-08-16 21:25:20 +03:00
## Results Summary
2020-08-24 14:56:01 +03:00
- Not included in the elm-optimize-level-2 tool because it was hard to find a benchmark that reported numbers to justify it.
2020-08-23 22:05:08 +03:00
- Though maybe we just need to be better at benchmarking it.
2020-08-17 00:38:39 +03:00
2020-08-16 21:25:20 +03:00
# Object Update
2020-08-23 22:05:08 +03:00
When updating a record in Elm via `{ record | field = new }` , Elm runs the following function:
2020-08-16 21:25:20 +03:00
```javascript
function _Utils_update(oldRecord, updatedFields) {
2020-08-19 15:45:30 +03:00
var newRecord = {};
for (var key in oldRecord) {
newRecord[key] = oldRecord[key];
}
for (var key in updatedFields) {
newRecord[key] = updatedFields[key];
}
return newRecord;
2020-08-16 21:25:20 +03:00
}
```
We tried a few different variations in order to see if we could speed this up.
The trick here is that we need to copy the entire record so that it has a new reference.
So, we can't just do `record.field = new` in the js.
All of these tricks rely on either the spread operator or `Object.assign` , both of which are not supported in IE.
## Replacing the implementation of `_Util_update`:
Spread operator
2020-08-19 15:45:30 +03:00
```javascript
2020-08-16 21:25:20 +03:00
const _Utils_update = (oldRecord, updatedFields) => {
2020-08-19 15:45:30 +03:00
var newRecord = { ...oldRecord };
for (var key in updatedFields) {
newRecord[key] = updatedFields[key];
}
return newRecord;
};
2020-08-16 21:25:20 +03:00
```
Spread for both
```javascript
2020-08-19 15:45:30 +03:00
const _Utils_update = (oldRecord, updatedFields) => ({
...oldRecord,
...updatedFields,
});
2020-08-16 21:25:20 +03:00
```
Use Object.assign
2020-08-19 15:45:30 +03:00
2020-08-16 21:25:20 +03:00
```javascript
2020-08-19 15:45:30 +03:00
const _Utils_update = (oldRecord, updatedFields) =>
Object.assign({}, oldRecord, updatedFields);
2020-08-16 21:25:20 +03:00
```
## Inline the call altogether
At the call site, replace
2020-08-21 16:55:30 +03:00
```js
_Utils_update(old, newFields);
2020-08-16 21:25:20 +03:00
```
2020-08-19 15:45:30 +03:00
2020-08-16 21:25:20 +03:00
with
2020-08-19 15:45:30 +03:00
2020-08-21 16:55:30 +03:00
```js
Object.assign({}, old, newFields);
2020-08-16 21:25:20 +03:00
```
2020-08-21 16:54:23 +03:00
Or we can use the spread operator inline:
2020-08-21 16:55:30 +03:00
```js
{...old, field: new}
2020-08-21 16:54:23 +03:00
```
2020-08-16 21:25:20 +03:00
## Result Summary
2020-08-24 14:56:01 +03:00
- Not included in elm-optimize-level-2 tool
2020-08-16 21:25:20 +03:00
- Again, all of these tricks rely on either the spread operator or `Object.assign` , both of which are not supported in IE.
2020-08-21 16:54:23 +03:00
- The most promising approach was inlining the call completely with `{...old, field: newValue}` .
- Gave a `501%` boost in chrome!
- And caused safari to reduce performance by 50% :sweat_smile:
2020-08-16 21:25:20 +03:00
2020-08-23 22:05:08 +03:00
Simply creating a new record and copying each field manually is significantly faster than any of these transformations (~9x in chrome, and ~6.5x in firefox). You can do this directly in Elm.
2020-08-16 21:25:20 +03:00
```
updateSingleRecordManually record =
{ one = 87
, two = record.two
, three = record.three
}
```
2020-08-17 00:38:39 +03:00
It's worth exploring automating this transformation, though of course there's a question of how much this affects asset size on larger projects.
2020-08-16 21:25:20 +03:00
2020-08-17 00:38:39 +03:00
However, it's hard to explore further without knowing the actual shape of the records being updated.
2020-08-16 21:25:20 +03:00
2020-08-17 07:15:04 +03:00
**Future work**
Explore more approaches. Next on TODO list:
2020-08-19 15:45:30 +03:00
2020-08-17 07:15:04 +03:00
```
_Utils_update(old, {a: newA})
```
2020-08-19 15:45:30 +03:00
2020-08-17 07:15:04 +03:00
to
2020-08-19 15:45:30 +03:00
2020-08-17 07:15:04 +03:00
```
{...old, a: newA}
```
2020-08-16 21:25:20 +03:00
2020-08-13 02:55:03 +03:00
# Inline Equality
If Elm's `==` is applied to any primitive such as:
2020-08-19 15:45:30 +03:00
- Int
- Float
- String
- Bool
2020-08-13 02:55:03 +03:00
2020-08-19 16:25:25 +03:00
Then we can inline the definition directly as JS strict equality: `===` .
2020-08-13 02:55:03 +03:00
2020-08-24 14:56:01 +03:00
Right now `elm-optimize-level-2` will infer if something is a primitive if a literal is used.
2020-08-16 21:25:20 +03:00
## Results Summary
2020-08-13 02:55:03 +03:00
2020-08-24 14:56:01 +03:00
- Included in `elm-optimize-level-2` tool.
2020-08-19 16:25:25 +03:00
- Looks to have the some impact on code that does a lot of equality comparisons, like parsing.
2020-08-17 00:38:39 +03:00
2020-08-19 15:45:30 +03:00
The `_Utils_eq` function is very likely deoptimized because it can take _any_ two values and either do a reference check, or do structural equality, which we also know takes a while.
2020-08-17 00:38:39 +03:00
So, my guess is the benefit here is from avoiding the call to a deoptimized function completely.
Chrome doesn't really see a speedup here though, so it's likely smart enough to do that already.
2020-08-13 02:55:03 +03:00
# Inline String.fromFloat/Int
2020-08-14 16:27:15 +03:00
Before
2020-08-21 16:55:30 +03:00
```js
String$fromFloat(val);
2020-08-14 16:27:15 +03:00
```
After:
2020-08-21 16:55:30 +03:00
```js
val + '';
2020-08-16 21:25:20 +03:00
```
## Results Summary
2020-08-17 00:38:39 +03:00
- Not included in the tool
2020-08-19 15:45:30 +03:00
This hasn't shown any measureable benefit. Likely because this is such a simple function that all js compilers are already optimizing the intermedaite calls.
2020-08-16 21:25:20 +03:00
# Arrowizing Functions
Before
2020-08-21 16:55:30 +03:00
```js
var x = function (x) {};
2020-08-16 21:25:20 +03:00
```
2020-08-19 15:45:30 +03:00
2020-08-16 21:25:20 +03:00
After
2020-08-19 15:45:30 +03:00
2020-08-21 16:55:30 +03:00
```js
var x = (x) => {};
2020-08-16 21:25:20 +03:00
```
2020-08-19 15:45:30 +03:00
This was done for asset size. The nuance being that it's done to potentially optimize the _minified_ size of code, but not necessarily the gzipped version.
2020-08-16 21:25:20 +03:00
2020-08-19 15:45:30 +03:00
This is still a benefit because the minified code is what ultimately needs to be parsed and parsing is one of the larger steps on the way to getting a page running.
2020-08-16 21:25:20 +03:00
## Results Summary
2020-08-24 14:56:01 +03:00
- Not included in the `elm-optimize-level-2` tool
2020-08-16 21:25:20 +03:00
- Comes with the caveat that the [code will not work on IE ](https://caniuse.com/#feat=arrow-functions )
2020-08-19 15:45:30 +03:00
We weren't able to pin down a benchmark where this reported a benefit in the numbers, though likely to explore this we need (1) A larger codebase, and (2)
2020-08-16 21:25:20 +03:00
We didn't include this in the first version of the tool because the effect seems to be so modest and carries the risk of breaking things on IE.
We would have to add something like a `--modernize` or `--no-ie` flag to the tool, and I really like this tool having no configurability.
2020-08-19 15:45:30 +03:00
# Lifting Constants
2020-08-17 00:38:39 +03:00
**Future Work**
This transformation hasn't been attempted yet, but the idea is that if a constant is detected in a let statement, it can be declared moved to top-level instead of recalculated every function run.
2020-08-20 16:23:02 +03:00
This is risky! You do less computation, but you are (1) moving a bunch of computation to happen on start-up and (2) the results are allocated but can never be freed.
2020-08-19 15:45:30 +03:00
This could be worthwhile in HTML though, where there is a x === y check on nodes:
https://github.com/elm/virtual-dom/blob/master/src/Elm/Kernel/VirtualDom.js#L706-L709
2020-08-20 16:23:02 +03:00
So, if two nodes were reference equal, you wouldn't have to ever diff them. I imagine this could be a big benefit if there was a long list where each element contained a somewhat large "constant" node for some UI thing.
2020-08-19 16:25:25 +03:00
# Eta Conversion
This is when you add or remove anonymous functions:
`map (f x y) zs` to `map (\z -> f x y z) zs`
Because of our previous optimizations where we can call a function directly, this can make sure we're getting the fast version of `f` !
2020-08-21 15:46:31 +03:00
# Tail Recursion Modulo Cons
2020-08-21 16:44:29 +03:00
Whew, what a name!
2020-08-21 15:46:31 +03:00
2020-08-23 22:05:08 +03:00
In Elm, if you have a recursive function that calls itself at the top level, then it will be compiled into a while loop. Here's an example:
2020-08-21 16:44:29 +03:00
```elm
sum : Int -> List Int -> Int
sum current list =
case list of
[] ->
current
(x :: remaining) ->
sum (x + current) remaining
```
The critical part here is that `sum` is called as the _first thing_ on that branch. Because that's easy to detect, we can reliably convert the above code into a `while` loop. (Note, all this stuff is called _Tail Call Optimization_ ).
However, it's a pretty common case where we can't quite do that.
Let's take a look at an implementation of `List.map` . (Note, this isn't how Elm currently does it, this is just for illustrative purposes.)
We could implement `List.map` like this:
```elm
type List a = Nil | Cons a (List a)
map : (a -> b) -> List a -> List b
map func list =
case list of
Nil -> Nil
Cons x xs ->
Cons (func x) (map func xs)
```
But our recursive function(`map`) is not the _first thing_ being called in that branch! It's the `Cons` constructor! (Heyy, Tail Recursion Modulo **Cons** )
The idea for this transformation is that we could take the above code, and generate the following JS code.
```js
function map(func, list) {
var first;
var prev;
while (true) {
if (list.$ === 'Nil') {
if (prev) {
prev.b = Nil;
} else {
first = Nil;
}
return first;
} else {
var node = Cons(func(list.a), null);
if (prev) {
prev.b = node;
} else {
first = node;
}
prev = node;
list = list.b;
}
}
}
```
2020-08-22 02:04:54 +03:00
# Skip Allocating Tuples in Case Statements
2020-08-23 22:05:08 +03:00
It's pretty common to put things in a tuple (or threeple) to start a case statement.
2020-08-22 02:04:54 +03:00
```elm
case (a, b) of
(ThingOne, ThingTwo) ->
--...
```
We could skip allocating the tuple though.