Skip to content

Commit 3b738d5

Browse files
committed
fix: cache line alignment
1 parent 5f97407 commit 3b738d5

4 files changed

Lines changed: 25 additions & 19 deletions

File tree

src/core/order_book.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ void OrderBook::handle_price_level_update(
159159
FIX::MDUpdateAction action,
160160
const FIX44::MarketDataIncrementalRefresh::NoMDEntries& group,
161161
bool is_book_clear_needed) {
162-
uint64_t px = utils::Double::toUint64(
163-
group.get(e_px_).getValue(), binance::Config::get_price_ticks_per_unit(symbol));
162+
uint64_t px =
163+
utils::Double::toUint64(group.get(temp_vars_.e_px).getValue(),
164+
binance::Config::get_price_ticks_per_unit(symbol));
164165
//
165166
switch (action.getValue()) {
166167
case FIX::MDUpdateAction_DELETE:
@@ -173,8 +174,9 @@ void OrderBook::handle_price_level_update(
173174
[[fallthrough]];
174175
}
175176
case FIX::MDUpdateAction_NEW: {
176-
uint64_t sz = utils::Double::toUint64(
177-
group.get(e_sz_).getValue(), binance::Config::get_size_ticks_per_unit(symbol));
177+
uint64_t sz =
178+
utils::Double::toUint64(group.get(temp_vars_.e_sz).getValue(),
179+
binance::Config::get_size_ticks_per_unit(symbol));
178180
bid_ask_map[px] = sz;
179181
} break;
180182
default:

src/core/order_book.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ class OrderBook {
4343
absl::btree_map<uint64_t, uint64_t, std::greater<>> bid_map_;
4444
/// @brief sorted list of offers (ascending), key=price, value=size
4545
absl::btree_map<uint64_t, uint64_t> ask_map_;
46-
//
47-
FIX::MDEntryPx e_px_;
48-
FIX::MDEntrySize e_sz_;
46+
// Temporary variables can share a cache line
47+
struct {
48+
FIX::MDEntryPx e_px;
49+
FIX::MDEntrySize e_sz;
50+
} temp_vars_;
4951
inline void handle_price_level_update(
5052
auto& bid_ask_map,
5153
binance::SymbolEnum symbol,

src/core/trade.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ namespace core {
1313
/// @brief Binance trade object
1414
struct alignas(utils::Env::CACHE_LINE_SIZE) Trade {
1515
public:
16-
Trade(const SideEnum side,
17-
const uint64_t px,
16+
Trade(const uint64_t px,
1817
const uint64_t sz,
1918
const uint64_t id,
19+
const SideEnum side,
2020
std::array<char, 16> time)
21-
: side(side), px(px), sz(sz), id(id), time(time) {}
21+
: px(px), sz(sz), id(id), side(side), time(time) {}
2222

2323
uint64_t px;
2424
uint64_t sz;

src/ui/trade_box.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,16 @@ void TradeBox::on_trade(const FIX44::MarketDataIncrementalRefresh& msg) {
196196
std::optional<binance::SymbolEnum> symbol;
197197
FIX::MDEntryPx e_px;
198198
FIX::MDEntrySize e_sz;
199-
uint64_t price, size;
200199
FIX::MDEntryType e_type;
201-
FIX::TransactTime time;
200+
FIX::TransactTime e_time;
201+
uint64_t price, size;
202202
FIX::TradeID trade_id;
203203
std::string trade_id_str;
204204
uint64_t trade_id_uint;
205205
// Binance's "side" field ("AggressorSide") is a custom field, not part of the FIX spec
206206
constexpr int AGGRESSOR_TAG = 2446;
207207
FIX::CharField side_field(AGGRESSOR_TAG);
208208
binance::SideEnum side{};
209-
//
210209
std::string raw_time;
211210
std::string time_only;
212211
for (int i = 1; i <= num_entries; i++) {
@@ -237,30 +236,33 @@ void TradeBox::on_trade(const FIX44::MarketDataIncrementalRefresh& msg) {
237236
switch (e_type.getValue()) {
238237
case FIX::MDEntryType_TRADE: {
239238
if (group.isSetField(AGGRESSOR_TAG)) {
240-
group.getField(side_field);
241-
side = binance::Side::from_str(side_field.getValue());
242239
price = utils::Double::toUint64(
243240
group.get(e_px).getValue(),
244241
binance_config_.get_price_ticks_per_unit(symbol.value()));
242+
245243
size = utils::Double::toUint64(
246244
group.get(e_sz).getValue(),
247245
binance_config_.get_size_ticks_per_unit(symbol.value()));
246+
248247
group.getField(trade_id);
249248
trade_id_str = trade_id.getValue();
250249
std::from_chars(trade_id_str.data(), trade_id_str.data() + trade_id_str.size(),
251250
trade_id_uint);
252251

252+
group.getField(side_field);
253+
side = binance::Side::from_str(side_field.getValue());
254+
253255
std::array<char, 16> tm_arr = {};
254256
if (group.isSetField(FIX::FIELD::TransactTime)) {
255-
group.getField(time);
256-
FIX::UtcTimeStamp tval = time.getValue();
257-
int microseconds = tval.getMillisecond() * 1000;
257+
group.getField(e_time);
258+
FIX::UtcTimeStamp tval = e_time.getValue();
259+
int microseconds = tval.getMillisecond() * 1000 + tval.getMicroecond();
258260
std::snprintf(tm_arr.data(), tm_arr.size(), "%02d:%02d:%02d.%06d",
259261
tval.getHour(), tval.getMinute(), tval.getSecond(),
260262
microseconds);
261263
}
262264

263-
trade_ring_.push_back(core::Trade(side, price, size, trade_id_uint, tm_arr));
265+
trade_ring_.push_back(core::Trade(price, size, trade_id_uint, side, tm_arr));
264266
screen_.post_event(ftxui::Event::Custom);
265267
} else {
266268
spdlog::error("trade with no side. message [{}]", msg.toString());

0 commit comments

Comments
 (0)