Live data from Hacker News

Ask HN: How can I learn about performance optimization?

news.ycombinator.com

141–150 of 153 posts

Re: Ask HN: How can I learn about performance optimization?

#141

What you really want to learn about is observability, benchmarking and instrumentation. Once you are an expert in these topics for your domain, optimization will be about making obvious choices within localized constraints.

Yes and no. I would agree that observability is the place to start. But knowing what needs to be optimized doesn't necessarily mean you know how to optimize it. Also, while premature optimization is obviously a problem, knowing more about how to actually write optimized code can help you make more informed decisions earlier on in the development process.

> But knowing what needs to be optimized doesn't necessarily mean you know how to optimize it.

True, although not knowing what needs to be optimized guarantees that you don't know how to optimize it :).

> knowing more about how to actually write optimized code can help you make more informed decisions earlier on in the development process.

Totally agree though, despite poking a bit of fun.

This is something that I've been decently good at for many years. If I had to give someone new to it advice, it'd be:

- learn how to repeatably measure your system without introducing too much overhead. If you get this wrong, you're going to end up tricking yourself into believing you've made an improvement but instead got lucky/unlucky.

- once you've found the repeatably-measureable hotspots, the optimization approach is going to depend dramatically on the problem domain. Optimizing for database disk throughput is different than optimizing for http server response-latency is different than squeezing more polygons into a frame in a game.

The one very important thing to keep in mind though is that you're going to need to peel back the abstractions and understand what's happening under the hood. This applies to both parts. Maybe the way to measure what's happening in your network service is to use tcpdump to capture the raw packets and see that you're sending a bunch of small writes into a socket instead of a single big write (why is that a problem? :D). Or something like NVidia NSight can provide a ton of insight into what's happening on your CPUs and GPU frame-to-frame.

Re: Ask HN: How can I learn about performance optimization?

#142
post #113

For several years I have worked primarily with performance optimizations in the context of video games (and previously in the context of surgical simulation). This differs subtly from optimization in certain other areas, so I figured I'd add my own perspective to this already excellent comment section. 1. First and foremost: measure early, measure often. It's been said so often and it still needs repeating. In fact,…

> The easiest way to make things go faster is to do less work. Use a more efficient algorithm, refactor code to eliminate unnecessary operations, move repeated work outside of loops. There are many flavours, but very often the biggest performance boosts are gained by simply solving the same problem through fewer instructions. I definitely agree with this one, especially on the level of "don't make network calls in yo…

Great thing for appealing to what performs better, and then code in that manner.

Simplicity is often the best because necessary complexity will arrive on its own.

Re: Ask HN: How can I learn about performance optimization?

#143
post #77
post #76

Former HFT dev here. Know fundamentals: sources of performance issues = things that eat/waste CPU cycles, things that reach too far down the memory hierarchy. Usually the latter. E.g. L2 cache to RAM - order of magnitude slower; RAM to disk: 4+ orders of magnitude slower. Things that eat CPU: iterations, string operations. Things that waste CPU: lock contentions in multi-threaded environments, wait states. You can us…

Addendum: never forget Amdahl's Law. And never forget Knuth's full quote: “Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is th…

This quote has done more damage to software usability than any other idea of the last 60 years. It has lead us to a state where software is unbearably slow because nothing was worth optimizing. Just like it's easy to go broke with thousands of minor expenses, it's easy to create sluggish software that has no obvious bottleneck because the whole damn thing is slow.

Re: Ask HN: How can I learn about performance optimization?

#144

For several years I have worked primarily with performance optimizations in the context of video games (and previously in the context of surgical simulation). This differs subtly from optimization in certain other areas, so I figured I'd add my own perspective to this already excellent comment section. 1. First and foremost: measure early, measure often. It's been said so often and it still needs repeating. In fact,…

Agree with all this, and would add that concrete measurements not only tell you whether your work has actually lead to an improvement, but also your manager and promo committee ;)

Re: Ask HN: How can I learn about performance optimization?

#145
I admire Daniel Lemire’s work on SIMD implementations. [Lemire]

[Lemire] https://lemire.me/en/#publications

