From 5601ad91f5767ba090bdb08b9b760524ac3b3992 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Mon, 2 Dec 2024 13:08:47 +0100 Subject: [PATCH] a bunch of allocation optimizations --- solanaceae/ngc_ext/ngcext.cpp | 1 + solanaceae/ngc_ft1/flow_only.cpp | 1 + solanaceae/ngc_ft1/ngcft1.cpp | 1 + solanaceae/ngc_ft1_sha1/ft1_sha1_info.cpp | 1 + solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp | 10 +++++----- solanaceae/ngc_ft1_sha1/sha1_ngcft1.hpp | 3 +++ 6 files changed, 12 insertions(+), 5 deletions(-) diff --git a/solanaceae/ngc_ext/ngcext.cpp b/solanaceae/ngc_ext/ngcext.cpp index 8fc6064..aa1d718 100644 --- a/solanaceae/ngc_ext/ngcext.cpp +++ b/solanaceae/ngc_ext/ngcext.cpp @@ -255,6 +255,7 @@ bool NGCEXTEventProvider::parse_ft1_data_ack( _DATA_HAVE(sizeof(e.transfer_id), std::cerr << "NGCEXT: packet too small, missing transfer_id\n"; return false) e.transfer_id = data[curser++]; + e.sequence_ids.reserve(std::max(data_size-curser, 1)/sizeof(uint16_t)); while (curser < data_size) { _DATA_HAVE(sizeof(uint16_t), std::cerr << "NGCEXT: packet too small, missing seq_id\n"; return false) uint16_t seq_id = data[curser++]; diff --git a/solanaceae/ngc_ft1/flow_only.cpp b/solanaceae/ngc_ft1/flow_only.cpp index c7247b8..01e8abf 100644 --- a/solanaceae/ngc_ft1/flow_only.cpp +++ b/solanaceae/ngc_ft1/flow_only.cpp @@ -86,6 +86,7 @@ int64_t FlowOnly::canSend(float time_delta) { std::vector FlowOnly::getTimeouts(void) const { std::vector list; + list.reserve(_in_flight.size()/3); // we dont know, so we just guess // after 3 rtt delay, we trigger timeout const auto now_adjusted = getTimeNow() - getCurrentDelay()*3.f; diff --git a/solanaceae/ngc_ft1/ngcft1.cpp b/solanaceae/ngc_ft1/ngcft1.cpp index d51b383..331cf2e 100644 --- a/solanaceae/ngc_ft1/ngcft1.cpp +++ b/solanaceae/ngc_ft1/ngcft1.cpp @@ -672,6 +672,7 @@ bool NGCFT1::onEvent(const Events::NGCEXT_ft1_data_ack& e) { { std::vector seqs; + seqs.reserve(e.sequence_ids.size()); for (const auto it : e.sequence_ids) { // TODO: improve this o.o seqs.push_back({e.transfer_id, it}); diff --git a/solanaceae/ngc_ft1_sha1/ft1_sha1_info.cpp b/solanaceae/ngc_ft1_sha1/ft1_sha1_info.cpp index 885ca82..8bfe468 100644 --- a/solanaceae/ngc_ft1_sha1/ft1_sha1_info.cpp +++ b/solanaceae/ngc_ft1_sha1/ft1_sha1_info.cpp @@ -63,6 +63,7 @@ size_t FT1InfoSHA1::chunkSize(size_t chunk_index) const { std::vector FT1InfoSHA1::toBuffer(void) const { std::vector buffer; + buffer.reserve(256+8+4+20*chunks.size()); assert(!file_name.empty()); // TODO: optimize diff --git a/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp b/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp index 457efa5..e5f213e 100644 --- a/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp +++ b/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp @@ -250,7 +250,7 @@ float SHA1_NGCFT1::iterate(float delta) { //std::cerr << "---------- new tick ----------\n"; _mfb.tick(); // does not need to be called as often, once every sec would be enough, but the pointer deref + atomic bool should be very fast - entt::dense_map peer_open_requests; + _peer_open_requests.clear(); { // timers // sending transfers @@ -299,7 +299,7 @@ float SHA1_NGCFT1::iterate(float delta) { } } { // requested chunk timers - _os.registry().view().each([delta, &peer_open_requests](Components::FT1ChunkSHA1Requested& ftchunk_requested) { + _os.registry().view().each([this, delta](Components::FT1ChunkSHA1Requested& ftchunk_requested) { for (auto it = ftchunk_requested.chunks.begin(); it != ftchunk_requested.chunks.end();) { it->second.timer += delta; @@ -307,7 +307,7 @@ float SHA1_NGCFT1::iterate(float delta) { if (it->second.timer >= 60.f) { it = ftchunk_requested.chunks.erase(it); } else { - peer_open_requests[it->second.c] += 1; + _peer_open_requests[it->second.c] += 1; it++; } } @@ -447,7 +447,7 @@ float SHA1_NGCFT1::iterate(float delta) { Systems::chunk_picker_updates( _cr, _os.registry(), - peer_open_requests, + _peer_open_requests, _receiving_transfers, _nft, delta @@ -456,7 +456,7 @@ float SHA1_NGCFT1::iterate(float delta) { // transfer statistics systems Systems::transfer_tally_update(_os.registry(), getTimeNow()); - if (peer_open_requests.empty()) { + if (_peer_open_requests.empty()) { return 2.f; } else { // pretty conservative and should be ajusted on a per peer, per delay basis diff --git a/solanaceae/ngc_ft1_sha1/sha1_ngcft1.hpp b/solanaceae/ngc_ft1_sha1/sha1_ngcft1.hpp index 5ccc28e..badbae9 100644 --- a/solanaceae/ngc_ft1_sha1/sha1_ngcft1.hpp +++ b/solanaceae/ngc_ft1_sha1/sha1_ngcft1.hpp @@ -74,6 +74,9 @@ class SHA1_NGCFT1 : public ToxEventI, public RegistryMessageModelEventI, public // only used to remove participation on peer exit entt::dense_map _tox_peer_to_contact; + // reset every iterate; kept here as an allocation optimization + entt::dense_map _peer_open_requests; + void updateMessages(ObjectHandle ce); std::optional> selectPeerForRequest(ObjectHandle ce);