Skip to content

Commit

Permalink
remove shared ptr allocations (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
geseq authored Dec 20, 2023
1 parent b3dcdc5 commit c4b1101
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
22 changes: 12 additions & 10 deletions include/orderbook.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <cstdint>
#include <map>
#include <sstream>

Expand Down Expand Up @@ -51,7 +52,7 @@ class OrderBook {

std::atomic_uint64_t matching_ = 1;

std::shared_ptr<Order> cancelOrder(OrderID id);
Decimal cancelOrder(OrderID id);
void addTrigOrder(OrderID id, Type type, Side side, Decimal qty, Decimal price, Decimal trigPrice, Flag flag);
void processOrder(OrderID id, Type type, Side side, Decimal qty, Decimal price, Flag flag);
void postProcess(Decimal& lp);
Expand Down Expand Up @@ -256,39 +257,40 @@ void OrderBook<Notification>::cancelOrder(uint64_t tok, OrderID id) {
throw std::invalid_argument("invalid token received: cannot maintain determinism");
}

auto order = cancelOrder(id);
if (order == nullptr) {
auto qty = cancelOrder(id); // an order with 0 qty shouldn't be in the book
if (qty.is_zero()) {
notification_.putOrder(MsgType::CancelOrder, OrderStatus::Rejected, id, uint64_t(0), Error::OrderNotExists);
return;
}

notification_.putOrder(MsgType::CancelOrder, OrderStatus::Canceled, id, order->qty);
notification_.putOrder(MsgType::CancelOrder, OrderStatus::Canceled, id, qty);
}

template <class Notification>
std::shared_ptr<Order> OrderBook<Notification>::cancelOrder(OrderID id) {
Decimal OrderBook<Notification>::cancelOrder(OrderID id) {
if (orders_.find(id, orderbook::OrderIDCompare()) == orders_.end()) {
// TODO cancel triger
return nullptr;
return uint64_t(0);
}

auto it = orders_.find(id, OrderIDCompare());
if (it != orders_.end()) {
auto& pool = order_pool_;
std::shared_ptr<Order> order(&*it, [&pool](Order* ptr) { pool.release(ptr); });
auto* order = &*it;
scope_exit defer([&pool, &order]() { pool.release(order); });
orders_.erase(*it);

if (order->side == Side::Buy) {
bids_.remove(order);
return order;
return order->qty;
}

asks_.remove(order);

return order;
return order->qty;
}

return nullptr;
return uint64_t(0);
}

template <class Notification>
Expand Down
2 changes: 1 addition & 1 deletion include/pricelevel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class PriceLevel {
Decimal volume();
OrderQueue* getQueue();
void append(Order* order);
void remove(const std::shared_ptr<Order>& order);
void remove(Order* order);
Decimal processMarketOrder(const TradeNotification& tn, const PostOrderFill& pf, OrderID takerOrderID, Decimal qty, Flag flag);
Decimal processLimitOrder(const TradeNotification& tn, const PostOrderFill& pf, OrderID& takerOrderID, Decimal& price, Decimal qty, Flag& flag);

Expand Down
4 changes: 2 additions & 2 deletions src/pricelevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ void PriceLevel<CompareType>::append(Order* order) {
}

template <class CompareType>
void PriceLevel<CompareType>::remove(const std::shared_ptr<Order>& order) {
void PriceLevel<CompareType>::remove(Order* order) {
auto price = order->getPrice(price_type_);

auto q = order->queue;
if (q != nullptr) {
q->remove(order.get());
q->remove(order);
}

if (q->len() == 0) {
Expand Down
2 changes: 1 addition & 1 deletion test/pricelevel_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ TEST_F(PriceLevelTest, TestPriceLevel) {
FAIL() << "invalid price levels";
}

bidLevel.remove(o1);
bidLevel.remove(o1.get());

{
auto regularBegin = tree.begin();
Expand Down

0 comments on commit c4b1101

Please sign in to comment.