Live data from Hacker News

Ask HN: How can I learn about performance optimization?

news.ycombinator.com

81–90 of 153 posts

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

#81
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…

Nowadays, the other 97 is slow too.

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

#82
The most interesting thing I've learned in this regard (from Casey Muratori) is non-pessimization. Non pessimization means don't make the computer do unnecessary work. Just write the simplest code that does the thing. Unfortunately almost no software is written like that.

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

#83

Earlier quoted context omitted.

Note "faster" is not the only thing to optimize for, and it (wall clock time) is actually a bit unusual as it doesn't represent an exhaustible resource. That is, if you have a rarely used slow part of the system, that might make it seem unimportant, but not if running it uses all the disk space or drains your phone battery. Even if that doesn't happen, there are things you can optimize in the unimportant parts - you…

> Note "faster" is not the only thing to optimize for That is very true! I simply think of it first because it is often the biggest problem at my work. One of the extreme exceptions was optimising Wavetale for the Nintendo Switch, where we had to decrease memory usage from over 20GiB to below 3GiB. > is actually a bit unusual as it doesn't represent an exhaustible resource. Not in the context of game development, how…

In realtime audio programming, for example, the time budget can be as low as 1.3 ms (64 samples @ 48kHz). And every single missed deadline will manifest as an ugly pop which you will try to avoid at all cost.

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

#84
post #17

If you want to learn how to understand the performance of the whole system I can recommend Brendan Gregg's Systems Performance: Enterprise and the Cloud ( https://www.brendangregg.com/blog/2020-07-15/systems-perform... ). It is a good book that teaches a lot of basics and techniques and gives a good understanding of the impact different system components can have on performance.

I am so fascinated by how differently people interpreted this thread, really shows the diversity of computing and performance work. Here's a book about performance in the context of "enterprise and the cloud". I've worked with performance optimizations for years, but never touched a network connection. Because for me it's all in the context of optimizing single player video games, which primarily leads to a focus on…

"I am so fascinated by how differently people interpreted this thread, really shows the diversity of computing and performance work."

Well yeah, my first reaction to the question was: Optimize for what?

The question probably would have benefited from a bit more details about his job. Plattform, domain, etc.

I am also in the same boat as you, where I have 16 ms to do everything. So some of the general things we optimize for, also apply elsewhere, but many others not so much.

My main generic advice would be: things that happen only sometimes, can usually be slow, but things you need to do often ("hot spots") they need attention.

But of course this does not apply to a programm that checks for example whether the airbag of the car needs to fire, because some very rare condition was met. This code only runs very rarely - but if it does, there should be no garbage collector kicking in at that moment, no slow DB lookup, no waiting for a UI process to finish or alike (which should not have a connection anyway to the critical parts).

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

#86
The difficult thing is to benchmark the software correctly and evaluate the impact of a change. Most of the examples on the Internet are useless micro-optimizations. I evaluated a program some time ago that did several speed tricks. But the reason it was slow was because it reread a file on each iteration in a loop.

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

#87

Measure everything and be extremely critical. Be ready to challenge common and popular held assumptions. Here is something I wrote about extreme performance in JavaScript that is discarded by most programmers because most people that program JavaScript professionally cannot really program. https://github.com/prettydiff/wisdom/blob/master/performance...

You dismiss a lot of modern technologies as "unnecessarily complicated", you advocate for reinventing the wheel coming across with a very "I know better" attitude. For example you create your own bundler, when modern bundlers are very mature and good. For example you dismiss SSR as unnecessary and then basically roll your own. You dismiss modern frameworks out of hand then list performance improvements they can make…

Yes, I do advocate for reinventing the wheel and I do so with a high level of arrogance. That is one of the benefits of measuring everything extensively… you get to be arrogant because you know what is superior according to a bunch of objective evidence.

I rolled my own bundler only because it’s tiny and without dependencies. I am not rewriting ESLint even though I absolutely dread its large number of dependencies.

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

#88
As you can tell from the diversity of responses here it really depends on what you're doing. In my work I use C++, and "optimization" typically involves making a heavy computation run faster (measured in wall clock time) or making a particular subsystem use less memory.

The number one most important thing you can do is dive in and start profiling real-world code. Find a part of your software that is too slow or uses too many resources, and use whatever the standard profiler is for your development environment to figure out why. Performance optimization is a very empirical discipline. Yes there are general principles, but if you don't measure your baseline or your changes you won't know how good your optimization was. In my experience, the first attempt at a fix is often flat-out wrong! Doing this first will also help motivate your reading.

Once you know how to measure the performance of your software, I recommend learning the basics of modern computer architecture. At a minimum, learn about CPU caches, how they work, and how to design your code to use them effectively. I find Algorithms for Modern Hardware to be a good resource for this [1], but there are many others. Relatedly, you should have a rough idea of how long it takes for your computer to do various basic things (fetch something from memory, fetch something from cache, etc.). There's a table at [2] that gives a good idea. Don't worry too much about the absolute values--the order of magnitude is what's important.

You should also study fundamental data structures, but understand that for low-level programming 95% of the time the correct answer will be to shove everything into a simple flat array (e.g. std::vector in C++), maybe with some sort of index on top. Fancy data structures are more important in higher-level languages that are structurally unable to make effective use of modern hardware.

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

[2] https://gist.github.com/jboner/2841832

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

#89
post #64

Earlier quoted context omitted.

> Not in the context of game development, however! There you typically don't care about wall time. Instead, you work towards a set frame rate meaning you have a set slice of time (usually 16.7 or 33.3 ms) to go through the entire game and render loop each time. I was about to bring a similar example: Some of our really old daily data processing at work might need some attention and optimization in the future, because…

If you can run the daily batch processing in parallel, can't you have one batch start at T+0, the next one starts at T+24h, then the first one finishes at T+28h and so on?

That leads to an infinite backlog no ? If you need more than 24h to process 24h of data ?

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

#90

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,…

Note "faster" is not the only thing to optimize for, and it (wall clock time) is actually a bit unusual as it doesn't represent an exhaustible resource. That is, if you have a rarely used slow part of the system, that might make it seem unimportant, but not if running it uses all the disk space or drains your phone battery. Even if that doesn't happen, there are things you can optimize in the unimportant parts - you…

This is so true and so often ignored: there can be optimization for space (RAM and or ROM), energy, security, robustness, etc. often they oppose or compete somehow with speed.
Post reply on HN