Cleanup: compute AverageReportedPacketLossRatio only once when a new observation is added.

Prior to this change, we unnecessarily re-compute AverageReportedPacketLossRatio a few times for the same observation.

Bug: webrtc:12707
Change-Id: I5de3d06bf3a32a39638a6f72baf7ec12144c6399
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/366583
Reviewed-by: Per Kjellander <perkj@webrtc.org>
Commit-Queue: Diep Bui <diepbp@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#43298}
diff --git a/modules/congestion_controller/goog_cc/loss_based_bwe_v2.cc b/modules/congestion_controller/goog_cc/loss_based_bwe_v2.cc
index 559f23a..f7852fd 100644
--- a/modules/congestion_controller/goog_cc/loss_based_bwe_v2.cc
+++ b/modules/congestion_controller/goog_cc/loss_based_bwe_v2.cc
@@ -234,7 +234,7 @@
 
   // Do not increase the estimate if the average loss is greater than current
   // inherent loss.
-  if (GetAverageReportedLossRatio() > best_candidate.inherent_loss &&
+  if (average_reported_loss_ratio_ > best_candidate.inherent_loss &&
       config_->not_increase_if_inherent_loss_less_than_average_loss &&
       current_best_estimate_.loss_limited_bandwidth <
           best_candidate.loss_limited_bandwidth) {
@@ -300,7 +300,7 @@
     RTC_LOG(LS_INFO) << "Resetting loss based BWE to "
                      << bounded_bandwidth_estimate.kbps()
                      << "due to loss. Avg loss rate: "
-                     << GetAverageReportedLossRatio();
+                     << average_reported_loss_ratio_;
     current_best_estimate_.loss_limited_bandwidth = bounded_bandwidth_estimate;
     current_best_estimate_.inherent_loss = 0;
   } else {
@@ -775,12 +775,13 @@
   return valid;
 }
 
-double LossBasedBweV2::GetAverageReportedLossRatio() const {
-  return config_->use_byte_loss_rate ? GetAverageReportedByteLossRatio()
-                                     : GetAverageReportedPacketLossRatio();
+void LossBasedBweV2::UpdateAverageReportedLossRatio() {
+  average_reported_loss_ratio_ =
+      (config_->use_byte_loss_rate ? CalculateAverageReportedByteLossRatio()
+                                   : CalculateAverageReportedPacketLossRatio());
 }
 
-double LossBasedBweV2::GetAverageReportedPacketLossRatio() const {
+double LossBasedBweV2::CalculateAverageReportedPacketLossRatio() const {
   if (num_observations_ <= 0) {
     return 0.0;
   }
@@ -802,7 +803,7 @@
   return num_lost_packets / num_packets;
 }
 
-double LossBasedBweV2::GetAverageReportedByteLossRatio() const {
+double LossBasedBweV2::CalculateAverageReportedByteLossRatio() const {
   if (num_observations_ <= 0) {
     return 0.0;
   }
@@ -999,11 +1000,10 @@
 
 double LossBasedBweV2::GetHighBandwidthBias(DataRate bandwidth) const {
   if (IsValid(bandwidth)) {
-    const double average_reported_loss_ratio = GetAverageReportedLossRatio();
-    return AdjustBiasFactor(average_reported_loss_ratio,
+    return AdjustBiasFactor(average_reported_loss_ratio_,
                             config_->higher_bandwidth_bias_factor) *
                bandwidth.kbps() +
-           AdjustBiasFactor(average_reported_loss_ratio,
+           AdjustBiasFactor(average_reported_loss_ratio_,
                             config_->higher_log_bandwidth_bias_factor) *
                std::log(1.0 + bandwidth.kbps());
   }
@@ -1075,10 +1075,9 @@
 
 void LossBasedBweV2::CalculateInstantUpperBound() {
   DataRate instant_limit = max_bitrate_;
-  const double average_reported_loss_ratio = GetAverageReportedLossRatio();
-  if (average_reported_loss_ratio > config_->instant_upper_bound_loss_offset) {
+  if (average_reported_loss_ratio_ > config_->instant_upper_bound_loss_offset) {
     instant_limit = config_->instant_upper_bound_bandwidth_balance /
-                    (average_reported_loss_ratio -
+                    (average_reported_loss_ratio_ -
                      config_->instant_upper_bound_loss_offset);
   }
 
@@ -1179,7 +1178,7 @@
       observation;
 
   partial_observation_ = PartialObservation();
-
+  UpdateAverageReportedLossRatio();
   CalculateInstantUpperBound();
   return true;
 }
diff --git a/modules/congestion_controller/goog_cc/loss_based_bwe_v2.h b/modules/congestion_controller/goog_cc/loss_based_bwe_v2.h
index 14e9d3d..3e543f2 100644
--- a/modules/congestion_controller/goog_cc/loss_based_bwe_v2.h
+++ b/modules/congestion_controller/goog_cc/loss_based_bwe_v2.h
@@ -169,12 +169,12 @@
   bool IsConfigValid() const;
 
   // Returns `0.0` if not enough loss statistics have been received.
-  double GetAverageReportedLossRatio() const;
-  double GetAverageReportedPacketLossRatio() const;
+  void UpdateAverageReportedLossRatio();
+  double CalculateAverageReportedPacketLossRatio() const;
   // Calculates the average loss ratio over the last `observation_window_size`
   // observations but skips the observation with min and max loss ratio in order
   // to filter out loss spikes.
-  double GetAverageReportedByteLossRatio() const;
+  double CalculateAverageReportedByteLossRatio() const;
   std::vector<ChannelParameters> GetCandidates(bool in_alr) const;
   DataRate GetCandidateBandwidthUpperBound() const;
   Derivatives GetDerivatives(const ChannelParameters& channel_parameters) const;
@@ -220,6 +220,7 @@
   LossBasedBweV2::Result loss_based_result_ = LossBasedBweV2::Result();
   HoldInfo last_hold_info_ = HoldInfo();
   PaddingInfo last_padding_info_ = PaddingInfo();
+  double average_reported_loss_ratio_ = 0.0;
 };
 
 }  // namespace webrtc