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
150
151
152
//! **How many allocations a program reading makes, and — with `perf-probe` — which phase makes them.**
//!
//! ```text
//! Remove-Item C:\Users\zc\Desktop\cpp_modules\.cppls\cache.redb
//! $env:CPPLS_MODULES_PROJECT = "C:\Users\zc\Desktop\cpp_modules"
//! cargo test --release -p cpp_code_analysis --test allocation_probe -- --ignored --nocapture
//! cargo test --release -p cpp_code_analysis --features perf-probe --test allocation_probe -- --ignored --nocapture
//! ```
//!
//! Separate from `module_hover_probe` on purpose: the counter wraps the allocator, and two atomics per allocation cost
//! 1.7 s on a 4.7 s read — so a probe that counts cannot also be the probe that times. The count is what does not move
//! when the machine is loaded, which makes it the honest instrument for a change that removes allocations.
//!
//! The baseline before the `SmolStr` token, the borrowed name spellings and the tree-scan directive question was
//! **217 535 897 allocations, 11 814 MB**; the same read now makes **149 931 213 allocations, 10 458 MB**.
use std::path::PathBuf;
use mimalloc_safe::MiMalloc;
struct Counting;
// **This thread's allocations for the current measurement.** The generation pair is what makes the count honest for a
// worker thread the index spawned before `perf::reset()`: a stale generation means "count from here", so a thread's
// first mark after a reset carries only its own work.
//
// A comment and not a doc comment: a `thread_local!` is a macro invocation, and rustdoc generates no documentation for
// one (`unused_doc_comments`, which `-D warnings` turns into an error).
#[cfg(feature = "perf-probe")]
thread_local! {
static HERE: std::cell::Cell<(usize, usize)> = const { std::cell::Cell::new((0, 0)) };
}
// Read by hand while a measurement is being read off, which is why nothing calls it: the probe prints the totals and a
// caller who wants this thread's share asks for it in the run being investigated.
#[cfg(feature = "perf-probe")]
#[allow(dead_code)]
fn allocations_here() -> usize {
HERE.with(|cell| cell.get().1)
}
fn note_here(bytes: usize) {
#[cfg(not(feature = "perf-probe"))]
let _ = bytes;
#[cfg(feature = "perf-probe")]
{
let generation = cpp_code_analysis::perf::generation();
HERE.with(|cell| {
let (seen, count) = cell.get();
cell.set(if seen == generation {
(seen, count + 1)
} else {
(generation, 1)
});
});
cpp_code_analysis::perf::note(bytes);
}
}
static ALLOCATIONS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
static BYTES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
unsafe impl std::alloc::GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: std::alloc::Layout) -> *mut u8 {
ALLOCATIONS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
BYTES.fetch_add(layout.size(), std::sync::atomic::Ordering::Relaxed);
// **And the library's own counter**, which is what the phase marks read. It exists only with the feature, so
// a default build has neither the counter nor this call.
note_here(layout.size());
// SAFETY: the same layout is passed through to the allocator underneath.
unsafe { MiMalloc.alloc(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: std::alloc::Layout) {
// SAFETY: the pointer and layout are the ones this allocator handed out.
unsafe { MiMalloc.dealloc(ptr, layout) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: std::alloc::Layout, new_size: usize) -> *mut u8 {
ALLOCATIONS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
BYTES.fetch_add(new_size, std::sync::atomic::Ordering::Relaxed);
note_here(new_size);
// SAFETY: forwarded unchanged to the allocator underneath.
unsafe { MiMalloc.realloc(ptr, layout, new_size) }
}
unsafe fn alloc_zeroed(&self, layout: std::alloc::Layout) -> *mut u8 {
ALLOCATIONS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
BYTES.fetch_add(layout.size(), std::sync::atomic::Ordering::Relaxed);
note_here(layout.size());
// SAFETY: forwarded unchanged to the allocator underneath.
unsafe { MiMalloc.alloc_zeroed(layout) }
}
}
#[global_allocator]
static GLOBAL: Counting = Counting;
use cpp_code_analysis::{DiskFiles, OpenDocuments, Session, SessionFiles, WatchFilter};
fn project() -> PathBuf {
PathBuf::from(
std::env::var("CPPLS_MODULES_PROJECT")
.unwrap_or_else(|_| r"C:\Users\zc\Desktop\cpp_modules".to_owned()),
)
}
#[test]
#[ignore = "a probe, not a test: it measures the allocation count of a real project's unit read"]
fn a_program_read_allocates() {
let root = project();
let main = root.join("src").join("main.cpp");
assert!(main.is_file(), "the project's main.cpp: {}", main.display());
// **`Session::open` runs the toolchain discovery**, which is what gives the project its include paths — a plain
// `with_config` has none, and then `import std;` resolves to nothing and the read is a no-op.
let mut session = Session::open(
root.clone(),
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&root),
);
session.index_everything();
let text = std::fs::read_to_string(&main).expect("main.cpp reads");
session.did_open(&main, &text);
session.index_everything();
// The measurement starts here: whatever the project scan did is not the read.
#[cfg(feature = "perf-probe")]
cpp_code_analysis::perf::reset();
let allocations = ALLOCATIONS.load(std::sync::atomic::Ordering::Relaxed);
let bytes = BYTES.load(std::sync::atomic::Ordering::Relaxed);
let read = session.read_the_modules_a_file_imports(&main);
let allocations = ALLOCATIONS.load(std::sync::atomic::Ordering::Relaxed) - allocations;
let bytes = BYTES.load(std::sync::atomic::Ordering::Relaxed) - bytes;
println!("read {read} unit(s)");
println!("allocations: {allocations}");
println!(
"bytes: {bytes} ({:.1} MB)",
bytes as f64 / (1024.0 * 1024.0)
);
#[cfg(feature = "perf-probe")]
{
println!("per phase:");
for (label, allocations, bytes) in cpp_code_analysis::perf::per_phase() {
println!(
" {label:<20} {allocations:>12} {:>7.1} MB",
bytes as f64 / (1024.0 * 1024.0)
);
}
}
}