From: Steinar H. Gunderson Date: Thu, 29 Jun 2017 20:53:24 +0000 (+0200) Subject: Only report disk space anew every second. Saves 20% CPU or so on the UI thread (!). X-Git-Tag: 1.6.1~16 X-Git-Url: https://git.sesse.net/?p=nageru;a=commitdiff_plain;h=cfff356a1739a0ed600c34e2de7eb64150ddf94d Only report disk space anew every second. Saves 20% CPU or so on the UI thread (!). --- diff --git a/disk_space_estimator.cpp b/disk_space_estimator.cpp index a196105..86e5e87 100644 --- a/disk_space_estimator.cpp +++ b/disk_space_estimator.cpp @@ -50,7 +50,12 @@ void DiskSpaceEstimator::report_write(const std::string &filename, uint64_t pts) double bytes_per_second = double(st.st_size - measure_points.front().size) / (pts - measure_points.front().pts) * TIMEBASE; double seconds_left = free_bytes / bytes_per_second; - callback(free_bytes, seconds_left); + + // Only report every second, since updating the UI can be expensive. + if (last_pts_reported == 0 || pts - last_pts_reported >= TIMEBASE) { + callback(free_bytes, seconds_left); + last_pts_reported = pts; + } } measure_points.push_back({ pts, st.st_size }); diff --git a/disk_space_estimator.h b/disk_space_estimator.h index c5b63bf..73b392c 100644 --- a/disk_space_estimator.h +++ b/disk_space_estimator.h @@ -44,6 +44,7 @@ private: off_t size; }; std::deque measure_points; + uint64_t last_pts_reported = 0; // Metrics. std::atomic metric_disk_free_bytes{-1};