rapidgzip-core 0.2.1

Parallel gzip, zlib, and raw-DEFLATE decoder using rapidgzip's marker/window algorithm
Documentation
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
//! Empirical concurrency control for parallel decode paths.
//!
//! Parallel gzip paths can become limited by memory bandwidth, task size, or
//! speculative buffers well before every visible processor is useful. The
//! controller therefore measures native worker completions while probing a
//! budget-derived range. Measurements happen before ordered output handoff, so
//! a slow `Read` consumer does not masquerade as a slow decoder.

use crate::runtime::RuntimeState;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

const RATE_TOLERANCE: f64 = 0.03;
const SAMPLES_PER_CANDIDATE: usize = 5;
const MINIMUM_CALIBRATION_WAVES: usize = 8;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Phase {
    Baseline,
    Down,
    Up,
    Stable,
}

/// Learns a useful decode/resolve concurrency without assuming a fixed cap.
///
/// A large request begins near twice the square root of the smaller of its
/// worker budget and visible processor count. That grows with both inputs
/// without immediately multiplying speculative memory by every processor.
/// Calibration first grows from that conservative bootstrap while each
/// increase improves throughput, then probes downward around the best setting.
/// A lower setting within the noise tolerance is preferred because it reduces
/// memory pressure without a material throughput loss.
#[derive(Debug)]
pub(crate) struct AdaptiveConcurrency {
    maximum: usize,
    current: usize,
    down_step: usize,
    up_step: usize,
    phase: Phase,
    sample_bytes: u64,
    generation: usize,
    interval_bytes: u64,
    interval_started: Option<Instant>,
    sample_rates: [f64; SAMPLES_PER_CANDIDATE],
    sample_count: usize,
    best_limit: usize,
    peak_rate: f64,
    measured: bool,
    retried_upward_candidate: bool,
}

impl AdaptiveConcurrency {
    pub(crate) fn new(
        maximum: usize,
        machine_parallelism: usize,
        sample_bytes: usize,
        work_items: usize,
    ) -> Self {
        debug_assert!(maximum != 0);
        let limits = controller_limits(maximum, machine_parallelism, work_items);
        Self {
            maximum: limits.maximum,
            current: limits.initial,
            down_step: limits.down_step,
            up_step: limits.up_step,
            phase: if limits.calibrate {
                Phase::Baseline
            } else {
                Phase::Stable
            },
            sample_bytes: sample_bytes.max(1) as u64,
            generation: 0,
            interval_bytes: 0,
            interval_started: None,
            sample_rates: [0.0; SAMPLES_PER_CANDIDATE],
            sample_count: 0,
            best_limit: limits.initial,
            peak_rate: 0.0,
            measured: false,
            retried_upward_candidate: false,
        }
    }

    pub(crate) const fn current_limit(&self) -> usize {
        self.current
    }

    /// Largest worker rank that calibration can enable for this machine.
    pub(crate) const fn worker_pool_limit(&self) -> usize {
        self.maximum
    }

    pub(crate) const fn generation(&self) -> usize {
        self.generation
    }

    pub(crate) const fn is_stable(&self) -> bool {
        matches!(self.phase, Phase::Stable)
    }

    /// Empirically selected worker count, if calibration actually ran.
    pub(crate) const fn best_limit(&self) -> Option<usize> {
        if self.is_stable() && self.measured {
            Some(self.best_limit)
        } else {
            None
        }
    }

    /// Discards a partial sample after an external limit change.
    pub(crate) fn pause_observation(&mut self) {
        self.reset_candidate();
    }

    /// Marks the beginning of native work performed under `generation`.
    ///
    /// Work begun before a concurrency change is deliberately excluded from
    /// the new candidate.  The first new task starts the candidate's clock;
    /// old tasks that are still leaving the pipeline can only contend with it,
    /// not inflate its completed-byte count.
    pub(crate) fn start_work(&mut self, generation: usize, now: Instant) {
        if !self.is_stable() && generation == self.generation && self.interval_started.is_none() {
            self.interval_started = Some(now);
        }
    }

