perf: New size hint strategy
This commit is contained in:
parent
f236a98fed
commit
bbb16e0e14
|
@ -16,7 +16,8 @@ impl SizeHint {
|
||||||
/// Get the current value
|
/// Get the current value
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get(&self) -> usize {
|
pub fn get(&self) -> usize {
|
||||||
self.value.load(Ordering::Acquire)
|
let value = self.value.load(Ordering::Acquire);
|
||||||
|
value + value / 8 + 75
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update size hint based on given value.
|
/// Update size hint based on given value.
|
||||||
|
@ -24,10 +25,24 @@ impl SizeHint {
|
||||||
/// There is no guarantee that the value of get() after calling update() is same
|
/// There is no guarantee that the value of get() after calling update() is same
|
||||||
/// as the value passed on update()
|
/// as the value passed on update()
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn update(&self, mut value: usize) {
|
pub fn update(&self, value: usize) {
|
||||||
value = value + value / 8 + 75;
|
let mut old = self.value.load(Ordering::Acquire);
|
||||||
if unlikely!(self.get() < value) {
|
if old == 0 {
|
||||||
self.value.store(value, Ordering::Release);
|
old = value;
|
||||||
}
|
}
|
||||||
|
self.value
|
||||||
|
.store(old - old / 4 + value / 4, Ordering::Release);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_update() {
|
||||||
|
let hint = SizeHint::new();
|
||||||
|
|
||||||
|
for size in 1..=100 {
|
||||||
|
let cap = hint.get();
|
||||||
|
assert!(size <= cap);
|
||||||
|
assert!(cap <= size + size / 8 + 75);
|
||||||
|
hint.update(size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue