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
//! A heap allocator that gives ownership of the value like a `Box`, but allocates in batches.
//!
//! `Pool` allocates objects on the heap in batches.
//! All objects must be of the same type like in `Vec`.
//! It allows fast multithreaded parallel allocation of objects.
//! If the `Pool` runs out of memory, it will allocate more but will not move the old values
//! from their place.
//!
//! When objects are dropped, their memory is returned to the pool to be reused for
//! future allocations.
//! Only when all the objects and the `Pool` are dropped will the memory be released.
//!
//! It gives the option to allocate each object on a separate CPU cache line, increasing performance
//! of multithreaded access to adjacent elements.
//!
//! The `Object` class has exclusive ownership of the value contained within. When dropped, the
//! owned object will be dropped as well. The memory, however, will be returned to the `Pool` it
//! was allocated from to be available for other allocations.
//!
//! # Examples
//!
//! ```
//! use veryfast::pool::{Pool, Object};
//!
//! let pool = Pool::new();
//!
//! let var1 = pool.push(15i32);
//! let mut var2 = pool.push(7);
//! *var2 = *var1;
//! assert_eq!(*var1, *var2);
//!
//! let mut vec = Vec::new();
//! for i in 0..10 {
//!     vec.push(pool.push(i));
//! }
//! for i in &vec {
//!     print!("{} ", **i);
//! }
//! ```
//!
//! An example using a scoped thread pool:
//!
//! ```
//! # extern crate veryfast;
//! extern crate scoped_threadpool;
//!
//! fn slow(val: &mut i32) {
//!     *val += 1;
//! }
//!
//! let mut thread_pool = scoped_threadpool::Pool::new(4);
//! let memory_pool = veryfast::pool::Pool::with_params(true);
//!
//! let mut vec = Vec::new();
//!
//! for i in 0..10 {
//!     vec.push(memory_pool.push(i));
//! }
//!
//! thread_pool.scoped(|scoped| {
//!     for e in &mut vec {
//!         scoped.execute(move || {
//!             slow(&mut **e);
//!         });
//!     }
//! });
//!
//! for i in 0..10 {
//!     assert_eq!(*vec[i], vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10][i]);
//! }
//! ```

use std::heap::{Heap, Layout, Alloc};
use std::fmt;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::ptr;
use std::sync::Mutex;

use super::crossbeam::sync::MsQueue;

/// A fast heap-allocator. Allocates objects in a batch, but transfers the ownership to the `Object`.
///
/// Allocations will first check if there is an already free slot to use, and use that.
/// If no, It will take a lock and allocate a batch of memory.
///
/// When objects are dropped, their memory will be returned to the pool to be used again later.
/// The memory of the batches will be deallocated only when the `Pool` and all the related `Object`s
/// are dropped.
pub struct Pool<T> {
    data: Mutex<Vec<*const T>>,
    free: MsQueue<*mut T>,
    layout: Layout,
    batch: usize,
    stride: usize,
}

/// A pointer type that owns its content.
///
/// Created from a `Pool`. The `Object` owns the value inside it and has exclusive access to it.
///
pub struct Object<'active, T: 'active> {
    obj: *mut T,
    manager: &'active Pool<T>,
}

impl<T> Pool<T> {
    /// Creates a new `Pool`.
    #[inline]
    pub fn new() -> Pool<T> {
        Pool::with_params(false)
    }

    /// Creates a new `Pool`.
    ///
    /// - `align_to_cache`: Should each object be on a separate CPU cache line. Speeds up
    /// multithreaded usage, but hurts single-threaded cache locality a bit and requires a bit more memory.
    /// Has no effect if `size_of::<T>` is already a multiple of a cache line size.
    #[inline]
    pub fn with_params(align_to_cache: bool) -> Pool<T> {
        Pool::with_system_params(align_to_cache, 64, 64)
    }

