5 ms·
In my case, the choice is made for me (I write Apple native, so Swift, it is...). I have found that his advice on using profilers was very important. I thought
by ChrisMarshallNY 4y ago
In my case, the choice is made for me (I write Apple native, so Swift, it is...).
I have found that his advice on using profilers was very important. I thought I knew where it would be slow, but the profiler usually stuck its tongue out at me and laughed.
When I ran a C++ shop, optimization was a black art. Things like keeping code and pipelines in low-level caches could have 100X impact on performance, so we would do things like optimize to avoid breaking cache. This often resulted in things like copying and pasting lines of code to run sequentially, instead of in a loop, or as a subroutine/method.
It's difficult. There's usually some "low-hanging fruit" that will give awesome speedups, then, it gets difficult.
- mrfox321 4y agoDo your tricks still apply to modern c++ compilers?
- moonchild 4y agoI cannot speak for c++, but I recently rewrote an optimised c routine in assembly. A 2-4x speedup obtained. I might have gotten partway with careful tuning of the source, the way the parent suggests, but I could have not gotten all the way.
- favorited 4y ago[Not GP] Modern optimizers are great, but writing cache-efficient code is still up to the programmer.
- jandrewrogers 4y agoModern C++ compilers are mixed bag when it comes to optimization, brilliant in some areas and inexplicably obtuse in others requiring the programmer to be quite explicit. This has improved significantly with time but I am still sometimes surprised by the code the compiler has difficulty optimizing, or which are only recognized if written in very narrow ways. As a practical matter, compilers have a limited ability to reason over large areas of code in the way a programmer can but even very local optimizations are sometimes missed. This is why it is frequently helpful to look at the code generated by the C++ compiler. It gives you insight into the kinds of optimizations the compiler can see and which ones it can't, so you can focus on the ones it struggles with. This knowledge becomes out-of-date on the scale of years, so I periodically re-check what I think I know about what the compiler can optimize. For some things, like vectorization, the compiler's optimizer almost never produces a good result for non-trivial code and you'll have to do it yourself.
- orangepurple 4y agoI recommend using Godbolt (https://godbolt.org/ https://godbolt.org/) to view the assembly output of your compiled language (C++, etc)
- zen_1 4y agogdb's disas command (or asm view in general) is also very helpful, especially if you want to try control-c profiling [1] and then looking at the asm instructions in the neighborhood of where you halted your program. [1]: https://stackoverflow.com/questions/375913/how-can-i-profile-c-code-running-on-linux https://stackoverflow.com/questions/375913/how-can-i-profile...
- ChrisMarshallNY 4y agoGodbolt is awesome. We used to use it all the time. The story I heard, is that the author threw it together to reinforce his side of an argument about code efficiency.
- account42 4y ago> Godbolt is awesome. We used to use it all the time. I wonder how Matt Godbolt feels about being used or about being called "it". Joking of course - it was an odd choice to use $surname.org for one specific tool he built.
- gpderetta 4y agoThe tool is called Compiler Explorer; I think Matt just hosted it in his own otherwise unused existing personal domain, but the name famously stuck as it is short and memorable (at $WORK, our private compiler explorer installation is under go/godbolt!).
- jandrewrogers 4y agoI should add that some types of optimizations (cache efficiency being a big one) are outside the scope of the compiler because it is implicitly part of the code specification that the compiler needs to faithfully reproduce e.g. data structure layout is required to be a certain way for interoperability reasons.
- ChrisMarshallNY 4y agoProbably, but I haven't done that stuff in over 5 years. We tended to use clang/llvm. I know that we often had to actively work against the native optimizations. We had a team from Intel, that used to run these awesome tools on our code, and helped us to see a lot of these weird dichotomies.
- pclmulqdq 4y agoI know people don't like macros and pragmas, but unfortunately I have found that in cycle-sensitive code, they are really the way to go. I have found that with judicious use of those tools, you can kind of write the code in a way that feels natural (and looks normal to a normal programmer), and still get what you need. It is still slightly scary, but a lot less scary than copy-pasting the same code over and over again.
- pjscott 4y agoStatic inline functions can often do the same thing with more type safety. (Not always, of course! But it’s worth thinking about when optimizing.)