<div dir="ltr">Ok, I filed a ticket for the inlining issue as well - <a href="https://ghc.haskell.org/trac/ghc/ticket/14211">https://ghc.haskell.org/trac/ghc/ticket/14211</a>. The reproduction test case is in the same repo on the "inlining-issue" branch, here - <a href="https://github.com/harendra-kumar/ghc-perf/tree/inlining-issue">https://github.com/harendra-kumar/ghc-perf/tree/inlining-issue</a> .<div><br></div><div><p style="color:rgb(0,0,0);font-family:Verdana,Arial,"Bitstream Vera Sans",Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,221)">Performance with manually inlining a function is more than 10% faster compared to factoring out code and using INLINE pragma.</p><p style="color:rgb(0,0,0);font-family:Verdana,Arial,"Bitstream Vera Sans",Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,221)"><code style="color:rgb(102,0,0);border:1px solid rgb(238,221,204);border-radius:0.25em;padding:0px 0.3em;background:rgb(250,250,250)">stack bench</code> for compiler inlined code</p><pre class="gmail-wiki" style="background:rgb(247,247,247);border:1px solid rgb(215,215,215);border-radius:0.3em;margin:1em 1.75em;padding:0.25em;overflow:auto;color:rgb(0,0,0);font-size:13px">time                 46.71 ms   (45.53 ms .. 47.79 ms)
</pre><p style="color:rgb(0,0,0);font-family:Verdana,Arial,"Bitstream Vera Sans",Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,221)"><code style="color:rgb(102,0,0);border:1px solid rgb(238,221,204);border-radius:0.25em;padding:0px 0.3em;background:rgb(250,250,250)">stack bench --flag ghc-perf:manual</code> for manually inlined code</p><pre class="gmail-wiki" style="background:rgb(247,247,247);border:1px solid rgb(215,215,215);border-radius:0.3em;margin:1em 1.75em;padding:0.25em;overflow:auto;color:rgb(0,0,0);font-size:13px">time                 39.46 ms   (38.92 ms .. 39.94 ms)</pre></div><div class="gmail_extra"><br></div><div class="gmail_extra">-harendra</div><div class="gmail_extra"><br><div class="gmail_quote">On 9 September 2017 at 14:08, Harendra Kumar <span dir="ltr"><<a href="mailto:harendra.kumar@gmail.com" target="_blank">harendra.kumar@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">The code is at: <a href="https://github.com/harendra-kumar/asyncly" target="_blank">https://github.com/<wbr>harendra-kumar/asyncly</a>. The benchmark code is in "benchmark/Main.hs".  The relevant function is "asyncly_basic".<div><br></div><div>If you want to run it, you can use the following steps to reproduce the behavior I reported below:<div><br></div><div>1) Run "stack build"</div><div>2) Run "stack runghc benchmark/Main.hs" for runghc figures</div><div>3) Run "stack ghc benchmark/Main.hs && benchmark/Main" to compile and run normally</div><div>4) Run "stack ghc -- -O2 benchmark/Main.hs && benchmark/Main" to compile and run with -O2 flag</div><div><br></div><div>Just look at the first benchmark (asyncly-serial), you can comment out all others if you want to. Note that the library gets compiled without any optimization flags (see the ghc options in the cabal file). So what we are seeing here is just the effect of -O2 on compiling benchmarks/Main.hs.</div><div><br></div><div>I am also trying to isolate the problem to a minimal case. I tried removing all the INLINE pragmas in the library to make sure that I am not screwing it up by asking the compiler to inline aggressively, but that does not seem to make any difference to the situation. Let me know if you need any information from me or help in running it.</div><div><br></div><div>There are three issues that I am trying to get answers for:</div><div><br></div><div>1) Why runghc is faster? It means that there is a possibility for the program to run as fast as runghc runs it. How do I get that performance or an explanation of it?</div><div><br></div><div>2) Why -O1/O2 degrades performance so much by 4-5x.</div><div><br></div><div>3) The third one is the original problem that I posted in this thread, compiler is unable to match manual inlining. It is possible that this is an issue only when -O1/O2 is used and not when -O0 is used.</div><div><br></div><div>Thanks for the help.</div><span class="gmail-HOEnZb"><font color="#888888"><div><br></div></font></span><div><span class="gmail-HOEnZb"><font color="#888888">-harendra</font></span><div><div class="gmail-h5"><br><div><div class="gmail_extra"><br><div class="gmail_quote">On 9 September 2017 at 13:30, Matthew Pickering <span dir="ltr"><<a href="mailto:matthewtpickering@gmail.com" target="_blank">matthewtpickering@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Do you have the code?<br>
<div class="gmail-m_-5073377170268516606gmail-HOEnZb"><div class="gmail-m_-5073377170268516606gmail-h5"><br>
On Sat, Sep 9, 2017 at 6:05 AM, Harendra Kumar <<a href="mailto:harendra.kumar@gmail.com" target="_blank">harendra.kumar@gmail.com</a>> wrote:<br>
> While trying to come up with a minimal example I discovered one more<br>
> puzzling thing. runghc is fastest, ghc is slower, ghc with optimization is<br>
> slowest. This is completely reverse of the expected order.<br>
><br>
> ghc -O1 (-O2 is similar):<br>
><br>
> time                 15.23 ms   (14.72 ms .. 15.73 ms)<br>
><br>
> ghc -O0:<br>
><br>
> time                 3.612 ms   (3.548 ms .. 3.728 ms)<br>
><br>
> runghc:<br>
><br>
> time                 2.250 ms   (2.156 ms .. 2.348 ms)<br>
><br>
><br>
> I am grokking it further. Any pointers will be helpful. I understand that<br>
> -O2 can sometimes be slower e.g. aggressive inlining can sometimes be<br>
> counterproductive. But 4x variation is a lot and this is the case with -O1<br>
> as well which should be relatively safer than -O2 in general. Worst of all<br>
> runghc is significantly faster than ghc. What's going on?<br>
><br>
> -harendra<br>
><br>
><br>
> On 8 September 2017 at 18:49, Harendra Kumar <<a href="mailto:harendra.kumar@gmail.com" target="_blank">harendra.kumar@gmail.com</a>><br>
> wrote:<br>
>><br>
>> I will try creating a minimal example and open a ticket for the inlining<br>
>> problem, the one I am sure about.<br>
>><br>
>> -harendra<br>
>><br>
>> On 8 September 2017 at 18:35, Simon Peyton Jones <<a href="mailto:simonpj@microsoft.com" target="_blank">simonpj@microsoft.com</a>><br>
>> wrote:<br>
>>><br>
>>> I know that this is not an easy request, but can either of you produce a<br>
>>> small example that demonstrates your problem?   If so, please open a ticket.<br>
>>><br>
>>><br>
>>><br>
>>> I don’t like hearing about people having to use trial and error  with<br>
>>> INLINE or SPECIALISE pragmas.  But I can’t even begin to solve the problem<br>
>>> unless I can reproduce it.<br>
>>><br>
>>><br>
>>><br>
>>> Simon<br>
>>><br>
>>><br>
>>><br>
>>> From: ghc-devs [mailto:<a href="mailto:ghc-devs-bounces@haskell.org" target="_blank">ghc-devs-bounces@haske<wbr>ll.org</a>] On Behalf Of<br>
>>> Harendra Kumar<br>
>>> Sent: 08 September 2017 13:50<br>
>>> To: Mikolaj Konarski <<a href="mailto:mikolaj.konarski@gmail.com" target="_blank">mikolaj.konarski@gmail.com</a>><br>
>>> Cc: <a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
>>> Subject: Re: Performance degradation when factoring out common code<br>
>>><br>
>>><br>
>>><br>
>>> I should also point out that I saw performance improvements by manually<br>
>>> factoring out and propagating some common expressions to outer loops in<br>
>>> performance sensitive paths. Now I have made this a habit to do this<br>
>>> manually. Not sure if something like this has also been fixed with that<br>
>>> ticket or some other ticket.<br>
>>><br>
>>><br>
>>><br>
>>> -harendra<br>
>>><br>
>>><br>
>>><br>
>>> On 8 September 2017 at 17:34, Harendra Kumar <<a href="mailto:harendra.kumar@gmail.com" target="_blank">harendra.kumar@gmail.com</a>><br>
>>> wrote:<br>
>>><br>
>>> Thanks Mikolaj! I have seen some surprising behavior quite a few times<br>
>>> recently and I was wondering whether GHC should do better. In one case I had<br>
>>> to use SPECIALIZE very aggressively, in another version of the same code it<br>
>>> worked well without that. I have been doing a lot of trial and error with<br>
>>> the INLINE/NOINLINE pragmas to figure out what the right combination is.<br>
>>> Sometimes it just feels like black magic, because I cannot find a rationale<br>
>>> to explain the behavior. I am not sure if there are any more such problems<br>
>>> lurking in, perhaps this is an area where some improvement looks possible.<br>
>>><br>
>>><br>
>>><br>
>>> -harendra<br>
>>><br>
>>><br>
>>><br>
>>><br>
>>><br>
>>> On 8 September 2017 at 17:10, Mikolaj Konarski<br>
>>> <<a href="mailto:mikolaj.konarski@gmail.com" target="_blank">mikolaj.konarski@gmail.com</a>> wrote:<br>
>>><br>
>>> Hello,<br>
>>><br>
>>> I've had a similar problem that's been fixed in 8.2.1:<br>
>>><br>
>>> <a href="https://ghc.haskell.org/trac/ghc/ticket/12603" rel="noreferrer" target="_blank">https://ghc.haskell.org/trac/g<wbr>hc/ticket/12603</a><br>
>>><br>
>>> You can also use some extreme global flags, such as<br>
>>><br>
>>> ghc-options: -fexpose-all-unfoldings -fspecialise-aggressively<br>
>>><br>
>>> to get most the GHC subtlety and shyness out of the way<br>
>>> when experimenting.<br>
>>><br>
>>> Good luck<br>
>>> Mikolaj<br>
>>><br>
>>><br>
>>><br>
>>><br>
>>> On Fri, Sep 8, 2017 at 11:21 AM, Harendra Kumar<br>
>>> <<a href="mailto:harendra.kumar@gmail.com" target="_blank">harendra.kumar@gmail.com</a>> wrote:<br>
>>> > Hi,<br>
>>> ><br>
>>> > I have this code snippet for the bind implementation of a Monad:<br>
>>> ><br>
>>> >     AsyncT m >>= f = AsyncT $ \_ stp yld -><br>
>>> >         let run x = (runAsyncT x) Nothing stp yld<br>
>>> >             yield a _ Nothing  = run $ f a<br>
>>> >             yield a _ (Just r) = run $ f a <> (r >>= f)<br>
>>> >         in m Nothing stp yield<br>
>>> ><br>
>>> > I want to have multiple versions of this implementation parameterized<br>
>>> > by a<br>
>>> > function, like this:<br>
>>> ><br>
>>> > bindWith k (AsyncT m) f = AsyncT $ \_ stp yld -><br>
>>> >     let run x = (runAsyncT x) Nothing stp yld<br>
>>> >         yield a _ Nothing  = run $ f a<br>
>>> >         yield a _ (Just r) = run $ f a `k` (bindWith k r f)<br>
>>> >     in m Nothing stp yield<br>
>>> ><br>
>>> > And then the bind function becomes:<br>
>>> ><br>
>>> > (>>=) = bindWith (<>)<br>
>>> ><br>
>>> > But this leads to a performance degradation of more than 10%. inlining<br>
>>> > does<br>
>>> > not help, I tried INLINE pragma as well as the "inline" GHC builtin. I<br>
>>> > thought this should be a more or less straightforward replacement<br>
>>> > making the<br>
>>> > second version equivalent to the first one. But apparently there is<br>
>>> > something going on here that makes it perform worse.<br>
>>> ><br>
>>> > I did not look at the core, stg or asm yet. Hoping someone can quickly<br>
>>> > comment on it. Any ideas why is it so? Can this be worked around<br>
>>> > somehow?<br>
>>> ><br>
>>> > Thanks,<br>
>>> > Harendra<br>
>>> ><br>
>>><br>
>>> > ______________________________<wbr>_________________<br>
>>> > ghc-devs mailing list<br>
>>> > <a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
>>> > <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/ghc-devs</a><br>
>>> ><br>
>>><br>
>>><br>
>>><br>
>>><br>
>><br>
>><br>
><br>
><br>
> ______________________________<wbr>_________________<br>
> ghc-devs mailing list<br>
> <a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/ghc-devs</a><br>
><br>
</div></div></blockquote></div><br></div></div></div></div></div></div></div>
</blockquote></div><br></div></div>