Skip to main content

monocoque_core/
config.rs

1//! ZMTP configuration and buffer sizing
2//!
3//! This module provides configuration for buffer sizes used across ZMTP sockets.
4//! Tuning these values can significantly impact performance based on workload.
5
6/// Default read buffer size (8KB)
7///
8/// Used for arena-allocated read buffers. Tune based on expected message sizes:
9/// - Small messages (< 1KB): 4096 bytes sufficient
10/// - Medium messages (1-8KB): 8192 bytes (default)
11/// - Large messages (> 8KB): 16384 or 32768 bytes
12pub const DEFAULT_READ_BUF_SIZE: usize = 8192;
13
14/// Default write buffer size (8KB)
15///
16/// Used for `BytesMut` write buffers. Should match typical encoded message size.
17pub const DEFAULT_WRITE_BUF_SIZE: usize = 8192;
18
19/// Small read buffer size (4KB)
20///
21/// Optimized for REQ/REP with small messages (< 1KB).
22pub const SMALL_READ_BUF_SIZE: usize = 4096;
23
24/// Small write buffer size (4KB)
25///
26/// Optimized for encoding small messages.
27pub const SMALL_WRITE_BUF_SIZE: usize = 4096;
28
29/// Large read buffer size (16KB)
30///
31/// Optimized for DEALER/ROUTER with larger messages (8-16KB).
32pub const LARGE_READ_BUF_SIZE: usize = 16384;
33
34/// Large write buffer size (16KB)
35///
36/// Optimized for encoding larger messages.
37pub const LARGE_WRITE_BUF_SIZE: usize = 16384;
38
39/// Initial staging buffer capacity for decoder reassembly (256 bytes)
40///
41/// Pre-allocated to avoid initial reallocation on fragmented frames.
42/// Only used when frame spans multiple segments (slow path).
43pub const STAGING_BUF_INITIAL_CAP: usize = 256;
44
45/// Socket buffer configuration
46#[derive(Debug, Clone, Copy)]
47pub struct BufferConfig {
48    /// Read buffer size (arena allocation)
49    pub read_buf_size: usize,
50    /// Write buffer size (`BytesMut` capacity)
51    pub write_buf_size: usize,
52}
53
54impl Default for BufferConfig {
55    fn default() -> Self {
56        Self {
57            read_buf_size: DEFAULT_READ_BUF_SIZE,
58            write_buf_size: DEFAULT_WRITE_BUF_SIZE,
59        }
60    }
61}
62
63impl BufferConfig {
64    /// Configuration optimized for small messages (< 1KB)
65    ///
66    /// Best for REQ/REP ping-pong patterns.
67    #[must_use]
68    pub const fn small() -> Self {
69        Self {
70            read_buf_size: SMALL_READ_BUF_SIZE,
71            write_buf_size: SMALL_WRITE_BUF_SIZE,
72        }
73    }
74
75    /// Configuration optimized for large messages (8-16KB)
76    ///
77    /// Best for DEALER/ROUTER with larger payloads.
78    #[must_use]
79    pub const fn large() -> Self {
80        Self {
81            read_buf_size: LARGE_READ_BUF_SIZE,
82            write_buf_size: LARGE_WRITE_BUF_SIZE,
83        }
84    }
85
86    /// Custom buffer configuration
87    #[must_use]
88    pub const fn custom(read_buf_size: usize, write_buf_size: usize) -> Self {
89        Self {
90            read_buf_size,
91            write_buf_size,
92        }
93    }
94}