    /// Creates a new `Pool`.
    ///
    /// - `align_to_cache`: Should each object be on a separate CPU cache line. Speeds up
    /// multithreaded usage, but hurts single-threaded cache locality a bit and requires a bit more memory.
    /// Has no effect if `size_of::<T>` is already a multiple of a cache line size.
    /// 
    /// - `cache_line_size`: The size of an L1 cache line on the architecture. Must be a power of 2.
    /// 
    /// - `number_of_sets`: The number of [associativity](https://en.wikipedia.org/wiki/CPU_cache#Associativity) sets
    /// of the target processor. Decides the size of batch allocations.
    #[inline]
    pub fn with_system_params(align_to_cache: bool, cache_line_size: usize, number_of_sets: usize) -> Pool<T> {
        assert!(cache_line_size != 0, "Pool requested with cache_line_size = 0");
        assert!(number_of_sets != 0, "Pool requested with number_of_sets = 0");
        assert!(mem::size_of::<T>() != 0,
                "Pool requested with type of size 0");
        let batch_alignment = cache_line_size.max(mem::align_of::<T>());
        let align = ((mem::size_of::<T>() + mem::align_of::<T>() - 1) / mem::align_of::<T>()) * mem::align_of::<T>();
        let stride = if align_to_cache {
            ((cache_line_size + align - 1) / cache_line_size) * cache_line_size
        } else {
            align
        };
        let batch = (number_of_sets * cache_line_size / stride).max(1);
        let mem_size = batch * stride;
        let layout = Layout::from_size_align(mem_size, batch_alignment).expect("Pool requested with bad system cache parameters");
        Pool {
            data: Mutex::new(Vec::new()),
            free: MsQueue::new(),
            layout,
            batch,
            stride,
        }
    }

    /// Save the object on the heap. Will get a pointer that will drop it's content when
    /// dropped (like a `Box`). The memory will be reused though!
    ///
    /// Thread-safe. Very fast most of the time, but will take a bit longer if need to allocate
    /// more objects.
    ///
    /// Will panic if out of memory.
    #[inline]
    pub fn push(&self, obj: T) -> Object<T> {
        let slot = match self.free.try_pop() {
            Some(x) => x,
            None => self.expand(),
        };
        unsafe {
            ptr::write(slot, obj);
        }
        Object {
            obj: slot,
            manager: self,
        }
    }

    #[inline]
    fn expand(&self) -> *mut T {
        unsafe {
            let mut lock = self.data.lock().unwrap();
            if let Some(x) = self.free.try_pop() {
                return x;
            }
            let extra = Heap::default().alloc(self.layout.clone()).unwrap() as *mut T;
            // starting from 1 since index 0 will be returned
            for i in 1..self.batch {
                self.free.push((extra as usize + i * self.stride) as *mut T);
            }
            lock.push(extra);
            extra
        }
    }

    #[inline]
    fn ret_ptr(&self, obj: *mut T) {
        self.free.push(obj);
    }
}

impl<T> Default for Pool<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Drop for Pool<T> {
    #[inline]
    fn drop(&mut self) {
        let lock = match self.data.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        for block in lock.deref() {
            unsafe {
                Heap::default().dealloc(*block as *mut u8, self.layout.clone());
            }
        }
    }
}

impl<'active, T> Object<'active, T> {
    /// Returns the owned object from the pool-allocated memory.
    #[allow(needless_pass_by_value)]
    #[inline]
    pub fn recover(t: Self) -> T {
        let ret = unsafe {
            ptr::read(t.obj)
        };
        t.manager.ret_ptr(t.obj);
        ret
    }
}

impl<'active, T> Drop for Object<'active, T> {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            ptr::read(self.obj);
        }
        self.manager.ret_ptr(self.obj);
    }
}

impl<'active, T> Deref for Object<'active, T> {
    type Target = T;

    #[allow(inline_always)]
    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        unsafe { &*self.obj }
    }
}

impl<'active, T> DerefMut for Object<'active, T> {
    #[allow(inline_always)]
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { &mut *self.obj }
    }
}

unsafe impl<'active, T: Send> Send for Object<'active, T> {}

unsafe impl<'active, T: Sync> Sync for Object<'active, T> {}

unsafe impl<T: Send> Send for Pool<T> {}

unsafe impl<T: Send> Sync for Pool<T> {}

impl<T> fmt::Debug for Pool<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let pages = {
            self.data.lock().unwrap().len()
        };
        write!(f,
               "Pool {{ {} blocks, {} elements with {} stride in each. {} bytes allocated total for {} possible elements }}",
               pages,
               self.batch,
               self.stride,
               pages * self.layout.size(),
               pages * self.batch
               )
    }
}

// impl<T> fmt::Debug for Object<T>
// where T: fmt::Debug
// {
// #[inline]
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// (**self).fmt(f)
// }
// }
//
// impl<T> fmt::Display for Object<T>
// where T: fmt::Display
// {
// #[inline]
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// (**self).fmt(f)
// }
// }

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn object_dereference() {
        let val = 5u64;
        let pool = Pool::with_params(false);
        let mut val2 = pool.push(val);
        assert_eq!(*val2, val);
        let val3 = 7u64;
        *val2 = val3;
        assert_eq!(*val2, val3);
    }
}