Skip to content

Commit acd2faa

Browse files
committed
cache_weight: Fix BigDecimal impl and add test
1 parent 4b7139a commit acd2faa

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

graph/src/util/cache_weight.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl CacheWeight for String {
5555

5656
impl CacheWeight for BigDecimal {
5757
fn indirect_weight(&self) -> usize {
58-
(self.digits() as f32).log2() as usize
58+
((self.digits() as f32 * std::f32::consts::LOG2_10) / 8.0).ceil() as usize
5959
}
6060
}
6161

@@ -96,3 +96,12 @@ impl CacheWeight for graphql_parser::query::Value {
9696
}
9797
}
9898
}
99+
100+
#[test]
101+
fn big_decimal_cache_weight() {
102+
use std::str::FromStr;
103+
104+
// 22.4548 has 18 bits as binary, so 3 bytes.
105+
let n = BigDecimal::from_str("22.454800000000").unwrap();
106+
assert_eq!(n.indirect_weight(), 3);
107+
}

0 commit comments

Comments
 (0)