async_fifo/fifo/block_size.rs
1use alloc::sync::Arc;
2
3use super::{Fifo, FifoApi};
4
5#[derive(Default)]
6/// Custom Block Size
7///
8/// In channels, items are stored in contiguous blocks (chunks) of item slots.
9/// New blocks are allocated as items are sent through the channel.
10/// A large block size will result in less but bigger allocations.
11/// A small block size will result in more, smaller allocations.
12/// For channels transporting large amounts of items, a large block size is preferred.
13/// This structure allows you to create channels with custom block size (chunk length).
14///
15/// See the following pre-defined block sizes:
16/// - [`SmallBlockSize`]: 8 slots per block
17/// - [`DefaultBlockSize`]: 32 slots per block
18/// - [`LargeBlockSize`]: 4096 slots per block
19/// - [`HugeBlockSize`]: 1048576 slots per block
20///
21/// L must be equal to F x 8
22pub struct BlockSize<const L: usize, const F: usize>;
23
24impl<const L: usize, const F: usize> BlockSize<L, F> {
25 pub fn fifo<T>() -> Fifo<L, F, T> {
26 Fifo::default()
27 }
28
29 pub fn arc_fifo<T: 'static>() -> Arc<dyn FifoApi<T>> {
30 Arc::new(Self::fifo())
31 }
32}
33
34/// Block size suitable for sending items one by one, from time to time
35///
36/// Each block will have 8 slots.
37pub type SmallBlockSize = BlockSize<8, 1>;
38
39/// Reasonable default block size
40///
41/// Each block will have 32 slots.
42pub type DefaultBlockSize = BlockSize<32, 4>;
43
44/// Block size suitable for batch sending of many items
45///
46/// Each block will have 4096 slots.
47pub type LargeBlockSize = BlockSize<4096, 512>;
48
49/// Block size suitable for batch sending of tons of items
50///
51/// Each block will have 1 048 576 slots.
52pub type HugeBlockSize = BlockSize<1048576, 131072>;