pub struct ReplayBuffer { /* private fields */ }Expand description
Fixed-capacity ring buffer with uniform random sampling.
Pre-allocates all arrays at construction for zero-allocation push. Oldest transitions are overwritten when capacity is reached.
Supports optional extra f32 columns (e.g. log-probs, value estimates)
via ColumnHandle. When no extra columns are registered, there is
zero overhead — no allocations and no branches in the hot push/sample path.
Implementations§
Source§impl ReplayBuffer
impl ReplayBuffer
Sourcepub fn new(capacity: usize, obs_dim: usize, act_dim: usize) -> Self
pub fn new(capacity: usize, obs_dim: usize, act_dim: usize) -> Self
Create a ring buffer with fixed capacity. All arrays are pre-allocated.
Sourcepub fn register_column(&mut self, name: &str, dim: usize) -> ColumnHandle
pub fn register_column(&mut self, name: &str, dim: usize) -> ColumnHandle
Register an extra f32 column with the given name and dimensionality.
Returns a ColumnHandle for O(1) push/sample access.
Must be called before any push() — the column is pre-allocated to
match the buffer’s capacity.
Sourcepub fn push_extra(
&mut self,
handle: ColumnHandle,
values: &[f32],
) -> Result<(), RloxError>
pub fn push_extra( &mut self, handle: ColumnHandle, values: &[f32], ) -> Result<(), RloxError>
Push extra column data for the most recently pushed transition.
Must be called after push() and before the next push().
The values slice length must match the column’s registered dim.
Sourcepub fn push_slices(
&mut self,
obs: &[f32],
next_obs: &[f32],
action: &[f32],
reward: f32,
terminated: bool,
truncated: bool,
) -> Result<(), RloxError>
pub fn push_slices( &mut self, obs: &[f32], next_obs: &[f32], action: &[f32], reward: f32, terminated: bool, truncated: bool, ) -> Result<(), RloxError>
Push a transition from borrowed slices, avoiding intermediate allocation.
Sourcepub fn push_batch(
&mut self,
obs_batch: &[f32],
next_obs_batch: &[f32],
actions_batch: &[f32],
rewards: &[f32],
terminated: &[bool],
truncated: &[bool],
) -> Result<(), RloxError>
pub fn push_batch( &mut self, obs_batch: &[f32], next_obs_batch: &[f32], actions_batch: &[f32], rewards: &[f32], terminated: &[bool], truncated: &[bool], ) -> Result<(), RloxError>
Push multiple transitions at once from flat arrays.
obs_batch shape: [n * obs_dim], next_obs_batch: same,
actions_batch: [n * act_dim], others: [n].
Sourcepub fn push(&mut self, record: ExperienceRecord) -> Result<(), RloxError>
pub fn push(&mut self, record: ExperienceRecord) -> Result<(), RloxError>
Push a transition, overwriting the oldest if at capacity.
Prefer push_slices to avoid the intermediate
Vec<f32> allocations inside ExperienceRecord.
Sourcepub fn sample(
&self,
batch_size: usize,
seed: u64,
) -> Result<SampledBatch, RloxError>
pub fn sample( &self, batch_size: usize, seed: u64, ) -> Result<SampledBatch, RloxError>
Sample a batch of transitions uniformly at random.
Uses ChaCha8Rng seeded with seed for deterministic cross-platform
reproducibility. Returns owned SampledBatch.
If extra columns have been registered, their data is included in
SampledBatch::extra.
Sourcepub fn sample_into(
&self,
batch: &mut SampledBatch,
batch_size: usize,
seed: u64,
) -> Result<(), RloxError>
pub fn sample_into( &self, batch: &mut SampledBatch, batch_size: usize, seed: u64, ) -> Result<(), RloxError>
Sample into a pre-allocated batch, reusing its capacity.
Same as sample() but avoids allocation by reusing batch.
Sourcepub fn extra_columns(&self) -> &ExtraColumns
pub fn extra_columns(&self) -> &ExtraColumns
Access the extra columns storage (for advanced use / testing).
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ReplayBuffer
impl RefUnwindSafe for ReplayBuffer
impl Send for ReplayBuffer
impl Sync for ReplayBuffer
impl Unpin for ReplayBuffer
impl UnwindSafe for ReplayBuffer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more