iter_log/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use rayon::iter::plumbing::{Consumer, Folder, UnindexedConsumer};
use rayon::prelude::*;
use std::env::var;
use std::io::{self, Write};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::OnceLock;
use std::sync::{Arc, Mutex};

fn enable_log() -> &'static bool {
    static ENABLE_LOG: OnceLock<bool> = OnceLock::new();
    ENABLE_LOG.get_or_init(|| var("ENABLE_ITER_LOG").is_ok())
}

const BARS: &str = "--------------------------------------------------";
static STEP_PERCENT: usize = 2;

/// Extension trait to add a `log_progress` method to regular iterators.
pub trait LogProgressExt: Iterator + Sized {
    /// Wraps the iterator with progress logging.
    ///
    /// This method will print progress updates every 2% of the way through the iteration.
    ///
    /// # Returns
    ///
    /// Returns a new `LogProgress` iterator which tracks and logs progress.
    fn log_progress(self, name: &str) -> LogProgress<Self>;
}

impl<I> LogProgressExt for I
where
    I: Iterator, // Ensure `I` is an iterator
{
    fn log_progress(self, name: &str) -> LogProgress<Self> {
        let total = self.size_hint().1.unwrap_or(0); // Get the total number of items (if possible)

        LogProgress {
            iter: self,
            progress: 0,
            total,
            name: name.to_string(),
            t0: std::time::Instant::now(),
        }
    }
}

/// A struct that wraps an iterator and tracks progress.
pub struct LogProgress<I> {
    iter: I,
    progress: usize,
    total: usize,
    name: String,
    t0: std::time::Instant,
}

impl<I: Iterator> Iterator for LogProgress<I> {
    type Item = I::Item;

    /// Returns the next item in the iteration, while logging progress at intervals.
    ///
    /// The method increments the progress count and checks if the progress reaches a new milestone
    /// (i.e., a multiple of 2). If so, it logs the progress to the console.
    ///
    /// # Returns
    ///
    /// Returns the next item in the iterator, or `None` if the iterator is finished.
    fn next(&mut self) -> Option<Self::Item> {
        let item = self.iter.next()?;
        let step = STEP_PERCENT / 2;

        if *enable_log() {
            if self.progress == 0 {
                print!(
                    "+{}+\n| {}{}|\n+{}+\n|",
                    BARS,
                    self.name,
                    " ".repeat(49 - self.name.len()),
                    BARS,
                );
            }
            self.progress += 1;
            let old_percent = (self.progress * 100) / self.total;
            let new_percent = ((self.progress + 1) * 100) / self.total;

            // Log the progress if we hit a new milestone
            if new_percent / STEP_PERCENT > old_percent / STEP_PERCENT {
                print!("{}", "=".repeat(step));
                io::stdout().flush().unwrap();
            }
        }

        Some(item)
    }
}

impl<I> LogProgress<I> {
    fn finish(&self) {
        if *enable_log() {
            println!("|\n+{}+", "-".repeat(50));
            let elapsed = self.t0.elapsed();
            let time_str = format!("{:?}", elapsed);
            print!(
                "| Took {} to complete{}|\n+{}+\n",
                time_str,
                " ".repeat(32 - time_str.chars().count()),
                BARS
            );
        }
    }
}

impl<I> Drop for LogProgress<I> {
    fn drop(&mut self) {
        self.finish();
    }
}

/// A struct to handle ordered progress logging during parallel iteration.
struct OrderedLogger {
    last_logged: AtomicUsize,
    pending_logs: Mutex<Vec<usize>>,
    t0: std::time::Instant,
}

impl OrderedLogger {
    /// Creates a new `OrderedLogger` instance.
    ///
    /// This function initializes the logger with a fresh `AtomicUsize` and an empty vector for
    /// pending progress updates.
    ///
    /// # Returns
    ///
    /// Returns an `Arc` (thread-safe reference) of the `OrderedLogger`.
    fn new(t0: std::time::Instant) -> Arc<Self> {
        Arc::new(Self {
            last_logged: AtomicUsize::new(0),
            pending_logs: Mutex::new(Vec::new()),
            t0,
        })
    }

