Skip to content

Commit

Permalink
RateCalculator reset optimization (#785)
Browse files Browse the repository at this point in the history
* Replace buffer with `std::vector` and simplify its reset

* cosmetic

* Fix Rust dependencies by switching to release versions

Co-authored-by: Nazar Mokrynskyi <[email protected]>
  • Loading branch information
jmillan and nazar-pc authored Mar 3, 2022
1 parent ff17d13 commit 4783b3d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
6 changes: 3 additions & 3 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ 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"

[dev-dependencies.actix-web]
default-features = false
features = ["macros"]
version = "4.0.0-rc.3"
version = "4.0.1"

[[bench]]
name = "direct_data"
Expand Down
9 changes: 5 additions & 4 deletions worker/include/RTC/RateCalculator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ namespace RTC
: windowSizeMs(windowSizeMs), scale(scale), windowItems(windowItems)
{
this->itemSizeMs = std::max(windowSizeMs / windowItems, static_cast<size_t>(1));

Reset();
this->buffer.resize(windowItems);
}
void Update(size_t size, uint64_t nowMs);
uint32_t GetRate(uint64_t nowMs);
Expand All @@ -38,7 +37,9 @@ namespace RTC
void RemoveOldData(uint64_t nowMs);
void Reset()
{
this->buffer.reset(new BufferItem[this->windowItems]);
std::memset(
static_cast<void*>(&this->buffer.front()), 0, sizeof(BufferItem) * this->buffer.size());

this->newestItemStartTime = 0u;
this->newestItemIndex = -1;
this->oldestItemStartTime = 0u;
Expand All @@ -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<BufferItem[]> buffer;
std::vector<BufferItem> buffer;
// Time (in milliseconds) for last item in the time window.
uint64_t newestItemStartTime{ 0u };
// Index for the last item in the time window.
Expand Down
10 changes: 5 additions & 5 deletions worker/src/RTC/RateCalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -114,15 +114,15 @@ 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;

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;
}
}
Expand Down

0 comments on commit 4783b3d

Please sign in to comment.