|
| 1 | +use sharded_slab::Pool; |
| 2 | +use sharded_slab::Slab; |
| 3 | + |
| 4 | +use std::sync::{Arc, Mutex}; |
| 5 | + |
| 6 | +// Slabs provide pre-allocated storage for many instances of a single data type. |
| 7 | +// When a large number of values of a single type are required, this can be more efficient than allocating each item individually. |
| 8 | +// Since the allocated items are the same size, memory fragmentation is reduced, and creating and removing new items can be very cheap. |
| 9 | + |
| 10 | +pub fn sharded_slab_read() { |
| 11 | + let slab = Arc::new(Slab::new()); |
| 12 | + |
| 13 | + let slab2 = slab.clone(); |
| 14 | + let thread2 = std::thread::spawn(move || { |
| 15 | + let key = slab2.insert("hello from thread two").unwrap(); |
| 16 | + assert_eq!(slab2.get(key).unwrap(), "hello from thread two"); |
| 17 | + key |
| 18 | + }); |
| 19 | + |
| 20 | + let key1 = slab.insert("hello from thread one").unwrap(); |
| 21 | + assert_eq!(slab.get(key1).unwrap(), "hello from thread one"); |
| 22 | + |
| 23 | + // Wait for thread 2 to complete. |
| 24 | + let key2 = thread2.join().unwrap(); |
| 25 | + |
| 26 | + // The item inserted by thread 2 remains in the slab. |
| 27 | + assert_eq!(slab.get(key2).unwrap(), "hello from thread two"); |
| 28 | +} |
| 29 | + |
| 30 | +pub fn sharded_slab_write() { |
| 31 | + let slab = Arc::new(Slab::new()); |
| 32 | + |
| 33 | + let key = slab |
| 34 | + .insert(Mutex::new(String::from("hello world"))) |
| 35 | + .unwrap(); |
| 36 | + |
| 37 | + let slab2 = slab.clone(); |
| 38 | + let thread2 = std::thread::spawn(move || { |
| 39 | + let hello = slab2.get(key).expect("item missing"); |
| 40 | + let mut hello = hello.lock().expect("mutex poisoned"); |
| 41 | + *hello = String::from("hello everyone!"); |
| 42 | + }); |
| 43 | + |
| 44 | + thread2.join().unwrap(); |
| 45 | + |
| 46 | + let hello = slab.get(key).expect("item missing"); |
| 47 | + let hello = hello.lock().expect("mutex poisoned"); |
| 48 | + assert_eq!(hello.as_str(), "hello everyone!"); |
| 49 | +} |
| 50 | + |
| 51 | +pub fn sharded_slab_pool() { |
| 52 | + let pool: Pool<String> = Pool::new(); |
| 53 | + |
| 54 | + let mut guard = pool.create().unwrap(); |
| 55 | + let key = guard.key(); |
| 56 | + guard.push_str("hello world"); |
| 57 | + |
| 58 | + drop(guard); // release the guard, allowing immutable access. |
| 59 | + assert_eq!(pool.get(key).unwrap(), String::from("hello world")); |
| 60 | + |
| 61 | + // Mark this entry to be cleared. |
| 62 | + pool.clear(key); |
| 63 | + // The cleared entry is no longer available in the pool |
| 64 | + assert!(pool.get(key).is_none()); |
| 65 | +} |
| 66 | + |
| 67 | +pub fn slab_example() { |
| 68 | + let mut slab = slab::Slab::new(); |
| 69 | + |
| 70 | + let hello = slab.insert("hello"); |
| 71 | + let world = slab.insert("world"); |
| 72 | + |
| 73 | + assert_eq!(slab[hello], "hello"); |
| 74 | + assert_eq!(slab[world], "world"); |
| 75 | + |
| 76 | + slab[world] = "earth"; |
| 77 | + assert_eq!(slab[world], "earth"); |
| 78 | +} |
0 commit comments