    /// Records native bytes completed under `generation`.
    ///
    /// Returns true when either the active limit or calibration state changed.
    pub(crate) fn observe_work(
        &mut self,
        generation: usize,
        decoded_bytes: usize,
        now: Instant,
    ) -> bool {
        if self.is_stable() || generation != self.generation || decoded_bytes == 0 {
            return false;
        }
        let Some(started) = self.interval_started else {
            return false;
        };
        self.interval_bytes = self.interval_bytes.saturating_add(decoded_bytes as u64);
        if self.interval_bytes < self.sample_bytes {
            return false;
        }
        let Some(elapsed) = now.checked_duration_since(started) else {
            return false;
        };
        if elapsed.is_zero() {
            return false;
        }

        self.sample_rates[self.sample_count] =
            self.interval_bytes as f64 / elapsed.as_nanos() as f64;
        self.sample_count += 1;
        self.interval_bytes = 0;
        self.interval_started = Some(now);
        if self.sample_count < SAMPLES_PER_CANDIDATE {
            return false;
        }

        let mut samples = self.sample_rates;
        samples.sort_by(f64::total_cmp);
        self.evaluate(samples[SAMPLES_PER_CANDIDATE / 2])
    }

    fn evaluate(&mut self, rate: f64) -> bool {
        self.measured = true;
        match self.phase {
            Phase::Baseline => {
                self.peak_rate = rate;
                self.best_limit = self.current;
                self.begin_upward_search()
            }
            Phase::Down => {
                if rate >= self.peak_rate * (1.0 - RATE_TOLERANCE) {
                    self.peak_rate = self.peak_rate.max(rate);
                    self.best_limit = self.current;
                    let lower = self.current.saturating_sub(self.down_step).max(1);
                    if lower < self.current {
                        self.switch_to(lower, Phase::Down)
                    } else {
                        self.finish_at_best()
                    }
                } else {
                    self.finish_at_best()
                }
            }
            Phase::Up => {
                if self.current == self.maximum && self.maximum <= self.up_step.saturating_mul(2) {
                    self.peak_rate = self.peak_rate.max(rate);
                    self.best_limit = self.current;
                    return self.finish_at_best();
                }
                if rate > self.peak_rate * (1.0 + RATE_TOLERANCE) {
                    self.peak_rate = rate;
                    self.best_limit = self.current;
                    self.retried_upward_candidate = false;
                    let higher = self.current.saturating_add(self.up_step).min(self.maximum);
                    if higher > self.current {
                        self.switch_to(higher, Phase::Up)
                    } else {
                        self.begin_downward_search()
                    }
                } else if self.retried_upward_candidate {
                    self.retried_upward_candidate = false;
                    self.begin_downward_search()
                } else {
                    self.retried_upward_candidate = true;
                    self.retry_candidate()
                }
            }
            Phase::Stable => false,
        }
    }

    fn begin_upward_search(&mut self) -> bool {
        let higher = self
            .best_limit
            .saturating_add(self.up_step)
            .min(self.maximum);
        if higher <= self.best_limit {
            return self.begin_downward_search();
        }
        self.switch_to(higher, Phase::Up)
    }

    fn begin_downward_search(&mut self) -> bool {
        let lower = self.best_limit.saturating_sub(self.down_step).max(1);
        if lower >= self.best_limit {
            return self.finish_at_best();
        }
        self.switch_to(lower, Phase::Down)
    }

    fn switch_to(&mut self, limit: usize, phase: Phase) -> bool {
        debug_assert_ne!(limit, self.current);
        self.current = limit;
        self.phase = phase;
        self.generation = self.generation.wrapping_add(1);
        self.reset_candidate();
        true
    }

    fn retry_candidate(&mut self) -> bool {
        self.generation = self.generation.wrapping_add(1);
        self.reset_candidate();
        true
    }

