r/cpp • u/akinocal • Apr 07 '26
How to achieve P90 sub-microsecond latency in a C++ FIX engine
https://akinocal1.substack.com/p/how-to-achieve-p90-sub-microsecond
68
Upvotes
r/cpp • u/akinocal • Apr 07 '26
46
u/apropostt Apr 07 '26 edited Apr 07 '26
This is one of the better HFT articles I've seen.
Good testing methodology. This is one of the better articles I've seen on HFT... even if it is a lowkey advert.
9
u/matthieum Apr 07 '26
I feel like we should talk about alternatives, here:
I don't follow, at all.
A vector of index to field values works very, very, well.
Even better, since most applications are only interested in a specific subset of fields anyway, an array of index to field values works very, very, well.
Why even materialize the sequence of fields?
Why not avoid message instances at all, whenever possible?
I suppose this is write-only? For reads, using memory mapped files may result in unpredictable latency -- if the content has to be loaded from disk to memory. Is there such a risk for writes?
Also, how are write errors surfaced? SIGBUS? SIGSEV? Silence? (Imagine, eg., that the disk you're writing to is disconnected/fails, then what?) I must admit always being wary of I/O within the application; hope the exchange you're connected to will cancel all open orders quickly if the application goes down...
And finally, in which scenarios do writes never make it to the disk? Apart from a disconnected disk, I imagine a hard stop/reboot of the OS.
8
u/Big_Target_1405 Apr 07 '26 edited Apr 07 '26
For inbound messages you're better off just having bespoke parsing that extracts only the fields you actually care about per message type. This can be done with templates or code generation.
A generic KVP based parser like this will always be slower.
For outbound most fast systems will stamp out a message type/configuration once, padding fields to fixed lengths, and then cache their offsets. True variable length fields can be tacked on to the end, since FIX tag order doesn't really matter.
Sending a message then just means overwriting ASCII fields and resending the buffer.
For true HFT it's becoming increasingly common not to even do this in software. NICs are FPGA enabled and key message types are translated in hardware. All those pesky binary to string conversions can be done in parallel.
The 'immediate mode' stuff is suspect. Instead of burning 600ns on your strategy thread you can get more mileage by handing orders off to another thread (or hardware) via a queue (~80-100ns) and then spend the difference grabbing the next market data packet. Typically only critical checks would be done synchronously. That said, you can build this design on top.
Like sibling post in suspect of using memory mapped logging on the main thread.
That all said this is a great writeup and the llfix code looks excellent.
3
u/pavel_v Apr 08 '26
I looked at the source code of your engine out of curiosity. Mostly the lower level utilities, just to see how non-domain specific things are done. I'm wondering how the UserspaceSpinlock works in practice. It uses
for the implementation.
The
lock/try_lockdoes directly__sync_val_compare_and_swap(for GCC) which is a bit sub-optimal, AFAIK, due to the full seq_cst barrier and the Read-Modify-Write instead of test-test-and-set, for example.However, I don't understand how the lock
synchronizes withthe unlock which just does plain store:?
3
u/drbazza Apr 08 '26
Your
FixStringViewclass has ac_str()andlength()but not asize()which is kind of standard instdalgorithms. Many code bases have concepts or templated adapters or just plain-old functions that expectc_str()andsize(). Maybe change that to be more 'idiomatic'?2
u/PeeK1e Apr 07 '26
I have nothing to add to the topic. But please just use an image made by humans e.g. unsplash, free and open licenses for images created by humans. Don't use slop please, we already have enough of that.