I learn a lot by reading my compiler’s and profiler’s documentation.

For Rust, the Rust Performance Book by Nicholas Nethercote et al. [Nethercote] seems like a nice place to start after reading the Cargo and rustc books.

[Nethercote] https://nnethercote.github.io/perf-book/

Algorithms for Modern Hardware by Sergey Slotin [Slotin] is a dense and approachable overview.

[Slotin] https://en.algorithmica.org/hpc/

Quantitative understanding of the underlying implementations and computer architecture has been invaluable for me. Computer architecture: a quantitative approach by John L. Hennessy and David A. Patterson [H&P] and Computer organization and design: the hardware/software interface by Patterson and Hennessy [P&H ARM, P&H RISC] are two introductory books I like the best. There are three editions of the second book: the ARM, MIPS and RISC-V editions.

[H&P] https://www.google.com/books/edition/_/cM8mDwAAQBAJ

[P&H ARM] https://www.google.com/books/edition/_/jxHajgEACAAJ

[P&H RISC] https://www.google.com/books/edition/_/e8DvDwAAQBAJ

Compiler Explorer by Matt Godbolt [Godbolt] can help better understand what code a compiler generates under different circumstances.

[Godbolt] https://godbolt.org

The official CPU architecture manuals from CPU vendors are surprisingly readable and information-rich. I only read the fragments that I need or that I am interested in and move on. Here is the Intel’s one [Intel]. I use the Combined Volume Set, which is a huge PDF comprising all the ten volumes. It is easier to search in when it’s all in one file. I can open several copies on different pages to make navigation easier.

Intel also has a whole optimization reference manual [Intel] (scroll down, it’s all on the same page). The manual helps understand what exactly the CPU is doing.

[Intel] https://www.intel.com/content/www/us/en/developer/articles/t...

Personally, I believe in automated benchmarks that measure end-to-end what is actually important and notify you when a change impacts performance for the worse.

Re: Ask HN: How can I learn about performance optimization?

#147
post #70

The book "Writing Efficient Programs", by Jon Bentley, is still a valuable resource. See the short subthread starting with a comment I posted here some years ago: https://news.ycombinator.com/item?id=13407192 A few people had replied, agreeing with my opinion, and giving some more details. One of them called the book "gold". I have also posted about the book a few other times on HN, over the years. Those comments can…

>hn.algia.com

Sorry, only noticed the typo now:

Should be hn.algolia.com

Re: Ask HN: How can I learn about performance optimization?

#148

Earlier quoted context omitted.

Yes and no. I would agree that observability is the place to start. But knowing what needs to be optimized doesn't necessarily mean you know how to optimize it. Also, while premature optimization is obviously a problem, knowing more about how to actually write optimized code can help you make more informed decisions earlier on in the development process.

> But knowing what needs to be optimized doesn't necessarily mean you know how to optimize it. True, although not knowing what needs to be optimized guarantees that you don't know how to optimize it :). > knowing more about how to actually write optimized code can help you make more informed decisions earlier on in the development process. Totally agree though, despite poking a bit of fun. This is something that I've…

> The one very important thing to keep in mind though is that you're going to need to peel back the abstractions and understand what's happening under the hood.

I agree with this although I think it can sometimes be a red herring for those new to optimization. It's possible to spend a lot of time digging deeper when that's not really what's needed. For example, you might find a particular database query that's really slow and start looking at what configuration tweaks you can make to your database server. But maybe if you structure the application a bit differently, that query isn't necessary at all.

It's a great skill to be able to peel back layers of abstraction and continuing to optimize all the way down. But it's an equally important skill to be able to know what later really needs optimizing.

Re: Ask HN: How can I learn about performance optimization?

#150
post #116

Earlier quoted context omitted.

Do you guys still buy the beefiest Intel Xeons in order to fit your main application + OS entirely within the L3 cache? There was a CppCon talk from a HFT dev about this 10 years ago.

Do you happen to know what's the CppCon talk called ?

It was this one: https://www.youtube.com/watch?v=NH1Tta7purM

Not 10 years, merely 6. I was mistaken and thought it was posted in 2015.

Post reply on HN