Live data from Hacker News

Ask HN: What is your go to performance optimization?

news.ycombinator.com

21–30 of 47 posts

Re: Ask HN: What is your go to performance optimization?

#21
Push less data through wires.

The memory hierarchy is so stark on modern hardware that the 30-year-old adage that "the fastest code is the code you don't run" is maybe less important than, "the fastest code is the code that doesn't spend much time talking to the memory controller."

And it's even worse once we start talking about accessing memory that's on an entirely different computer. Serialization/deserialization, IPC, network calls, and all those other things we do with reckless abandon in modern service-oriented and distributed applications are just unbelievably expensive.

Last year I took a slow heavily parallelized batch job and improved its throughput by 60% by getting rid of both scale-out and multithreading and just taking it all down to a single thread. Everyone expected it to be slower because we were using a small fraction as many CPU cores, but in truth it was faster because the time savings from having fewer memory fences, less data copying, and less network I/O was just that great. And then the performance gains kept coming because, having simplified things to that extent, I was then in a much better position to judiciously re-introduce parallelism in ways ways that weren't so wasteful.

Re: Ask HN: What is your go to performance optimization?

#22

Push less data through wires. The memory hierarchy is so stark on modern hardware that the 30-year-old adage that "the fastest code is the code you don't run" is maybe less important than, "the fastest code is the code that doesn't spend much time talking to the memory controller." And it's even worse once we start talking about accessing memory that's on an entirely different computer. Serialization/deserialization,…

> Push less data through wires.

In the systems I work on this has been a big one. In SQL people are pretty good about not writing `select *` in production code, but when querying directory servers, redis, mongodb, etc. people get sloppy. When a system is small, it's enticing to pull in lots of data and work with it in code instead of writing real queries. This doesn't scale.

Re: Ask HN: What is your go to performance optimization?

#23
I realize you're asking about performance optimizations, but since you put it in the context of a workload's cloud bill being too large, I'll chip in and say that by far the largest impact on cost I've seen over the years is to just rightsize the infrastructure the workloads are running on. What I see more than anything is applications that are reserving 10x more CPU or memory than they're actually using. In some cases this might mean amortizing resource usage over time, by asynchronously consuming some kind of queue, in cases where the extreme reservation of resources is due to some temporary usage spike (downstream client doing some batch processing, for example).

Re: Ask HN: What is your go to performance optimization?

#24

Push less data through wires. The memory hierarchy is so stark on modern hardware that the 30-year-old adage that "the fastest code is the code you don't run" is maybe less important than, "the fastest code is the code that doesn't spend much time talking to the memory controller." And it's even worse once we start talking about accessing memory that's on an entirely different computer. Serialization/deserialization,…

> Push less data through wires. In the systems I work on this has been a big one. In SQL people are pretty good about not writing `select *` in production code, but when querying directory servers, redis, mongodb, etc. people get sloppy. When a system is small, it's enticing to pull in lots of data and work with it in code instead of writing real queries. This doesn't scale.

Unless you use an ORM, in which case I'm used to seeing it be all SELECT * all the time. And then you get an entire generation of engineers who've never known any other way to talk to a database going around complaining about how this Miata is so slow when really it's just that nobody ever taught them how to shift out of first gear.

Re: Ask HN: What is your go to performance optimization?

#26

Push less data through wires. The memory hierarchy is so stark on modern hardware that the 30-year-old adage that "the fastest code is the code you don't run" is maybe less important than, "the fastest code is the code that doesn't spend much time talking to the memory controller." And it's even worse once we start talking about accessing memory that's on an entirely different computer. Serialization/deserialization,…

I've seen that happen a lot as JSON(XHR) and ORMs started to become more common... certain queries would return way too much data from related (auto-fetched) records and it was just slow AF on remote computers.

Another common one is just poor query performance from a database. Lack of appropriate indexes, or other relatively easy optimizations.

Similarly, finding a method of caching that's just bad (in-memory database, with sql queries instead of a dictionary). Isn't so bad for one call, very bad when a a given request (login) makes over 200 calls to this cache for configuration settings. It wasn't a problem per request but in aggregate.

Re: Ask HN: What is your go to performance optimization?

#28
post #5

I've spent the last couple of years identifying and resolving N+1 problems in a Django codebase. https://planetscale.com/blog/what-is-n-1-query-problem-and-h... Aside from the performance gains, it's very satisfying to go from 1,000+ inefficient DB queries to 1-2 optimized queries.

That's a well written article. For developers newer to relational databases, there's a heuristic at the beginning which I remember hearing elsewhere and keeping it in mind when I'm doing query work. "You might expect that many small queries would be fast and one large, complex query will be slow. This is rarely the case. In practice, the opposite is true."

It's a great heuristic.

A big part of why it works out that way is that the query planner can only optimize for the query you give it. If you give it a bunch of small queries, it can only make relatively inconsequential micro-optimizations. The one big query gives it a lot more degrees of freedom and opportunities to make big gains.

Here's another great resource for getting more out of relational databases: https://use-the-index-luke.com/

Re: Ask HN: What is your go to performance optimization?

#29
Making sane DB indices and constraints. It's amazing how often people just don't add indices even when the access pattern is clear from the outset. "Premature optimization is the root of all evil!" ok, so when are we actually going to add that index? (Answer: never)

Re: Ask HN: What is your go to performance optimization?

#30
Assuming you gate things like merges on test results . . .

Remove end-to-end tests. Replace with contract tests and service-level functional tests.

Much faster feedback! At the same time, better coverage. The only serious problem with the approach is that it upsets the magical thinkers in your org. Often those folks are managers.

Post reply on HN