From 4783b3dde93d43b5b120a8088af873aa5661202f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Thu, 3 Mar 2022 18:43:05 +0100 Subject: [PATCH] RateCalculator reset optimization (#785) * Replace buffer with `std::vector` and simplify its reset * cosmetic * Fix Rust dependencies by switching to release versions Co-authored-by: Nazar Mokrynskyi --- rust/Cargo.toml | 6 +++--- worker/include/RTC/RateCalculator.hpp | 9 +++++---- worker/src/RTC/RateCalculator.cpp | 10 +++++----- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/rust/Cargo.toml b/rust/Cargo.toml index dfe24df98a..8b8e3291d5 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -65,8 +65,8 @@ features = ["serde", "v4"] version = "0.8.2" [dev-dependencies] -actix = "0.12.0" -actix-web-actors = "4.0.0-beta.11" +actix = "0.13.0" +actix-web-actors = "4.1.0" async-io = "1.6.0" criterion = "0.3.5" env_logger = "0.9.0" @@ -74,7 +74,7 @@ env_logger = "0.9.0" [dev-dependencies.actix-web] default-features = false features = ["macros"] -version = "4.0.0-rc.3" +version = "4.0.1" [[bench]] name = "direct_data" diff --git a/worker/include/RTC/RateCalculator.hpp b/worker/include/RTC/RateCalculator.hpp index 7792345982..d4e974e740 100644 --- a/worker/include/RTC/RateCalculator.hpp +++ b/worker/include/RTC/RateCalculator.hpp @@ -24,8 +24,7 @@ namespace RTC : windowSizeMs(windowSizeMs), scale(scale), windowItems(windowItems) { this->itemSizeMs = std::max(windowSizeMs / windowItems, static_cast(1)); - - Reset(); + this->buffer.resize(windowItems); } void Update(size_t size, uint64_t nowMs); uint32_t GetRate(uint64_t nowMs); @@ -38,7 +37,9 @@ namespace RTC void RemoveOldData(uint64_t nowMs); void Reset() { - this->buffer.reset(new BufferItem[this->windowItems]); + std::memset( + static_cast(&this->buffer.front()), 0, sizeof(BufferItem) * this->buffer.size()); + this->newestItemStartTime = 0u; this->newestItemIndex = -1; this->oldestItemStartTime = 0u; @@ -65,7 +66,7 @@ namespace RTC // Item Size (in milliseconds), calculated as: windowSizeMs / windowItems. size_t itemSizeMs{ 0u }; // Buffer to keep data. - std::unique_ptr buffer; + std::vector buffer; // Time (in milliseconds) for last item in the time window. uint64_t newestItemStartTime{ 0u }; // Index for the last item in the time window. diff --git a/worker/src/RTC/RateCalculator.cpp b/worker/src/RTC/RateCalculator.cpp index 067ede99a0..997a93b1c3 100644 --- a/worker/src/RTC/RateCalculator.cpp +++ b/worker/src/RTC/RateCalculator.cpp @@ -38,7 +38,7 @@ namespace RTC this->windowSizeMs, this->windowItems); - BufferItem& oldestItem = buffer[this->oldestItemIndex]; + BufferItem& oldestItem = this->buffer[this->oldestItemIndex]; this->totalCount -= oldestItem.count; oldestItem.count = 0u; oldestItem.time = 0u; @@ -47,14 +47,14 @@ namespace RTC } // Set the newest item. - BufferItem& item = buffer[this->newestItemIndex]; + BufferItem& item = this->buffer[this->newestItemIndex]; item.count = size; item.time = nowMs; } else { // Update the newest item. - BufferItem& item = buffer[this->newestItemIndex]; + BufferItem& item = this->buffer[this->newestItemIndex]; item.count += size; } @@ -114,7 +114,7 @@ namespace RTC while (this->oldestItemStartTime < newoldestTime) { - BufferItem& oldestItem = buffer[this->oldestItemIndex]; + BufferItem& oldestItem = this->buffer[this->oldestItemIndex]; this->totalCount -= oldestItem.count; oldestItem.count = 0u; oldestItem.time = 0u; @@ -122,7 +122,7 @@ namespace RTC if (++this->oldestItemIndex >= this->windowItems) this->oldestItemIndex = 0; - const BufferItem& newOldestItem = buffer[this->oldestItemIndex]; + const BufferItem& newOldestItem = this->buffer[this->oldestItemIndex]; this->oldestItemStartTime = newOldestItem.time; } }