<div dir="ltr">While trying to come up with a minimal example I discovered one more puzzling thing. runghc is fastest, ghc is slower, ghc with optimization is slowest. This is completely reverse of the expected order.<div><br></div><div>







<p class="gmail-p1"><span class="gmail-s1">ghc -O1 (-O2 is similar): </span></p><p class="gmail-p1"><span class="gmail-s1">time <span class="gmail-Apple-converted-space">                </span>15.23 ms <span class="gmail-Apple-converted-space">  </span>(14.72 ms .. 15.73 ms)</span></p><p class="gmail-p1"><span class="gmail-s1">ghc -O0:</span></p><p class="gmail-p1"><span class="gmail-s1">







</span></p><p class="gmail-p1"><span class="gmail-s1">time <span class="gmail-Apple-converted-space">                </span>3.612 ms <span class="gmail-Apple-converted-space">  </span>(3.548 ms .. 3.728 ms)</span></p><p class="gmail-p1"><span class="gmail-s1">runghc:</span></p><p class="gmail-p1">







</p><p class="gmail-p1"><span class="gmail-s1">time <span class="gmail-Apple-converted-space">                </span>2.250 ms <span class="gmail-Apple-converted-space">  </span>(2.156 ms .. 2.348 ms)</span></p><p class="gmail-p1"><span class="gmail-s1"><br></span></p><p class="gmail-p1"><span class="gmail-s1">I am grokking it further. Any pointers will be helpful. I understand that -O2 can sometimes be slower e.g. aggressive inlining can sometimes be counterproductive. But 4x variation is a lot and this is the case with -O1 as well which should be relatively safer than -O2 in general. Worst of all runghc is significantly faster than ghc. What's going on?</span></p><p class="gmail-p1"><span class="gmail-s1">-harendra</span></p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 8 September 2017 at 18:49, 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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra">I will try creating a minimal example and open a ticket for the inlining problem, the one I am sure about. </div><span class="HOEnZb"><font color="#888888"><div class="gmail_extra"><br></div><div class="gmail_extra">-harendra</div></font></span><div><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On 8 September 2017 at 18:35, Simon Peyton Jones <span dir="ltr"><<a href="mailto:simonpj@microsoft.com" target="_blank">simonpj@microsoft.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div lang="EN-GB" link="blue" vlink="purple">
<div class="m_-6330208574288526084m_4046352102699041120WordSection1">
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">I know that this is not an easy request</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">,
 but can either of you produce a small example that demonstrates your problem?   If so, please open a ticket.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">I don’t like hearing about people having to use trial and error  with INLINE or SPECIALISE pragmas.  But I can’t even begin to solve the problem
 unless I can reproduce it.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">Simon<u></u><u></u></span></p>
<p class="MsoNormal"><a name="m_-6330208574288526084_m_4046352102699041120__MailEndCompose"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></a></p>
<span></span>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #e1e1e1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif"> ghc-devs [mailto:<a href="mailto:ghc-devs-bounces@haskell.org" target="_blank">ghc-devs-bounces@haske<wbr>ll.org</a>]
<b>On Behalf Of </b>Harendra Kumar<br>
<b>Sent:</b> 08 September 2017 13:50<br>
<b>To:</b> Mikolaj Konarski <<a href="mailto:mikolaj.konarski@gmail.com" target="_blank">mikolaj.konarski@gmail.com</a>><br>
<b>Cc:</b> <a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
<b>Subject:</b> Re: Performance degradation when factoring out common code<u></u><u></u></span></p>
</div>
</div><div><div class="m_-6330208574288526084h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
I should also point out that I saw performance improvements by manually factoring out and propagating some common expressions to outer loops in performance sensitive paths. Now I have made this a habit to do this manually. Not sure if something like this has
 also been fixed with that ticket or some other ticket.<u></u><u></u></p>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
<u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
-harendra<u></u><u></u></p>
</div>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
<u></u> <u></u></p>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
On 8 September 2017 at 17:34, Harendra Kumar <<a href="mailto:harendra.kumar@gmail.com" target="_blank">harendra.kumar@gmail.com</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
Thanks Mikolaj! I have seen some surprising behavior quite a few times recently and I was wondering whether GHC should do better. In one case I had to use SPECIALIZE very aggressively, in another version of the same code it worked well without that. I have
 been doing a lot of trial and error with the INLINE/NOINLINE pragmas to figure out what the right combination is. Sometimes it just feels like black magic, because I cannot find a rationale to explain the behavior. I am not sure if there are any more such
 problems lurking in, perhaps this is an area where some improvement looks possible.<span class="m_-6330208574288526084m_4046352102699041120hoenzb"><span style="color:#888888"><u></u><u></u></span></span></p>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
<u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
<span style="color:#888888">-harendra<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
<span style="color:#888888"><u></u> <u></u></span></p>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
<u></u> <u></u></p>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
On 8 September 2017 at 17:10, Mikolaj Konarski <<a href="mailto:mikolaj.konarski@gmail.com" target="_blank">mikolaj.konarski@gmail.com</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
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" 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<u></u><u></u></p>
<div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
<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 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 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 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 somehow?<br>
><br>
> Thanks,<br>
> Harendra<br>
><u></u><u></u></p>
</div>
</div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
> ______________________________<wbr>_________________<br>
> ghc-devs mailing list<br>
> <a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
> <a href="https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmail.haskell.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fghc-devs&data=02%7C01%7Csimonpj%40microsoft.com%7C5ff3c69fb9d447c47b5908d4f6b832de%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636404718373134824&sdata=zyHYozym6TzL61Tq5CSERjqhKlxr%2ByV0j%2FyHtxmXmVE%3D&reserved=0" target="_blank">
http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/ghc-devs</a><br>
><u></u><u></u></p>
</blockquote>
</div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
<u></u> <u></u></p>
</div>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
<u></u> <u></u></p>
</div>
</div></div></div>
</div>
</div>

</blockquote></div><br></div></div></div></div>
</blockquote></div><br></div>