Skip to main content

BufferPool

Struct BufferPool 

Source
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

Source

pub fn new(max_pool_size: usize) -> Self

Create a new buffer pool with default capacity hints.

§Parameters
  • max_pool_size: Maximum number of buffers to pool per type
§Examples
use hedl_stream::BufferPool;

let pool = BufferPool::new(10);
Source

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 type
  • string_capacity_hint: Initial capacity for String buffers
  • value_capacity_hint: Initial capacity for Vec<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);
Source

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");
Source

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);
Source

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));
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);

Trait Implementations§

Source§

impl Debug for BufferPool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.