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
#![cfg(feature = "arcstr")]
use crate::{Context, SizeOf};
use arcstr::{ArcStr, Substr};
use core::mem::size_of;
#[repr(C, align(8))]
struct FakeThinInner {
len_flags: usize,
strong: usize,
data: [u8; 0],
}
impl SizeOf for ArcStr {
fn size_of_children(&self, context: &mut Context) {
if context.insert_ptr(self.as_ptr()) {
let bytes = size_of::<FakeThinInner>() + self.len();
context.add(bytes).add_shared(bytes);
if !ArcStr::is_static(self) {
context.add_distinct_allocation();
}
}
}
}
impl SizeOf for Substr {
#[inline]
fn size_of_children(&self, context: &mut Context) {
self.parent().size_of_children(context);
}
}
#[cfg(test)]
mod tests {
use super::FakeThinInner;
use crate::{SizeOf, TotalSize};
use arcstr::{ArcStr, Substr};
use core::mem::size_of;
#[test]
fn arcstr_smoke() {
let empty = ArcStr::new();
assert_eq!(
empty.size_of(),
TotalSize::new(
size_of::<ArcStr>() + size_of::<FakeThinInner>(),
0,
size_of::<FakeThinInner>(),
0,
),
);
let shared = size_of::<FakeThinInner>() + 4;
let total = size_of::<ArcStr>() + shared;
let static_string = arcstr::literal!("whee");
assert_eq!(static_string.size_of(), TotalSize::new(total, 0, shared, 0));
let allocated_string = ArcStr::from("whee");
assert_eq!(
allocated_string.size_of(),
TotalSize::new(total, 0, shared, 1),
);
}
#[test]
fn shared_arcstr() {
let string1 = ArcStr::from("whee");
let string2 = string1.clone();
let string3 = string1.clone();
let string4 = string1.clone();
let size =
crate::size_of_values([&string1 as _, &string2 as _, &string3 as _, &string4 as _]);
let shared = size_of::<FakeThinInner>() + 4;
let total = size_of::<ArcStr>() * 4 + shared;
assert_eq!(size, TotalSize::new(total, 0, shared, 1));
}
#[test]
fn substr_smoke() {
let empty = Substr::new();
assert_eq!(
empty.size_of(),
TotalSize::new(
size_of::<Substr>() + size_of::<FakeThinInner>(),
0,
size_of::<FakeThinInner>(),
0,
),
);
let shared = size_of::<FakeThinInner>() + 4;
let total = size_of::<Substr>() + shared;
let static_substr = Substr::full(arcstr::literal!("whee"));
assert_eq!(static_substr.size_of(), TotalSize::new(total, 0, shared, 0));
let allocated_substr = Substr::full(ArcStr::from("whee"));
assert_eq!(
allocated_substr.size_of(),
TotalSize::new(total, 0, shared, 1),
);
let sliced_allocated_substr = ArcStr::from("whee").substr(..2);
assert_eq!(
sliced_allocated_substr.size_of(),
TotalSize::new(total, 0, shared, 1),
);
}
}