Files
lucisv/crates/vlog_parser/src/stats.rs
T

40 lines
1.6 KiB
Rust

//! How much parsing this thread has done — a work counter, not a timer.
//!
//! A `textDocument/codeAction` used to parse the same buffer five times, and
//! the only honest way to prove that it no longer does is to count the parses
//! rather than to time them (take 5 §P1,
//! `docs/features/perf5-p1/README.md`). A stopwatch assertion on a 1.3 MB
//! buffer is a flake; a count is not.
//!
//! **Per thread, deliberately.** A process-wide counter would be unusable as
//! an assertion, because `cargo test` runs tests in parallel and every other
//! test in the binary would be adding to it; and it would answer the wrong
//! question anyway, since what a caller wants to know is what *this* request
//! did. A request is answered on one thread, so a thread-local count is that
//! number exactly. It costs one non-atomic add per whole-file parse.
use std::cell::Cell;
thread_local! {
static PARSES: Cell<u64> = const { Cell::new(0) };
}
/// How many whole-file parses this thread has run.
///
/// Counted in [`crate::parse`] and [`crate::parse_tolerant`], which is every
/// way a whole file is parsed: [`crate::parse_source`] and
/// [`crate::parse_source_tolerant`] both funnel through them. A *lex* failure
/// parses nothing and is not counted; neither is a fragment read (macro
/// expansion), which re-reads a token slice rather than a file.
///
/// The count is monotone and never reset, so a caller measures a *delta*
/// across the work it is asking about.
pub fn parses() -> u64 {
PARSES.with(Cell::get)
}
/// Count one whole-file parse.
pub(crate) fn count_parse() {
PARSES.with(|parses| parses.set(parses.get() + 1));
}