sp_im/vector/
pool.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use crate::{
6  config::POOL_SIZE,
7  nodes::{
8    chunk::Chunk,
9    rrb::Node,
10  },
11  util::Pool,
12};
13
14/// A memory pool for `Vector`.
15pub struct RRBPool<A> {
16  pub(crate) node_pool: Pool<Chunk<Node<A>>>,
17  pub(crate) value_pool: Pool<Chunk<A>>,
18  pub(crate) size_pool: Pool<Chunk<usize>>,
19}
20
21impl<A> RRBPool<A> {
22  /// Create a new memory pool with the given size.
23  pub fn new(size: usize) -> Self { Self::with_sizes(size, size, size) }
24
25  /// Create a new memory pool with the given sizes for each subpool.
26  pub fn with_sizes(
27    node_pool_size: usize,
28    leaf_pool_size: usize,
29    size_table_pool_size: usize,
30  ) -> Self {
31    Self {
32      node_pool: Pool::new(node_pool_size),
33      value_pool: Pool::new(leaf_pool_size),
34      size_pool: Pool::new(size_table_pool_size),
35    }
36  }
37
38  /// Fill the memory pool with preallocated chunks.
39  pub fn fill(&self) {
40    self.node_pool.fill();
41    self.value_pool.fill();
42    self.size_pool.fill();
43  }
44
45  /// Get the size of the node subpool.
46  pub fn node_pool_size(&self) -> usize { self.node_pool.get_pool_size() }
47
48  /// Get the size of the leaf node subpool.
49  pub fn leaf_pool_size(&self) -> usize { self.value_pool.get_pool_size() }
50
51  /// Get the size of the size table subpool.
52  pub fn size_table_pool_size(&self) -> usize { self.size_pool.get_pool_size() }
53}
54
55impl<A> Default for RRBPool<A> {
56  /// Construct a pool with a reasonable default pool size.
57  fn default() -> Self { Self::new(POOL_SIZE) }
58}
59
60impl<A> Clone for RRBPool<A> {
61  fn clone(&self) -> Self {
62    Self {
63      node_pool: self.node_pool.clone(),
64      value_pool: self.value_pool.clone(),
65      size_pool: self.size_pool.clone(),
66    }
67  }
68}