codspeed_divan_compat_walltime/bench/
mod.rs

1use std::{
2    cell::UnsafeCell,
3    fmt,
4    mem::{self, MaybeUninit},
5    num::NonZeroUsize,
6    sync::Barrier,
7};
8
9use crate::{
10    alloc::{
11        AllocOp, AllocOpMap, AllocTally, ThreadAllocInfo, ThreadAllocTally, TotalAllocTallyMap,
12    },
13    black_box, black_box_drop,
14    counter::{
15        AnyCounter, AsCountUInt, BytesCount, CharsCount, Counter, CounterCollection, CyclesCount,
16        IntoCounter, ItemsCount, KnownCounterKind, MaxCountUInt,
17    },
18    divan::SharedContext,
19    stats::{RawSample, SampleCollection, Stats, StatsSet, TimeSample},
20    thread_pool::BENCH_POOL,
21    time::{FineDuration, Timestamp, UntaggedTimestamp},
22    util::{self, sync::SyncWrap, Unit},
23};
24
25#[cfg(test)]
26mod tests;
27
28mod args;
29mod defer;
30mod options;
31
32use defer::{DeferSlot, DeferStore};
33
34pub use self::{
35    args::{BenchArgs, BenchArgsRunner},
36    options::BenchOptions,
37};
38
39pub(crate) const DEFAULT_SAMPLE_COUNT: u32 = 100;
40
41/// Enables contextual benchmarking in [`#[divan::bench]`](attr.bench.html).
42///
43/// # Examples
44///
45/// ```
46/// use divan::{Bencher, black_box};
47///
48/// #[divan::bench]
49/// fn copy_from_slice(bencher: Bencher) {
50///     // Input and output buffers get used in the closure.
51///     let src = (0..100).collect::<Vec<i32>>();
52///     let mut dst = vec![0; src.len()];
53///
54///     bencher.bench_local(|| {
55///         black_box(&mut dst).copy_from_slice(black_box(&src));
56///     });
57/// }
58/// ```
59#[must_use = "a benchmark function must be registered"]
60pub struct Bencher<'a, 'b, C = BencherConfig> {
61    pub(crate) context: &'a mut BenchContext<'b>,
62    pub(crate) config: C,
63}
64
65/// Public-in-private type for statically-typed `Bencher` configuration.
66///
67/// This enables configuring `Bencher` using the builder pattern with zero
68/// runtime cost.
69pub struct BencherConfig<GenI = Unit> {
70    gen_input: GenI,
71}
72
73impl<C> fmt::Debug for Bencher<'_, '_, C> {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        f.debug_struct("Bencher").finish_non_exhaustive()
76    }
77}
78
79impl<'a, 'b> Bencher<'a, 'b> {
80    #[inline]
81    pub(crate) fn new(context: &'a mut BenchContext<'b>) -> Self {
82        Self { context, config: BencherConfig { gen_input: Unit } }
83    }
84}
85
86impl<'a, 'b> Bencher<'a, 'b> {
87    /// Benchmarks a function.
88    ///
89    /// The function can be benchmarked in parallel using the [`threads`
90    /// option](macro@crate::bench#threads). If the function is strictly
91    /// single-threaded, use [`Bencher::bench_local`] instead.
92    ///
93    /// # Examples
94    ///
95    /// ```
96    /// #[divan::bench]
97    /// fn bench(bencher: divan::Bencher) {
98    ///     bencher.bench(|| {
99    ///         // Benchmarked code...
100    ///     });
101    /// }
102    /// ```
103    pub fn bench<O, B>(self, benched: B)
104    where
105        B: Fn() -> O + Sync,
106    {
107        // Reusing `bench_values` for a zero-sized non-drop input type should
108        // have no overhead.
109        self.with_inputs(|| ()).bench_values(|_: ()| benched());
110    }
111
112    /// Benchmarks a function on the current thread.
113    ///
114    /// # Examples
115    ///
116    /// ```
117    /// #[divan::bench]
118    /// fn bench(bencher: divan::Bencher) {
119    ///     bencher.bench_local(|| {
120    ///         // Benchmarked code...
121    ///     });
122    /// }
123    /// ```
124    pub fn bench_local<O, B>(self, mut benched: B)
125    where
126        B: FnMut() -> O,
127    {
128        // Reusing `bench_local_values` for a zero-sized non-drop input type
129        // should have no overhead.
130        self.with_inputs(|| ()).bench_local_values(|_: ()| benched());
131    }
132
133    /// Generate inputs for the [benchmarked function](#input-bench).
134    ///
135    /// Time spent generating inputs does not affect benchmark timing.
136    ///
137    /// When [benchmarking in parallel](macro@crate::bench#threads), the input
138    /// generator is called on the same thread as the sample loop that uses that
139    /// input.
140    ///
141    /// # Examples
142    ///
143    /// ```
144    /// #[divan::bench]
145    /// fn bench(bencher: divan::Bencher) {
146    ///     bencher
147    ///         .with_inputs(|| {
148    ///             // Generate input:
149    ///             String::from("...")
150    ///         })
151    ///         .bench_values(|s| {
152    ///             // Use input by-value:
153    ///             s + "123"
154    ///         });
155    /// }
156    /// ```
157    pub fn with_inputs<G>(self, gen_input: G) -> Bencher<'a, 'b, BencherConfig<G>> {
158        Bencher { context: self.context, config: BencherConfig { gen_input } }
159    }
160}
161
162impl<'a, 'b, GenI> Bencher<'a, 'b, BencherConfig<GenI>> {
163    /// Assign a [`Counter`] for all iterations of the benchmarked function.
164    ///
165    /// This will either:
166    /// - Assign a new counter
167    /// - Override an existing counter of the same type
168    ///
169    /// If the counter depends on [generated inputs](Self::with_inputs), use
170    /// [`Bencher::input_counter`] instead.
171    ///
172    /// If context is not needed, the counter can instead be set via
173    /// [`#[divan::bench(counters = ...)]`](macro@crate::bench#counters).
174    ///
175    /// # Examples
176    ///
177    /// ```
178    /// use divan::{Bencher, counter::BytesCount};
179    ///
180    /// #[divan::bench]
181    /// fn char_count(bencher: Bencher) {
182    ///     let s: String = // ...
183    ///     # String::new();
184    ///
185    ///     bencher
186    ///         .counter(BytesCount::of_str(&s))
187    ///         .bench(|| {
188    ///             divan::black_box(&s).chars().count()
189    ///         });
190    /// }
191    /// ```
192    #[doc(alias = "throughput")]
193    pub fn counter<C>(self, counter: C) -> Self
194    where
195        C: IntoCounter,
196    {
197        let counter = AnyCounter::new(counter);
198        self.context.counters.set_counter(counter);
199        self
200    }
201}
202
203/// <span id="input-bench"></span> Benchmark over [generated inputs](Self::with_inputs).
204impl<'a, 'b, I, GenI> Bencher<'a, 'b, BencherConfig<GenI>>
205where
206    GenI: FnMut() -> I,
207{
208    /// Calls a closure to create a [`Counter`] for each input of the
209    /// benchmarked function.
210    ///
211    /// This will either:
212    /// - Assign a new counter
213    /// - Override an existing counter of the same type
214    ///
215    /// If the counter is constant, use [`Bencher::counter`] instead.
216    ///
217    /// When [benchmarking in parallel](macro@crate::bench#threads), the input
218    /// counter is called on the same thread as the sample loop that generates
219    /// and uses that input.
220    ///
221    /// # Examples
222    ///
223    /// The following example emits info for the number of bytes processed when
224    /// benchmarking [`char`-counting](std::str::Chars::count). The byte count
225    /// is gotten by calling [`BytesCount::of_str`] on each iteration's input
226    /// [`String`].
227    ///
228    /// ```
229    /// use divan::{Bencher, counter::BytesCount};
230    ///
231    /// #[divan::bench]
232    /// fn char_count(bencher: Bencher) {
233    ///     bencher
234    ///         .with_inputs(|| -> String {
235    ///             // ...
236    ///             # String::new()
237    ///         })
238    ///         .input_counter(BytesCount::of_str)
239    ///         .bench_refs(|s| {
240    ///             s.chars().count()
241    ///         });
242    /// }
243    /// ```
244    pub fn input_counter<C, F>(self, make_counter: F) -> Self
245    where
246        F: Fn(&I) -> C + Sync + 'static,
247        C: IntoCounter,
248    {
249        self.context.counters.set_input_counter(make_counter);
250        self
251    }
252
253    /// Creates a [`Counter`] from each input of the benchmarked function.
254    ///
255    /// This may be used if the input returns [`u8`]–[`u64`], [`usize`], or any
256    /// nesting of references to those types.
257    ///
258    /// # Examples
259    ///
260    /// The following example emits info for the number of items processed when
261    /// benchmarking [`FromIterator`] from
262    /// <code>[Range](std::ops::Range)<[usize]></code> to [`Vec`].
263    ///
264    /// ```
265    /// use divan::{Bencher, counter::ItemsCount};
266    ///
267    /// #[divan::bench]
268    /// fn range_to_vec(bencher: Bencher) {
269    ///     bencher
270    ///         .with_inputs(|| -> usize {
271    ///             // ...
272    ///             # 0
273    ///         })
274    ///         .count_inputs_as::<ItemsCount>()
275    ///         .bench_values(|n| -> Vec<usize> {
276    ///             (0..n).collect()
277    ///         });
278    /// }
279    /// ```
280    #[inline]
281    pub fn count_inputs_as<C>(self) -> Self
282    where
283        C: Counter,
284        I: AsCountUInt,
285    {
286        match KnownCounterKind::of::<C>() {
287            KnownCounterKind::Bytes => self.input_counter(|c| BytesCount::from(c)),
288            KnownCounterKind::Chars => self.input_counter(|c| CharsCount::from(c)),
289            KnownCounterKind::Cycles => self.input_counter(|c| CyclesCount::from(c)),
290            KnownCounterKind::Items => self.input_counter(|c| ItemsCount::from(c)),
291        }
292    }
293
294    /// Benchmarks a function over per-iteration [generated inputs](Self::with_inputs),
295    /// provided by-value.
296    ///
297    /// Per-iteration means the benchmarked function is called exactly once for
298    /// each generated input.
299    ///
300    /// The function can be benchmarked in parallel using the [`threads`
301    /// option](macro@crate::bench#threads). If the function is strictly
302    /// single-threaded, use [`Bencher::bench_local_values`] instead.
303    ///
304    /// # Examples
305    ///
306    /// ```
307    /// #[divan::bench]
308    /// fn bench(bencher: divan::Bencher) {
309    ///     bencher
310    ///         .with_inputs(|| {
311    ///             // Generate input:
312    ///             String::from("...")
313    ///         })
314    ///         .bench_values(|s| {
315    ///             // Use input by-value:
316    ///             s + "123"
317    ///         });
318    /// }
319    /// ```
320    pub fn bench_values<O, B>(self, benched: B)
321    where
322        B: Fn(I) -> O + Sync,
323        GenI: Fn() -> I + Sync,
324    {
325        self.context.bench_loop_threaded(
326            self.config.gen_input,
327            |input| {
328                // SAFETY: Input is guaranteed to be initialized and not
329                // currently referenced by anything else.
330                let input = unsafe { input.get().read().assume_init() };
331
332                benched(input)
333            },
334            // Input ownership is transferred to `benched`.
335            |_input| {},
336        );
337    }
338
339    /// Benchmarks a function over per-iteration [generated inputs](Self::with_inputs),
340    /// provided by-value.
341    ///
342    /// Per-iteration means the benchmarked function is called exactly once for
343    /// each generated input.
344    ///
345    /// # Examples
346    ///
347    /// ```
348    /// #[divan::bench]
349    /// fn bench(bencher: divan::Bencher) {
350    ///     let mut values = Vec::new();
351    ///     bencher
352    ///         .with_inputs(|| {
353    ///             // Generate input:
354    ///             String::from("...")
355    ///         })
356    ///         .bench_local_values(|s| {
357    ///             // Use input by-value:
358    ///             values.push(s);
359    ///         });
360    /// }
361    /// ```
362    pub fn bench_local_values<O, B>(self, mut benched: B)
363    where
364        B: FnMut(I) -> O,
365    {
366        self.context.bench_loop_local(
367            self.config.gen_input,
368            |input| {
369                // SAFETY: Input is guaranteed to be initialized and not
370                // currently referenced by anything else.
371                let input = unsafe { input.get().read().assume_init() };
372
373                benched(input)
374            },
375            // Input ownership is transferred to `benched`.
376            |_input| {},
377        );
378    }
379
380    /// Benchmarks a function over per-iteration [generated inputs](Self::with_inputs),
381    /// provided by-reference.
382    ///
383    /// Per-iteration means the benchmarked function is called exactly once for
384    /// each generated input.
385    ///
386    /// # Examples
387    ///
388    /// ```
389    /// #[divan::bench]
390    /// fn bench(bencher: divan::Bencher) {
391    ///     bencher
392    ///         .with_inputs(|| {
393    ///             // Generate input:
394    ///             String::from("...")
395    ///         })
396    ///         .bench_refs(|s| {
397    ///             // Use input by-reference:
398    ///             *s += "123";
399    ///         });
400    /// }
401    /// ```
402    pub fn bench_refs<O, B>(self, benched: B)
403    where
404        B: Fn(&mut I) -> O + Sync,
405        GenI: Fn() -> I + Sync,
406    {
407        // TODO: Allow `O` to reference `&mut I` as long as `I` outlives `O`.
408        self.context.bench_loop_threaded(
409            self.config.gen_input,
410            |input| {
411                // SAFETY: Input is guaranteed to be initialized and not
412                // currently referenced by anything else.
413                let input = unsafe { (*input.get()).assume_init_mut() };
414
415                benched(input)
416            },
417            // Input ownership was not transferred to `benched`.
418            |input| {
419                // SAFETY: This function is called after `benched` outputs are
420                // dropped, so we have exclusive access.
421                unsafe { (*input.get()).assume_init_drop() }
422            },
423        );
424    }
425
426    /// Benchmarks a function over per-iteration [generated inputs](Self::with_inputs),
427    /// provided by-reference.
428    ///
429    /// Per-iteration means the benchmarked function is called exactly once for
430    /// each generated input.
431    ///
432    /// # Examples
433    ///
434    /// ```
435    /// #[divan::bench]
436    /// fn bench(bencher: divan::Bencher) {
437    ///     bencher
438    ///         .with_inputs(|| {
439    ///             // Generate input:
440    ///             String::from("...")
441    ///         })
442    ///         .bench_local_refs(|s| {
443    ///             // Use input by-reference:
444    ///             *s += "123";
445    ///         });
446    /// }
447    /// ```
448    pub fn bench_local_refs<O, B>(self, mut benched: B)
449    where
450        B: FnMut(&mut I) -> O,
451    {
452        // TODO: Allow `O` to reference `&mut I` as long as `I` outlives `O`.
453        self.context.bench_loop_local(
454            self.config.gen_input,
455            |input| {
456                // SAFETY: Input is guaranteed to be initialized and not
457                // currently referenced by anything else.
458                let input = unsafe { (*input.get()).assume_init_mut() };
459
460                benched(input)
461            },
462            // Input ownership was not transferred to `benched`.
463            |input| {
464                // SAFETY: This function is called after `benched` outputs are
465                // dropped, so we have exclusive access.
466                unsafe { (*input.get()).assume_init_drop() }
467            },
468        );
469    }
470}
471
472/// State machine for how the benchmark is being run.
473#[derive(Clone, Copy)]
474pub(crate) enum BenchMode {
475    /// The benchmark is being run as `--test`.
476    ///
477    /// Don't collect samples and run exactly once.
478    Test,
479
480    /// Scale `sample_size` to determine the right size for collecting.
481    Tune { sample_size: u32 },
482
483    /// Simply collect samples.
484    Collect { sample_size: u32 },
485}
486
487impl BenchMode {
488    #[inline]
489    pub fn is_test(self) -> bool {
490        matches!(self, Self::Test)
491    }
492
493    #[inline]
494    pub fn is_tune(self) -> bool {
495        matches!(self, Self::Tune { .. })
496    }
497
498    #[inline]
499    pub fn is_collect(self) -> bool {
500        matches!(self, Self::Collect { .. })
501    }
502
503    #[inline]
504    pub fn sample_size(self) -> u32 {
505        match self {
506            Self::Test => 1,
507            Self::Tune { sample_size, .. } | Self::Collect { sample_size, .. } => sample_size,
508        }
509    }
510}
511
512/// `#[divan::bench]` loop context.
513///
514/// Functions called within the benchmark loop should be `#[inline(always)]` to
515/// ensure instruction cache locality.
516pub(crate) struct BenchContext<'a> {
517    shared_context: &'a SharedContext,
518
519    /// User-configured options.
520    pub options: &'a BenchOptions<'a>,
521
522    /// Whether the benchmark loop was started.
523    pub did_run: bool,
524
525    /// The number of threads to run the benchmark. The default is 1.
526    ///
527    /// When set to 1, the benchmark loop is guaranteed to stay on the current
528    /// thread and not spawn any threads.
529    pub thread_count: NonZeroUsize,
530
531    /// Recorded samples.
532    pub samples: SampleCollection,
533
534    /// Per-iteration counters grouped by sample.
535    counters: CounterCollection,
536}
537
538impl<'a> BenchContext<'a> {
539    /// Creates a new benchmarking context.
540    pub fn new(
541        shared_context: &'a SharedContext,
542        options: &'a BenchOptions,
543        thread_count: NonZeroUsize,
544    ) -> Self {
545        Self {
546            shared_context,
547            options,
548            thread_count,
549            did_run: false,
550            samples: SampleCollection::default(),
551            counters: options.counters.to_collection(),
552        }
553    }
554
555    /// Runs the single-threaded loop for benchmarking `benched`.
556    ///
557    /// # Safety
558    ///
559    /// See `bench_loop_threaded`.
560    pub fn bench_loop_local<I, O>(
561        &mut self,
562        gen_input: impl FnMut() -> I,
563        benched: impl FnMut(&UnsafeCell<MaybeUninit<I>>) -> O,
564        drop_input: impl Fn(&UnsafeCell<MaybeUninit<I>>),
565    ) {
566        // SAFETY: Closures are guaranteed to run on the current thread, so they
567        // can safely be mutable and non-`Sync`.
568        unsafe {
569            let gen_input = SyncWrap::new(UnsafeCell::new(gen_input));
570            let benched = SyncWrap::new(UnsafeCell::new(benched));
571            let drop_input = SyncWrap::new(drop_input);
572
573            self.thread_count = NonZeroUsize::MIN;
574            self.bench_loop_threaded::<I, O>(
575                || (*gen_input.get())(),
576                |input| (*benched.get())(input),
577                |input| drop_input(input),
578            )
579        }
580    }
581
582    /// Runs the multi-threaded loop for benchmarking `benched`.
583    ///
584    /// # Safety
585    ///
586    /// If `self.threads` is 1, the incoming closures will not escape the
587    /// current thread. This guarantee ensures `bench_loop_local` can soundly
588    /// reuse this method with mutable non-`Sync` closures.
589    ///
590    /// When `benched` is called:
591    /// - `I` is guaranteed to be initialized.
592    /// - No external `&I` or `&mut I` exists.
593    ///
594    /// When `drop_input` is called:
595    /// - All instances of `O` returned from `benched` have been dropped.
596    /// - The same guarantees for `I` apply as in `benched`, unless `benched`
597    ///   escaped references to `I`.
598    fn bench_loop_threaded<I, O>(
599        &mut self,
600        gen_input: impl Fn() -> I + Sync,
601        benched: impl Fn(&UnsafeCell<MaybeUninit<I>>) -> O + Sync,
602        drop_input: impl Fn(&UnsafeCell<MaybeUninit<I>>) + Sync,
603    ) {
604        self.did_run = true;
605
606        let mut current_mode = self.initial_mode();
607        let is_test = current_mode.is_test();
608
609        let record_sample = self.sample_recorder(gen_input, benched, drop_input);
610
611        let thread_count = self.thread_count.get();
612        let aux_thread_count = thread_count - 1;
613
614        let is_single_thread = aux_thread_count == 0;
615
616        // Per-thread sample info returned by `record_sample`. These are
617        // processed locally to emit user-facing sample info. As a result, this
618        // only contains `thread_count` many elements at a time.
619        let mut raw_samples = Vec::<Option<RawSample>>::new();
620
621        // The time spent benchmarking, in picoseconds.
622        //
623        // Unless `skip_ext_time` is set, this includes time external to
624        // `benched`, such as time spent generating inputs and running drop.
625        let mut elapsed_picos: u128 = 0;
626
627        // The minimum time for benchmarking, in picoseconds.
628        let min_picos = self.options.min_time().picos;
629
630        // The remaining time left for benchmarking, in picoseconds.
631        let max_picos = self.options.max_time().picos;
632
633        // Don't bother running if user specifies 0 max time or 0 samples.
634        if max_picos == 0 || !self.options.has_samples() {
635            return;
636        }
637
638        let timer = self.shared_context.timer;
639        let timer_kind = timer.kind();
640
641        let mut rem_samples = if current_mode.is_collect() {
642            Some(self.options.sample_count.unwrap_or(DEFAULT_SAMPLE_COUNT))
643        } else {
644            None
645        };
646
647        // Only measure precision if we need to tune sample size.
648        let timer_precision =
649            if current_mode.is_tune() { timer.precision() } else { FineDuration::default() };
650
651        if !is_test {
652            self.samples.time_samples.reserve(self.options.sample_count.unwrap_or(1) as usize);
653        }
654
655        let skip_ext_time = self.options.skip_ext_time.unwrap_or_default();
656        let initial_start = if skip_ext_time { None } else { Some(Timestamp::start(timer_kind)) };
657
658        let bench_overheads = timer.bench_overheads();
659
660        let _guard = codspeed::fifo::BenchGuard::new_with_runner_fifo();
661        while {
662            // Conditions for when sampling is over:
663            if elapsed_picos >= max_picos {
664                // Depleted the benchmarking time budget. This is a strict
665                // condition regardless of sample count and minimum time.
666                false
667            } else if rem_samples.unwrap_or(1) > 0 {
668                // More samples expected.
669                true
670            } else {
671                // Continue if we haven't reached the time floor.
672                elapsed_picos < min_picos
673            }
674        } {
675            let sample_size = current_mode.sample_size();
676            self.samples.sample_size = sample_size;
677
678            let barrier = if is_single_thread { None } else { Some(Barrier::new(thread_count)) };
679
680            // Sample loop helper:
681            let record_sample = || -> RawSample {
682                let mut counter_totals: [u128; KnownCounterKind::COUNT] =
683                    [0; KnownCounterKind::COUNT];
684
685                // Updates per-input counter info for this sample.
686                let mut count_input = |input: &I| {
687                    for counter_kind in KnownCounterKind::ALL {
688                        // SAFETY: The `I` type cannot change since `with_inputs`
689                        // cannot be called more than once on the same `Bencher`.
690                        if let Some(count) =
691                            unsafe { self.counters.get_input_count(counter_kind, input) }
692                        {
693                            let total = &mut counter_totals[counter_kind as usize];
694                            *total = (*total).saturating_add(count as u128);
695                        }
696                    }
697                };
698
699                // Sample loop:
700                let ([start, end], alloc_info) =
701                    record_sample(sample_size as usize, barrier.as_ref(), &mut count_input);
702
703                RawSample { start, end, timer, alloc_info, counter_totals }
704            };
705
706            // Sample loop:
707            raw_samples.clear();
708            BENCH_POOL.par_extend(&mut raw_samples, aux_thread_count, |_| record_sample());
709
710            // Convert `&[Option<RawSample>]` to `&[Sample]`.
711            let raw_samples: &[RawSample] = {
712                if let Some(thread) = raw_samples
713                    .iter()
714                    .enumerate()
715                    .find_map(|(thread, sample)| sample.is_none().then_some(thread))
716                {
717                    panic!("Divan benchmarking thread {thread} panicked");
718                }
719
720                unsafe {
721                    assert_eq!(mem::size_of::<RawSample>(), mem::size_of::<Option<RawSample>>());
722                    std::slice::from_raw_parts(raw_samples.as_ptr().cast(), raw_samples.len())
723                }
724            };
725
726            // If testing, exit the benchmarking loop immediately after timing a
727            // single run.
728            if is_test {
729                break;
730            }
731
732            let slowest_sample = raw_samples.iter().max_by_key(|s| s.duration()).unwrap();
733            let slowest_time = slowest_sample.duration();
734
735            // TODO: Make tuning be less influenced by early runs. Currently if
736            // early runs are very quick but later runs are slow, benchmarking
737            // will take a very long time.
738            //
739            // TODO: Make `sample_size` consider time generating inputs and
740            // dropping inputs/outputs. Currently benchmarks like
741            // `Bencher::bench_refs(String::clear)` take a very long time.
742            if current_mode.is_tune() {
743                // Clear previous smaller samples.
744                self.samples.clear();
745                self.counters.clear_input_counts();
746
747                // If within 100x timer precision, continue tuning.
748                let precision_multiple = slowest_time.picos / timer_precision.picos;
749                if precision_multiple <= 100 {
750                    current_mode = BenchMode::Tune { sample_size: sample_size * 2 };
751                } else {
752                    current_mode = BenchMode::Collect { sample_size };
753                    rem_samples = Some(self.options.sample_count.unwrap_or(DEFAULT_SAMPLE_COUNT));
754                }
755            }
756
757            // Returns the sample's duration adjusted for overhead.
758            let sample_duration_sub_overhead = |raw_sample: &RawSample| {
759                let overhead = bench_overheads.total_overhead(sample_size, &raw_sample.alloc_info);
760
761                FineDuration {
762                    picos: raw_sample
763                        .duration()
764                        .clamp_to(timer_precision)
765                        .picos
766                        .saturating_sub(overhead.picos),
767                }
768                .clamp_to(timer_precision)
769            };
770
771            for raw_sample in raw_samples {
772                let sample_index = self.samples.time_samples.len();
773
774                self.samples
775                    .time_samples
776                    .push(TimeSample { duration: sample_duration_sub_overhead(raw_sample) });
777
778                if !raw_sample.alloc_info.tallies.is_empty() {
779                    self.samples
780                        .alloc_info_by_sample
781                        .insert(sample_index as u32, raw_sample.alloc_info.clone());
782                }
783
784                // Insert per-input counter information.
785                for counter_kind in KnownCounterKind::ALL {
786                    if !self.counters.uses_input_counts(counter_kind) {
787                        continue;
788                    }
789
790                    let total_count = raw_sample.counter_totals[counter_kind as usize];
791
792                    // Cannot overflow `MaxCountUInt` because `total_count`
793                    // cannot exceed `MaxCountUInt::MAX * sample_size`.
794                    let per_iter_count = (total_count / sample_size as u128) as MaxCountUInt;
795
796                    self.counters.push_counter(AnyCounter::known(counter_kind, per_iter_count));
797                }
798
799                if let Some(rem_samples) = &mut rem_samples {
800                    *rem_samples = rem_samples.saturating_sub(1);
801                }
802            }
803
804            if let Some(initial_start) = initial_start {
805                let last_end = raw_samples.iter().map(|s| s.end).max().unwrap();
806                elapsed_picos = last_end.duration_since(initial_start, timer).picos;
807            } else {
808                // Progress by at least 1ns to prevent extremely fast
809                // functions from taking forever when `min_time` is set.
810                let progress_picos = slowest_time.picos.max(1_000);
811                elapsed_picos = elapsed_picos.saturating_add(progress_picos);
812            }
813        }
814        core::mem::drop(_guard);
815
816        // Reset flag for ignoring allocations.
817        crate::alloc::IGNORE_ALLOC.set(false);
818    }
819
820    /// Returns a closure that takes the sample size and input counter, and then
821    /// returns a newly recorded sample.
822    fn sample_recorder<I, O>(
823        &self,
824        gen_input: impl Fn() -> I,
825        benched: impl Fn(&UnsafeCell<MaybeUninit<I>>) -> O,
826        drop_input: impl Fn(&UnsafeCell<MaybeUninit<I>>),
827    ) -> impl Fn(usize, Option<&Barrier>, &mut dyn FnMut(&I)) -> ([Timestamp; 2], ThreadAllocInfo)
828    {
829        // We defer:
830        // - Usage of `gen_input` values.
831        // - Drop destructor for `O`, preventing it from affecting sample
832        //   measurements. Outputs are stored into a pre-allocated buffer during
833        //   the sample loop. The allocation is reused between samples to reduce
834        //   time spent between samples.
835
836        let timer_kind = self.shared_context.timer.kind();
837
838        move |sample_size: usize, barrier: Option<&Barrier>, count_input: &mut dyn FnMut(&I)| {
839            let mut defer_store = DeferStore::<I, O>::default();
840
841            let mut saved_alloc_info = ThreadAllocInfo::new();
842            let mut save_alloc_info = || {
843                if crate::alloc::IGNORE_ALLOC.get() {
844                    return;
845                }
846
847                if let Some(alloc_info) = ThreadAllocInfo::try_current() {
848                    // SAFETY: We have exclusive access.
849                    saved_alloc_info = unsafe { alloc_info.as_ptr().read() };
850                }
851            };
852
853            // Synchronize all threads to start timed section simultaneously and
854            // clear every thread's memory profiling info.
855            //
856            // This ensures work external to the timed section does not affect
857            // the timing of other threads.
858            let sync_threads = |is_start: bool| {
859                sync_impl(barrier, is_start);
860
861                // Monomorphize implementation to reduce code size.
862                #[inline(never)]
863                fn sync_impl(barrier: Option<&Barrier>, is_start: bool) {
864                    // Ensure benchmarked section has a `ThreadAllocInfo`
865                    // allocated for the current thread and clear previous info.
866                    let alloc_info = if is_start { ThreadAllocInfo::current() } else { None };
867
868                    // Synchronize all threads.
869                    //
870                    // This is the final synchronization point for the end.
871                    if let Some(barrier) = barrier {
872                        barrier.wait();
873                    }
874
875                    if let Some(mut alloc_info) = alloc_info {
876                        // SAFETY: We have exclusive access.
877                        let alloc_info = unsafe { alloc_info.as_mut() };
878
879                        alloc_info.clear();
880
881                        // Synchronize all threads.
882                        if let Some(barrier) = barrier {
883                            barrier.wait();
884                        }
885                    }
886                }
887            };
888
889            // The following logic chooses how to efficiently sample the
890            // benchmark function once and assigns `sample_start`/`sample_end`
891            // before/after the sample loop.
892            //
893            // NOTE: Testing and benchmarking should behave exactly the same
894            // when getting the sample time span. We don't want to introduce
895            // extra work that may worsen measurement quality for real
896            // benchmarking.
897            let sample_start: UntaggedTimestamp;
898            let sample_end: UntaggedTimestamp;
899
900            if size_of::<I>() == 0 && (size_of::<O>() == 0 || !mem::needs_drop::<O>()) {
901                // Use a range instead of `defer_store` to make the benchmarking
902                // loop cheaper.
903
904                // Run `gen_input` the expected number of times in case it
905                // updates external state used by `benched`.
906                for _ in 0..sample_size {
907                    let input = gen_input();
908                    count_input(&input);
909
910                    // Inputs are consumed/dropped later.
911                    mem::forget(input);
912                }
913
914                sync_threads(true);
915                sample_start = UntaggedTimestamp::start(timer_kind);
916
917                // Sample loop:
918                for _ in 0..sample_size {
919                    // SAFETY: Input is a ZST, so we can construct one out of
920                    // thin air.
921                    let input = unsafe { UnsafeCell::new(MaybeUninit::<I>::zeroed()) };
922
923                    mem::forget(black_box(benched(&input)));
924                }
925
926                sample_end = UntaggedTimestamp::end(timer_kind);
927                sync_threads(false);
928                save_alloc_info();
929
930                // Drop outputs and inputs.
931                for _ in 0..sample_size {
932                    // Output only needs drop if ZST.
933                    if size_of::<O>() == 0 {
934                        // SAFETY: Output is a ZST, so we can construct one out
935                        // of thin air.
936                        unsafe { _ = mem::zeroed::<O>() }
937                    }
938
939                    if mem::needs_drop::<I>() {
940                        // SAFETY: Input is a ZST, so we can construct one out
941                        // of thin air and not worry about aliasing.
942                        unsafe { drop_input(&UnsafeCell::new(MaybeUninit::<I>::zeroed())) }
943                    }
944                }
945            } else {
946                defer_store.prepare(sample_size);
947
948                match defer_store.slots() {
949                    // Output needs to be dropped. We defer drop in the sample
950                    // loop by inserting it into `defer_store`.
951                    Ok(defer_slots_slice) => {
952                        // Initialize and store inputs.
953                        for DeferSlot { input, .. } in defer_slots_slice {
954                            // SAFETY: We have exclusive access to `input`.
955                            let input = unsafe { &mut *input.get() };
956                            let input = input.write(gen_input());
957                            count_input(input);
958
959                            // Make input opaque to benchmarked function.
960                            black_box(input);
961                        }
962
963                        // Create iterator before the sample timing section to
964                        // reduce benchmarking overhead.
965                        let defer_slots_iter = defer_slots_slice.iter();
966
967                        sync_threads(true);
968                        sample_start = UntaggedTimestamp::start(timer_kind);
969
970                        // Sample loop:
971                        for defer_slot in defer_slots_iter {
972                            // SAFETY: All inputs in `defer_store` were
973                            // initialized and we have exclusive access to the
974                            // output slot.
975                            unsafe {
976                                let output = benched(&defer_slot.input);
977                                *defer_slot.output.get() = MaybeUninit::new(output);
978                            }
979                        }
980
981                        sample_end = UntaggedTimestamp::end(timer_kind);
982                        sync_threads(false);
983                        save_alloc_info();
984
985                        // Prevent the optimizer from removing writes to inputs
986                        // and outputs in the sample loop.
987                        black_box(defer_slots_slice);
988
989                        // Drop outputs and inputs.
990                        for DeferSlot { input, output } in defer_slots_slice {
991                            // SAFETY: All outputs were initialized in the
992                            // sample loop and we have exclusive access.
993                            unsafe { (*output.get()).assume_init_drop() }
994
995                            if mem::needs_drop::<I>() {
996                                // SAFETY: The output was dropped and thus we
997                                // have exclusive access to inputs.
998                                unsafe { drop_input(input) }
999                            }
1000                        }
1001                    }
1002
1003                    // Output does not need to be dropped.
1004                    Err(defer_inputs_slice) => {
1005                        // Initialize and store inputs.
1006                        for input in defer_inputs_slice {
1007                            // SAFETY: We have exclusive access to `input`.
1008                            let input = unsafe { &mut *input.get() };
1009                            let input = input.write(gen_input());
1010                            count_input(input);
1011
1012                            // Make input opaque to benchmarked function.
1013                            black_box(input);
1014                        }
1015
1016                        // Create iterator before the sample timing section to
1017                        // reduce benchmarking overhead.
1018                        let defer_inputs_iter = defer_inputs_slice.iter();
1019
1020                        sync_threads(true);
1021                        sample_start = UntaggedTimestamp::start(timer_kind);
1022
1023                        // Sample loop:
1024                        for input in defer_inputs_iter {
1025                            // SAFETY: All inputs in `defer_store` were
1026                            // initialized.
1027                            black_box_drop(unsafe { benched(input) });
1028                        }
1029
1030                        sample_end = UntaggedTimestamp::end(timer_kind);
1031                        sync_threads(false);
1032                        save_alloc_info();
1033
1034                        // Prevent the optimizer from removing writes to inputs
1035                        // in the sample loop.
1036                        black_box(defer_inputs_slice);
1037
1038                        // Drop inputs.
1039                        if mem::needs_drop::<I>() {
1040                            for input in defer_inputs_slice {
1041                                // SAFETY: We have exclusive access to inputs.
1042                                unsafe { drop_input(input) }
1043                            }
1044                        }
1045                    }
1046                }
1047            }
1048
1049            // SAFETY: These values are guaranteed to be the correct variant
1050            // because they were created from the same `timer_kind`.
1051            let interval = unsafe {
1052                [sample_start.into_timestamp(timer_kind), sample_end.into_timestamp(timer_kind)]
1053            };
1054
1055            (interval, saved_alloc_info)
1056        }
1057    }
1058
1059    #[inline]
1060    fn initial_mode(&self) -> BenchMode {
1061        if self.shared_context.action.is_test() {
1062            BenchMode::Test
1063        } else if let Some(sample_size) = self.options.sample_size {
1064            BenchMode::Collect { sample_size }
1065        } else {
1066            BenchMode::Tune { sample_size: 1 }
1067        }
1068    }
1069
1070    pub fn compute_stats(&self) -> Stats {
1071        let time_samples = &self.samples.time_samples;
1072        let alloc_info_by_sample = &self.samples.alloc_info_by_sample;
1073
1074        let sample_count = time_samples.len();
1075        let sample_size = self.samples.sample_size;
1076
1077        let total_count = self.samples.iter_count();
1078
1079        let total_duration = self.samples.total_duration();
1080        let mean_duration = FineDuration {
1081            picos: total_duration.picos.checked_div(total_count as u128).unwrap_or_default(),
1082        };
1083
1084        // Samples sorted by duration.
1085        let sorted_samples = self.samples.sorted_samples();
1086        let median_samples = util::slice_middle(&sorted_samples);
1087
1088        let index_of_sample = |sample: &TimeSample| -> usize {
1089            util::slice_ptr_index(&self.samples.time_samples, sample)
1090        };
1091
1092        let counter_count_for_sample =
1093            |sample: &TimeSample, counter_kind: KnownCounterKind| -> Option<MaxCountUInt> {
1094                let counts = self.counters.counts(counter_kind);
1095
1096                let index = if self.counters.uses_input_counts(counter_kind) {
1097                    index_of_sample(sample)
1098                } else {
1099                    0
1100                };
1101
1102                counts.get(index).copied()
1103            };
1104
1105        let min_duration =
1106            sorted_samples.first().map(|s| s.duration / sample_size).unwrap_or_default();
1107        let max_duration =
1108            sorted_samples.last().map(|s| s.duration / sample_size).unwrap_or_default();
1109
1110        let median_duration = if median_samples.is_empty() {
1111            FineDuration::default()
1112        } else {
1113            let sum: u128 = median_samples.iter().map(|s| s.duration.picos).sum();
1114            FineDuration { picos: sum / median_samples.len() as u128 } / sample_size
1115        };
1116
1117        let counts = KnownCounterKind::ALL.map(|counter_kind| {
1118            let median: MaxCountUInt = {
1119                let mut sum: u128 = 0;
1120
1121                for sample in median_samples {
1122                    let sample_count = counter_count_for_sample(sample, counter_kind)? as u128;
1123
1124                    // Saturating add in case `MaxUIntCount > u64`.
1125                    sum = sum.saturating_add(sample_count);
1126                }
1127
1128                (sum / median_samples.len() as u128) as MaxCountUInt
1129            };
1130
1131            Some(StatsSet {
1132                fastest: sorted_samples
1133                    .first()
1134                    .and_then(|s| counter_count_for_sample(s, counter_kind))?,
1135                slowest: sorted_samples
1136                    .last()
1137                    .and_then(|s| counter_count_for_sample(s, counter_kind))?,
1138                median,
1139                mean: self.counters.mean_count(counter_kind),
1140            })
1141        });
1142
1143        let sample_alloc_info = |sample: Option<&TimeSample>| -> Option<&ThreadAllocInfo> {
1144            sample
1145                .and_then(|sample| u32::try_from(index_of_sample(sample)).ok())
1146                .and_then(|index| self.samples.alloc_info_by_sample.get(&index))
1147        };
1148
1149        let sample_alloc_tally = |sample: Option<&TimeSample>, op: AllocOp| -> ThreadAllocTally {
1150            sample_alloc_info(sample)
1151                .map(|alloc_info| alloc_info.tallies.get(op))
1152                .copied()
1153                .unwrap_or_default()
1154        };
1155
1156        let mut alloc_total_max_count = 0u128;
1157        let mut alloc_total_max_size = 0u128;
1158        let mut alloc_total_tallies = TotalAllocTallyMap::default();
1159
1160        for alloc_info in alloc_info_by_sample.values() {
1161            alloc_total_max_count += alloc_info.max_count as u128;
1162            alloc_total_max_size += alloc_info.max_size as u128;
1163            alloc_info.tallies.add_to_total(&mut alloc_total_tallies);
1164        }
1165
1166        let sample_size = f64::from(sample_size);
1167        Stats {
1168            sample_count: sample_count as u32,
1169            iter_count: total_count,
1170            time: StatsSet {
1171                fastest: min_duration,
1172                slowest: max_duration,
1173                median: median_duration,
1174                mean: mean_duration,
1175            },
1176            max_alloc: StatsSet {
1177                fastest: {
1178                    let alloc_info = sample_alloc_info(sorted_samples.first().copied());
1179
1180                    AllocTally {
1181                        count: alloc_info.map(|info| info.max_count as f64).unwrap_or_default()
1182                            / sample_size,
1183                        size: alloc_info.map(|info| info.max_size as f64).unwrap_or_default()
1184                            / sample_size,
1185                    }
1186                },
1187                slowest: {
1188                    let alloc_info = sample_alloc_info(sorted_samples.last().copied());
1189
1190                    AllocTally {
1191                        count: alloc_info.map(|info| info.max_count as f64).unwrap_or_default()
1192                            / sample_size,
1193                        size: alloc_info.map(|info| info.max_size as f64).unwrap_or_default()
1194                            / sample_size,
1195                    }
1196                },
1197                // TODO: Switch to median of alloc info itself, rather than
1198                // basing off of median times.
1199                median: {
1200                    let alloc_info_for_median =
1201                        |index| sample_alloc_info(median_samples.get(index).copied());
1202
1203                    let max_count_for_median = |index: usize| -> f64 {
1204                        alloc_info_for_median(index)
1205                            .map(|info| info.max_count as f64)
1206                            .unwrap_or_default()
1207                    };
1208
1209                    let max_size_for_median = |index: usize| -> f64 {
1210                        alloc_info_for_median(index)
1211                            .map(|info| info.max_size as f64)
1212                            .unwrap_or_default()
1213                    };
1214
1215                    let median_count = median_samples.len().max(1) as f64;
1216
1217                    let median_max_count = max_count_for_median(0) + max_count_for_median(1);
1218                    let median_max_size = max_size_for_median(0) + max_size_for_median(1);
1219
1220                    AllocTally {
1221                        count: median_max_count / median_count / sample_size,
1222                        size: median_max_size / median_count / sample_size,
1223                    }
1224                },
1225                mean: AllocTally {
1226                    count: alloc_total_max_count as f64 / total_count as f64,
1227                    size: alloc_total_max_size as f64 / total_count as f64,
1228                },
1229            }
1230            .transpose(),
1231            alloc_tallies: AllocOpMap {
1232                values: AllocOp::ALL
1233                    .map(|op| StatsSet {
1234                        fastest: {
1235                            let fastest = sample_alloc_tally(sorted_samples.first().copied(), op);
1236
1237                            AllocTally {
1238                                count: fastest.count as f64 / sample_size,
1239                                size: fastest.size as f64 / sample_size,
1240                            }
1241                        },
1242                        slowest: {
1243                            let slowest = sample_alloc_tally(sorted_samples.last().copied(), op);
1244
1245                            AllocTally {
1246                                count: slowest.count as f64 / sample_size,
1247                                size: slowest.size as f64 / sample_size,
1248                            }
1249                        },
1250                        median: {
1251                            let tally_for_median = |index: usize| -> ThreadAllocTally {
1252                                sample_alloc_tally(median_samples.get(index).copied(), op)
1253                            };
1254
1255                            let a = tally_for_median(0);
1256                            let b = tally_for_median(1);
1257
1258                            let median_count = median_samples.len().max(1) as f64;
1259
1260                            let avg_count = (a.count as f64 + b.count as f64) / median_count;
1261                            let avg_size = (a.size as f64 + b.size as f64) / median_count;
1262
1263                            AllocTally {
1264                                count: avg_count / sample_size,
1265                                size: avg_size / sample_size,
1266                            }
1267                        },
1268                        mean: {
1269                            let tally = alloc_total_tallies.get(op);
1270                            AllocTally {
1271                                count: tally.count as f64 / total_count as f64,
1272                                size: tally.size as f64 / total_count as f64,
1273                            }
1274                        },
1275                    })
1276                    .map(StatsSet::transpose),
1277            },
1278            counts,
1279        }
1280    }
1281}
1282
1283impl<T> StatsSet<AllocTally<T>> {
1284    #[inline]
1285    pub fn transpose(self) -> AllocTally<StatsSet<T>> {
1286        AllocTally {
1287            count: StatsSet {
1288                fastest: self.fastest.count,
1289                slowest: self.slowest.count,
1290                median: self.median.count,
1291                mean: self.mean.count,
1292            },
1293            size: StatsSet {
1294                fastest: self.fastest.size,
1295                slowest: self.slowest.size,
1296                median: self.median.size,
1297                mean: self.mean.size,
1298            },
1299        }
1300    }
1301}