Debian Codegraph @

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

raw
1 use std::{
2 any::{Any, TypeId},
3 cmp::Ordering,
4 mem::ManuallyDrop,
5 sync::OnceLock,
6 };
7
8 use super::{BenchEntryRunner, GroupEntry};
9
10 // use crate::util::sort::natural_cmp;
11
12 /// Compile-time entry for a generic benchmark function, generated by
13 /// `#[divan::bench]`.
14 ///
15 /// Unlike `BenchEntry`, this is for a specific generic type or `const`.
16 ///
17 /// Although this type contains trivially-`Copy` data, it *should not* implement
18 /// `Clone` because the memory address of each instance is used to determine the
19 /// relative order in `GroupEntry.generic_benches` when sorting benchmarks by
20 /// location.
21 pub struct GenericBenchEntry {
22 /// The associated group, for entry metadata.
23 pub group: &'static GroupEntry,
24
25 /// The benchmarking function.
26 pub bench: BenchEntryRunner,
27
28 /// A generic type.
29 pub ty: Option<EntryType>,
30
31 /// A `const` value and associated data.
32 pub const_value: Option<EntryConst>,
33 }
34
35 impl GenericBenchEntry {
36 pub(crate) fn display_name(&self) -> &str {
37 match (&self.ty, &self.const_value) {
38 (_, Some(const_value)) => const_value.name(),
39 (Some(ty), None) => ty.display_name(),
40 (None, None) => unreachable!(),
41 }
42 }
43 }
44
45 /// Generic type instantiation.
46 pub struct EntryType {
47 /// [`std::any::type_name`].
48 get_type_name: fn() -> &'static str,
49
50 /// [`std::any::TypeId::of`].
51 #[allow(dead_code)]
52 get_type_id: fn() -> TypeId,
53 }
54
55 impl EntryType {
56 /// Creates an instance for the given type.
57 pub const fn new<T: Any>() -> Self {
58 Self {
59 get_type_name: std::any::type_name::<T>,
60 get_type_id: TypeId::of::<T>,
61 }
62 }
63
64 pub(crate) fn raw_name(&self) -> &'static str {
65 (self.get_type_name)()
66 }
67
68 pub(crate) fn display_name(&self) -> &'static str {
69 let mut type_name = self.raw_name();
70
71 // Remove module components in type name.
72 while let Some((prev, next)) = type_name.split_once("::") {
73 // Do not go past generic type boundary.
74 if prev.contains('<') {
75 break;
76 }
77 type_name = next;
78 }
79
80 type_name
81 }
82 }
83
84 /// A reference to a `const` as a `&'static T`.
85 #[allow(dead_code)]
86 pub struct EntryConst {
87 /// `&'static T`.
88 value: *const (),
89
90 /// [`PartialOrd::partial_cmp`].
91 partial_cmp: unsafe fn(*const (), *const ()) -> Option<Ordering>,
92
93 /// [`ToString::to_string`].
94 to_string: unsafe fn(*const ()) -> String,
95
96 /// Cached `to_string` result.
97 cached_string: ManuallyDrop<OnceLock<&'static str>>,
98 }
99
100 // SAFETY: `T: Send + Sync`.
101 unsafe impl Send for EntryConst {}
102 unsafe impl Sync for EntryConst {}
103
104 #[allow(dead_code)]
105 impl EntryConst {
106 /// Creates entry data for a `const` values.
107 pub const fn new<T>(value: &'static T) -> Self
108 where
109 T: PartialOrd + ToString + Send + Sync,
110 {
111 unsafe fn partial_cmp<T: PartialOrd>(a: *const (), b: *const ()) -> Option<Ordering> {
112 T::partial_cmp(&*a.cast(), &*b.cast())
113 }
114
115 unsafe fn to_string<T: ToString>(value: *const ()) -> String {
116 T::to_string(&*value.cast())
117 }
118
119 Self {
120 value: value as *const T as *const (),
121 partial_cmp: partial_cmp::<T>,
122 to_string: to_string::<T>,
123 cached_string: ManuallyDrop::new(OnceLock::new()),
124 }
125 }
126
127 /// [`ToString::to_string`].
128 #[inline]
129 pub(crate) fn name(&self) -> &str {
130 self.cached_string.get_or_init(|| {
131 // SAFETY: The function is guaranteed to call `T::to_string`.
132 let string = unsafe { (self.to_string)(self.value) };
133
134 Box::leak(string.into_boxed_str())
135 })
136 }
137 }