pub struct BufferPool { /* private fields */ }Expand description
Buffer pool for String and Vec<Value> reuse.
Maintains pools of pre-allocated buffers to reduce allocation overhead during high-throughput parsing operations.
§Memory Management
- Buffers are cleared (content removed) when released but capacity is retained
- Pool size is limited to prevent unbounded memory growth
- Acquire operations fall back to fresh allocation if pool is empty
- Release operations drop buffers if pool is full
§Thread Safety
This is NOT thread-safe. Each parser instance should have its own pool. For multi-threaded scenarios, use one pool per thread.
§Examples
§Basic Usage
use hedl_stream::BufferPool;
let mut pool = BufferPool::new(10);
// Acquire a string buffer
let mut s = pool.acquire_string();
s.push_str("hello");
// Release it back to pool
pool.release_string(s);
// Next acquire reuses the buffer
let s2 = pool.acquire_string();
assert_eq!(s2.len(), 0); // Cleared but capacity retained§With Capacity Hints
use hedl_stream::BufferPool;
let mut pool = BufferPool::with_capacity_hints(10, 256, 16);
let s = pool.acquire_string();
assert!(s.capacity() >= 256);
let v = pool.acquire_value_vec();
assert!(v.capacity() >= 16);Implementations§
Source§impl BufferPool
impl BufferPool
Sourcepub fn with_capacity_hints(
max_pool_size: usize,
string_capacity_hint: usize,
value_capacity_hint: usize,
) -> Self
pub fn with_capacity_hints( max_pool_size: usize, string_capacity_hint: usize, value_capacity_hint: usize, ) -> Self
Create a buffer pool with custom capacity hints.
Capacity hints determine the initial capacity of newly allocated buffers when the pool is empty.
§Parameters
max_pool_size: Maximum buffers to pool per typestring_capacity_hint: Initial capacity for String buffersvalue_capacity_hint: Initial capacity forVec<Value>buffers
§Examples
use hedl_stream::BufferPool;
// Pool for parsing with long lines and wide rows
let pool = BufferPool::with_capacity_hints(20, 1024, 50);Sourcepub fn acquire_string(&mut self) -> String
pub fn acquire_string(&mut self) -> String
Acquire a String buffer from the pool.
Returns a pooled buffer if available, otherwise allocates a new one with the configured capacity hint.
§Examples
use hedl_stream::BufferPool;
let mut pool = BufferPool::new(10);
let mut s = pool.acquire_string();
s.push_str("data");Sourcepub fn release_string(&mut self, s: String)
pub fn release_string(&mut self, s: String)
Release a String buffer back to the pool.
The buffer is cleared but retains its capacity. If the pool is full, the buffer is dropped.
§Examples
use hedl_stream::BufferPool;
let mut pool = BufferPool::new(10);
let mut s = pool.acquire_string();
s.push_str("data");
pool.release_string(s);
// Buffer is reused with content cleared
let s2 = pool.acquire_string();
assert_eq!(s2.len(), 0);Sourcepub fn acquire_value_vec(&mut self) -> Vec<Value>
pub fn acquire_value_vec(&mut self) -> Vec<Value>
Acquire a Vec<Value> buffer from the pool.
Returns a pooled buffer if available, otherwise allocates a new one with the configured capacity hint.
§Examples
use hedl_stream::BufferPool;
use hedl_core::Value;
let mut pool = BufferPool::new(10);
let mut v = pool.acquire_value_vec();
v.push(Value::Int(42));Sourcepub fn release_value_vec(&mut self, v: Vec<Value>)
pub fn release_value_vec(&mut self, v: Vec<Value>)
Release a Vec<Value> buffer back to the pool.
The buffer is cleared but retains its capacity. If the pool is full, the buffer is dropped.
§Examples
use hedl_stream::BufferPool;
use hedl_core::Value;
let mut pool = BufferPool::new(10);
let mut v = pool.acquire_value_vec();
v.push(Value::Int(42));
pool.release_value_vec(v);
// Buffer is reused with content cleared
let v2 = pool.acquire_value_vec();
assert_eq!(v2.len(), 0);Sourcepub fn string_pool_size(&self) -> usize
pub fn string_pool_size(&self) -> usize
Get the current number of String buffers in the pool.
§Examples
use hedl_stream::BufferPool;
let mut pool = BufferPool::new(10);
assert_eq!(pool.string_pool_size(), 0);
let s = pool.acquire_string();
pool.release_string(s);
assert_eq!(pool.string_pool_size(), 1);Sourcepub fn value_pool_size(&self) -> usize
pub fn value_pool_size(&self) -> usize
Get the current number of Vec<Value> buffers in the pool.
§Examples
use hedl_stream::BufferPool;
let mut pool = BufferPool::new(10);
assert_eq!(pool.value_pool_size(), 0);
let v = pool.acquire_value_vec();
pool.release_value_vec(v);
assert_eq!(pool.value_pool_size(), 1);Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear all buffers from the pool.
This releases all pooled buffers, freeing memory back to the allocator. Useful for manual memory management or cleanup.
§Examples
use hedl_stream::BufferPool;
let mut pool = BufferPool::new(10);
let s = pool.acquire_string();
pool.release_string(s);
assert_eq!(pool.string_pool_size(), 1);
pool.clear();
assert_eq!(pool.string_pool_size(), 0);Sourcepub fn max_pool_size(&self) -> usize
pub fn max_pool_size(&self) -> usize
Get the maximum pool size.
§Examples
use hedl_stream::BufferPool;
let pool = BufferPool::new(15);
assert_eq!(pool.max_pool_size(), 15);