-
Notifications
You must be signed in to change notification settings - Fork 198
Open
Description
Summary
Allow users to provide custom Batch implementations for memory allocation optimization, while providing a default implementation in crate::impls::Batch.
Motivation
The current Batch<T> type is internal (pub(crate)) and not customizable. Users who need custom memory allocation strategies (e.g., arena allocators, pre-allocated pools) cannot provide their own batch container implementation.
Current State
Batch<T>is defined inopenraft/src/base/batch/mod.rsaspub(crate) enum Batch<T>- Two variants:
Single(T)(inline, no heap) andVec(Vec<T>)(heap-allocated) - Used for batching:
Batch<C::D>,Batch<C::Entry>,Batch<Option<CoreResponder<C>>>
Proposed Design
1. Define RaftBatch Trait
Create a trait in openraft/src/base/batch/ that captures the required interface:
pub trait RaftBatch<T>:
OptionalSend + Sized + Default + Clone + Debug + Eq + IntoIterator<Item = T> + 'static
where
T: OptionalSend + 'static,
{
type Iter<'a>: Iterator<Item = &'a T> + ExactSizeIterator where T: 'a, Self: 'a;
type IterMut<'a>: Iterator<Item = &'a mut T> + ExactSizeIterator where T: 'a, Self: 'a;
fn from_item(item: T) -> Self;
fn from_vec(vec: Vec<T>) -> Self;
fn from_exact_iter<I: ExactSizeIterator<Item = T>>(iter: I) -> Self;
fn len(&self) -> usize;
fn is_empty(&self) -> bool { self.len() == 0 }
fn first(&self) -> Option<&T>;
fn last(&self) -> Option<&T>;
fn iter(&self) -> Self::Iter<'_>;
fn iter_mut(&mut self) -> Self::IterMut<'_>;
fn extend(&mut self, other: Self);
// Auto-implemented display methods
fn display(&self) -> DisplayBatch<'_, T, Self> where T: Display { ... }
fn display_n(&self, max: usize) -> DisplayBatch<'_, T, Self> where T: Display { ... }
}2. Add to RaftTypeConfig
pub trait RaftTypeConfig: ... {
/// Batch container type for grouping multiple items efficiently.
type Batch<T>: RaftBatch<T> where T: OptionalSend + 'static;
}3. Default Implementation
- Keep
Batch<T>inopenraft/src/base/batch/mod.rs - Make it
pub(currentlypub(crate)) - Implement
RaftBatch<T>trait for it - Re-export from
crate::impls::Batch
4. Update declare_raft_types! Macro
Add default:
(Batch<T>, , $crate::impls::Batch<T> where T: $crate::OptionalSend + 'static),Files to Modify
| File | Change |
|---|---|
openraft/src/base/batch/raft_batch.rs |
New: Define RaftBatch trait |
openraft/src/base/batch/mod.rs |
Make Batch public, impl RaftBatch |
openraft/src/base/mod.rs |
Export RaftBatch trait |
openraft/src/type_config.rs |
Add Batch<T> associated type |
openraft/src/impls/mod.rs |
Re-export Batch from base |
openraft/src/raft/mod.rs |
Update declare_raft_types! macro |
openraft/src/lib.rs |
Export RaftBatch trait |
openraft/src/core/raft_msg/mod.rs |
Use C::Batch<_> |
openraft/src/engine/command.rs |
Use C::Batch<_> |
openraft/src/raft/api/app.rs |
Use C::Batch<_> |
openraft/src/core/merged_raft_msg_receiver.rs |
Use C::Batch<_> |
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels