vyre_driver/
command_reuse_policy.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub struct CommandReuseInputs {
19 pub repeat_count: u32,
22 pub per_launch_overhead_ns: u64,
25 pub record_overhead_ns: u64,
27 pub replay_overhead_ns: u64,
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum CommandReuseDecision {
34 PlainLaunches,
37 RecordAndReplay {
40 savings_ns: u128,
43 },
44}
45
46#[must_use]
53pub fn decide_command_reuse(inputs: CommandReuseInputs) -> CommandReuseDecision {
54 if inputs.repeat_count <= 1 {
55 return CommandReuseDecision::PlainLaunches;
56 }
57 if inputs.per_launch_overhead_ns <= inputs.replay_overhead_ns {
58 return CommandReuseDecision::PlainLaunches;
61 }
62 let per_call_savings =
63 u128::from(inputs.per_launch_overhead_ns) - u128::from(inputs.replay_overhead_ns);
64 let total_call_savings = u128::from(inputs.repeat_count) * per_call_savings;
65 let record_overhead_ns = u128::from(inputs.record_overhead_ns);
66 if total_call_savings <= record_overhead_ns {
67 return CommandReuseDecision::PlainLaunches;
68 }
69 let savings_ns = total_call_savings - record_overhead_ns;
70 CommandReuseDecision::RecordAndReplay { savings_ns }
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 fn inp(rep: u32, launch: u64, record: u64, replay: u64) -> CommandReuseInputs {
78 CommandReuseInputs {
79 repeat_count: rep,
80 per_launch_overhead_ns: launch,
81 record_overhead_ns: record,
82 replay_overhead_ns: replay,
83 }
84 }
85
86 #[test]
87 fn single_dispatch_is_plain() {
88 assert_eq!(
90 decide_command_reuse(inp(1, 5_000, 25_000, 500)),
91 CommandReuseDecision::PlainLaunches
92 );
93 }
94
95 #[test]
96 fn zero_repeat_is_plain() {
97 assert_eq!(
98 decide_command_reuse(inp(0, 5_000, 25_000, 500)),
99 CommandReuseDecision::PlainLaunches
100 );
101 }
102
103 #[test]
104 fn replay_no_cheaper_than_launch_is_plain() {
105 assert_eq!(
107 decide_command_reuse(inp(1000, 5_000, 25_000, 5_000)),
108 CommandReuseDecision::PlainLaunches
109 );
110 }
111
112 #[test]
113 fn small_repeat_under_amortisation_is_plain() {
114 assert_eq!(
116 decide_command_reuse(inp(5, 5_000, 25_000, 500)),
117 CommandReuseDecision::PlainLaunches
118 );
119 }
120
121 #[test]
122 fn large_repeat_above_amortisation_picks_record_and_replay() {
123 assert_eq!(
126 decide_command_reuse(inp(100, 5_000, 25_000, 500)),
127 CommandReuseDecision::RecordAndReplay {
128 savings_ns: 425_000
129 }
130 );
131 }
132
133 #[test]
134 fn savings_strictly_positive_when_record_and_replay() {
135 let dec = decide_command_reuse(inp(1000, 5_000, 25_000, 500));
136 match dec {
137 CommandReuseDecision::RecordAndReplay { savings_ns } => assert!(savings_ns > 0),
138 other => panic!("expected RecordAndReplay; got {:?}", other),
139 }
140 }
141
142 #[test]
143 fn widened_arithmetic_preserves_extreme_savings() {
144 let dec = decide_command_reuse(inp(u32::MAX, u64::MAX / 2, 25_000, 1));
146 match dec {
147 CommandReuseDecision::RecordAndReplay { savings_ns } => {
148 assert_eq!(
149 savings_ns,
150 u128::from(u32::MAX) * (u128::from(u64::MAX / 2) - 1) - 25_000
151 );
152 }
153 other => panic!("expected RecordAndReplay; got {:?}", other),
154 }
155 }
156
157 #[test]
158 fn command_reuse_policy_source_uses_exact_widened_arithmetic() {
159 let source = include_str!("command_reuse_policy.rs");
160
161 assert!(
162 !source.contains(concat!("saturating", "_mul"))
163 && !source.contains(concat!("saturating", "_sub")),
164 "Fix: command-reuse policy must use exact widened arithmetic, not saturating replay-cost math."
165 );
166 assert!(
167 source.contains("u128::from(inputs.per_launch_overhead_ns)")
168 && source.contains("u128::from(inputs.repeat_count)")
169 && source.contains("total_call_savings - record_overhead_ns"),
170 "Fix: command-reuse savings must stay widened through the verdict."
171 );
172 }
173}