Re: how to achieve low latency in C++
bubai wrote:
Currently I am involved in developing a trading system. The server
side is written in C++ the agenda here is that the system has to be a
low latent system.
There are a few fundamental principles you should follow. Your server will
exhibit low latency to the extent you adhere to these principles:
1) Avoid data copying. Prefer zero-copy algorithms. For example, when you
send/receive data through a socket use a ring-buffer with wrapping iterators.
2) Avoid dynamic memory allocation on fast code paths, such as receiving market
data and sending orders. Preallocate as much as possible. Use mapped memory.
3) Avoid lock contention. Use wait-free and lock-free algorithms if possible,
share as little data as possible between threads.
4) Avoid context switching. Don't have more threads ready to run than hardware
CPUs. Use fifo realtime process priority, so that your threads do not get
context switched off when it still has data to process.
One of the top latency killers is formatting strings using things like
std::stringstream or snprintf into std::string. This is because they do a fancy
data copy (1), dynamic memory allocation (2), locking a mutex (3) when doing the
memory allocation (unless some fancy multi-thread allocator is used that avoid
locking a mutex).
> Is there any book or reference or white papers
which tell us what are the techniques to write low latency code in C+
+.... Any help will be highly appreciated.
Start with http://www.kegel.com/c10k.html
--
Max