    /// Logs the progress if it matches the expected step and ensures ordered output.
    ///
    /// # Arguments
    ///
    /// * `progress` - The current progress percentage.
    ///
    /// # Notes
    ///
    /// This method ensures that progress logs are printed in the correct order, even when the
    /// parallel tasks report progress asynchronously.
    fn log_progress(&self, progress: usize) {
        if *enable_log() {
            let mut pending = self.pending_logs.lock().unwrap();
            let step = STEP_PERCENT / 2;

            if progress == self.last_logged.load(Ordering::Relaxed) + STEP_PERCENT {
                // Print the progress immediately if it's the next expected one
                print!("{}", "=".repeat(step));
                io::stdout().flush().unwrap();
                self.last_logged.fetch_add(STEP_PERCENT, Ordering::Relaxed);

                // Print any pending logs that can now be processed in order
                while let Some(&next) = pending.first() {
                    if next == self.last_logged.load(Ordering::Relaxed) + STEP_PERCENT {
                        print!("{}", "=".repeat(step));
                        io::stdout().flush().unwrap();
                        self.last_logged.fetch_add(STEP_PERCENT, Ordering::Relaxed);
                        pending.remove(0);
                    } else {
                        break;
                    }
                }
            } else {
                // If progress is not expected yet, store it for later
                pending.push(progress);
                pending.sort_unstable(); // Sort pending progress updates
            }
        }
    }

    fn finish(&self) {
        if *enable_log() {
            println!("|\n+{}+", "-".repeat(50));
            let elapsed = self.t0.elapsed();
            let time_str = format!("{:?}", elapsed);
            print!(
                "| Took {} to complete{}|\n+{}+\n",
                time_str,
                " ".repeat(32 - time_str.chars().count()),
                BARS
            );
        }
    }
}

impl Drop for OrderedLogger {
    fn drop(&mut self) {
        self.finish();
    }
}

/// A struct that wraps a parallel iterator and tracks progress.
pub struct LogProgressPar<I> {
    iter: I,
    progress: Arc<AtomicUsize>,
    total: usize,
    logger: Arc<OrderedLogger>,
}

impl<I> ParallelIterator for LogProgressPar<I>
where
    I: ParallelIterator,
{
    type Item = I::Item;

    /// Drives the parallel iteration using a custom consumer.
    ///
    /// This method wraps the original consumer with the `LogProgressConsumer`, which tracks the
    /// progress and logs it at the specified intervals.
    ///
    /// # Arguments
    ///
    /// * `consumer` - The base consumer that will process the items of the parallel iterator.
    ///
    /// # Returns
    ///
    /// Returns the result of the parallel iteration after it is consumed.
    fn drive_unindexed<C>(self, consumer: C) -> C::Result
    where
        C: UnindexedConsumer<Self::Item>,
    {
        let wrapped_consumer = LogProgressConsumer {
            base: consumer,
            progress: self.progress,
            total: self.total,
            logger: self.logger,
        };
        self.iter.drive_unindexed(wrapped_consumer)
    }
}

/// A consumer for parallel iterations that tracks progress and logs it.
struct LogProgressConsumer<C> {
    base: C,
    progress: Arc<AtomicUsize>,
    total: usize,
    logger: Arc<OrderedLogger>,
}

impl<C, T> Consumer<T> for LogProgressConsumer<C>
where
    C: Consumer<T>,
{
    type Folder = LogProgressFolder<C::Folder>;
    type Reducer = C::Reducer;
    type Result = C::Result;

    /// Splits the consumer at the specified index and returns two new consumers.
    ///
    /// This method ensures that progress tracking is correctly propagated through both consumers.
    ///
    /// # Arguments
    ///
    /// * `index` - The index at which to split the consumer.
    ///
    /// # Returns
    ///
    /// Returns two new consumers and a reducer for parallel reduction.
    fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
        let (left, right, reducer) = self.base.split_at(index);
        (
            LogProgressConsumer {
                base: left,
                progress: Arc::clone(&self.progress),
                total: self.total,
                logger: Arc::clone(&self.logger),
            },
            LogProgressConsumer {
                base: right,
                progress: Arc::clone(&self.progress),
                total: self.total,
                logger: Arc::clone(&self.logger),
            },
            reducer,
        )
    }

    fn into_folder(self) -> Self::Folder {
        LogProgressFolder {
            base: self.base.into_folder(),
            progress: self.progress,
            total: self.total,
            logger: Arc::clone(&self.logger),
        }
    }

    fn full(&self) -> bool {
        self.base.full()
    }
}

