Skip to content

Commit 5f97407

Browse files
committed
feat: cache line alignment
1 parent 676a5fa commit 5f97407

19 files changed

Lines changed: 99 additions & 36 deletions

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ NB: this app uses `make` as a recipe book, but it's not essential:
151151
- FTXUI snapshot testing
152152
- performance
153153
- ✅ store prices and sizes as integrals (ticks as `uint64_t`) for performance
154+
- ✅ cache line alignment
154155
- release compile flags
155156
- profiling (valgrind/cachegrind)
156157
- profile-guided optimization (pgo)

src/binance/auth.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#pragma once
22

33
#include <mutex>
4+
#include <new>
45
#include <string>
56
#include <vector>
67

8+
#include "../utils/env.h"
79
#include "iauth.h"
810

911
namespace binance {
@@ -41,7 +43,7 @@ class Auth final : public IAuth {
4143
private:
4244
// when authenticating with Binance, each session authenticates independently,
4345
// and in parallel. requires synchronisation because of file access.
44-
mutable std::mutex mutex_;
46+
alignas(utils::Env::CACHE_LINE_SIZE) mutable std::mutex mutex_;
4547

4648
std::string& api_key_;
4749
std::string& private_pem_path_;

src/binance/fix_app.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ class FixApp final : public FIX::Application, public FIX44::MessageCracker {
4242
// TODO: we will have one of these per instrument
4343

4444
private:
45-
static constexpr std::string THREAD_NAME_ = "tradercppFIX2";
46-
static constexpr std::string PRICE_SESSION_QUALIFIER_ = "PX";
47-
static constexpr std::string TRADE_SESSION_QUALIFIER_ = "TX";
48-
static constexpr std::string ORDER_SESSION_QUALIFIER_ = "OX";
45+
static inline constexpr std::string THREAD_NAME_ = "tradercppFIX2";
46+
static inline constexpr std::string PRICE_SESSION_QUALIFIER_ = "PX";
47+
static inline constexpr std::string TRADE_SESSION_QUALIFIER_ = "TX";
48+
static inline constexpr std::string ORDER_SESSION_QUALIFIER_ = "OX";
4949
const std::vector<std::string>& symbols_;
5050
const std::unique_ptr<IAuth> auth_;
5151
const uint16_t MAX_DEPTH_;

src/binance/message_handling_mode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct MessageHandlingMode {
1919
SEQUENTIAL = 2,
2020
};
2121

22-
static constexpr uint16_t FIELD_ID = 25'035;
22+
static inline constexpr uint16_t FIELD_ID = 25'035;
2323

2424
static constexpr uint16_t to_int(Mode m) noexcept { return static_cast<uint16_t>(m); }
2525

src/binance/worker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace binance {
1818
/// @brief Binance DI container
1919
class Worker final {
2020
public:
21-
static constexpr std::string THREAD_NAME_ = "tradercppFIX1";
21+
static inline constexpr std::string THREAD_NAME_ = "tradercppFIX1";
2222
Worker(std::unique_ptr<FixApp> app,
2323
std::unique_ptr<FIX::FileStoreFactory> store,
2424
FIX::SessionSettings settings,

src/core/bid_ask.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
#include <cmath>
44

5+
#include "../utils/env.h"
6+
57
namespace core {
68

79
/// @brief basic bid/ask level
8-
struct BidAsk {
10+
struct alignas(utils::Env::CACHE_LINE_SIZE) BidAsk {
911
public:
1012
BidAsk() = default;
1113
explicit BidAsk(u_int64_t bidsz, u_int64_t bidpx, u_int64_t askpx, u_int64_t asksz)
@@ -14,7 +16,7 @@ struct BidAsk {
1416
// NB: using UINT64_MAX as a sentinel value.
1517
// maybe it's smarter to use UINT64_MAX-1 as a sentinel,
1618
// in order to catch overflow prices in the market?
17-
static constexpr u_int64_t SENTINEL_ = UINT64_MAX;
19+
static inline constexpr u_int64_t SENTINEL_ = UINT64_MAX;
1820

1921
u_int64_t bid_sz = SENTINEL_;
2022
u_int64_t bid_px = SENTINEL_;

src/core/order_book.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <mutex>
99

1010
#include "../binance/symbol.h"
11+
#include "../utils/env.h"
1112
#include "absl/container/btree_map.h"
1213
#include "bid_ask.h"
1314

@@ -37,7 +38,7 @@ class OrderBook {
3738
private:
3839
// mutex for reading/writing to bid/ask maps
3940
// NB: UI-bound, so performance is acceptable
40-
mutable std::mutex mutex_;
41+
alignas(utils::Env::CACHE_LINE_SIZE) mutable std::mutex mutex_;
4142
/// @brief sorted list of bids (descending), key=price, value=size
4243
absl::btree_map<uint64_t, uint64_t, std::greater<>> bid_map_;
4344
/// @brief sorted list of offers (ascending), key=price, value=size

src/core/trade.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,29 @@
33
#include <cmath>
44

55
#include "../binance/side.h"
6+
#include "../utils/env.h"
67

78
using binance::Side;
89
using binance::SideEnum;
910

1011
namespace core {
1112

1213
/// @brief Binance trade object
13-
struct Trade {
14+
struct alignas(utils::Env::CACHE_LINE_SIZE) Trade {
1415
public:
15-
Trade(std::string time,
16-
const SideEnum side,
16+
Trade(const SideEnum side,
1717
const uint64_t px,
1818
const uint64_t sz,
19-
const uint64_t id)
20-
: time(std::move(time)), side(side), px(px), sz(sz), id(id) {}
19+
const uint64_t id,
20+
std::array<char, 16> time)
21+
: side(side), px(px), sz(sz), id(id), time(time) {}
2122

22-
std::string time;
23-
SideEnum side;
2423
uint64_t px;
2524
uint64_t sz;
2625
uint64_t id;
26+
SideEnum side;
27+
// 15 characters + newline, e.g. "07:17:50.031794"
28+
std::array<char, 16> time;
2729
};
2830

2931
} // namespace core

src/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "spdlog/sinks/basic_file_sink.h"
77
#include "spdlog/spdlog.h"
88
#include "ui/app/ui_app.h"
9+
#include "utils/env.h"
910
#include "utils/threading.h"
1011

1112
int main() {
@@ -19,8 +20,8 @@ int main() {
1920
// Set a global pattern without the logger name
2021
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] [%t] %v");
2122
spdlog::flush_every(std::chrono::microseconds(100));
22-
2323
spdlog::info("hello");
24+
utils::Env::log_arch();
2425

2526
// Binance market data connectivity
2627
auto b_conf = binance::Config::from_env();

src/ui/app/ui_app.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void App::start() {
7777
const ftxui::Component row2 =
7878
Horizontal({Vertical({wallet_box_.get_component() | flex}) | flex,
7979
Vertical({log_box_->get_component() | flex}) | flex});
80-
const ftxui::Component root = Vertical({row1 | size(HEIGHT, EQUAL, 20), row2 | flex});
80+
const ftxui::Component root = Vertical({row1 | flex, row2 | size(HEIGHT, EQUAL, 6)});
8181
screen_->loop(root);
8282
}
8383

0 commit comments

Comments
 (0)