purescript-backend-optimizer icon indicating copy to clipboard operation
purescript-backend-optimizer copied to clipboard

Add docs on how to think through inline directives

Open jam-awake opened this issue 3 years ago • 4 comments

This is my attempt at explaining the what, why, and how of inline directives, so I am better equipped to fix #6 in a future PR (e.g. #21).

jam-awake avatar Sep 09 '22 20:09 jam-awake

🤔... I'm not sure how to replace the examples I provided with correct ones that still illustrate the idea I'm trying to convey.

jam-awake avatar Sep 09 '22 23:09 jam-awake

🤔... I'm not sure how to replace the examples I provided with correct ones that still illustrate the idea I'm trying to convey.

I think in general, you should start from smaller examples and build up. I think you jump into let bindings and compound expressions too quickly. For example:

1 + 42

Can be reduced to 43, since the optimizer knows how to evaluate primitive addition (You can also mention examples of other primitive operations it knows how to evaluate). However:

a + 42

where a is a free variable, cannot be reduced because we don't know what a is yet.

{ foo: 42 }.foo

Can be reduced to 42, because we know what expression (42) is at the record property foo. However:

a.foo

where a is a free variable, cannot be reduced because we don't know what a is yet.

case Right 42 of
  Right a -> a + 1
  Left b -> b 

Can be reduced to 42 + 1, because we known which constructor is being caseed on. And that can subsequently be reduced to 43 based on the previous examples.

a = { foo: 42 }
b = a.foo + 1

Cannot be reduced yet because in the expression a.foo, a is still free. However, if we inline our a binding, such that our expression is now:

b = { foo: 42 }.foo + 1

We can follow the same steps to reduce b to 42 + 1, and then 43. The basis for the optimizer is determining when and how things like a should be inlined to expose opportunities to further evaluate and reduce our code, which is done through a set of conservative heuristics.

Then inlining directives are a way for authors to tell the optimizer when a top-level binding should be inlined apart from it's own heuristics if we know that doing so is advantageous.

natefaubion avatar Sep 09 '22 23:09 natefaubion

Ok, I rewrote the document with your feedback in mind.

jam-awake avatar Sep 14 '22 22:09 jam-awake

This PR seems to have been open for a very long time, though it seams to have all issues resolved. How can I help it to be merged?

Neppord avatar Jun 12 '24 11:06 Neppord