From 797ba111ec2cdab77b905bc95098ae2daecb4348 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 23 Sep 2024 16:53:54 +0000 Subject: [PATCH] fix: Emplace MDConsumerHandle instead of copy assign --- .../position/PositionServicePNLHelper.cpp | 21 +++++++++++--- src/lib_spark/spark/md/MBOServiceServer.cpp | 28 +++++++++++++------ 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/app_sparkd/sparkd/position/PositionServicePNLHelper.cpp b/src/app_sparkd/sparkd/position/PositionServicePNLHelper.cpp index e9c0bacd1..dedbbc216 100644 --- a/src/app_sparkd/sparkd/position/PositionServicePNLHelper.cpp +++ b/src/app_sparkd/sparkd/position/PositionServicePNLHelper.cpp @@ -114,11 +114,24 @@ class HandleIndex { LOG(md, Diag, "PositionService::HandleIndex::on_security security:{}", bts::securities::descriptor(*sec)); - if (_consumer->id() == id) { - index.lock([handle](auto &index) { - index[SecurityKey(handle.key())] = handle; - }); + if (_consumer->id() != id) { + return; } + index.lock( + [handle](std::unordered_map + &index) { + SecurityKey sec_key{handle.key()}; + if (index.find(sec_key) != index.end()) { + return; + } + auto [_, is_new] = + index.try_emplace(sec_key, std::move(handle)); + if (is_new) { + LOG(md, Info, + "MBOServiceServer added synthetic security: {}", + *sec_key); + } + }); }); } } diff --git a/src/lib_spark/spark/md/MBOServiceServer.cpp b/src/lib_spark/spark/md/MBOServiceServer.cpp index 226144587..8cd40c416 100644 --- a/src/lib_spark/spark/md/MBOServiceServer.cpp +++ b/src/lib_spark/spark/md/MBOServiceServer.cpp @@ -55,14 +55,26 @@ class HandleIndex { } // Subscribe to on_security auto id = _consumer->id(); - _consumer->on_security( - [this, id](const SecurityPtr &, MDConsumerHandle handle) { - if (_consumer->id() == id) { - index.lock([handle](auto &index) { - index[SecurityKey(handle.key())] = handle; - }); - } - }); + _consumer->on_security([this, id]( + const SecurityPtr &, + MDConsumerHandle handle) { + if (_consumer->id() != id) { + return; + } + index.lock( + [handle]( + std::unordered_map &index) { + SecurityKey sec_key{handle.key()}; + if (index.find(sec_key) != index.end()) { + return; + } + auto [_, is_new] = + index.try_emplace(sec_key, std::move(handle)); + if (is_new) { + LOG(md, Info, "MBOServiceServer added security: {}", *sec_key); + } + }); + }); } } };