From f715618eee00843441ea723f52165c6fa4ffd6f7 Mon Sep 17 00:00:00 2001 From: Victor Boivie Date: Tue, 6 Jul 2021 23:14:51 +0200 Subject: [PATCH] Use flat_map in RTCPReceiver RTCPReceiver initially used a std::map, which made RTCPReceiver::IncomingPacket's use of std::map represent ~0.45% CPU in highly loaded media servers. Using std::unordered_map in change 216321 reduced it only slightly, to 0.39%. This is the second attempt to reduce it even further. By using a flat_map and taking advantage of the increased cache locality, the hope is that it will be reduced. These maps generally have low cardinality (indexed by SSRC), and are looked up often, but modified less often, which make them a potential candidate for flat_map. Bug: webrtc:12689 Change-Id: I6733ccf3484d1c54e661250fb6712971b80fa2a9 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/225203 Commit-Queue: Victor Boivie Reviewed-by: Danil Chapovalov Cr-Commit-Position: refs/heads/master@{#34432} --- modules/rtp_rtcp/BUILD.gn | 1 + modules/rtp_rtcp/source/rtcp_receiver.cc | 11 +++-------- modules/rtp_rtcp/source/rtcp_receiver.h | 18 ++++++------------ 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/modules/rtp_rtcp/BUILD.gn b/modules/rtp_rtcp/BUILD.gn index 3e25d18d84..778baf6e15 100644 --- a/modules/rtp_rtcp/BUILD.gn +++ b/modules/rtp_rtcp/BUILD.gn @@ -296,6 +296,7 @@ rtc_library("rtp_rtcp") { "../../rtc_base:rtc_base_approved", "../../rtc_base:rtc_numerics", "../../rtc_base:safe_minmax", + "../../rtc_base/containers:flat_map", "../../rtc_base/experiments:field_trial_parser", "../../rtc_base/synchronization:mutex", "../../rtc_base/system:no_unique_address", diff --git a/modules/rtp_rtcp/source/rtcp_receiver.cc b/modules/rtp_rtcp/source/rtcp_receiver.cc index ca61ba6359..3ab78df17c 100644 --- a/modules/rtp_rtcp/source/rtcp_receiver.cc +++ b/modules/rtp_rtcp/source/rtcp_receiver.cc @@ -809,14 +809,9 @@ void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) { // Clear our lists. rtts_.erase(bye.sender_ssrc()); - for (auto it = received_report_blocks_.begin(); - it != received_report_blocks_.end();) { - if (it->second.report_block().sender_ssrc == bye.sender_ssrc()) { - received_report_blocks_.erase(it++); - } else { - ++it; - } - } + EraseIf(received_report_blocks_, [&](const auto& elem) { + return elem.second.report_block().sender_ssrc == bye.sender_ssrc(); + }); TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc()); if (tmmbr_info) diff --git a/modules/rtp_rtcp/source/rtcp_receiver.h b/modules/rtp_rtcp/source/rtcp_receiver.h index 62c6e01417..fa9f367c9e 100644 --- a/modules/rtp_rtcp/source/rtcp_receiver.h +++ b/modules/rtp_rtcp/source/rtcp_receiver.h @@ -13,9 +13,7 @@ #include #include -#include #include -#include #include #include "api/array_view.h" @@ -27,6 +25,7 @@ #include "modules/rtp_rtcp/source/rtcp_packet/dlrr.h" #include "modules/rtp_rtcp/source/rtcp_packet/tmmb_item.h" #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h" +#include "rtc_base/containers/flat_map.h" #include "rtc_base/synchronization/mutex.h" #include "rtc_base/system/no_unique_address.h" #include "rtc_base/thread_annotations.h" @@ -368,7 +367,7 @@ class RTCPReceiver final { std::list received_rrtrs_ RTC_GUARDED_BY(rtcp_receiver_lock_); // Received RRTR information mapped by remote ssrc. - std::unordered_map::iterator> + flat_map::iterator> received_rrtrs_ssrc_it_ RTC_GUARDED_BY(rtcp_receiver_lock_); // Estimated rtt, zero when there is no valid estimate. @@ -377,21 +376,16 @@ class RTCPReceiver final { int64_t oldest_tmmbr_info_ms_ RTC_GUARDED_BY(rtcp_receiver_lock_); // Mapped by remote ssrc. - std::unordered_map tmmbr_infos_ + flat_map tmmbr_infos_ RTC_GUARDED_BY(rtcp_receiver_lock_); // Round-Trip Time per remote sender ssrc. - std::unordered_map rtts_ - RTC_GUARDED_BY(rtcp_receiver_lock_); - - // TODO(boivie): `received_report_blocks_` should be converted - // to std::unordered_map, but as there are too many tests that assume a - // specific order, it's not easily done. + flat_map rtts_ RTC_GUARDED_BY(rtcp_receiver_lock_); // Report blocks per local source ssrc. - std::map received_report_blocks_ + flat_map received_report_blocks_ RTC_GUARDED_BY(rtcp_receiver_lock_); - std::unordered_map last_fir_ + flat_map last_fir_ RTC_GUARDED_BY(rtcp_receiver_lock_); // The last time we received an RTCP Report block for this module.