cuprate_database::resize

Function percent

Source
pub fn percent(current_size_bytes: usize, percent: f32) -> NonZeroUsize
Expand description

Memory map resize by a percentage.

§Method

This function will multiply current_size_bytes by percent.

Any input <= 1.0 or non-normal float (f32::NAN, f32::INFINITY) will make the returning NonZeroUsize the same as current_size_bytes (rounded up to the OS page size).

let page_size: usize = PAGE_SIZE.get();

// Anything below the page size will round up to the page size.
for i in 0..=page_size {
    assert_eq!(percent(i, 1.0).get(), page_size);
}

// Same for 2 page sizes.
for i in (page_size + 1)..=(page_size * 2) {
    assert_eq!(percent(i, 1.0).get(), page_size * 2);
}

// Weird floats do nothing.
assert_eq!(percent(page_size, f32::NAN).get(), page_size);
assert_eq!(percent(page_size, f32::INFINITY).get(), page_size);
assert_eq!(percent(page_size, f32::NEG_INFINITY).get(), page_size);
assert_eq!(percent(page_size, -1.0).get(), page_size);
assert_eq!(percent(page_size, 0.999).get(), page_size);

§Panics

This function will panic if current_size_bytes * percent is closer to usize::MAX than the OS page size.

// Ridiculous large numbers panic.
percent(usize::MAX, 1.001);