Use u32 for block allocation thresholds

An u32 is enough to represent 128 TB worth of Immix blocks, and means a
GenerationConfig only takes up 8 bytes of space, instead of 16 bytes.
parent e5b1919f
......@@ -24,9 +24,9 @@ macro_rules! set_from_env {
}};
}
const DEFAULT_YOUNG_THRESHOLD: usize = (8 * 1024 * 1024) / BLOCK_SIZE;
const DEFAULT_MATURE_THRESHOLD: usize = (16 * 1024 * 1024) / BLOCK_SIZE;
const DEFAULT_MAILBOX_THRESHOLD: usize = 1;
const DEFAULT_YOUNG_THRESHOLD: u32 = (8 * 1024 * 1024) / (BLOCK_SIZE as u32);
const DEFAULT_MATURE_THRESHOLD: u32 = (16 * 1024 * 1024) / (BLOCK_SIZE as u32);
const DEFAULT_MAILBOX_THRESHOLD: u32 = 1;
const DEFAULT_GROWTH_FACTOR: f64 = 1.5;
const DEFAULT_GROWTH_THRESHOLD: f64 = 0.9;
const DEFAULT_SUSPENSION_CHECK_INTERVAL: f64 = 0.1;
......@@ -63,11 +63,11 @@ pub struct Config {
/// The number of memory blocks that can be allocated before triggering a
/// young collection.
pub young_threshold: usize,
pub young_threshold: u32,
/// The number of memory blocks that can be allocated before triggering a
/// mature collection.
pub mature_threshold: usize,
pub mature_threshold: u32,
/// The block allocation growth factor for the heap.
pub heap_growth_factor: f64,
......@@ -78,7 +78,7 @@ pub struct Config {
/// The number of memory blocks that can be allocated before triggering a
/// mailbox collection.
pub mailbox_threshold: usize,
pub mailbox_threshold: u32,
/// The block allocation growth factor for the mailbox heap.
pub mailbox_growth_factor: f64,
......@@ -128,8 +128,8 @@ impl Config {
f64
);
set_from_env!(self, young_threshold, "YOUNG_THRESHOLD", usize);
set_from_env!(self, mature_threshold, "MATURE_THRESHOLD", usize);
set_from_env!(self, young_threshold, "YOUNG_THRESHOLD", u32);
set_from_env!(self, mature_threshold, "MATURE_THRESHOLD", u32);
set_from_env!(self, heap_growth_factor, "HEAP_GROWTH_FACTOR", f64);
set_from_env!(
......@@ -139,7 +139,7 @@ impl Config {
f64
);
set_from_env!(self, mailbox_threshold, "MAILBOX_THRESHOLD", usize);
set_from_env!(self, mailbox_threshold, "MAILBOX_THRESHOLD", u32);
set_from_env!(
self,
......
......@@ -89,8 +89,12 @@ impl Bucket {
self.age += 1;
}
pub fn number_of_blocks(&self) -> usize {
self.blocks.len()
pub fn number_of_blocks(&self) -> u32 {
// The maximum value of u32 is 4 294 967 295. With every block being 32
// KB in size, this means we have an upper limit of 128 TB per process.
// That seems more than enough, and allows us to more efficiently store
// this number compared to using an u64/usize.
self.blocks.len() as u32
}
pub fn current_block(&self) -> Option<DerefPointer<Block>> {
......
......@@ -3,15 +3,15 @@
pub struct GenerationConfig {
/// The maximum number of blocks that can be allocated before triggering a
/// garbage collection.
pub threshold: usize,
pub threshold: u32,
/// The number of blocks that have been allocated since the last garbage
/// collection.
pub block_allocations: usize,
pub block_allocations: u32,
}
impl GenerationConfig {
pub fn new(threshold: usize) -> Self {
pub fn new(threshold: u32) -> Self {
GenerationConfig {
threshold,
block_allocations: 0,
......@@ -23,17 +23,17 @@ impl GenerationConfig {
/// The `blocks` argument should specify the current number of live blocks.
pub fn should_increase_threshold(
&self,
blocks: usize,
blocks: u32,
growth_threshold: f64,
) -> bool {
let percentage = blocks as f64 / self.threshold as f64;
let percentage = f64::from(blocks) / f64::from(self.threshold);
percentage >= growth_threshold
}
pub fn increment_threshold(&mut self, growth_factor: f64) {
self.threshold =
(self.threshold as f64 * growth_factor).ceil() as usize;
(f64::from(self.threshold) * growth_factor).ceil() as u32;
}
pub fn allocation_threshold_exceeded(&self) -> bool {
......
......@@ -218,7 +218,7 @@ impl LocalAllocator {
}
}
pub fn number_of_young_blocks(&self) -> usize {
pub fn number_of_young_blocks(&self) -> u32 {
self.young_generation
.iter()
.map(|bucket| bucket.number_of_blocks())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment