/x9

high performance message passing library

Primary LanguageCBSD 2-Clause "Simplified" LicenseBSD-2-Clause

X9

X9 is a low level high performance message passing library, based on a lock-free ring buffer implemented with atomic variables, for low latency multithreading work.
It allows for multiple producers/consumers to concurrently access the same underlying ring buffer and provides both spinning (busy loop) and non-blocking read and write functions.

The library is based on three concepts:

  • A message, which is a user defined struct.
  • A x9_inbox, which is where messages are both written to and read from.
  • A x9_node, which is an abstraction that unifies x9_inbox(es).

The library provides multiple functions to both read from and write to a x9_inbox, as the right choice depends on the user needs.
Refer to x9.h, where all public functions are properly documented and their use cases explained, and the examples folder for comprehensive examples of different architectures.

Enabling X9_DEBUG at compile time will print to stdout the reason why the functions x9_inbox_is_valid and x9_node_is_valid returned 'false' (if they indeed returned 'false'), or why x9_select_inbox_from_node did not return a valid x9_inbox.

To use the library just link with x9.c and include x9.h where necessary.

X9 is as generic, performant and intuitive as C allows, without forcing the user to any sort of build system preprocessor hell, pseudo-C macro based library, or worse.
It was originally written in the context of an high-frequency-trading system that this author developed, and was made public in June of 2023.
It is released under the BSD-2-Clause license, with the purpose of serving others and other programs.

Benchmarks

  • Single producer and single consumer transmitting 100M messages via a single x9_inbox.
  • Run on Intel 11900k (cpu and ram untuned).
  • Msg size expressed in bytes, and Inbox size in number of slots in the ring buffer.
  • (See /profiling for how to run your own tests)
Inbox size | Msg size | Time (secs) | Msgs/second
-------------------------------------------------
      1024 |       16 |        6.07 |      16.46M
      1024 |       32 |        5.99 |      16.70M
      1024 |       64 |        6.02 |      16.62M
-------------------------------------------------
      2048 |       16 |        6.08 |      16.45M
      2048 |       32 |        5.99 |      16.70M
      2048 |       64 |        6.04 |      16.55M
-------------------------------------------------
      4096 |       16 |        6.08 |      16.45M
      4096 |       32 |        5.96 |      16.79M
      4096 |       64 |        6.02 |      16.61M
-------------------------------------------------