    fn finish_at_best(&mut self) -> bool {
        let changed = self.current != self.best_limit || !self.is_stable();
        self.current = self.best_limit;
        self.phase = Phase::Stable;
        self.generation = self.generation.wrapping_add(1);
        self.reset_candidate();
        changed
    }

    fn reset_candidate(&mut self) {
        self.interval_bytes = 0;
        self.interval_started = None;
        self.sample_rates = [0.0; SAMPLES_PER_CANDIDATE];
        self.sample_count = 0;
    }
}

/// Admission control shared by parallel decode paths.
///
/// Worker ranks are created lazily as upward probes request them and retire
/// after a persistent downward decision. Candidate measurements count native
/// completions before ordered output handoff. Stable operation costs one
/// atomic rank check per task without touching the controller mutex.
pub(crate) struct AdaptiveWorkers {
    controller: Mutex<AdaptiveConcurrency>,
    generation: AtomicUsize,
    calibrating: AtomicBool,
    worker_pool_limit: usize,
    observed_limit_epoch: AtomicUsize,
    pub(crate) runtime: Arc<RuntimeState>,
}

impl AdaptiveWorkers {
    pub(crate) fn new(
        maximum: usize,
        machine_parallelism: usize,
        sample_bytes: usize,
        work_items: usize,
        runtime: Arc<RuntimeState>,
    ) -> Self {
        let controller =
            AdaptiveConcurrency::new(maximum, machine_parallelism, sample_bytes, work_items);
        let current_limit = controller.current_limit();
        let generation = controller.generation();
        let calibrating = !controller.is_stable();
        let worker_pool_limit = controller.worker_pool_limit();
        runtime.set_adaptive_target(current_limit);
        runtime.set_best_workers(controller.best_limit());
        Self {
            controller: Mutex::new(controller),
            generation: AtomicUsize::new(generation),
            calibrating: AtomicBool::new(calibrating),
            worker_pool_limit,
            observed_limit_epoch: AtomicUsize::new(runtime.limit_epoch()),
            runtime,
        }
    }

    pub(crate) fn current_limit(&self) -> usize {
        self.runtime.effective_worker_limit()
    }

    pub(crate) fn worker_enabled(&self, worker_index: usize) -> bool {
        worker_index < self.current_limit()
    }

    pub(crate) const fn worker_pool_limit(&self) -> usize {
        self.worker_pool_limit
    }

    pub(crate) fn wait_until_enabled_or_retire(
        &self,
        worker_index: usize,
        stopped: &AtomicBool,
    ) -> bool {
        const RETIRE_AFTER: Duration = Duration::from_millis(250);
        self.runtime.wait_for_limit_change(RETIRE_AFTER);
        !stopped.load(Ordering::Relaxed) && self.worker_enabled(worker_index)
    }

    pub(crate) fn start_work(&self) -> Option<usize> {
        if !self.calibrating.load(Ordering::Acquire) {
            return None;
        }
        let generation = self.generation.load(Ordering::Acquire);
        let limit_epoch = self.runtime.limit_epoch();
        let mut controller = self
            .controller
            .lock()
            .expect("adaptive worker mutex poisoned");
        if self
            .observed_limit_epoch
            .swap(limit_epoch, Ordering::AcqRel)
            != limit_epoch
            || self.current_limit() != controller.current_limit()
        {
            controller.pause_observation();
            return None;
        }
        controller.start_work(generation, Instant::now());
        Some(generation)
    }

