Skip to content

Commit d27fc78

Browse files
committed
fix: replacing heap with copies, replacing polymorphism + dynamic casts with variants + visit, housekeeping
1 parent 0b184ed commit d27fc78

35 files changed

Lines changed: 102 additions & 123 deletions

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ build-release:
5959
test:
6060
cmake --preset debug
6161
cmake --build --preset=debug
62-
ctest -j$(shell nproc) --preset=debug
62+
ctest -j$(shell nproc) --preset=debug -LE BENCHMARK
63+
# coverage
6364
lcov --gcov-tool gcov --capture --directory . --output-file lcov.info
6465
source .venv/bin/activate && \
6566
gcovr -r . --exclude 'tests/*' --sonarqube -o sonar-coverage.xml
@@ -74,7 +75,6 @@ bench:
7475
--benchmark_out_format=json \
7576
--benchmark_report_aggregates_only=false
7677

77-
7878
## tidy: 🧹 tidy things up before committing code
7979
.PHONY: tidy
8080
tidy:

benchmarks/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ target_link_libraries(benchmarks PRIVATE
1616
)
1717

1818
add_test(NAME benchmarks COMMAND benchmarks)
19+
20+
set_tests_properties(benchmarks PROPERTIES LABELS "BENCHMARK")

benchmarks/core/book_side_benchmark.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
#include <benchmark/benchmark.h>
22
#include <fmt/ranges.h>
33

4-
#include <random>
5-
6-
#include "core/bid_ask.h"
74
#include "core/order_book.h"
85
#include "spdlog/spdlog.h"
96

