TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Compiler Optimizations are Awesome

203 pointsby turingbookalmost 8 years ago

13 comments

jcranmeralmost 8 years ago
Several years ago, I happened on a blog post where someone demonstrated a very fast variant of the N-queens solution based on hand-written, SSE vectorized that was presented as very fast. I managed to write a faster, non-vectorized C solution that was recursive and that the compiler couldn&#x27;t vectorize, and much easier to understand than the vectorized original it was based off of.<p>Turns out that main reason the vaunted &quot;overkilled&quot; solution was so abysmally slow was that the author happily used BSF and BTC in the hottest part of the loop... which are actually rather slow instructions, particularly when you&#x27;re using them to control a branch (compare-and-jump is a fused µop in practice, but BTC-and-jump is not).<p>The point of this tail is that if you want to absolutely wring the last clock cycle out of a hot path, you usually need good microarchitectural knowledge about which operations are going to be faster and which are not. Sure, you can beat a compiler with hand-written, hand-optimized assembly code most of the time--but the people who have the skills to write such code are going to be the people working on the compilers.<p>The tools for optimizing compilers are getting better, and probably faster than we are capable of pumping out performance engineers to hand-craft the inner loops. In the past decade, we&#x27;ve seen polyhedral loop transformations become production-quality. Auto-vectorization is getting better, particularly when user-directed (think #pragma openmp simd); I know Intel has been pushing &quot;outer-loop vectorization&quot; very hard in the past few years. The other big fruit on the horizon is superoptimizers: I suspect we&#x27;ll see superoptimizers shipping in production compilers within a decade or two.
评论 #14459092 未加载
评论 #14458668 未加载
评论 #14460227 未加载
评论 #14460722 未加载
scraftalmost 8 years ago
Is anyone else in games development here? If we are looking to run the game at 60 FPS, we have 16.67 ms per frame to do everything required to run the game. Because of this real time requirement, a decent amount of profiling is typically done on each game. I typically see that the frame time is getting split up over a whole array of different sections of the game, i.e.:<p>- Calculating skeleton animations (updating bone positions, sometimes skinning vertices too)<p>- Clipping geometry in the scene (finding out what things are inside&#x2F;outside the camera frustum, etc.)<p>- Processing game logic, things like AI can be quite costly, so much is game dependent<p>- Walking through all the geometry that needs drawing and issueing draw calls<p>- Decompressing streaming audio and sending it to a sound driver buffer&#x2F;queue<p>- Stepping the physics world (integrating positions&#x2F;rotation working and resolving out intersections, etc.)<p>The difference between a non optimized, and an optimized build, is often 5 FPS and 60 FPS, and optimizing a single hot file or function would not get the game running anywhere near 60 FPS. I think the idea that optimizing compilers aren&#x27;t required is completely laughable, but then again I only have one perspective from the games development scene - maybe someone else will reply and say they make AAA games in C&#x2F;C++ and don&#x27;t need compiler optimizations :)
评论 #14460494 未加载
评论 #14462191 未加载
评论 #14461486 未加载
lukegoalmost 8 years ago
I often think about Proebsting&#x27;s Law: Compiler Advances Double Computing Power Every 18 <i>Years</i>. Sure, optimizing compilers are nice to have, but maybe their complexity is disproportionate to their benefit?<p>I love the way Dynamo [1] is able to reproduce many of the benefits with a fraction of the complexity by doing some of the optimizations at runtime with simpler algorithms. Can we use this approach to &quot;garbage collect&quot; some of the complexity embodied in humongous projects like LLVM?<p>[1] Dynamo: <a href="https:&#x2F;&#x2F;people.cs.umass.edu&#x2F;~emery&#x2F;classes&#x2F;cmpsci691s-fall2004&#x2F;papers&#x2F;bala00dynamo.pdf" rel="nofollow">https:&#x2F;&#x2F;people.cs.umass.edu&#x2F;~emery&#x2F;classes&#x2F;cmpsci691s-fall20...</a>
评论 #14458740 未加载
评论 #14458344 未加载
pettersalmost 8 years ago
It should be quite easy to see the value of optimizing compilers. Compile your program with optimizations turned off. Now make it as fast as your release build again, while still keeping them off. For much of my code, I think this would take years.
评论 #14458237 未加载
评论 #14458325 未加载
评论 #14458937 未加载
fizixeralmost 8 years ago
Great talk by DJB.<p>IMO he couldn&#x27;t give a convincing answer to the guy who asked about LuaJIT author being out of a job. But there&#x27;s a clear answer. JIT authors are not out of job not because optimizing compilers are not dead, but because they&#x27;re writing compilers, their distinguishing ability is writing &quot;pre-compiled&quot; code.<p>You might say, &quot;well a JIT author sped up your code&#x27;s execution so he&#x2F;she is writing an optimizing compiler&quot;. Well you have to realize that, traditionally, JIT authors don&#x27;t just translate the code into object code, they also apply these things called &quot;compiler optimizations&quot;. The point is that if they didn&#x27;t do that, and simply produced a faithful translation of the code, they would still make the code faster because of pre-compilation (and if they enabled the &quot;compiler optimizations&quot;, the code wouldn&#x27;t run significantly faster than the simply pre-compiled code).<p>Regardless of whether I agree with it or not, &quot;Optimizing compilers are dead&quot; is not the same as saying &quot;JIT authors will be out of business&quot;. (Even compiler writers won&#x27;t be out of business).
评论 #14458658 未加载
CJeffersonalmost 8 years ago
Having used some language with awful compilers, compiler optimisations let me write cleaner code.<p>In languages with bad optimisers I have to worry about separating code in a hot loop out into a function -- the cost of a function call is too high. This one in particular I find can lead to some horrible code, as functions grow larger and larger and lots of cutting+pasting happens to avoid function call costs.<p>On a smaller note, making sure I cache the values of function calls which won&#x27;t change -- when instead I could trust the compiler to know the value won&#x27;t change and the the caching itself.
评论 #14460060 未加载
lmmalmost 8 years ago
&gt; If an optimizing compiler can speed up code by, for example, 50%, then suddenly we need to optimize a lot less code by hand.<p>This doesn&#x27;t follow at all. If you had one hot loop and a bunch of cold code, and auto-optimize your code to be a measly factor of 2 faster, you&#x27;re still going to need to hand-optimize the hot loop and what it does to the cold code is irrelevant.<p>&gt; hand-optimized code has higher ongoing maintenance costs than does portable source code; we’d like to avoid it when there’s a better way to meet our performance goals.<p>True, but again, only applies if you can optimize by enough to make hand-optimization unnecessary.<p>&gt; we’d also have to throw away many of those 16 GB phones that are cheap and plentiful and fairly useful today.<p>This part is nonsense. No-one&#x27;s got anything like 16GB of <i>code</i> on their phone.<p>Optimization could be valuable but current compilers are too opaque, making optimization too much of a black art. I believe we need to do something along the lines of &quot;turning the database inside out&quot; ( <a href="https:&#x2F;&#x2F;www.confluent.io&#x2F;blog&#x2F;turning-the-database-inside-out-with-apache-samza&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.confluent.io&#x2F;blog&#x2F;turning-the-database-inside-ou...</a> ); we should turn the compiler inside out, build it as more of a library, give the developer more insight into what&#x27;s going on, have a high level language that lets you understand how it compiles. Interesting and vaguely along the same lines: <a href="https:&#x2F;&#x2F;www.microsoft.com&#x2F;en-us&#x2F;research&#x2F;publication&#x2F;coq-worlds-best-macro-assembler&#x2F;?from=http%3A%2F%2Fresearch.microsoft.com%2Fen-us%2Fum%2Fpeople%2Fnick%2Fcoqasm.pdf" rel="nofollow">https:&#x2F;&#x2F;www.microsoft.com&#x2F;en-us&#x2F;research&#x2F;publication&#x2F;coq-wor...</a> .
评论 #14458947 未加载
zurnalmost 8 years ago
TL;DR &quot;Optimizing compilers are still good to have because they are cheaper than programmer labour needed for hand optimization&quot;<p>The original DJB presentation, which this is a response to, is very good and interesting.<p>It would really be nice if the field of compiler engineering started to address the obvious neglected areas, like optimizing memory layout and data types&#x2F;representations based on partial evaluation &#x2F; profile feedback.
评论 #14458291 未加载
评论 #14458294 未加载
评论 #14459442 未加载
fovcalmost 8 years ago
In the linked slides, DJB talks about a language for communication with the compiler, separating optimizations from specification. This reminded me of VPRI&#x27;s &quot;Meaning separated from optimization&quot; [1] principle. Does anyone know what became of that line of thinking? Is this idea making it&#x27;s way into Ohm? I remember reading a post&#x2F;paper about optimizing Nile&#x2F;Gezira to better exploit the CPU cache (and the struggle to use SIMD), but can&#x27;t seem to find it now.<p>[1] <a href="http:&#x2F;&#x2F;www.vpri.org&#x2F;pdf&#x2F;rn2006002_nsfprop.pdf" rel="nofollow">http:&#x2F;&#x2F;www.vpri.org&#x2F;pdf&#x2F;rn2006002_nsfprop.pdf</a>
评论 #14462256 未加载
jerrrealmost 8 years ago
&gt; Compiler optimization reduces code size<p>Nope, much is gained by unrolling loops, inlining functions etc, which all increase code size.<p>Of course C++ compilation with no optimization at all can be rather wasteful with performance and code size, but to squeeze the final performance out you probably need to sacrifice code size (whether manual or automatic)
评论 #14458807 未加载
nullcalmost 8 years ago
I&#x27;m disappointed at the lack of figures.
mrkgnaoalmost 8 years ago
I&#x27;m posting this as a top-level comment, but it&#x27;s really a reply to the discussion downthread about compilers being able to work magic if we let them. Better still, why not help them?<p>Something I took for granted for the longest time about Haskell (which remains the only language I know of with the feature) is the ability to write user-defined &quot;rewrite rules&quot;. You can say, &quot;okay, GHC, I know for a fact that if I use these functions in such-and-such way, you can replace it by <i>this</i> instead&quot;.<p><pre><code> {-# RULES foo x (bar x) = superOptimizedFooBar x #-} </code></pre> A rule like this would be based on the programmer&#x27;s knowledge of FooBar theory, which tells her that such an equality holds. The compiler hasn&#x27;t studied lax monoidal FooBaroids and cannot be expected to infer this on its own. :)<p>Now, anywhere a user of this code writes something like<p><pre><code> foo [1,2,3] (bar [1,2,3]) </code></pre> the compiler will substitute<p><pre><code> superOptimizedFooBar [1,2,3] </code></pre> in its place. This is a nice way to bring the compiler &quot;closer&quot; to the programmer, and allow the library author to integrate domain-specific knowledge into the compiler&#x27;s optimizations.<p>You can also &quot;specialize&quot; by using faster implementations in certain cases. For example,<p><pre><code> timesFour :: Num a =&gt; a -&gt; a timesFour = a + a + a + a timesFourInt :: Int -&gt; Int timesFourInt x = rightShift x 2 {-# RULES timesFour :: Int -&gt; Int = timesFourInt #-} </code></pre> If you call timesFour on a double, it will use addition (ha!) but using it on an Int uses bitshifting instead because this rule fires.<p>High-performance Haskell libraries like vector, bytestring, text, pipes, or conduit <i>capitalize</i> on this feature, among other techniques. When compiling code written using libraries like this, this is how it goes:<p>- rule #1 fires somewhere - it rewrites the code into something that matches rule #2, &quot;clearing the way&quot; for it to fire - rule #2 fires - rule #3 fires - rule #1 fires again - rule #4 fires<p>and so on, triggering a &quot;cascade&quot; of optimizations.<p>The promise of Haskell is that we already have a &quot;sufficiently smart compiler&quot;: <i>today</i>, with good libraries, GHC is capable of turning clear, high-level, reusable functional code with chains of function compositions and folds and so on into tight, fast loops.<p>--<p>I must add, though, that getting rewrite rules to fire in cascades to get &quot;mad gainz&quot; requires one to grok how the GHC inliner&#x2F;specializer works.<p><a href="http:&#x2F;&#x2F;mpickering.github.io&#x2F;posts&#x2F;2017-03-20-inlining-and-specialisation.html" rel="nofollow">http:&#x2F;&#x2F;mpickering.github.io&#x2F;posts&#x2F;2017-03-20-inlining-and-sp...</a><p>Data.Vector also utilizes an internal representation that makes fusion explicit and hence predictable (inevitable, even) called a &quot;bundle&quot;:<p><a href="https:&#x2F;&#x2F;www.stackage.org&#x2F;haddock&#x2F;lts-8.16&#x2F;vector-0.11.0.0&#x2F;Data-Vector-Fusion-Bundle.html" rel="nofollow">https:&#x2F;&#x2F;www.stackage.org&#x2F;haddock&#x2F;lts-8.16&#x2F;vector-0.11.0.0&#x2F;Da...</a><p>but this relies on rewrite rules too, e.g. the previous module contains this rule:<p><pre><code> {-# RULES &quot;zipWithM xs xs [Vector.Stream]&quot; forall f xs. zipWithM f xs xs = mapM (\x -&gt; f x x) xs #-}</code></pre>
评论 #14461508 未加载
评论 #14459174 未加载
评论 #14462229 未加载
faragonalmost 8 years ago
Is there any compiler using &quot;machine learning&quot; for SIMD optimization?
评论 #14462434 未加载