how to achieve low latency in C++

B

bubai

Hi,
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. 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.

Thanks in Advance
Ayan
 
T

thomas

Hi,
    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. 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.

Thanks in Advance
Ayan

IMO, It's more of a design problem rather than a language issue.
If you mean how to write specific code, I don't think it will greatly
reduce latency.
But it always worth trying though it's not only related to the
language. Algorithm, OS, caching all can affect performance.
I think the system design is more important, but it's not directly
related to language.
 
J

jacob navia

bubai a écrit :
Hi,
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. 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.

Thanks in Advance
Ayan

Very easy:

If you start a long operation, check as often as you can, if there is something new
in the pipeline.

And, if your system is multi-threaded, have a thread check periodically if there is
something new to answer to, during the time other threads are calculating/processing
previous inputs.

It depends on your specific application/setup/hardware/ etc.
 
V

Vladimir Jovic

jacob said:
If you start a long operation, check as often as you can, if there is
something new
in the pipeline.

And, if your system is multi-threaded, have a thread check periodically
if there is
something new to answer to, during the time other threads are
calculating/processing
previous inputs.

Blocked reads FTW!
 
A

Angus

Blocked reads FTW!

As mentioned by other responders it will very much depend on what your
software is doing. Consider an event driven, asynchronous approach.
ie you send an asynchronous request, get on with whatever you can do
and then at some later point you will receive a message that your
request has completed.

Not sure if this answers your question. Would need more info on your
requirements/architecture/etc.
 
M

Maxim Yegorushkin

bubai said:
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
 
J

Jorgen Grahn

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.
[...]

Two very late observations:

- "Low latency" is a useless requirement. Maybe it means "less than
ten seconds" in the trading world? Who knows. Don't optimize where
it's not needed. Adding threading, for example, adds a whole new
dimension of complexity to the code.

- We don't know what I/O mechanisms and which protocols are used. I
would concentrate on them first. For example, there are many ways
you can mess up TCP performance with a badly designed application
protocol or badly tuned software.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top