refactor: Use SmallString instead of std::array in ExchangeOrderID

This commit is contained in:
John Colvin committed 2025-06-06 17:41:53 +00:00
1 parent f3d32dd918
commit 34e7e1e01c
12 files changed
+62 -38

No files matched your search

@@ -482,8 +482,7 @@ void EMExecutionGateway::_process_cb_and_fire_er(const EMCallbackMsg &em_cb) {
std::string exch_oid = std::to_string(em_cb.order_key.value());
// TODO, should we put EM order or Deribit ?
// (this is not used by EMNativeOrder so we are ok)
auto &er_exch_oid = std::get<ExchangeOrderID>(er.exchange_order_id).value();
to_array(exch_oid, &er_exch_oid);
std::get<ExchangeOrderID>(er.exchange_order_id).value().copy_from(exch_oid);
er.price.value() = em_cb.order_state.order_px;
er.quantity = em_cb.order_state.order_qty;
// fractional qty
+28 -14
View File
@@ -7,6 +7,7 @@
#include <cstring>
#include <fmt/format.h>
#include <nlohmann/json.hpp>
#include <stdexcept>
#include <string>
namespace bts::core {
@@ -55,14 +56,16 @@ struct SmallString {
std::array<char, N> data_;
std::uint8_t length_ = 0;
friend struct fmt::formatter<bts::core::SmallString<N>>;
friend std::ostream &operator<<(
std::ostream &out, const SmallString<N> &lhs) noexcept;
friend bool operator==(
const SmallString<N> &lhs, const std::string &rhs) noexcept;
const SmallString<N> &lhs, const std::string &rhs) noexcept {
const std::size_t sz = lhs.size();
return sz == rhs.size() && std::memcmp(&lhs.data_[0], &rhs[0], sz) == 0;
}
friend bool operator!=(
const SmallString<N> &lhs, const std::string &rhs) noexcept;
const SmallString<N> &lhs, const std::string &rhs) noexcept {
return !(lhs == rhs);
}
};
template <std::size_t N>
@@ -72,7 +75,7 @@ template <std::size_t N>
void from_json(const nlohmann::json &j, SmallString<N> &ss);
template <std::size_t N>
std::ostream &operator<<(std::ostream &out, const SmallString<N> &lhs) noexcept;
std::ostream &operator<<(std::ostream &out, const SmallString<N> &ss);
template <std::size_t N>
bool operator==(const SmallString<N> &lhs, const std::string &rhs) noexcept;
@@ -175,8 +178,12 @@ void SmallString<N>::copy_to(std::string &dest) const {
template <std::size_t N>
std::int32_t SmallString<N>::toi() const {
if (length_ == 0) {
throw std::logic_error(
"SmallString<N>::toi failed because string is empty");
}
std::int32_t rv = 0;
for (std::size_t i = length_ - 1; i > -1; i--) {
for (std::size_t i = length_ - 1; i >= 0; i--) {
rv = rv * 10 + (data_[i] - '0');
}
return rv;
@@ -184,8 +191,12 @@ std::int32_t SmallString<N>::toi() const {
template <std::size_t N>
std::int64_t SmallString<N>::tol() const {
if (length_ == 0) {
throw std::logic_error(
"SmallString<N>::tol failed because string is empty");
}
std::int64_t rv = 0;
for (std::size_t i = length_ - 1; i > -1; i--) {
for (std::size_t i = length_ - 1; i >= 0; i--) {
rv = rv * 10 + (data_[i] - '0');
}
return rv;
@@ -193,6 +204,10 @@ std::int64_t SmallString<N>::tol() const {
template <std::size_t N>
std::uint64_t SmallString<N>::toul() const {
if (length_ == 0) {
throw std::logic_error(
"SmallString<N>::toul failed because string is empty");
}
std::uint64_t rv = 0;
for (std::size_t i = length_ - 1; i >= 0; i--) {
rv = rv * 10ul + static_cast<std::uint32_t>(data_[i] - '0');
@@ -240,9 +255,9 @@ void from_json(const nlohmann::json &j, SmallString<N> &ss) {
}
template <std::size_t N>
std::ostream &operator<<(
std::ostream &out, const SmallString<N> &small_str) noexcept {
return out.write(small_str.data_, small_str.length_);
std::ostream &operator<<(std::ostream &out, const SmallString<N> &ss) {
out << ss.to_string_view();
return out;
}
template <std::size_t N>
@@ -267,7 +282,6 @@ struct fmt::formatter<bts::core::SmallString<N>> {
template <typename FormatContext>
auto format(const bts::core::SmallString<N> &s, FormatContext &ctx) const
-> decltype(ctx.out()) {
return fmt::format_to(
ctx.out(), "{}", std::string_view(&s.data_[0], s.length_));
return fmt::format_to(ctx.out(), "{}", s.to_string_view());
}
};
+1 -1
View File
@@ -230,7 +230,7 @@ void try_add(
auto u64_key = std::visit(
overloaded{
[](orderman::ExchangeOrderID const &str) {
return std::stoul(str.value().data());
return str.value().toul();
},
[](orderman::ExchangeOrderID64 oid) { return oid.value(); }},
ex_key);
@@ -351,7 +351,7 @@ void CCAPIExecutionGateway::_on_order_cb(const CCAPIOrderState &s) {
er.manual_order_indicator =
api::orderman::ManualOrderIndicator::MANUAL_ORDER_INDICATOR_NO;
auto &er_exch_oid = std::get<ExchangeOrderID>(er.exchange_order_id).value();
to_array(s.cc_order_key, &er_exch_oid);
er_exch_oid.copy_from(s.cc_order_key);
er.price.value() = s.order_px;
er.quantity = s.order_qty;
er.msg = s.msg;
@@ -236,9 +236,9 @@ void GLBXExecutionGateway::on_exe_report(
msg.get<fix::ManualOrderIndicator>()
? api::orderman::MANUAL_ORDER_INDICATOR_YES
: api::orderman::MANUAL_ORDER_INDICATOR_YES;
str_to(
msg.get<fix::OrderID>(),
&std::get<ExchangeOrderID>(data->exchange_order_id));
std::get<ExchangeOrderID>(data->exchange_order_id)
.value()
.copy_from(msg.get<fix::OrderID>());
if (msg.has<fix::Price>())
data->price = securities::decode_exchange_price(
@@ -407,8 +407,8 @@ void ICEExecutionGateway::on_exe_report(
api::orderman::NativeOrderStatus(msg.get<fix::OrdStatus>());
data->manual_order_indicator = api::orderman::MANUAL_ORDER_INDICATOR_NO;
std::string oid_string = get_ice_order_id_string(msg);
str_to(oid_string, &std::get<ExchangeOrderID>(data->exchange_order_id));
data->exchange_order_id.emplace<ExchangeOrderID>(
get_ice_order_id_string(msg));
data->ice_cl_ord_id = 0;
if (msg.has<fix::Int_ClOrdID>())
data->ice_cl_ord_id = msg.get<fix::Int_ClOrdID>();
@@ -777,8 +777,7 @@ void fill_execution_report(const SEGOrderState &seg_os, ExecutionReport &er) {
er.manual_order_indicator =
api::orderman::ManualOrderIndicator::MANUAL_ORDER_INDICATOR_NO;
std::string exch_oid = std::to_string(cr_msg.key.value());
auto &er_exch_oid = std::get<ExchangeOrderID>(er.exchange_order_id).value();
to_array(exch_oid, &er_exch_oid);
std::get<ExchangeOrderID>(er.exchange_order_id).value().copy_from(exch_oid);
er.price.value() = seg_os.price().value();
er.quantity = seg_os.qty();
// fractional qty
@@ -10,8 +10,7 @@ namespace bts::orderman {
static std::string get_exch_id_str(ExchangeOrderIDVar exch_id) {
std::string rv;
if (std::holds_alternative<bts::orderman::ExchangeOrderID>(exch_id)) {
rv = bts::orderman::to_trim_str(
std::get<bts::orderman::ExchangeOrderID>(exch_id));
std::get<bts::orderman::ExchangeOrderID>(exch_id).value().copy_to(rv);
} else {
rv = std::to_string(*std::get<bts::orderman::ExchangeOrderID64>(exch_id));
}
@@ -0,0 +1,11 @@
#pragma once
#include "spark/core/small_string.h"
#include "spark/utils/ValueWrapper.h"
namespace bts::orderman {
using ExchangeOrderID =
utils::ValueWrapper<struct ExchangeOrderIDTag, core::SmallString<32>>;
} // namespace bts::orderman
@@ -6,6 +6,7 @@
#include "spark/orderman/account_id.h"
#include "spark/orderman/account_name.h"
#include "spark/orderman/order_key.h"
#include "spark/orderman/orderman_utils/exchange_order_id.h"
#include "spark/utils/ValueWrapper.h"
#include "spark/utils/bts_assert.h"
#include "spark/utils/bts_chrono.h"
@@ -173,8 +174,6 @@ inline bool in_terminal_state(bts::api::orderman::OrderStatus order_status) {
}
// Exchange order ids
using ExchangeOrderID =
utils::ValueWrapper<struct ExchangeOrderIDTag, std::array<char, 32>>;
using ExchangeOrderID64 =
utils::ValueWrapper<struct ExchangeOrderID64Tag, uint64_t>;
// Exchange timestamps
@@ -192,9 +191,7 @@ using ExchangeOrderIDVar = std::variant<ExchangeOrderID, ExchangeOrderID64>;
inline ExchangeOrderIDVar get_exchange_order_id(
api::orderman::NativeOrderState const &st) {
if (st.exchange_order_id().size()) {
ExchangeOrderID result;
str_to<ExchangeOrderID>(st.exchange_order_id(), &result);
return result;
return ExchangeOrderID{{st.exchange_order_id()}};
} else {
return ExchangeOrderID64{st.exchange_order_id_u64()};
}
@@ -205,7 +202,7 @@ inline void set_exchange_order_id(
overloaded{
[out](ExchangeOrderID const &str) {
out->set_exchange_order_id_u64(0);
*(out->mutable_exchange_order_id()) = to_trim_strn(str);
str.value().copy_to(*out->mutable_exchange_order_id());
},
[out](ExchangeOrderID64 oid) {
out->set_exchange_order_id_u64(oid.value());
@@ -331,7 +328,7 @@ inline void set_update_quantity(
} // namespace bts
FMT_IMPL(bts::orderman::AccountID, id, "{}", *id);
FMT_IMPL(bts::orderman::ExchangeOrderID, id, "{}", bts::orderman::to_strn(id));
FMT_IMPL(bts::orderman::ExchangeOrderID, id, "{}", id.value());
FMT_IMPL(bts::orderman::ExchangeOrderID64, id, "{}", *id);
template <>
@@ -19,6 +19,7 @@
#include <math.h>
#include <queue>
#include <sys/time.h>
#include <variant>
namespace bts {
namespace orderman {
@@ -287,8 +288,10 @@ api::orderman::OrderDefinition ICENativeOrder::on_init_from_exe_report(
}
auto &native_state = get_native_state(state);
native_state.set_exchange_order_id(
to_trim_str(std::get<ExchangeOrderID>(er.exchange_order_id)));
bts_assert(std::holds_alternative<ExchangeOrderID>(er.exchange_order_id));
std::get<ExchangeOrderID>(er.exchange_order_id)
.value()
.copy_to(*native_state.mutable_exchange_order_id());
native_state.set_last_msg_seq_num(er.msg_seq_num);
native_state.set_native_type(er.order_type);
@@ -887,7 +890,9 @@ void ICENativeOrder::on_exe_report(const NewExecutionReportEvent &er) {
bool got_first_exchange_order_id = !has_exchange_order_id();
if (got_first_exchange_order_id) {
me->exchange_order_id =
to_trim_str(std::get<ExchangeOrderID>(er.data->exchange_order_id));
std::get<ExchangeOrderID>(er.data->exchange_order_id)
.value()
.to_string();
native_state.set_exchange_order_id(me->exchange_order_id);
add_exchange_order_id_to_fix();
}
@@ -274,7 +274,7 @@ void iLink2OrderEncoder::init(
}
void iLink2OrderEncoder::update_exchange_order_id(ExchangeOrderIDVar const &v) {
auto oid = to_trim_str(std::get<ExchangeOrderID>(v));
auto oid = std::get<ExchangeOrderID>(v).value().to_string();
me->update_encoding.add<jetfix::fix::OrderID>(oid);
me->cancel_encoding.add<jetfix::fix::OrderID>(oid);
//CME EXECUTION NEEDS TO BE UPDATED, can be commented out