use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering};
static LIVE: AtomicUsize = AtomicUsize::new(0);
static PEAK: AtomicUsize = AtomicUsize::new(0);
static TOTAL: AtomicUsize = AtomicUsize::new(0);
static ALLOCS: AtomicUsize = AtomicUsize::new(0);
struct Counting;
impl Counting {
#[inline]
fn add(bytes: usize) {
TOTAL.fetch_add(bytes, Ordering::Relaxed);
ALLOCS.fetch_add(1, Ordering::Relaxed);
let now = LIVE.fetch_add(bytes, Ordering::Relaxed) + bytes;
PEAK.fetch_max(now, Ordering::Relaxed);
}
#[inline]
fn sub(bytes: usize) {
LIVE.fetch_sub(bytes, Ordering::Relaxed);
}
}
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
Counting::add(layout.size());
unsafe { System.alloc(layout) }
}
unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) {
Counting::sub(layout.size());
unsafe { System.dealloc(pointer, layout) }
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
Counting::add(layout.size());
unsafe { System.alloc_zeroed(layout) }
}
unsafe fn realloc(&self, pointer: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
if new_size >= layout.size() {
Counting::add(new_size - layout.size());
} else {
Counting::sub(layout.size() - new_size);
}
unsafe { System.realloc(pointer, layout, new_size) }
}
}
#[global_allocator]
static GLOBAL: Counting = Counting;
fn live() -> usize {
LIVE.load(Ordering::Relaxed)
}
fn peak() -> usize {
PEAK.load(Ordering::Relaxed)
}
fn total() -> usize {
TOTAL.load(Ordering::Relaxed)
}
fn allocs() -> usize {
ALLOCS.load(Ordering::Relaxed)
}
fn mb(bytes: usize) -> f64 {
bytes as f64 / (1024.0 * 1024.0)
}
struct Stage {
name: &'static str,
live_before: usize,
total_before: usize,
allocs_before: usize,
peak_before: usize,
tokens: usize,
}
impl Stage {
fn begin(name: &'static str, tokens: usize) -> Self {
Stage {
name,
live_before: live(),
total_before: total(),
allocs_before: allocs(),
peak_before: peak(),
tokens,
}
}
fn end(self, also_live: usize) -> usize {
let held = live().saturating_sub(self.live_before);
let churn = total() - self.total_before;
let requests = allocs() - self.allocs_before;
let average = if requests == 0 {
0.0
} else {
churn as f64 / requests as f64
};
println!(
"{:<10} held {:>11} B ({:>7.2} MB) churn {:>11} B ({:>8.2} MB) allocs {:>10} avg {:>6.1} B peak {:>7.2} MB{}",
self.name,
held,
mb(held),
churn,
mb(churn),
requests,
average,
mb(peak().saturating_sub(self.peak_before) + live()),
if self.tokens == 0 {
String::new()
} else {
format!(
" | {:>6.1} B/token held, {:>8.1} B/token churn",
held as f64 / self.tokens as f64,
churn as f64 / self.tokens as f64
)
}
);
let _ = also_live;
held
}
}
#[test]
#[ignore = "a probe: needs CPPLS_PROBE_FILE, and reports rather than asserts"]
fn what_a_parse_costs() {
let path = std::env::var("CPPLS_PROBE_FILE").expect("CPPLS_PROBE_FILE names the file to read");
let text = std::fs::read_to_string(&path).expect("the probe file must be readable");
println!(
"\nfile {path}\n {} B ({:.1} KB)",
text.len(),
text.len() as f64 / 1024.0
);
let repeat: usize = std::env::var("CPPLS_PROBE_REPEAT")
.ok()
.and_then(|value| value.parse().ok())
.unwrap_or(1);
if repeat > 1 {
println!(
"\n# {repeat} readings, one after another — the live bytes stay, the churn adds up"
);
for index in 0..repeat {
let (tokens, _) = cpp_parser::lex(&text, &cpp_parser::LexerConfig::default());
let tree = cpp_parser::CppParser::parse(&text, cpp_parser::ParserConfig::default());
let root = tree.get_red_root();
let preprocessing = cpp_code_analysis::preprocess::preprocess(&text, tree.get_tokens());
let scopes = cpp_code_analysis::sema::scopes::build_scopes_with(
&root,
&cpp_code_analysis::sema::scopes::NoMacroBodies,
false,
);
let errors: Vec<cpp_parser::SourceRange> = tree
.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range))
.collect();
let (facts, _) = cpp_code_analysis::sema::declarations::build_facts(
&scopes,
&preprocessing,
&root,
&errors,
false,
);
let held = facts.len();
drop(facts);
drop(scopes);
drop(root);
drop(tree);
drop(tokens);
if index % 10 == 0 || index + 1 == repeat {
println!(
"read {:3} live {:6.2} MB peak {:7.2} MB churn {:8.2} MB ({} declaration(s))",
index + 1,
mb(live()),
mb(peak()),
mb(total()),
held
);
}
}
println!(
"done: live {:.2} MB, peak {:.2} MB, churn {:.2} MB in {} allocation(s)",
mb(live()),
mb(peak()),
mb(total()),
allocs()
);
return;
}
let lexing = Stage::begin("lex", 0);
let (tokens, _) = cpp_parser::lex(&text, &cpp_parser::LexerConfig::default());
let token_count = tokens.len();
println!(
"tokens {token_count} ({:.2} B of source per token)\n",
text.len() as f64 / token_count as f64
);
let mut lexing = lexing;
lexing.tokens = token_count;
lexing.end(0);
let parsing = Stage::begin("parse", token_count);
let tree = cpp_parser::CppParser::parse(&text, cpp_parser::ParserConfig::default());
parsing.end(0);
let root = tree.get_red_root();
let preprocessing_stage = Stage::begin("preprocess", token_count);
let preprocessing = cpp_code_analysis::preprocess::preprocess(&text, tree.get_tokens());
preprocessing_stage.end(0);
let scoping = Stage::begin("scopes", token_count);
let scopes = cpp_code_analysis::sema::scopes::build_scopes_with(
&root,
&cpp_code_analysis::sema::scopes::NoMacroBodies,
false,
);
scoping.end(0);
let errors: Vec<cpp_parser::SourceRange> = tree
.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range))
.collect();
let making = Stage::begin("facts", token_count);
let (facts, _) = cpp_code_analysis::sema::declarations::build_facts(
&scopes,
&preprocessing,
&root,
&errors,
false,
);
making.end(0);
println!(
"\ntotal held {:.2} MB, peak {:.2} MB, churn {:.2} MB in {} allocation(s) — {} declaration(s)",
mb(live()),
mb(peak()),
mb(total()),
allocs(),
facts.len()
);
println!(
" {:.1} B held/token, {:.1} B churn/token, {:.1} allocation(s)/token",
live() as f64 / token_count as f64,
total() as f64 / token_count as f64,
allocs() as f64 / token_count as f64
);
drop(facts);
drop(scopes);
drop(root);
drop(tree);
println!(
"after the drops: held {:.2} MB (the allocator keeps the pages; this is the number that stays)",
mb(live())
);
drop(tokens);
}