    pub(crate) fn observe_work(&self, generation: Option<usize>, decoded_bytes: usize) -> bool {
        let Some(generation) = generation else {
            return false;
        };
        let mut controller = self
            .controller
            .lock()
            .expect("adaptive worker mutex poisoned");
        let changed = controller.observe_work(generation, decoded_bytes, Instant::now());
        if changed {
            self.generation
                .store(controller.generation(), Ordering::Release);
            self.calibrating
                .store(!controller.is_stable(), Ordering::Release);
            self.runtime.set_adaptive_target(controller.current_limit());
            self.runtime.set_best_workers(controller.best_limit());
        }
        changed
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct ControllerLimits {
    initial: usize,
    maximum: usize,
    down_step: usize,
    up_step: usize,
    calibrate: bool,
}

/// Derives both the bootstrap and worker-pool extent from the smaller of
/// affinity-visible processors and the caller's maximum worker budget.
fn controller_limits(
    maximum: usize,
    machine_parallelism: usize,
    work_items: usize,
) -> ControllerLimits {
    let visible = machine_parallelism.max(1);
    let maximum = maximum.min(visible);
    if maximum <= 4 {
        return ControllerLimits {
            initial: maximum,
            maximum,
            down_step: 1,
            up_step: 1,
            calibrate: false,
        };
    }

    let scaled_budget = maximum.saturating_mul(4);
    let square_root = scaled_budget.isqrt();
    let initial = square_root
        .saturating_add(usize::from(
            square_root.saturating_mul(square_root) < scaled_budget,
        ))
        .max(1)
        .min(maximum);
    let down_step = (initial / 8).max(1);
    let up_step = initial.max(1);
    let search_maximum = maximum;
    // Each candidate needs several waves of independent tasks to get beyond
    // startup and transition costs. On shorter streams, probing consumes a
    // material fraction of the entire decode, so the machine-derived bootstrap
    // is a better end-to-end choice than an optimum found just before EOF.
    let calibrate =
        initial < search_maximum && work_items >= initial.saturating_mul(MINIMUM_CALIBRATION_WAVES);
    ControllerLimits {
        initial,
        maximum: if calibrate { search_maximum } else { initial },
        down_step,
        up_step,
        calibrate,
    }
}

/// Initial worker target shared by path admission and steady-state control.
///
/// Keeping this derivation in one place ensures that an empirical path probe
/// never starts more workers than the marker pipeline would initially admit.
pub(crate) fn initial_parallelism(
    maximum: usize,
    machine_parallelism: usize,
    work_items: usize,
) -> usize {
    controller_limits(maximum.max(1), machine_parallelism, work_items).initial
}

#[cfg(test)]
mod tests {
    use super::{AdaptiveConcurrency, ControllerLimits, SAMPLES_PER_CANDIDATE, controller_limits};
    use std::time::{Duration, Instant};

    const SAMPLE_BYTES: usize = 100;

    fn feed_rate(controller: &mut AdaptiveConcurrency, cursor: &mut Instant, rate: u64) -> bool {
        let generation = controller.generation();
        controller.start_work(generation, *cursor);
        let mut changed = false;
        for _ in 0..SAMPLES_PER_CANDIDATE {
            *cursor += Duration::from_nanos((SAMPLE_BYTES as u64 * 1_000 / rate).max(1));
            changed |= controller.observe_work(generation, SAMPLE_BYTES, *cursor);
        }
        changed
    }

    #[test]
    fn machine_wide_pool_is_sublinear_and_bidirectional() {
        assert_eq!(
            controller_limits(64, 88, usize::MAX),
            ControllerLimits {
                initial: 16,
                maximum: 64,
                down_step: 2,
                up_step: 16,
                calibrate: true,
            }
        );
        assert_eq!(controller_limits(64, 64, usize::MAX).initial, 16);
        assert_eq!(controller_limits(44, 44, usize::MAX).initial, 14);
        assert_eq!(controller_limits(64, 8, usize::MAX).initial, 6);
    }

    #[test]
    fn smaller_explicit_budgets_control_the_bootstrap() {
        let controller = AdaptiveConcurrency::new(16, 88, SAMPLE_BYTES, usize::MAX);
        assert_eq!(controller.current_limit(), 8);
        assert_eq!(controller.worker_pool_limit(), 16);
        assert!(!controller.is_stable());
    }

    #[test]
    fn modest_budget_grows_from_bootstrap_to_the_requested_ceiling() {
        let mut cursor = Instant::now();
        let mut controller = AdaptiveConcurrency::new(16, 88, SAMPLE_BYTES, usize::MAX);
        assert_eq!(controller.current_limit(), 8);
        assert!(feed_rate(&mut controller, &mut cursor, 1_000));
        assert_eq!(controller.current_limit(), 16);
        assert!(feed_rate(&mut controller, &mut cursor, 500));
        assert_eq!(controller.current_limit(), 16);
        assert!(controller.is_stable());
    }

    #[test]
    fn bootstrap_is_monotonic_across_requested_budgets() {
        let below = controller_limits(16, 88, usize::MAX);
        let middle = controller_limits(44, 88, usize::MAX);
        let above = controller_limits(45, 88, usize::MAX);
        assert_eq!(below.initial, 8);
        assert_eq!(middle.initial, 14);
        assert_eq!(above.initial, 14);
        assert!(middle.maximum <= above.maximum);
    }

    #[test]
    fn searches_down_and_prefers_a_near_tied_lower_limit() {
        let mut cursor = Instant::now();
        let mut controller = AdaptiveConcurrency::new(64, 81, SAMPLE_BYTES, usize::MAX);
        assert_eq!(controller.current_limit(), 16);
        assert!(feed_rate(&mut controller, &mut cursor, 1_000));
        assert_eq!(controller.current_limit(), 32);
        assert!(feed_rate(&mut controller, &mut cursor, 990));
        assert_eq!(controller.current_limit(), 32);
        assert!(feed_rate(&mut controller, &mut cursor, 990));
        assert_eq!(controller.current_limit(), 14);
        assert!(feed_rate(&mut controller, &mut cursor, 990));
        assert_eq!(controller.current_limit(), 12);
        assert!(feed_rate(&mut controller, &mut cursor, 900));
        assert_eq!(controller.current_limit(), 14);
        assert!(controller.is_stable());
    }

    #[test]
    fn searches_up_until_a_candidate_stops_improving() {
        let mut cursor = Instant::now();
        let mut controller = AdaptiveConcurrency::new(64, 81, SAMPLE_BYTES, usize::MAX);
        assert!(feed_rate(&mut controller, &mut cursor, 1_000));
        assert_eq!(controller.current_limit(), 32);
        assert!(feed_rate(&mut controller, &mut cursor, 1_100));
        assert_eq!(controller.current_limit(), 48);
        assert!(feed_rate(&mut controller, &mut cursor, 1_180));
        assert_eq!(controller.current_limit(), 64);
        assert!(feed_rate(&mut controller, &mut cursor, 1_170));
        assert_eq!(controller.current_limit(), 64);
        assert!(feed_rate(&mut controller, &mut cursor, 1_170));
        assert_eq!(controller.current_limit(), 46);
        assert!(feed_rate(&mut controller, &mut cursor, 1_000));
        assert_eq!(controller.current_limit(), 48);
        assert!(controller.is_stable());
    }

    #[test]
    fn ignores_work_from_an_old_generation() {
        let mut cursor = Instant::now();
        let mut controller = AdaptiveConcurrency::new(64, 81, SAMPLE_BYTES, usize::MAX);
        let old_generation = controller.generation();
        assert!(feed_rate(&mut controller, &mut cursor, 1_000));
        assert_ne!(controller.generation(), old_generation);
        controller.start_work(old_generation, cursor);
        cursor += Duration::from_millis(1);
        assert!(!controller.observe_work(old_generation, SAMPLE_BYTES * 10, cursor));
        assert_eq!(controller.current_limit(), 32);
    }

    #[test]
    fn short_inputs_use_the_machine_bootstrap_without_probing() {
        let controller = AdaptiveConcurrency::new(64, 88, SAMPLE_BYTES, 100);
        assert_eq!(controller.current_limit(), 16);
        assert_eq!(controller.worker_pool_limit(), 16);
        assert!(controller.is_stable());
    }
}