@@ -135,27 +135,18 @@ impl Timespec {
135
135
136
136
pub fn sub_timespec ( & self , other : & Timespec ) -> Result < Duration , Duration > {
137
137
if self >= other {
138
- // NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
139
- // to optimize it into a branchless form (see also #75545):
140
- //
141
- // 1. `self.tv_sec - other.tv_sec` shows up as a common expression
142
- // in both branches, i.e. the `else` must have its `- 1`
143
- // subtraction after the common one, not interleaved with it
144
- // (it used to be `self.tv_sec - 1 - other.tv_sec`)
145
- //
146
- // 2. the `Duration::new` call (or any other additional complexity)
147
- // is outside of the `if`-`else`, not duplicated in both branches
148
- //
149
- // Ideally this code could be rearranged such that it more
150
- // directly expresses the lower-cost behavior we want from it.
151
138
let ( secs, nsec) = if self . tv_nsec . as_inner ( ) >= other. tv_nsec . as_inner ( ) {
152
139
(
153
- ( self . tv_sec - other. tv_sec ) as u64 ,
140
+ self . tv_sec . wrapping_sub ( other. tv_sec ) as u64 ,
154
141
self . tv_nsec . as_inner ( ) - other. tv_nsec . as_inner ( ) ,
155
142
)
156
143
} else {
144
+ // Following sequence of assertions explain why `self.tv_sec - 1` does not underflow.
145
+ debug_assert ! ( self . tv_nsec < other. tv_nsec) ;
146
+ debug_assert ! ( self . tv_sec > other. tv_sec) ;
147
+ debug_assert ! ( self . tv_sec > i64 :: MIN ) ;
157
148
(
158
- ( self . tv_sec - other. tv_sec - 1 ) as u64 ,
149
+ ( self . tv_sec - 1 ) . wrapping_sub ( other. tv_sec ) as u64 ,
159
150
self . tv_nsec . as_inner ( ) + ( NSEC_PER_SEC as u32 ) - other. tv_nsec . as_inner ( ) ,
160
151
)
161
152
} ;
0 commit comments