UnkindPartition / traverse-with-class

MIT License
5 stars 5 forks source link

Generate INLINE pragma in TH #7

Closed wdanilo closed 6 years ago

wdanilo commented 6 years ago

Hi! Could I ask you to generate INLINE pragma in GTraversal TH generator? Such code is sometimes used in high-performance demanded places and making it inline is in most cases what we want to do here, right?

Thank you!

UnkindPartition commented 6 years ago

Hi Daniło,

I don't mind the pragmas themselves, but you'll need to do the work :)

Specifically,

  1. Add the pragmas.
  2. Post the benchmarks that show improved performance over the no-pragma code compiled with -O and -O2.

After that, and if there are no obvious downsides (like a dramatically increased compilation time), I'll be happy to merge the change.

wdanilo commented 6 years ago

Dear Roman, I made a pull request with the changes, however I will not be able to do the benchmarks, because they are tricky. They are of course doable but require significant amount of work. Sometimes GHC will think this expression is small enough to be inlined, sometimes not. Finding situation where its not is tricky, thats why a lot of standard libraries just put inline pragma on every small util to be sure the work will be done. In fact, putting INLINE pragma on related utils like gfoldl etc is also a good idea.

Cheers, Wojciech

UnkindPartition commented 6 years ago

thats why a lot of standard libraries just put inline pragma on every small util to be sure the work will be done

I don't agree with this practice, and that is why I asked for the benchmarks.

As you say, most of the time GHC will inline the functions that should be inlined — you don't need a pragma for that.

When it's not inlined, the compiler has decided not to inline it. It knows something about the context of the code, optimization level etc. that we, the library authors, don't and can't know in advance. So we should trust the compiler by default. Adding a pragma is an exceptional situation and requires some kind of justification. In addition, if the compiler (uncontroversially) should have inlined something but did not, we should file a ghc bug.

If you can't come up with an example where the function is not inlined, perhaps you are solving a problem that does not exist?