<div dir="ltr">I haven't looked at the second one, but for the first you may be interested in the source code - <a href="https://hackage.haskell.org/package/ghc-internal-9.1001.0/docs/src/GHC.Internal.Real.html#%5E">https://hackage.haskell.org/package/ghc-internal-9.1001.0/docs/src/GHC.Internal.Real.html#%5E</a> - and the comments around [Inlining] and [Powers with small exponent].<div><br></div><div>Notably, it only covers until x^a until a = 5, and indeed with 10^5 the results of the two different ways of writing the number are the same.</div><div><br></div><div>--</div><div>Noon</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, 2 Jul 2024 at 12:35, George Colpitts <<a href="mailto:george.colpitts@gmail.com">george.colpitts@gmail.com</a>> wrote:<br></div><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"><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">It seems like I have found two optimization bugs. Before filing bugs , I wanted to double check if others agreed and can reproduce the bugs.</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">Compile and run the attached file:</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">ghc -O2 optBugs.hs </div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">Loaded package environment from /Users/avie/.ghc/aarch64-darwin-9.10.1/environments/default</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">[1 of 2] Compiling Main ( optBugs.hs, optBugs.o )</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">optBugs.hs:45:49: warning: [GHC-18042] [-Wtype-defaults]</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> • Defaulting the type variable ‘b0’ to type ‘Integer’ in the following constraints</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> (Integral b0) arising from a use of ‘^’ at optBugs.hs:45:49</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> (Num b0) arising from the literal ‘6’ at optBugs.hs:45:50</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> • In the first argument of ‘counts’, namely ‘(10 ^ 6)’</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> In the second argument of ‘(.)’, namely ‘counts (10 ^ 6)’</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> In the second argument of ‘(.)’, namely ‘elems . counts (10 ^ 6)’</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> |</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">45 | solve = product . map (+ 1) . elems . counts (10^6) -- 1000000</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> | ^</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">[2 of 2] Linking optBugs</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">avie@Anabela-Air Documents % ./optBugs +RTS -s</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">./optBugs +RTS -s</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">39001250856960000</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> 2,932,564,768 bytes allocated in the heap</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> 143,405,696 bytes copied during GC</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> 76,022,072 bytes maximum residency (4 sample(s))</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> 11,034,312 bytes maximum slop</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> 164 MiB total memory in use (0 MiB lost due to fragmentation)</div></blockquote><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> Tot time (elapsed) Avg pause Max pause</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> Gen 0 263 colls, 0 par 0.032s 0.036s 0.0001s 0.0020s</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> Gen 1 4 colls, 0 par 0.048s 0.060s 0.0150s 0.0368s</div></blockquote></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> INIT time 0.003s ( 0.003s elapsed)</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> MUT time 3.343s ( 3.333s elapsed)</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> GC time 0.080s ( 0.096s elapsed)</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> EXIT time 0.002s ( 0.008s elapsed)</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> Total time 3.428s ( 3.440s elapsed)</div></blockquote><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> %GC time 0.0% (0.0% elapsed)</div></blockquote><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> Alloc rate 877,099,801 bytes per MUT second</div></blockquote><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> Productivity 97.5% of total user, 96.9% of total elapsed</div></blockquote><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">Now change the line</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> </div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> solve = product . map (+ 1) . elems . counts (10^6) -- 1000000<br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">to</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> solve = product . map (+ 1) . elems . counts 1000000</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">save the file to disk, compile and rerun:</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">ghc -O2 optBugs.hs </div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">Loaded package environment from /Users/avie/.ghc/aarch64-darwin-9.10.1/environments/default</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">[1 of 2] Compiling Main ( optBugs.hs, optBugs.o ) [Source file changed]</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">[2 of 2] Linking optBugs [Objects changed]</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">avie@Anabela-Air Documents % ./optBugs +RTS -s</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">./optBugs +RTS -s</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">39001250856960000</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> 1,920,053,000 bytes allocated in the heap</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> 16,488 bytes copied during GC</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> 8,028,224 bytes maximum residency (2 sample(s))</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> 393,152 bytes maximum slop</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> 22 MiB total memory in use (0 MiB lost due to fragmentation)</div></blockquote><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> Tot time (elapsed) Avg pause Max pause</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> Gen 0 239 colls, 0 par 0.000s 0.001s 0.0000s 0.0002s</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> Gen 1 2 colls, 0 par 0.000s 0.001s 0.0007s 0.0011s</div></blockquote></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> INIT time 0.003s ( 0.003s elapsed)</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> MUT time 2.011s ( 2.015s elapsed)</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> GC time 0.001s ( 0.002s elapsed)</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> EXIT time 0.000s ( 0.007s elapsed)</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> Total time 2.015s ( 2.027s elapsed)</div></blockquote><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> %GC time 0.0% (0.0% elapsed)</div></blockquote><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> Alloc rate 954,738,679 bytes per MUT second</div></blockquote><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> Productivity 99.8% of total user, 99.4% of total elapsed</div></blockquote><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">Allocation decreased by a billion bytes and runtime has decreased by almost a third. This is hard to believe. The obvious explanation is that I dropped a zero when I wrote out 10^6 but I checked that I have six zeros. Also I get the same answer in both cases.</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">Next uncomment the following two lines in factor:</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> -- let p = sm ! m<br> <br> -- sm = smallest maxN<br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">and comment out the line:</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> </div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> let p = smallest maxN ! m</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">save the file, compile and rerun:</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">ghc -O2 optBugs.hs </div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">Loaded package environment from /Users/avie/.ghc/aarch64-darwin-9.10.1/environments/default</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">[1 of 2] Compiling Main ( optBugs.hs, optBugs.o ) [Source file changed]</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">[2 of 2] Linking optBugs [Objects changed]</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">avie@Anabela-Air Documents % ./optBugs +RTS -s</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">./optBugs +RTS -s</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">39001250856960000</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> 16,051,152 bytes allocated in the heap</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> 10,040 bytes copied during GC</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> 44,328 bytes maximum residency (1 sample(s))</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> 29,400 bytes maximum slop</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"> 22 MiB total memory in use (0 MiB lost due to fragmentation)</div></blockquote></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br> Tot time (elapsed) Avg pause Max pause<br> Gen 0 2 colls, 0 par 0.000s 0.000s 0.0001s 0.0002s<br> Gen 1 1 colls, 0 par 0.000s 0.001s 0.0015s 0.0015s<br><br> INIT time 0.003s ( 0.003s elapsed)<br> MUT time 0.019s ( 0.018s elapsed)<br> GC time 0.000s ( 0.002s elapsed)<br> EXIT time 0.001s ( 0.002s elapsed)<br> Total time 0.023s ( 0.025s elapsed)<br><br> %GC time 0.0% (0.0% elapsed)<br><br> Alloc rate 866,786,478 bytes per MUT second<br><br> Productivity 78.9% of total user, 72.5% of total elapsed<br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large">Allocation has dropped from 2 billion bytes to 16 million and runtime has dropped from 2 seconds to 0.02 seconds. I think the optimizer should move the call to smallest outside of the "loop" in go. This seems like a bug to me.</div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div><div class="gmail_default" style="font-family:"times new roman",serif;font-size:large"><br></div></div>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div><br clear="all"><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div dir="ltr">Noon van der Silk<br><br><a href="http://silky.github.io/" target="_blank">http://silky.github.io/</a><br><br>"My programming language is kindness."</div></div></div>