10-
/// @brief the order book is composed of two asymetrically-sorted collections,
7+
/// @brief the order book is composed of two asymmetrically-sorted collections,
118
/// this test is for one of those collections,
129
/// (the bid side in this case, because of the additional complexity of a DESC sort)
1310
class BookSideFixture : public benchmark::Fixture {
@@ -21,7 +18,7 @@ class BookSideFixture : public benchmark::Fixture {
2118
}
2219

2320
void TearDown([[maybe_unused]] const benchmark::State& state) override {
24-
spdlog::info("Levels: [{}]", bids_);
21+
// spdlog::info("Levels: [{}]", bids_);
2522
}
2623

2724
absl::btree_map<uint64_t, uint64_t, std::greater<>> bids_;
@@ -30,7 +27,7 @@ class BookSideFixture : public benchmark::Fixture {
3027
};
3128

3229
/// @brief deterministically populate the order book
33-
BENCHMARK_DEFINE_F(BookSideFixture, BM_BookUpdate)(benchmark::State& state) {
30+
BENCHMARK_DEFINE_F(BookSideFixture, BENCH_BookUpdate)(benchmark::State& state) {
3431
int i = 0;
3532
for (auto _ : state) {
3633
bids_[i] = MID_PRICE - i;
@@ -44,4 +41,4 @@ BENCHMARK_DEFINE_F(BookSideFixture, BM_BookUpdate)(benchmark::State& state) {
4441
benchmark::Counter(state.iterations(), benchmark::Counter::kIsRate);
4542
}
4643

47-
BENCHMARK_REGISTER_F(BookSideFixture, BM_BookUpdate)->Iterations(500'000);
44+
BENCHMARK_REGISTER_F(BookSideFixture, BENCH_BookUpdate)->Iterations(500'000);

benchmarks/core/price_update_benchmark.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#include <benchmark/benchmark.h>
2-
#include <fmt/ranges.h>
32

4-
#include <random>
5-
6-
#include "core/bid_ask.h"
73
#include "core/order_book.h"
84
#include "spdlog/spdlog.h"
95

@@ -98,10 +94,10 @@ class PriceUpdateFixture : public benchmark::Fixture {
9894
};
9995

10096
/// @brief deterministically populate the order book
101-
BENCHMARK_DEFINE_F(PriceUpdateFixture, BM_PriceUpdate)(benchmark::State& state) {
97+
BENCHMARK_DEFINE_F(PriceUpdateFixture, BENCH_PriceUpdate)(benchmark::State& state) {
10298
int i = 0;
10399
for (auto _ : state) {
104-
book_->apply_increment(test_messages_[i], false);
100+
book_->apply_increment(test_messages_[i++], false);
105101
if (i == MSG_COUNT - 1) {
106102
i = 0;
107103
}
@@ -111,4 +107,4 @@ BENCHMARK_DEFINE_F(PriceUpdateFixture, BM_PriceUpdate)(benchmark::State& state)
111107
benchmark::Counter(state.iterations(), benchmark::Counter::kIsRate);
112108
}
113109

114-
BENCHMARK_REGISTER_F(PriceUpdateFixture, BM_PriceUpdate)->Iterations(500'000);
110+
BENCHMARK_REGISTER_F(PriceUpdateFixture, BENCH_PriceUpdate)->Iterations(500'000);

src/binance/auth.h

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

33
#include <mutex>
4-
#include <new>
54
#include <string>
65
#include <vector>
76

src/binance/config.h

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

3-
#include <charconv>
4-
#include <cmath>
53
#include <cstdint>
64
#include <format>
75
#include <stdexcept>
86
#include <string>
9-
#include <string_view>
107
#include <vector>
118

129
#include "../utils/double.h"

src/binance/fix_app.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#include <algorithm>
1212
#include <cassert>
13-
#include <cstdint>
1413
#include <format>
1514
#include <memory>
1615
#include <string>
@@ -215,17 +214,17 @@ void FixApp::fromApp(const FIX::Message& msg,
215214

216215
void FixApp::onMessage(const FIX44::MarketDataSnapshotFullRefresh& m,
217216
[[maybe_unused]] const FIX::SessionID& sessionID) {
218-
order_queue_.enqueue(std::make_shared<const FIX44::MarketDataSnapshotFullRefresh>(m));
217+
order_queue_.enqueue(MarketMessageVariant{m});
219218
}
220219
void FixApp::onMessage(const FIX44::MarketDataIncrementalRefresh& m,
221220
const FIX::SessionID& sessionID) {
222221
if (sessionID.getSessionQualifier() == PX_SESSION_QUALIFIER_) {
223-
order_queue_.enqueue(std::make_shared<const FIX44::MarketDataIncrementalRefresh>(m));
222+
order_queue_.enqueue(MarketMessageVariant{m});
224223
} else if (sessionID.getSessionQualifier() == TX_SESSION_QUALIFIER_) {
225-
trade_queue_.enqueue(std::make_shared<const FIX44::MarketDataIncrementalRefresh>(m));
224+
trade_queue_.enqueue(m);
226225
} else {
227226
spdlog::error(
228-
"ivalid session for market data incremental refresh, qualifier [{}], id [{}]",
227+
"invalid session for market data incremental refresh, qualifier [{}], id [{}]",
229228
sessionID.getSessionQualifier(), sessionID.toString());
230229
}
231230
}

src/binance/fix_app.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
#include <quickfix/Application.h>
44
#include <quickfix/SessionID.h>
5+
#include <quickfix/fix44/MarketDataIncrementalRefresh.h>
6+
#include <quickfix/fix44/MarketDataSnapshotFullRefresh.h>
57
#include <quickfix/fix44/Message.h>
68
#include <quickfix/fix44/MessageCracker.h>
79

8-
#include <cstdint>
910
#include <memory>
1011
#include <string>
1112
#include <vector>
1213

1314
#include "concurrentqueue.h"
1415
#include "iauth.h"
16+
#include "market_message_variant.h"
1517

1618
namespace binance {
1719

@@ -35,9 +37,9 @@ class FixApp final : public FIX::Application, public FIX44::MessageCracker {
3537
void subscribe_to_trades(const FIX::SessionID& session_id) const;
3638

3739
/// @brief queue of market messages from Binance
38-
moodycamel::ConcurrentQueue<std::shared_ptr<const FIX44::Message>> order_queue_;
40+
moodycamel::ConcurrentQueue<MarketMessageVariant> order_queue_;
3941
/// @brief queue of trade messages from Binance
40-
moodycamel::ConcurrentQueue<std::shared_ptr<const FIX44::Message>> trade_queue_;
42+
moodycamel::ConcurrentQueue<FIX44::MarketDataIncrementalRefresh> trade_queue_;
4143

4244
// TODO: performance implication of a polymorphic queue
4345
// TODO: perhaps better to run two queues, or something else
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <quickfix/fix44/MarketDataIncrementalRefresh.h>
4+
#include <quickfix/fix44/MarketDataSnapshotFullRefresh.h>
5+
6+
#include <variant>
7+
8+
namespace binance {
9+
10+
/// @brief a union type for price-update messages,
11+
/// so that they can be placed on the same queue,
12+
/// so that order can be maintained
13+
using MarketMessageVariant = std::variant<FIX44::MarketDataSnapshotFullRefresh,
14+
FIX44::MarketDataIncrementalRefresh>;
15+
16+
} // namespace binance

src/binance/message_handling_mode.h

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

3-
#include <cstdint>
43
#include <string>
54

65
namespace binance {

0 commit comments

Comments
 (0)