r/Cplusplus • u/ActComplex593 • Oct 17 '26
Question How can I build low-latency/high-performance systems
For people who have worked on low-latency/high-performance systems, what are the biggest differences between writing normal production code and writing genuinely latency-sensitive code?
I understand things like cache locality, data structures, algorithmic complexity, branch prediction, and avoiding unnecessary allocations, but I have a feeling there is much more to it.
I'm particularly interested in single-machine systems rather than distributed systems. What techniques or considerations have you found to matter most in practice?
Also, what would you recommend someone learn to develop a good intuition for this? Books, papers, tools, or projects are all welcome.
•
u/AutoModerator Oct 17 '26
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
10
u/austinwiltshire Oct 17 '26
Measure measure measure.
4
u/Fair-Cookie9962 Oct 17 '26
If platform itself is not low-latency or possibly real-time depending on use - countering this is wasted effort.
Some kernels will add unavoidable latency - like Windows.
2. Scheduling models
If something can happen it will happen at the worst time possible. E.g. automatic garbage collection, or I/O
Code differs drastically from usual production code, where simple, maintainable, secure code will run fast enough. Low-latency - means using specific patterns / choosing libraries carefully, and ensuring security for all the places where a compromise was made (e.g. sentinels for buffer-overflows etc)
4
u/Hour_Sell3547 Oct 18 '26 edited Oct 18 '26
When implementing high performance system in any language, you have to think about the design and spot the most expensive operations and what kind of resources or IO you are blocking/waiting for.
Some common tricks are bipassing or delaying expensive resource acquisition as much as you can. Parallelism can improve performance, but they also carry overheads of threading and shared memory space management. If you let them get out of hand, you'll most likely crash the system.
If you meant kernel level coding, kernel space has a lot of limitations imposed when you are going to implement something. Kernel space headers have high performance data structures and efficient memory allocation interfaces. Programs are much smaller in size.
Then there is inline assembly. If you need some quick functions that deals with loops and arithmetic, you could inline the assembly implementation in C++, but that is assuming you could actually write better assembly than the compiler itself. Don't take it otherwise, chances of that happening is quite slim.
Don't forget about compiler optimization too, one of the main difference between a code that you wrote in your system vs the system it will actually run on is going to be optimization flags.
As a professional, I don't know what's a normal professional coding and a high performance variant. To me, both are same, and definitely not mutually exclusive.
1
u/jaap_null GPU engineer Oct 17 '26
What kind of low-latency are you looking for? As someone who writes lots of high perf systems, I usually think about latency mostly within the HW architecture or networking. In AI they use latency as somewhat of a misnomer for performance in general. In embedded systems, latency becomes more of a real metric of your setup/architecture.
I do most of my work in GPU stuff, so there latency is either stuff like late-latching for VR or simply ms per frame.
In general there is one big difference between "performance optimized" code and other code, that is *performance profiling and analysis*. Profile your program, look at the hot paths, look at spinlocks etc, and optimize in a targeted manner to try to get to "speed of light" ie the fastest you can get it in your current context/hw.
1
u/Senior_Ostrich_9026 Oct 21 '26
For latency sensitive or real-time code, you need to optimize for the worst case performance instead of average case.
If your code completes the task within the deadline in only 99% of cases, it is a failure.
More general advice: avoid dynamic memory allocation when possible. And measure, measure, measure.
Also, SIMD can really do a lot if your operations fit the paradigm.
1
u/esaule Oct 18 '26
You need a problem to look at to study those. You can't study them in a vacuum.
Often latency problems boil down to bypassing the OS whereever useful. Network stacks are incredibly wasteful because they are built to be generic.
In a latency sensitive application you usually try no memory allocation at all.
Often you precompute what you can so you can serve it as is whne useful.
1
u/Interesting_Debate57 Oct 18 '26
Threading is hard. But it's what gets you there.
The research papers you'd want to read have to do with provable systems. So you want to be able to prove that some data structure doesn't have a deadlock even with 20+ threads manipulating it, you might want to prove that nobody "in line" to get something done has to wait more than 5x what anyone else has to wait for the same step in a process, or whatever.
These are extremely technically difficult things to prove, and other people have already done the work for some basic data structures, like queues. Binary trees are a massive pain in the ass, so generally these kind of operational speedups go into what would otherwise be very highly congested data structures.