1use serde::{Deserialize, Serialize};
4
5const GIB: u64 = 1024 * 1024 * 1024;
6const MIB: usize = 1024 * 1024;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
9#[serde(rename_all = "lowercase")]
10pub enum MemoryMode {
11 Lean,
12 Balanced,
13}
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub struct MemoryProfile {
17 pub resolver_threads: Option<usize>,
19 pub decay_ms: Option<isize>,
21 pub block_cache_bytes: usize,
22 pub bind_batch_bytes: usize,
24 pub refresh_floor_bytes: u64,
25}
26
27impl MemoryMode {
28 pub const fn profile(self) -> MemoryProfile {
29 self.profile_for_platform(cfg!(windows), cfg!(target_os = "linux"))
30 }
31
32 const fn profile_for_platform(self, windows: bool, linux: bool) -> MemoryProfile {
33 match self {
34 Self::Lean => MemoryProfile {
35 resolver_threads: if windows { None } else { Some(4) },
36 decay_ms: if linux { Some(0) } else { None },
37 block_cache_bytes: 128 * MIB,
38 bind_batch_bytes: 384 * MIB,
39 refresh_floor_bytes: GIB / 2,
40 },
41 Self::Balanced => MemoryProfile {
42 resolver_threads: None,
43 decay_ms: None,
44 block_cache_bytes: 256 * MIB,
45 bind_batch_bytes: 768 * MIB,
46 refresh_floor_bytes: GIB,
47 },
48 }
49 }
50
51 pub fn automatic(total_ram_bytes: Option<u64>) -> Self {
52 match total_ram_bytes {
53 Some(bytes) if bytes < 8 * GIB => Self::Lean,
54 _ => Self::Balanced,
55 }
56 }
57}
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62 #[test]
63 fn memory_profiles_match_the_limit_table() {
64 for (platform, windows, linux, threads, decay) in [
65 ("Linux", false, true, Some(4), Some(0)),
66 ("macOS", false, false, Some(4), None),
67 ("Windows", true, false, None, None),
68 ] {
69 assert_eq!(
70 MemoryMode::Lean.profile_for_platform(windows, linux),
71 MemoryProfile {
72 resolver_threads: threads,
73 decay_ms: decay,
74 block_cache_bytes: 128 * MIB,
75 bind_batch_bytes: 384 * MIB,
76 refresh_floor_bytes: 512 * 1024 * 1024,
77 },
78 "{platform} lean",
79 );
80 assert_eq!(
81 MemoryMode::Balanced.profile_for_platform(windows, linux),
82 MemoryProfile {
83 resolver_threads: None,
84 decay_ms: None,
85 block_cache_bytes: 256 * MIB,
86 bind_batch_bytes: 768 * MIB,
87 refresh_floor_bytes: 1024 * 1024 * 1024,
88 },
89 "{platform} balanced",
90 );
91 }
92 for mode in [MemoryMode::Lean, MemoryMode::Balanced] {
93 assert_eq!(
94 mode.profile(),
95 mode.profile_for_platform(cfg!(windows), cfg!(target_os = "linux"))
96 );
97 }
98 }
99 #[test]
100 fn memory_automatic_thresholds_and_unavailable_ram() {
101 for (bytes, expected) in [
102 (Some(79 * GIB / 10), MemoryMode::Lean),
103 (Some(8 * GIB), MemoryMode::Balanced),
104 (Some(319 * GIB / 10), MemoryMode::Balanced),
105 (Some(32 * GIB), MemoryMode::Balanced),
106 (Some(128 * GIB), MemoryMode::Balanced),
107 (None, MemoryMode::Balanced),
108 ] {
109 assert_eq!(MemoryMode::automatic(bytes), expected);
110 }
111 }
112 #[test]
113 fn memory_fast_is_an_invalid_value() {
114 assert!(serde_json::from_str::<MemoryMode>("\"fast\"").is_err());
115 }
116}