Debian Codegraph @

debian/rust-vendor/codspeed-divan-compat/src/compat/entry/mod.rs

raw
1 //! Handpicked stubs from [divan::entry](https://github.com/nvzqz/divan/blob/main/src/entry/mod.rs)
2 //! Necessary to be able to use the [divan::bench](https://docs.rs/divan/0.1.17/divan/attr.bench.html) macro without changing it too much
3
4 mod generic;
5
6 use std::{
7 ptr,
8 sync::{
9 atomic::{AtomicPtr, Ordering as AtomicOrdering},
10 LazyLock,
11 },
12 };
13
14 use super::{
15 bench::{BenchOptions, Bencher},
16 BenchArgsRunner,
17 };
18
19 pub use generic::{EntryConst, EntryType, GenericBenchEntry};
20
21 /// Benchmark entries generated by `#[divan::bench]`.
22 ///
23 /// Note: generic-type benchmark entries are instead stored in `GROUP_ENTRIES`
24 /// in `generic_benches`.
25 pub static BENCH_ENTRIES: EntryList<BenchEntry> = EntryList::root();
26
27 /// Group entries generated by `#[divan::bench_group]`.
28 pub static GROUP_ENTRIES: EntryList<GroupEntry> = EntryList::root();
29
30 /// Determines how the benchmark entry is run.
31 #[derive(Clone, Copy)]
32 pub enum BenchEntryRunner {
33 /// Benchmark without arguments.
34 Plain(fn(Bencher)),
35 /// Benchmark with runtime arguments.
36 Args(fn() -> BenchArgsRunner),
37 }
38
39 /// Compile-time entry for a benchmark, generated by `#[divan::bench]`.
40 pub struct BenchEntry {
41 /// Entry metadata.
42 pub meta: EntryMeta,
43
44 /// The benchmarking function.
45 pub bench: BenchEntryRunner,
46 }
47
48 /// Compile-time entry for a benchmark group, generated by
49 /// `#[divan::bench_group]` or a generic-type `#[divan::bench]`.
50 pub struct GroupEntry {
51 /// Entry metadata.
52 pub meta: EntryMeta,
53
54 /// Generic `#[divan::bench]` entries.
55 ///
56 /// This is two-dimensional to make code generation simpler. The outer
57 /// dimension corresponds to types and the inner dimension corresponds to
58 /// constants.
59 pub generic_benches: Option<&'static [&'static [GenericBenchEntry]]>,
60 }
61
62 impl GroupEntry {
63 pub(crate) fn generic_benches_iter(&self) -> impl Iterator<Item = &'static GenericBenchEntry> {
64 self.generic_benches
65 .unwrap_or_default()
66 .iter()
67 .flat_map(|benches| benches.iter())
68 }
69 }
70
71 /// `BenchEntry` or `GenericBenchEntry`.
72 #[derive(Clone, Copy)]
73 pub(crate) enum AnyBenchEntry<'a> {
74 Bench(&'a BenchEntry),
75 GenericBench(&'a GenericBenchEntry),
76 }
77
78 /// Metadata common to `#[divan::bench]` and `#[divan::bench_group]`.
79 pub struct EntryMeta {
80 /// The entry's display name.
81 pub display_name: &'static str,
82
83 /// The entry's original name.
84 ///
85 /// This is used to find a `GroupEntry` for a `BenchEntry`.
86 pub raw_name: &'static str,
87
88 /// The entry's raw `module_path!()`.
89 pub module_path: &'static str,
90
91 /// Where the entry was defined.
92 pub location: EntryLocation,
93 /// Configures the benchmarker via attribute options.
94 pub bench_options: Option<LazyLock<BenchOptions>>,
95 }
96
97 impl EntryMeta {
98 #[inline]
99 pub(crate) fn module_path_components<'a>(&self) -> impl Iterator<Item = &'a str> {
100 self.module_path.split("::")
101 }
102 }
103
104 /// Where an entry is located.
105 #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
106 #[allow(missing_docs)]
107 pub struct EntryLocation {
108 pub file: &'static str,
109 pub line: u32,
110 pub col: u32,
111 }
112
113 /// Linked list of entries.
114 ///
115 /// This is implemented in a thread-safe way despite the fact that constructors
116 /// are run single-threaded.
117 pub struct EntryList<T: 'static> {
118 entry: Option<&'static T>,
119 next: AtomicPtr<Self>,
120 }
121
122 impl<T> EntryList<T> {
123 pub(crate) const fn root() -> Self {
124 Self {
125 entry: None,
126 next: AtomicPtr::new(ptr::null_mut()),
127 }
128 }
129
130 /// Dereferences the `next` pointer.
131 #[inline]
132 fn next(&self) -> Option<&Self> {
133 // SAFETY: `next` is only assigned by `push`, which always receives a
134 // 'static lifetime.
135 unsafe { self.next.load(AtomicOrdering::Relaxed).as_ref() }
136 }
137 }
138
139 // Externally used by macros or tests.
140 #[allow(missing_docs)]
141 impl<T> EntryList<T> {
142 #[inline]
143 pub const fn new(entry: &'static T) -> Self {
144 Self {
145 entry: Some(entry),
146 next: AtomicPtr::new(ptr::null_mut()),
147 }
148 }
149
150 /// Creates an iterator over entries in `self`.
151 #[inline]
152 pub fn iter(&self) -> impl Iterator<Item = &T> {
153 let mut list = Some(self);
154 std::iter::from_fn(move || -> Option<Option<&T>> {
155 let current = list?;
156 list = current.next();
157 Some(current.entry.as_ref().copied())
158 })
159 .flatten()
160 }
161
162 /// Inserts `other` to the front of the list.
163 ///
164 /// # Safety
165 ///
166 /// This function must be safe to call before `main`.
167 #[inline]
168 pub fn push(&'static self, other: &'static Self) {
169 let mut old_next = self.next.load(AtomicOrdering::Relaxed);
170 loop {
171 // Each publicly-created instance has `list.next` be null, so we can
172 // simply store `self.next` there.
173 other.next.store(old_next, AtomicOrdering::Release);
174
175 // SAFETY: The content of `other` can already be seen, so we don't
176 // need to strongly order reads into it.
177 let other = other as *const Self as *mut Self;
178 match self.next.compare_exchange_weak(
179 old_next,
180 other,
181 AtomicOrdering::AcqRel,
182 AtomicOrdering::Acquire,
183 ) {
184 // Successfully wrote our thread's value to the list.
185 Ok(_) => return,
186
187 // Lost the race, store winner's value in `other.next`.
188 Err(new) => old_next = new,
189 }
190 }
191 }
192 }
193
194 impl<'a> AnyBenchEntry<'a> {
195 /// Returns this entry's benchmark runner.
196 #[inline]
197 pub fn bench_runner(self) -> &'a BenchEntryRunner {
198 match self {
199 Self::Bench(BenchEntry { bench, .. })
200 | Self::GenericBench(GenericBenchEntry { bench, .. }) => bench,
201 }
202 }
203
204 #[inline]
205 pub fn meta(self) -> &'a EntryMeta {
206 match self {
207 Self::Bench(entry) => &entry.meta,
208 Self::GenericBench(entry) => &entry.group.meta,
209 }
210 }
211
212 #[inline]
213 pub fn display_name(self) -> &'a str {
214 match self {
215 Self::Bench(entry) => entry.meta.display_name,
216 Self::GenericBench(entry) => entry.display_name(),
217 }
218 }
219 }