diff --git a/src/lib/jetfix/codec/i32_toa.h b/src/lib/jetfix/codec/i32_toa.h index 7a390294e..afc257ebc 100644 --- a/src/lib/jetfix/codec/i32_toa.h +++ b/src/lib/jetfix/codec/i32_toa.h @@ -259,113 +259,65 @@ inline int u32_toa(uint32_t u, char* buffer, int pad) { } -inline int u64_toa(uint64_t value, char* buffer) +///https://github.com/miloyip/itoa-benchmark + +#define BEGIN2(n) \ + do { \ + int t = (n); \ + if(t < 10) *p++ = '0' + t; \ + else { \ + t *= 2; \ + *p++ = gDigitsLut[t]; \ + *p++ = gDigitsLut[t + 1]; \ + } \ + } while(0) +#define MIDDLE2(n) \ + do { \ + int t = (n) * 2; \ + *p++ = gDigitsLut[t]; \ + *p++ = gDigitsLut[t + 1]; \ + } while(0) +#define BEGIN4(n) \ + do { \ + int t4 = (n); \ + if(t4 < 100) BEGIN2(t4); \ + else { BEGIN2(t4 / 100); MIDDLE2(t4 % 100); } \ + } while(0) +#define MIDDLE4(n) \ + do { \ + int t4 = (n); \ + MIDDLE2(t4 / 100); MIDDLE2(t4 % 100); \ + } while(0) +#define BEGIN8(n) \ + do { \ + uint32_t t8 = (n); \ + if(t8 < 10000) BEGIN4(t8); \ + else { BEGIN4(t8 / 10000); MIDDLE4(t8 % 10000); } \ + } while(0) +#define MIDDLE8(n) \ + do { \ + uint32_t t8 = (n); \ + MIDDLE4(t8 / 10000); MIDDLE4(t8 % 10000); \ + } while(0) +#define MIDDLE16(n) \ + do { \ + uint64_t t16 = (n); \ + MIDDLE8(t16 / 100000000); MIDDLE8(t16 % 100000000); \ + } while(0) + +inline int u64_toa(uint64_t value, char* p) { - char *start = buffer; - if (value < 100000000) { - uint32_t v = static_cast(value); - if (v < 10000) { - const uint32_t d1 = (v / 100) << 1; - const uint32_t d2 = (v % 100) << 1; - - if (v >= 1000) - *buffer++ = gDigitsLut[d1]; - if (v >= 100) - *buffer++ = gDigitsLut[d1 + 1]; - if (v >= 10) - *buffer++ = gDigitsLut[d2]; - *buffer++ = gDigitsLut[d2 + 1]; - *buffer++ = '\0'; - } - else { - // value = bbbbcccc - const uint32_t b = v / 10000; - const uint32_t c = v % 10000; - const uint32_t d1 = (b / 100) << 1; - const uint32_t d2 = (b % 100) << 1; - const uint32_t d3 = (c / 100) << 1; - const uint32_t d4 = (c % 100) << 1; - if (value >= 10000000) - *buffer++ = gDigitsLut[d1]; - if (value >= 1000000) - *buffer++ = gDigitsLut[d1 + 1]; - if (value >= 100000) - *buffer++ = gDigitsLut[d2]; - *buffer++ = gDigitsLut[d2 + 1]; - *buffer++ = gDigitsLut[d3]; - *buffer++ = gDigitsLut[d3 + 1]; - *buffer++ = gDigitsLut[d4]; - *buffer++ = gDigitsLut[d4 + 1]; - *buffer++ = '\0'; - } - } - else if (value < 10000000000000000) { - const uint32_t v0 = static_cast(value / 100000000); - const uint32_t v1 = static_cast(value % 100000000); - const __m128i a0 = Convert8DigitsSSE2(v0); - const __m128i a1 = Convert8DigitsSSE2(v1); - // Convert to bytes, add '0' - const __m128i va = _mm_add_epi8(_mm_packus_epi16(a0, a1), reinterpret_cast(kAsciiZero)[0]); - - // Count number of digit - const unsigned mask = _mm_movemask_epi8(_mm_cmpeq_epi8(va, reinterpret_cast(kAsciiZero)[0])); -#ifdef _MSC_VER - unsigned long digit; - _BitScanForward(&digit, ~mask | 0x8000); -#else - unsigned digit = __builtin_ctz(~mask | 0x8000); -#endif - - // Shift digits to the beginning - __m128i result = ShiftDigits_SSE2(va, digit); - _mm_storeu_si128(reinterpret_cast<__m128i*>(buffer), result); - buffer[16 - digit] = '\0'; - } - else { - const uint32_t a = static_cast(value / 10000000000000000); // 1 to 1844 - value %= 10000000000000000; - - if (a < 10) - *buffer++ = '0' + static_cast(a); - else if (a < 100) { - const uint32_t i = a << 1; - *buffer++ = gDigitsLut[i]; - *buffer++ = gDigitsLut[i + 1]; - } - else if (a < 1000) { - *buffer++ = '0' + static_cast(a / 100); - - const uint32_t i = (a % 100) << 1; - *buffer++ = gDigitsLut[i]; - *buffer++ = gDigitsLut[i + 1]; - } - else { - const uint32_t i = (a / 100) << 1; - const uint32_t j = (a % 100) << 1; - *buffer++ = gDigitsLut[i]; - *buffer++ = gDigitsLut[i + 1]; - *buffer++ = gDigitsLut[j]; - *buffer++ = gDigitsLut[j + 1]; - } - - const uint32_t v0 = static_cast(value / 100000000); - const uint32_t v1 = static_cast(value % 100000000); - - const __m128i a0 = Convert8DigitsSSE2(v0); - const __m128i a1 = Convert8DigitsSSE2(v1); - - // Convert to bytes, add '0' - const __m128i va = _mm_add_epi8(_mm_packus_epi16(a0, a1), reinterpret_cast(kAsciiZero)[0]); - _mm_storeu_si128(reinterpret_cast<__m128i*>(buffer), va); - buffer[16] = '\0'; - return buffer+16-start; - } - return buffer-start-1; -} + auto start = p; + if(value < 100000000) BEGIN8(value); + else if(value < 10000000000000000) { BEGIN8(value / 100000000); MIDDLE8(value % 100000000); } + else { BEGIN4(value / 10000000000000000); MIDDLE16(value % 10000000000000000); } + *p = 0; + return p-start; +} inline int count_digits (int64_t value) { - static auto P01 = 10u; + static auto P01 = 10ull; static auto P02 = P01*10; static auto P03 = P02*10; static auto P04 = P03*10; @@ -413,7 +365,7 @@ inline int count_digits (int64_t value) inline int count_digits (uint64_t u) { - static auto P01 = 10u; + static auto P01 = 10ull; static auto P02 = P01*10; static auto P03 = P02*10; static auto P04 = P03*10; @@ -455,7 +407,7 @@ inline int count_digits (uint64_t u) return 20; } -inline int i64_toa(int32_t value, char* buffer, int pad) +inline int i64_toa(int64_t value, char* buffer, int pad) { auto start = buffer; uint64_t u = static_cast(value); @@ -482,7 +434,7 @@ inline int i64_toa(int32_t value, char* buffer, int pad) return (buffer-start) + u64_toa(u, buffer); } -inline int u64_toa(uint32_t u, char* buffer, int pad) +inline int u64_toa(uint64_t u, char* buffer, int pad) { auto start = buffer; if (pad) diff --git a/src/lib/orderman/orders/ICENativeOrder.cpp b/src/lib/orderman/orders/ICENativeOrder.cpp index 5809e592c..fa9eec8e2 100644 --- a/src/lib/orderman/orders/ICENativeOrder.cpp +++ b/src/lib/orderman/orders/ICENativeOrder.cpp @@ -11,6 +11,7 @@ #include "lib/orderman/orderman_utils/fix_order_codec.h" #include "lib/orderman/orders/PreActivationOrderServices.h" #include "lib/orderman/securities/ISecurityDB.h" +#include "lib/md/IntPrice.h" #include "lib/utils/PBUtils.h" #include "lib/utils/bts_chrono.h" @@ -46,7 +47,8 @@ struct ICENativeOrder::PImpl OrderState state; int price_encoding_precision; std::string exchange_order_id; - std::string sender_compid; + int sender_compid; + uint64_t id_scale, update_scale, cancel_scale; jetfix::FIXFragment update_encoding; jetfix::FIXFragment::Bookmark update_price_bm; @@ -114,8 +116,8 @@ void ICENativeOrder::on_init_new(const OrderDefinition& def, const PreActivation { transition(ORDER_STATUS_INACTIVE); auto &native_state = get_native_state(me->state); - *native_state.mutable_routing_record() = rq.routing_record; - me->sender_compid = rq.routing_record.sender_compid(); + *native_state.mutable_routing_record() = rq.routing_record; + setup_ids(rq.routing_record.sender_compid()); init_fix(); } else @@ -138,7 +140,7 @@ void ICENativeOrder::on_init_recovered(const OrderRecord& rec, const PreActivati me->previous_position_contribution = Quantity(rec.state().filled_quantity()); auto const &native_state = get_native_state(me->state); - me->sender_compid = native_state.routing_record().sender_compid(); + setup_ids(native_state.routing_record().sender_compid()); switch (me->state.status()) { case ORDER_STATUS_WORKING: @@ -587,8 +589,8 @@ OrderOpResult ICENativeOrder::on_update(Price p, Quantity q) auto const &ts = me->services->get_time_str(); me->update_transact_time_bm = encoding.overwrite( me->update_transact_time_bm, &ts.value()[0], ts.value().size() ); - static uint32_t update_id = 10000; //TODO Pedro please look - me->update_clordid_bm = encoding.overwrite( me->update_clordid_bm, get_clordid(update_id++), ice_orderid_sz() ); + auto update_clordid = get_clordid(me->update_scale + key().value()); + me->update_clordid_bm = encoding.overwrite( me->update_clordid_bm, update_clordid, count_digits(update_clordid) ); me->services->queue_fix(*me->session_services, jetfix::MessageTypes::CancelReplace, @@ -659,7 +661,7 @@ void ICENativeOrder::on_attached(api::orderman::OrderDefinition const &new_paren auto &native_state = get_native_state(me->state); *native_state.mutable_routing_record() = rq.routing_record; - me->sender_compid = rq.routing_record.sender_compid(); + setup_ids(rq.routing_record.sender_compid()); init_fix(); if (has_exchange_order_id()) add_exchange_order_id_to_fix(); @@ -698,9 +700,9 @@ OrderOpResult ICENativeOrder::on_cancel() { case ORDER_STATUS_WORKING: { - auto &encoding = me->cancel_encoding; - static uint32_t cancel_id = 0; //TODO Pedro please look - me->cancel_clordid_bm = encoding.overwrite( me->cancel_clordid_bm, get_clordid(cancel_id++), ice_orderid_sz() ); + auto &encoding = me->cancel_encoding; + auto cancel_clordid = get_clordid(me->cancel_scale + key().value()); + me->cancel_clordid_bm = encoding.overwrite( me->cancel_clordid_bm, cancel_clordid, count_digits(cancel_clordid) ); me->services->queue_fix(*me->session_services, jetfix::MessageTypes::Cancel, @@ -1034,16 +1036,18 @@ void ICENativeOrder::report_state() me->services->report_state(me->state); } -int ICENativeOrder::get_clordid(uint32_t bts_id) +void ICENativeOrder::setup_ids(const std::string& sender_compid) { - std::string orderid = std::to_string(bts_id); - int totalsz = me->sender_compid.size() + orderid.size(); - if(totalsz < 9) //9 is min size - orderid = std::string(9-totalsz, '0').append(orderid); - else if(totalsz > 12) //12 is max - orderid = orderid.substr(totalsz-12, orderid.size() - (totalsz-12)); - std::string combined_clorid = me->sender_compid+ orderid; - int id = std::atoi(combined_clorid.c_str()); + me->sender_compid = std::atoi(sender_compid.c_str()); + int digs = count_digits(me->sender_compid); + me->id_scale = md::pow10(12-digs); + me->update_scale = 8 * md::pow10(11-digs); + me->cancel_scale = 9 * md::pow10(11-digs); +} + +int64_t ICENativeOrder::get_clordid(uint32_t bts_id) +{ + int64_t id = me->id_scale * me->sender_compid + (bts_id % me->id_scale); return id; } @@ -1064,7 +1068,7 @@ void ICENativeOrder::init_fix() std::string memo(memo_sz(), ' '); encode_memo(key(), SecurityKey(sec.id()), &memo); - int combined_clorid = get_clordid(key().value()); + auto combined_clorid = get_clordid(key().value()); // initialize create FIX auto &create_encoding = me->create_encoding; @@ -1107,7 +1111,7 @@ void ICENativeOrder::init_fix() update_encoding.add(def.side()); me->update_transact_time_bm = update_encoding.add(datetime::FIX_timestamp(datetime::now())); me->update_quantity_bm = update_encoding.add(0, quantity_sz()); - me->update_clordid_bm = update_encoding.add(0, ice_orderid_sz()); + me->update_clordid_bm = update_encoding.add(combined_clorid, count_digits(combined_clorid)); update_encoding.add(def.time_in_force()); if(rr.account() != "") update_encoding.add(rr.account()); @@ -1128,7 +1132,7 @@ void ICENativeOrder::init_fix() cancel_encoding.add(rr.authorized_trader()); cancel_encoding.add(sec.ice().exchange_id()); cancel_encoding.add(def.side()); - me->cancel_clordid_bm = cancel_encoding.add(0, ice_orderid_sz()); + me->cancel_clordid_bm = cancel_encoding.add(combined_clorid, count_digits(combined_clorid)); // special tags switch (def.custom_def().custom_case()) diff --git a/src/lib/orderman/orders/ICENativeOrder.h b/src/lib/orderman/orders/ICENativeOrder.h index 093c4468c..e9ec652a9 100644 --- a/src/lib/orderman/orders/ICENativeOrder.h +++ b/src/lib/orderman/orders/ICENativeOrder.h @@ -67,7 +67,8 @@ class ICENativeOrder : public NativeOrder { api::orderman::NativeOrderState & get_native_state(api::orderman::OrderState &); - int get_clordid(uint32_t); + void setup_ids(const std::string& sender_compid); + int64_t get_clordid(uint32_t); }; } // namespace orderman