@@ -69,18 +69,34 @@ fn translate_progress_to_bar(
69
69
70
70
// We choose `N=10` here to make a `300ms * 10slots ~= 3000ms`
71
71
// sliding window for tracking the data transfer rate (in bytes/s).
72
- let mut last_update = Instant :: now ( ) ;
73
- let mut counter = MetricsCounter :: < 10 > :: new ( 0 , last_update) ;
72
+ let mut last_percentage_update = Instant :: now ( ) ;
73
+ let mut last_fast_update = Instant :: now ( ) ;
74
+ let mut counter = MetricsCounter :: < 10 > :: new ( 0 , last_percentage_update) ;
74
75
75
76
let mut tasks = Vec :: with_capacity ( 10 ) ;
76
- let update_interval = std:: time:: Duration :: from_millis ( 300 ) ;
77
- let short_check_interval = Duration :: from_millis ( 50 ) ;
77
+ let slow_check_interval = std:: time:: Duration :: from_millis ( 300 ) ;
78
+ let fast_check_interval = Duration :: from_millis ( 50 ) ;
79
+ let sleep_interval = Duration :: from_millis ( 10 ) ;
80
+ debug_assert_eq ! (
81
+ slow_check_interval. as_millis( ) % fast_check_interval. as_millis( ) ,
82
+ 0 ,
83
+ "progress should be smoother by keeping these as multiples of each other"
84
+ ) ;
85
+ debug_assert_eq ! (
86
+ fast_check_interval. as_millis( ) % sleep_interval. as_millis( ) ,
87
+ 0 ,
88
+ "progress should be smoother by keeping these as multiples of each other"
89
+ ) ;
78
90
79
91
while let Some ( root) = root. upgrade ( ) {
80
- let not_yet = last_update. elapsed ( ) < update_interval;
81
- if not_yet {
82
- std:: thread:: sleep ( short_check_interval) ;
92
+ std:: thread:: sleep ( sleep_interval) ;
93
+ let needs_update = last_fast_update. elapsed ( ) >= fast_check_interval;
94
+ if !needs_update {
95
+ continue ;
83
96
}
97
+ let now = Instant :: now ( ) ;
98
+ last_fast_update = now;
99
+
84
100
root. sorted_snapshot ( & mut tasks) ;
85
101
86
102
fn progress_by_id (
@@ -96,13 +112,14 @@ fn translate_progress_to_bar(
96
112
tasks. iter ( ) . find_map ( |( _, t) | cb ( t) )
97
113
}
98
114
115
+ const NUM_PHASES : usize = 2 ; // indexing + delta-resolution, both with same amount of objects to handle
99
116
if let Some ( objs) = find_in ( & tasks, |t| progress_by_id ( resolve_objects, t) ) {
100
117
// Resolving deltas.
101
118
let objects = objs. step . load ( Ordering :: Relaxed ) ;
102
119
let total_objects = objs. done_at . expect ( "known amount of objects" ) ;
103
120
let msg = format ! ( ", ({objects}/{total_objects}) resolving deltas" ) ;
104
121
105
- progress_bar. tick ( objects, total_objects, & msg) ?;
122
+ progress_bar. tick ( total_objects + objects, total_objects * NUM_PHASES , & msg) ?;
106
123
} else if let Some ( ( objs, read_pack) ) =
107
124
find_in ( & tasks, |t| progress_by_id ( read_pack_bytes, t) ) . and_then ( |read| {
108
125
find_in ( & tasks, |t| progress_by_id ( delta_index_objects, t) )
@@ -114,15 +131,15 @@ fn translate_progress_to_bar(
114
131
let total_objects = objs. done_at . expect ( "known amount of objects" ) ;
115
132
let received_bytes = read_pack. step . load ( Ordering :: Relaxed ) ;
116
133
117
- let now = Instant :: now ( ) ;
118
- if !not_yet {
134
+ let needs_percentage_update = last_percentage_update . elapsed ( ) >= slow_check_interval ;
135
+ if needs_percentage_update {
119
136
counter. add ( received_bytes, now) ;
120
- last_update = now;
137
+ last_percentage_update = now;
121
138
}
122
139
let ( rate, unit) = human_readable_bytes ( counter. rate ( ) as u64 ) ;
123
140
let msg = format ! ( ", {rate:.2}{unit}/s" ) ;
124
141
125
- progress_bar. tick ( objects, total_objects, & msg) ?;
142
+ progress_bar. tick ( objects, total_objects * NUM_PHASES , & msg) ?;
126
143
}
127
144
}
128
145
Ok ( ( ) )
0 commit comments