1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
//! A lock-free concurrent slab. //! //! Slabs provide pre-allocated storage for many instances of a single data //! type. When a large number of values of a single type are required, //! this can be more efficient than allocating each item individually. Since the //! allocated items are the same size, memory fragmentation is reduced, and //! creating and removing new items can be very cheap. //! //! This crate implements a lock-free concurrent slab, indexed by `usize`s. //! //! # Examples //! //! Inserting an item into the slab, returning an index: //! ```rust //! # use sharded_slab::Slab; //! let slab = Slab::new(); //! //! let key = slab.insert("hello world").unwrap(); //! assert_eq!(slab.get(key), Some(&"hello world")); //! ``` //! //! To share a slab across threads, it may be wrapped in an `Arc`: //! ```rust //! # use sharded_slab::Slab; //! use std::sync::Arc; //! let slab = Arc::new(Slab::new()); //! //! let slab2 = slab.clone(); //! let thread2 = std::thread::spawn(move || { //! let key = slab2.insert("hello from thread two").unwrap(); //! assert_eq!(slab2.get(key), Some(&"hello from thread two")); //! key //! }); //! //! let key1 = slab.insert("hello from thread one").unwrap(); //! assert_eq!(slab.get(key1), Some(&"hello from thread one")); //! //! // Wait for thread 2 to complete. //! let key2 = thread2.join().unwrap(); //! //! // The item inserted by thread 2 remains in the slab. //! assert_eq!(slab.get(key2), Some(&"hello from thread two")); //!``` //! //! If items in the slab must be mutated, a `Mutex` or `RwLock` may be used for //! each item, providing granular locking of items rather than of the slab: //! //! ```rust //! # use sharded_slab::Slab; //! use std::sync::{Arc, Mutex}; //! let slab = Arc::new(Slab::new()); //! //! let key = slab.insert(Mutex::new(String::from("hello world"))).unwrap(); //! //! let slab2 = slab.clone(); //! let thread2 = std::thread::spawn(move || { //! let mut hello = slab2.get(key).unwrap().lock().unwrap(); //! *hello = String::from("hello everyone!"); //! }); //! //! thread2.join().unwrap(); //! //! let hello = slab.get(key).unwrap().lock().unwrap(); //! assert_eq!(hello.as_str(), "hello everyone!"); //! ``` //! //! # Configuration //! //! For performance reasons, several values used by the slab are calculated as //! constants. In order to allow users to tune the slab's parameters, we provide //! a [`Config`] trait which defines these parameters as associated `consts`. //! The `Slab` type is generic over a `C: Config` parameter. //! //! [`Config`]: trait.Config.html //! //! # Comparison with Similar Crates //! //! - [`slab`]: Carl Lerche's `slab` crate provides a slab implementation with a //! similar API, implemented by storing all data in a single vector. //! //! Unlike `sharded_slab`, inserting and removing elements from the slab //! requires mutable access. This means that if the slab is accessed //! concurrently by multiple threads, it is necessary for it to be protected //! by a `Mutex` or `RwLock`. Items may not be inserted or removed (or //! accessed, if a `Mutex` is used) concurrently, even when they are //! unrelated. In many cases, the lock can become a significant bottleneck. On //! the other hand, this crate allows separate indices in the slab to be //! accessed, inserted, and removed concurrently without requiring a global //! lock. Therefore, when the slab is shared across multiple threads, this //! crate offers significantly better performance than `slab`. //! //! However, the lock free slab introduces some additional constant-factor //! overhead. This means that in use-cases where a slab is _not_ shared by //! multiple threads and locking is not required, this crate will likely offer //! slightly worse performance. //! //! In summary: `sharded-slab` offers significantly improved performance in //! concurrent use-cases, while `slab` should be preferred in single-threaded //! use-cases. //! //! [`slab`]: https://crates.io/crates/loom //! //! # Safety and Correctness //! //! Most implementations of lock-free data structures in Rust require some //! amount of unsafe code, and this crate is not an exception. In order to catch //! potential bugs in this unsafe code, we make use of [`loom`], a //! permutation-testing tool for concurrent Rust programs. All `unsafe` blocks //! this crate occur in accesses to `loom` `CausalCell`s. This means that when //! those accesses occur in this crate's tests, `loom` will assert that they are //! valid under the C11 memory model across multiple permutations of concurrent //! executions of those tests. //! //! In order to guard against the [ABA problem][aba], this crate makes use of //! _generational indices_. Each slot in the slab tracks a generation counter //! which is incremented every time a value is inserted into that slot, and the //! indices returned by [`Slab::insert`] include the generation of the slot when //! the value was inserted, packed into the high-order bits of the index. This //! ensures that if a value is inserted, removed, and a new value is inserted //! into the same slot in the slab, the key returned by the first call to //! `insert` will not map to the new value. //! //! Since a fixed number of bits are set aside to use for storing the generation //! counter, the counter will wrap around after being incremented a number of //! times. To avoid situations where a returned index lives long enough to see the //! generation counter wrap around to the same value, it is good to be fairly //! generous when configuring the allocation of index bits. //! //! [`loom`]: https://crates.io/crates/loom //! [aba]: https://en.wikipedia.org/wiki/ABA_problem //! [`Slab::insert`]: struct.Slab.html#method.insert //! //! # Performance //! //! These graphs were produced by [benchmarks] of the sharded slab implementation, //! using the [`criterion`] crate. //! //! The first shows the results of a benchmark where an increasing number of //! items are inserted and then removed into a slab concurrently by five //! threads. It compares the performance of the sharded slab implementation //! with a `RwLock<slab::Slab>`: //! //! <img width="1124" alt="Screen Shot 2019-10-01 at 5 09 49 PM" src="https://user-images.githubusercontent.com/2796466/66078398-cd6c9f80-e516-11e9-9923-0ed6292e8498.png"> //! //! The second graph shows the results of a benchmark where an increasing //! number of items are inserted and then removed by a _single_ thread. It //! compares the performance of the sharded slab implementation with an //! `RwLock<slab::Slab>` and a `mut slab::Slab`. //! //! <img width="925" alt="Screen Shot 2019-10-01 at 5 13 45 PM" src="https://user-images.githubusercontent.com/2796466/66078469-f0974f00-e516-11e9-95b5-f65f0aa7e494.png"> //! //! These benchmarks demonstrate that, while the sharded approach introduces //! a small constant-factor overhead, it offers significantly better //! performance across concurrent accesses. //! //! [benchmarks]: https://github.com/hawkw/sharded-slab/blob/master/benches/bench.rs //! [`criterion`]: https://crates.io/crates/criterion //! //! # Implementation Notes //! //! See [this page](implementation/index.html) for details on this crate's design //! and implementation. //! #![doc(html_root_url = "https://docs.rs/sharded-slab/0.0.2")] #[cfg(test)] macro_rules! thread_local { ($($tts:tt)+) => { loom::thread_local!{ $($tts)+ } } } #[cfg(not(test))] macro_rules! thread_local { ($($tts:tt)+) => { std::thread_local!{ $($tts)+ } } } pub mod implementation; mod page; pub(crate) mod sync; mod tid; pub(crate) use tid::Tid; pub(crate) mod cfg; mod iter; use cfg::CfgPrivate; pub use cfg::{Config, DefaultConfig}; use self::sync::{ atomic::{AtomicUsize, Ordering}, CausalCell, }; use page::Page; use std::{fmt, marker::PhantomData}; /// A sharded slab. /// /// See the [crate-level documentation](index.html) for details on using this type. pub struct Slab<T, C: cfg::Config = DefaultConfig> { shards: Box<[CausalCell<Shard<T, C>>]>, _cfg: PhantomData<C>, } struct Shard<T, C: cfg::Config> { #[cfg(debug_assertions)] tid: usize, sz: usize, len: AtomicUsize, // ┌─────────────┐ ┌────────┐ // │ page 1 │ │ │ // ├─────────────┤ ┌───▶│ next──┼─┐ // │ page 2 │ │ ├────────┤ │ // │ │ │ │XXXXXXXX│ │ // │ local_free──┼─┘ ├────────┤ │ // │ global_free─┼─┐ │ │◀┘ // ├─────────────┤ └───▶│ next──┼─┐ // │ page 3 │ ├────────┤ │ // └─────────────┘ │XXXXXXXX│ │ // ... ├────────┤ │ // ┌─────────────┐ │XXXXXXXX│ │ // │ page n │ ├────────┤ │ // └─────────────┘ │ │◀┘ // │ next──┼───▶ // ├────────┤ // │XXXXXXXX│ // └────────┘ // ... pages: Vec<Page<T, C>>, } impl<T> Slab<T> { /// Returns a new slab with the default configuration parameters. pub fn new() -> Self { Self::new_with_config() } /// Returns a new slab with the provided configuration parameters. pub fn new_with_config<C: cfg::Config>() -> Slab<T, C> { C::validate(); let mut shards = Vec::with_capacity(C::MAX_SHARDS); #[allow(unused_mut)] let mut idx = 0; shards.resize_with(C::MAX_SHARDS, || { let shard = Shard::new(idx); #[cfg(debug_assertions)] { idx += 1; } CausalCell::new(shard) }); Slab { shards: shards.into_boxed_slice(), _cfg: PhantomData, } } } impl<T, C: cfg::Config> Slab<T, C> { /// The number of bits in each index which are used by the slab. /// /// If other data is packed into the `usize` indices returned by /// [`Slab::insert`], user code is free to use any bits higher than the /// `USED_BITS`-th bit freely. /// /// This is determined by the [`Config`] type that configures the slab's /// parameters. By default, all bits are used; this can be changed by /// overriding the [`Config::RESERVED_BITS`][res] constant. /// /// [`Config`]: trait.Config.html /// [res]: trait.Config.html#associatedconstant.RESERVED_BITS /// [`Slab::insert`]: struct.Slab.html#method.insert pub const USED_BITS: usize = C::USED_BITS; /// Inserts a value into the slab, returning a key that can be used to /// access it. /// /// If this function returns `None`, then the shard for the current thread /// is full and no items can be added until some are removed, or the maximum /// number of shards has been reached. /// /// # Examples /// ```rust /// # use sharded_slab::Slab; /// let slab = Slab::new(); /// /// let key = slab.insert("hello world").unwrap(); /// assert_eq!(slab.get(key), Some(&"hello world")); /// ``` pub fn insert(&self, value: T) -> Option<usize> { let tid = Tid::<C>::current(); #[cfg(test)] println!("insert {:?}", tid); self.shards[tid.as_usize()] .with_mut(|shard| unsafe { // we are guaranteed to only mutate the shard while on its thread. (*shard).insert(value) }) .map(|idx| tid.pack(idx)) } /// Removes the value associated with the given key from the slab, returning /// it. /// /// If the slab does not contain a value for that key, `None` is returned /// instead. pub fn remove(&self, idx: usize) -> Option<T> { let tid = C::unpack_tid(idx); #[cfg(test)] println!("rm {:?}", tid); if tid.is_current() { self.shards[tid.as_usize()].with_mut(|shard| unsafe { // only called if this is the current shard (*shard).remove_local(idx) }) } else { self.shards[tid.as_usize()].with(|shard| unsafe { (*shard).remove_remote(idx) }) } } /// Return a reference to the value associated with the given key. /// /// If the slab does not contain a value for the given key, `None` is /// returned instead. /// /// # Examples /// /// ``` /// let slab = sharded_slab::Slab::new(); /// let key = slab.insert("hello world").unwrap(); /// /// assert_eq!(slab.get(key), Some(&"hello world")); /// assert_eq!(slab.get(12345), None); /// ``` pub fn get(&self, key: usize) -> Option<&T> { let tid = C::unpack_tid(key); #[cfg(test)] println!("get {:?}", tid); self.shards .get(tid.as_usize())? .with(|shard| unsafe { (*shard).get(key) }) } /// Returns `true` if the slab contains a value for the given key. /// /// # Examples /// /// ``` /// let slab = sharded_slab::Slab::new(); /// /// let key = slab.insert("hello world").unwrap(); /// assert!(slab.contains(key)); /// /// slab.remove(key).unwrap(); /// assert!(!slab.contains(key)); /// ``` pub fn contains(&self, key: usize) -> bool { self.get(key).is_some() } /// Returns the number of items currently stored in the slab. pub fn len(&self) -> usize { self.shards .iter() .map(|shard| shard.with(|shard| unsafe { (*shard).len() })) .sum() } /// Returns the current number of items which may be stored in the slab /// without allocating. pub fn capacity(&self) -> usize { self.total_capacity() - self.len() } /// Returns an iterator over all the items in the slab. pub fn unique_iter<'a>(&'a mut self) -> iter::UniqueIter<'a, T, C> { let mut shards = self.shards.iter_mut(); let shard = shards.next().expect("must be at least 1 shard"); let mut pages = shard.with(|shard| unsafe { (*shard).iter() }); let slots = pages.next().expect("must be at least 1 page").iter(); iter::UniqueIter { shards, slots, pages, } } fn total_capacity(&self) -> usize { self.shards .iter() .map(|shard| shard.with(|shard| unsafe { (*shard).total_capacity() })) .sum() } } impl<T, C: cfg::Config> Shard<T, C> { fn new(_idx: usize) -> Self { Self { #[cfg(debug_assertions)] tid: _idx, sz: C::INITIAL_SZ, len: AtomicUsize::new(0), pages: vec![Page::new(C::INITIAL_SZ, 0)], } } fn insert(&mut self, value: T) -> Option<usize> { #[cfg(debug_assertions)] debug_assert_eq!(Tid::<C>::current().as_usize(), self.tid); let mut value = Some(value); // Can we fit the value into an existing page? for page in self.pages.iter_mut() { if let Some(poff) = page.insert(&mut value) { self.len.fetch_add(1, Ordering::Relaxed); return Some(poff); } } // If not, can we allocate a new page? let pidx = self.pages.len(); if pidx >= C::MAX_PAGES { #[cfg(test)] println!("max pages (len={}, max={})", self.pages.len(), C::MAX_PAGES); // out of pages! return None; } // Add new page let sz = C::page_size(pidx); let mut page = Page::new(sz, self.sz); // Increment the total size of the shard, for the next time a new page // allocated. self.sz += sz; let poff = page.insert(&mut value).expect("new page should be empty"); self.len.fetch_add(1, Ordering::Relaxed); self.pages.push(page); Some(poff) } #[inline] fn get(&self, idx: usize) -> Option<&T> { #[cfg(debug_assertions)] debug_assert_eq!(Tid::<C>::from_packed(idx).as_usize(), self.tid); let addr = C::unpack_addr(idx); let i = addr.index(); #[cfg(test)] println!("-> {:?}; idx {:?}", addr, i); self.pages.get(i)?.get(addr, idx) } /// Remove an item on the shard's local thread. fn remove_local(&mut self, idx: usize) -> Option<T> { #[cfg(debug_assertions)] { debug_assert_eq!(Tid::<C>::current().as_usize(), self.tid); debug_assert_eq!(Tid::<C>::from_packed(idx).as_usize(), self.tid); } let addr = C::unpack_addr(idx); #[cfg(test)] println!("-> remove_local {:?}", addr); self.pages .get_mut(addr.index())? .remove_local(addr, C::unpack_gen(idx)) .map(|item| { self.len.fetch_sub(1, Ordering::Relaxed); item }) } /// Remove an item, while on a different thread from the shard's local thread. fn remove_remote(&self, idx: usize) -> Option<T> { #[cfg(debug_assertions)] { debug_assert_eq!(Tid::<C>::from_packed(idx).as_usize(), self.tid); debug_assert!(Tid::<C>::current().as_usize() != self.tid); } let addr = C::unpack_addr(idx); #[cfg(test)] println!("-> remove_remote {:?}", addr); self.pages .get(addr.index())? .remove_remote(addr, C::unpack_gen(idx)) .map(|item| { self.len.fetch_sub(1, Ordering::Relaxed); item }) } fn len(&self) -> usize { self.len.load(Ordering::Relaxed) } fn total_capacity(&self) -> usize { self.iter().map(Page::total_capacity).sum() } fn iter<'a>(&'a self) -> std::slice::Iter<'a, Page<T, C>> { self.pages.iter() } } impl<T: fmt::Debug, C: cfg::Config> fmt::Debug for Slab<T, C> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Slab") // .field("shards", &self.shards) .field("Config", &C::debug()) .finish() } } unsafe impl<T: Send, C: cfg::Config> Send for Slab<T, C> {} unsafe impl<T: Sync, C: cfg::Config> Sync for Slab<T, C> {} impl<T: fmt::Debug, C: cfg::Config> fmt::Debug for Shard<T, C> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut d = f.debug_struct("Shard"); #[cfg(debug_assertions)] d.field("tid", &self.tid); d.field("len", &self.len()) .field("sz", &self.sz) .field("pages", &self.pages) .finish() } } pub(crate) trait Pack<C: cfg::Config>: Sized { const LEN: usize; const BITS: usize; const SHIFT: usize = Self::Prev::SHIFT + Self::Prev::LEN; const MASK: usize = Self::BITS << Self::SHIFT; type Prev: Pack<C>; fn as_usize(&self) -> usize; fn from_usize(val: usize) -> Self; #[inline(always)] fn pack(&self, to: usize) -> usize { let value = self.as_usize(); debug_assert!(value <= Self::BITS); (to & !Self::MASK) | (value << Self::SHIFT) } #[inline(always)] fn from_packed(from: usize) -> Self { let value = (from & Self::MASK) >> Self::SHIFT; debug_assert!(value <= Self::BITS); Self::from_usize(value) } } impl<C: cfg::Config> Pack<C> for () { const BITS: usize = 0; const LEN: usize = 0; const SHIFT: usize = 0; const MASK: usize = 0; type Prev = (); fn as_usize(&self) -> usize { unreachable!() } fn from_usize(_val: usize) -> Self { unreachable!() } fn pack(&self, _to: usize) -> usize { unreachable!() } fn from_packed(_from: usize) -> Self { unreachable!() } } #[cfg(test)] mod tests;