Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make use of the last bit in Tid #107

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 76 additions & 10 deletions src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub trait Config: Sized {
pub(crate) trait CfgPrivate: Config {
const USED_BITS: usize = Generation::<Self>::LEN + Generation::<Self>::SHIFT;
const INITIAL_SZ: usize = next_pow2(Self::INITIAL_PAGE_SIZE);
const MAX_SHARDS: usize = next_pow2(Self::MAX_THREADS - 1);
const MAX_SHARDS: usize = next_pow2(Self::MAX_THREADS);
const ADDR_INDEX_SHIFT: usize = Self::INITIAL_SZ.trailing_zeros() as usize + 1;

fn page_size(n: usize) -> usize {
Expand Down Expand Up @@ -137,10 +137,10 @@ impl Config for DefaultConfig {
const INITIAL_PAGE_SIZE: usize = 32;

#[cfg(target_pointer_width = "64")]
const MAX_THREADS: usize = 4096;
const MAX_THREADS: usize = 8192;
#[cfg(target_pointer_width = "32")]
// TODO(eliza): can we find enough bits to give 32-bit platforms more threads?
const MAX_THREADS: usize = 128;
const MAX_THREADS: usize = 256;

const MAX_PAGES: usize = WIDTH / 2;
}
Expand Down Expand Up @@ -168,27 +168,93 @@ impl<C: Config> fmt::Debug for DebugConfig<C> {
#[cfg(test)]
mod tests {
use super::*;
use crate::page::slot::Lifecycle;
use crate::page::slot::LifecycleGen;
use crate::test_util;
use crate::Slab;
use crate::Tid;

#[cfg(target_pointer_width = "32")]
#[test]
fn default_config_stats() {
assert_eq!(DefaultConfig::MAX_PAGES, 16);
assert_eq!(DefaultConfig::MAX_SHARDS, 256);
assert_eq!(DefaultConfig::MAX_THREADS, 256);
assert_eq!(DefaultConfig::INITIAL_PAGE_SIZE, 32);
assert_eq!(DefaultConfig::INITIAL_SZ, 32);
assert_eq!(DefaultConfig::ADDR_INDEX_SHIFT, 6);
assert_eq!(DefaultConfig::USED_BITS, WIDTH);
assert_eq!(RefCount::<DefaultConfig>::LEN, 28);
assert_eq!(Lifecycle::<DefaultConfig>::LEN, 2);
assert_eq!(LifecycleGen::<DefaultConfig>::LEN, 2);
assert_eq!(Generation::<DefaultConfig>::LEN, 2);
assert_eq!(Tid::<DefaultConfig>::LEN, 8);
assert_eq!(Addr::<DefaultConfig>::LEN, 22);
}

#[cfg(target_pointer_width = "64")]
#[test]
fn default_config_stats() {
assert_eq!(DefaultConfig::MAX_PAGES, 32);
assert_eq!(DefaultConfig::MAX_SHARDS, 8192);
assert_eq!(DefaultConfig::MAX_THREADS, 8192);
assert_eq!(DefaultConfig::INITIAL_PAGE_SIZE, 32);
assert_eq!(DefaultConfig::INITIAL_SZ, 32);
assert_eq!(DefaultConfig::ADDR_INDEX_SHIFT, 6);
assert_eq!(DefaultConfig::USED_BITS, WIDTH);
assert_eq!(RefCount::<DefaultConfig>::LEN, 49);
assert_eq!(Lifecycle::<DefaultConfig>::LEN, 2);
assert_eq!(LifecycleGen::<DefaultConfig>::LEN, 13);
assert_eq!(Generation::<DefaultConfig>::LEN, 13);
assert_eq!(Tid::<DefaultConfig>::LEN, 13);
assert_eq!(Addr::<DefaultConfig>::LEN, 38);
}

struct GiantGenConfig;

impl Config for GiantGenConfig {
const INITIAL_PAGE_SIZE: usize = 1;
const MAX_THREADS: usize = 1;
const MAX_PAGES: usize = 1;
}

#[test]
#[cfg_attr(loom, ignore)]
#[should_panic]
fn validates_max_refs() {
struct GiantGenConfig;

// Configure the slab with a very large number of bits for the generation
// counter. This will only leave 1 bit to use for the slot reference
// counter, which will fail to validate.
impl Config for GiantGenConfig {
const INITIAL_PAGE_SIZE: usize = 1;
const MAX_THREADS: usize = 1;
const MAX_PAGES: usize = 1;
}

let _slab = Slab::<usize>::new_with_config::<GiantGenConfig>();
}

#[test]
fn giant_gen_config_stats() {
assert_eq!(GiantGenConfig::MAX_PAGES, 1);
assert_eq!(GiantGenConfig::MAX_SHARDS, 1);
assert_eq!(GiantGenConfig::MAX_THREADS, 1);
assert_eq!(GiantGenConfig::INITIAL_PAGE_SIZE, 1);
assert_eq!(GiantGenConfig::USED_BITS, WIDTH);
assert_eq!(GiantGenConfig::INITIAL_SZ, 1);
assert_eq!(GiantGenConfig::ADDR_INDEX_SHIFT, 1);
assert_eq!(RefCount::<GiantGenConfig>::LEN, 0);
assert_eq!(
LifecycleGen::<GiantGenConfig>::LEN,
WIDTH - RefCount::<GiantGenConfig>::LEN - Lifecycle::<GiantGenConfig>::LEN
);
assert_eq!(Lifecycle::<GiantGenConfig>::LEN, 2);
assert_eq!(
Generation::<GiantGenConfig>::LEN,
WIDTH
- GiantGenConfig::RESERVED_BITS
- Tid::<GiantGenConfig>::LEN
- Addr::<GiantGenConfig>::LEN
);
assert_eq!(Tid::<GiantGenConfig>::LEN, 0);
assert_eq!(Addr::<GiantGenConfig>::LEN, 2);
}

#[test]
#[cfg_attr(loom, ignore)]
fn big() {
Expand Down
16 changes: 11 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ pub use pool::Pool;

pub(crate) use tid::Tid;

use cfg::CfgPrivate;
use cfg::{CfgPrivate, WIDTH};
use shard::Shard;
use std::{fmt, marker::PhantomData, ptr, sync::Arc};

Expand Down Expand Up @@ -1021,6 +1021,14 @@ where
{
}

/// Generate bit mask with `len` 1 bits. (Rust 1.42.0 compatible)
const fn ones(len: usize) -> usize {
let shr = WIDTH - len;
let first_shr = shr / 2;
// right shift in two passes to avoid overflow
0usize.wrapping_sub(1) >> first_shr >> (shr - first_shr)
}

// === pack ===

pub(crate) trait Pack<C: cfg::Config>: Sized {
Expand Down Expand Up @@ -1048,10 +1056,8 @@ pub(crate) trait Pack<C: cfg::Config>: Sized {
/// left by `Self::SHIFT` bits to calculate this type's `MASK`.
///
/// This is computed automatically based on `Self::LEN`.
const BITS: usize = {
let shift = 1 << (Self::LEN - 1);
shift | (shift - 1)
};
const BITS: usize = ones(Self::LEN);

/// The number of bits to shift a number to pack it into a usize with other
/// values.
///
Expand Down
7 changes: 5 additions & 2 deletions src/page/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub(crate) struct Lifecycle<C> {
state: State,
_cfg: PhantomData<fn(C)>,
}
struct LifecycleGen<C>(Generation<C>);
pub(crate) struct LifecycleGen<C>(Generation<C>);

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
#[repr(usize)]
Expand Down Expand Up @@ -702,7 +702,10 @@ impl<C: cfg::Config> Pack<C> for RefCount<C> {
}

impl<C: cfg::Config> RefCount<C> {
pub(crate) const MAX: usize = Self::BITS - 1;
// Basically `Self::BITS.saturaing_sub(1)`, but Rust 1.42.0 compatible.
//
// Inspired by https://stackoverflow.com/a/53646925/8735881
pub(crate) const MAX: usize = [Self::BITS, 1][(Self::BITS < 1) as usize] - 1;

#[inline]
fn incr(self) -> Option<Self> {
Expand Down
2 changes: 1 addition & 1 deletion src/tid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ thread_local! {
// === impl Tid ===

impl<C: cfg::Config> Pack<C> for Tid<C> {
const LEN: usize = C::MAX_SHARDS.trailing_zeros() as usize + 1;
const LEN: usize = C::MAX_SHARDS.trailing_zeros() as usize;

type Prev = page::Addr<C>;

Expand Down
Loading