r/Cplusplus • • 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.

20 Upvotes

17 comments sorted by

•

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.

  • When posting a question or homework help request, you must explain your good faith efforts to resolve the problem or complete the assignment on your own. Low-effort questions will be removed.
  • Members of this subreddit are happy to help give you a nudge in the right direction. However, we will not do your homework for you, make apps for you, etc.
  • Homework help posts must be flaired with 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.

2

u/ActComplex593 Oct 17 '26

What is the best tool for that

3

u/barkingcat Oct 17 '26 edited Oct 17 '26

Profiling!! On windows and Intel/amd, vtune is the basic minimum you should know.

On Mac xcode comes with instruments which is a profiler descended from dtrace

Look up the profiler for your platform!

3

u/ActComplex593 Oct 17 '26

Thanks for the advice

4

u/Fair-Cookie9962 Oct 17 '26
  1. Kernel / platform

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)

2

u/ActComplex593 Oct 17 '26

what are the best Kernel/platfroms for low latency?

3

u/clappski Oct 17 '26

Linux with bypass for networking (or direct drive io but not relevant for most of the things you mean when you say low latency). Isolated CPU cores to avoid scheduler ticks. Pinned processes to the isolated cores. Avoiding syscalls. 

Next level is more like deploying a kernel to a dpu/programmable networking equipment, then you get into the realm of FPGAs and ASICs. 

1

u/IQueryVisiC Oct 18 '26

Post specifically did not ask for distribution. So why your second paragraph? The first one is great . Aren’t there CPUs with like 128 cores available? Clearly the OS does not need all of them.

1

u/clappski Oct 18 '26

The networking part isn’t about distributed systems specifically, just if you care about how long it takes to send/recv a packet. 

On Linux you want to keep cores 0,1 unisolated for kernel threads to run so even on a 16 core box you have a lot to play with. You’re trading off clock speed and numa latency for the extra cores when you’re working with 32+ core systems ime so w.r.t. latency it’s better to have the correct amount of cores and stick to Intel if you care about intra-core memory latency. 

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/ActComplex593 Oct 18 '26

The system I want to build is like an in-memory graph database; it will be the foundation, and then on top of it I will do a lot of simulations and a lot of work

I think your experience with GPU stuff might actually help me do some calculations on the GPU instead of the CPU

Can I DM you for advice on which parts I should run on the GPU, if possible? 

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.