impl<C, T> UnindexedConsumer<T> for LogProgressConsumer<C>
where
    C: UnindexedConsumer<T>,
{
    fn split_off_left(&self) -> Self {
        LogProgressConsumer {
            base: self.base.split_off_left(),
            progress: Arc::clone(&self.progress),
            total: self.total,
            logger: Arc::clone(&self.logger),
        }
    }

    fn to_reducer(&self) -> Self::Reducer {
        self.base.to_reducer()
    }
}

/// A folder for processing items in parallel while tracking progress.
struct LogProgressFolder<F> {
    base: F,
    progress: Arc<AtomicUsize>,
    total: usize,
    logger: Arc<OrderedLogger>,
}

impl<F, T> Folder<T> for LogProgressFolder<F>
where
    F: Folder<T>,
{
    type Result = F::Result;

    /// Consumes an item and tracks progress.
    ///
    /// This method updates the progress counter and logs progress at specified intervals.
    ///
    /// # Arguments
    ///
    /// * `item` - The item to consume and process.
    ///
    /// # Returns
    ///
    /// Returns a new `LogProgressFolder` with the updated state.
    fn consume(self, item: T) -> Self {
        let old_count = self.progress.fetch_add(1, Ordering::Relaxed);
        let old_percent = (old_count * 100) / self.total;
        let new_percent = ((old_count + 1) * 100) / self.total;

        if new_percent / STEP_PERCENT > old_percent / STEP_PERCENT {
            let rounded_percent = new_percent - (new_percent % STEP_PERCENT);
            self.logger.log_progress(rounded_percent);
        }

        LogProgressFolder {
            base: self.base.consume(item),
            progress: self.progress,
            total: self.total,
            logger: self.logger,
        }
    }

    fn complete(self) -> Self::Result {
        self.base.complete()
    }

    fn full(&self) -> bool {
        self.base.full()
    }
}

/// Extension trait to add a `log_progress` method to parallel iterators.
pub trait LogProgressParExt: Sized + ParallelIterator {
    /// Wraps the parallel iterator with progress logging.
    ///
    /// This method will print progress updates every 2% of the way through the iteration.
    ///
    /// # Returns
    ///
    /// Returns a new `LogProgressPar` iterator which tracks and logs progress.
    fn log_progress(self, name: &str) -> LogProgressPar<Self>;
}

impl<I> LogProgressParExt for I
where
    I: ParallelIterator + IndexedParallelIterator,
{
    fn log_progress(self, name: &str) -> LogProgressPar<Self> {
        let total = self.len(); // Get the total number of items
        let t0 = std::time::Instant::now();
        let logger = OrderedLogger::new(t0); // Create the logger

        if *enable_log() {
            print!(
                "+{}+\n| {}{}|\n+{}+\n|",
                BARS,
                name,
                " ".repeat(49 - name.len()),
                BARS,
            );
        }

        LogProgressPar {
            iter: self,
            progress: Arc::new(AtomicUsize::new(0)),
            total,
            logger,
        }
    }
}

/// A long computation function to simulate more expensive work per item.
pub fn long_computation(x: u32) -> u32 {
    // Simulate a heavy calculation by introducing a delay
    let mut result = x;
    for _ in 0..1_000_000 {
        result = result.saturating_mul(2);
    }
    result
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_long_computation() {
        assert_eq!(long_computation(2), 4294967295); // Expected result
    }
}