-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathlib.rs
More file actions
149 lines (131 loc) · 4.38 KB
/
Copy pathlib.rs
File metadata and controls
149 lines (131 loc) · 4.38 KB
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
use core::mem;
use core::sync::atomic::AtomicUsize;
/// For inspecting how much memory a value is using.
///
/// This trait specifically measures heap memory. If you want to measure stack memory too, add
/// `mem::size_of_val()` to it. (This only really matters for the outermost type in a hierarchy.)
pub trait MemoryUsage {
/// The **heap** memory usage of this type. The default implementation returns 0.
#[inline(always)]
fn heap_usage(&self) -> usize {
0
}
}
impl MemoryUsage for () {}
impl MemoryUsage for bool {}
impl MemoryUsage for u8 {}
impl MemoryUsage for u16 {}
impl MemoryUsage for u32 {}
impl MemoryUsage for u64 {}
impl MemoryUsage for u128 {}
#[cfg(feature = "ethnum")]
impl MemoryUsage for ethnum::u256 {}
impl MemoryUsage for usize {}
impl MemoryUsage for AtomicUsize {}
impl MemoryUsage for i8 {}
impl MemoryUsage for i16 {}
impl MemoryUsage for i32 {}
impl MemoryUsage for i64 {}
impl MemoryUsage for i128 {}
#[cfg(feature = "ethnum")]
impl MemoryUsage for ethnum::i256 {}
impl MemoryUsage for isize {}
impl MemoryUsage for f32 {}
impl MemoryUsage for f64 {}
#[cfg(feature = "decorum")]
impl MemoryUsage for decorum::Total<f32> {}
#[cfg(feature = "decorum")]
impl MemoryUsage for decorum::Total<f64> {}
impl<T: MemoryUsage + ?Sized> MemoryUsage for &T {
fn heap_usage(&self) -> usize {
(*self).heap_usage()
}
}
impl<T: MemoryUsage + ?Sized> MemoryUsage for Box<T> {
fn heap_usage(&self) -> usize {
mem::size_of_val::<T>(self) + T::heap_usage(self)
}
}
impl<T: MemoryUsage + ?Sized> MemoryUsage for std::sync::Arc<T> {
fn heap_usage(&self) -> usize {
let refcounts = mem::size_of::<usize>() * 2;
refcounts + mem::size_of_val::<T>(self) + T::heap_usage(self)
}
}
impl<T: MemoryUsage + ?Sized> MemoryUsage for std::rc::Rc<T> {
fn heap_usage(&self) -> usize {
let refcounts = mem::size_of::<usize>() * 2;
refcounts + mem::size_of_val::<T>(self) + T::heap_usage(self)
}
}
impl<T: MemoryUsage> MemoryUsage for [T] {
fn heap_usage(&self) -> usize {
self.iter().map(T::heap_usage).sum()
}
}
impl<T: MemoryUsage, const N: usize> MemoryUsage for [T; N] {
fn heap_usage(&self) -> usize {
self.iter().map(T::heap_usage).sum()
}
}
impl MemoryUsage for str {}
impl<T: MemoryUsage> MemoryUsage for Option<T> {
fn heap_usage(&self) -> usize {
self.as_ref().map_or(0, T::heap_usage)
}
}
impl<A: MemoryUsage, B: MemoryUsage> MemoryUsage for (A, B) {
fn heap_usage(&self) -> usize {
self.0.heap_usage() + self.1.heap_usage()
}
}
impl MemoryUsage for String {
fn heap_usage(&self) -> usize {
self.capacity()
}
}
impl<T: MemoryUsage> MemoryUsage for Vec<T> {
fn heap_usage(&self) -> usize {
self.capacity() * mem::size_of::<T>() + self.iter().map(T::heap_usage).sum::<usize>()
}
}
#[cfg(feature = "hash_map")]
impl<K: MemoryUsage + Eq + core::hash::Hash, V: MemoryUsage, S: core::hash::BuildHasher> MemoryUsage
for hashbrown::HashMap<K, V, S>
{
fn heap_usage(&self) -> usize {
self.allocation_size() + self.iter().map(|(k, v)| k.heap_usage() + v.heap_usage()).sum::<usize>()
}
}
#[cfg(feature = "hash_map")]
impl<K: MemoryUsage + Eq + core::hash::Hash, S: core::hash::BuildHasher> MemoryUsage for hashbrown::HashSet<K, S> {
fn heap_usage(&self) -> usize {
self.allocation_size() + self.iter().map(|k| k.heap_usage()).sum::<usize>()
}
}
impl<K: MemoryUsage, V: MemoryUsage> MemoryUsage for std::collections::BTreeMap<K, V> {
fn heap_usage(&self) -> usize {
// NB: this is best-effort, since we don't have a `capacity()` method on `BTreeMap`.
self.len() * mem::size_of::<(K, V)>() + self.iter().map(|(k, v)| k.heap_usage() + v.heap_usage()).sum::<usize>()
}
}
impl<T: MemoryUsage> MemoryUsage for std::collections::BTreeSet<T> {
fn heap_usage(&self) -> usize {
// NB: this is best-effort, since we don't have a `capacity()` method on `BTreeMap`.
self.len() * mem::size_of::<T>() + self.iter().map(|t| t.heap_usage()).sum::<usize>()
}
}
#[cfg(feature = "smallvec")]
impl<A: smallvec::Array> MemoryUsage for smallvec::SmallVec<A>
where
A::Item: MemoryUsage,
{
fn heap_usage(&self) -> usize {
self.as_slice().heap_usage()
+ if self.spilled() {
self.capacity() * mem::size_of::<A::Item>()
} else {
0
}
}
}