[yew-devel] [PATCH yew-comp 06/20] rrd: units: simplify calculations for get_grid_unit_base

Dominik Csapak d.csapak at proxmox.com
Fri May 30 14:21:48 CEST 2025


in get_grid_unit_base2, when resolving the mathematical calculations,
one can see that we always subtract 2 from the log2 to get to the
result. (with the exception of ranges smaller than 2 and very big
numbers where the floating point operation does not work properly
anymore in 64 bits, e.g. 2.0^1024 -> Infinity). This saves us the loop
and at least 2 calculations for the condition.

The second condition could never be reached, since doubling a number <
4.0 can never be > 15.0, so we can just remove it.

In get_grid_unit_base10, we can at least change the while loop to an if,
since that will only ever run once.

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 src/rrd/units.rs | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

diff --git a/src/rrd/units.rs b/src/rrd/units.rs
index ad72565..a15eb2d 100644
--- a/src/rrd/units.rs
+++ b/src/rrd/units.rs
@@ -10,7 +10,7 @@ pub(crate) fn get_grid_unit_base10(min: f64, max: f64) -> f64 {
     let mut l = range.log10() as i32;
 
     // the count can be between 1 and 10
-    while (range / 10.0_f64.powi(l)) < 2.0 {
+    if (range / 10.0_f64.powi(l)) < 2.0 {
         l -= 1;
     }
 
@@ -39,21 +39,13 @@ pub(crate) fn get_grid_unit_base2(min: f64, max: f64) -> f64 {
         panic!("get_grid_unit_base2: got zero or negative range - internal error");
     }
 
-    let mut l = range.log2() as i32;
+    let mut l = range.log2() as i32 - 2;
 
-    while (range / 2.0_f64.powi(l)) < 4.0 {
+    if range / 2.0_f64.powi(l) < 4.0 {
         l -= 1;
     }
 
-    let mut res = 2.0_f64.powi(l);
-
-    let count = range / res;
-
-    if count > 15.0 {
-        res *= 2.0;
-    }
-
-    res
+    2.0_f64.powi(l)
 }
 
 pub(crate) fn get_time_grid_unit(min: i64, max: i64) -> i64 {
-- 
2.39.5





More information about the yew-devel mailing list