Files
lucisv/crates/vlog_refactor/src/trace.rs
T
lucioandClaude Opus 5.5 7794d3938b TODO §65.1: rename and references through a generate block label; rename warnings and mentions scoped to the renamed declaration
A path segment naming a block (bare, indexed, nested, cross-file) is now a
reference to its label: vlog_semantic declares a `for` block's label in the
enclosing scope, makes `generate … endgenerate` no scope, gives a block a
member scope, walks an index in a path, and files blocks on a module's
hierarchical surface under `module.label` (FACTS_FORMAT 21). The LSP, CLI
and MCP rename/references get it through the engine; lucisv-mcp's `block`
refusal is lifted. No verifier normalization (attacks in
check/qa_block_paths.rs, qa_block_attacks.rs).

Reworks after QA: unresolved path segments warned in every file of the set;
sibling-arm members warned; a label beside a net is a duplicate-declaration;
function-local block keys never collide; file names path-qualified when base
names collide; rename warnings and lucisv-mcp mentions scoped to the
declaration's home set (rename/home.rs, scoped.rs), falling back to unscoped
on any doubt. TODO §65.4 and §65.6 filed.

docs/features/generate-label-rename. Workspace lib/bins/tests: 79 suites,
5 064 passed, 0 failed, 51 ignored (guard exit 0).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-07 01:01:23 -07:00

253 lines
9.9 KiB
Rust

//! Where a rename's time and its whole-file parses went.
//!
//! Take 5 §P2a (`docs/features/perf5-p2/README.md`): the tail rename costs
//! ~780 ms and nobody could say which stage that is. A stopwatch on the wire
//! cannot, because every stage of a rename is "read the workspace" in some
//! form. So the engine says it itself — one value the caller fills as it goes,
//! carrying a wall time and a *parse count* per stage.
//!
//! **Nothing here changes what a rename decides.** A trace is written to, read
//! once and dropped; no branch anywhere reads a field of it. The counters are
//! `vlog_parser::stats::parses()` deltas, which is a thread-local `Cell` read,
//! and the timers are `Instant`s taken at stage boundaries — the cost of the
//! whole instrument is a few dozen nanoseconds on a request measured in
//! hundreds of milliseconds.
//!
//! The counts are the honest half. A wall time moves with the machine's load;
//! "this rename parsed 5 200 whole files where 1 300 would do" does not, which
//! is why the parse-count assertions in the tests are written against these
//! fields rather than against a stopwatch.
use std::time::Instant;
/// A stage's wall time and the whole-file parses it ran.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Stage {
/// Milliseconds spent inside the stage.
pub ms: f64,
/// Whole-file parses the stage ran — a `vlog_parser::stats::parses()`
/// delta, so a lex failure (which parses nothing) is not counted.
pub parses: u64,
}
impl Stage {
/// Run `work`, recording its wall time and its parses into `self`.
///
/// Additive: a stage measured twice accumulates, which is what the
/// per-file loops want.
pub(crate) fn measure<T>(&mut self, work: impl FnOnce() -> T) -> T {
let (started, parses) = (Instant::now(), vlog_parser::stats::parses());
let out = work();
self.ms += started.elapsed().as_secs_f64() * 1000.0;
self.parses += vlog_parser::stats::parses() - parses;
out
}
}
/// Where the scope build's time went, by what the build does with each file.
///
/// The review's §3 item 1 asked for the 229 ms scope stage split, because an
/// aggregate cannot say whether a parser or an index optimization is the
/// lever. The split that answers it is **by file class**: the request's own
/// file, the files whose text spells one of the names in play (whose
/// `Semantics` every later stage reads), and the rest — the container files
/// §P2d counted, which are analysed for their index record alone and whose
/// analysis is dropped unread. `merge` is `IndexBuilder::finish`, the sort and
/// the whole-index merge, which is index work and not parse work.
///
/// `reused` is R3b's: records taken from the caller's known set instead of
/// analysed. Zero for every caller that hands none.
///
/// The lead file is in `lead` and in neither `kept_files` nor `other_files` —
/// it is always analysed and always kept, so counting it with either class
/// would blur exactly the comparison this exists to make.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct IndexTrace {
/// The request's own file, analysed before any other so the retention rule
/// can read it.
pub lead: Stage,
/// The files the retention rule keeps, analysed.
pub kept: Stage,
/// The files it does not, analysed for the record alone.
pub other: Stage,
/// Records taken from the caller's known set — the lookup and the clone,
/// with no parse in it.
pub reused: Stage,
/// How many files fell in each class, and their bytes.
pub kept_files: usize,
pub kept_bytes: usize,
pub other_files: usize,
pub other_bytes: usize,
pub reused_files: usize,
pub reused_bytes: usize,
/// `IndexBuilder::finish`: the per-file records sorted and merged.
pub merge: Stage,
}
impl IndexTrace {
/// One group of the trace line, in the order the build runs.
fn line(&self) -> String {
format!(
"index{{lead={:.1}ms kept={:.1} other={:.1} reused={:.1} merge={:.1} \
kept_files={} kept_bytes={} other_files={} other_bytes={} \
reused_files={} reused_bytes={}}}",
self.lead.ms,
self.kept.ms,
self.other.ms,
self.reused.ms,
self.merge.ms,
self.kept_files,
self.kept_bytes,
self.other_files,
self.other_bytes,
self.reused_files,
self.reused_bytes,
)
}
}
/// Where the warning pass's time went, scan by scan.
///
/// The five scans of `rename::warnings`, in the order they run. Each is a
/// whole pass over the in-scope files, so the split says which of them the
/// stage's milliseconds are — `scanned` is how many files got past the
/// `text.contains(name)` prefilter into a lex, which is the work the times are
/// mostly made of.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct WarnTrace {
/// The `` `define`` scan.
pub defines: Stage,
/// The macro-invocation-argument scan.
pub macro_uses: Stage,
/// The unstructured-SVA scan.
pub sva: Stage,
/// The unresolved-candidate scan.
pub unfinished: Stage,
/// The hierarchical-path scans: unresolved segments and same-label arms.
pub paths: Stage,
/// The edited files' unaccounted-occurrence scan.
pub unaccounted: Stage,
/// Files whose text spelled the name, and which were therefore lexed.
pub scanned: usize,
}
impl WarnTrace {
/// One group of the trace line, in the order the scans run.
fn line(&self) -> String {
format!(
"warnings{{defines={:.1}ms macro_uses={:.1} sva={:.1} unfinished={:.1} \
paths={:.1} unaccounted={:.1} scanned={}}}",
self.defines.ms,
self.macro_uses.ms,
self.sva.ms,
self.unfinished.ms,
self.paths.ms,
self.unaccounted.ms,
self.scanned,
)
}
}
/// What the verification pass did, stage by stage.
///
/// The five sub-stages of [`crate::check`]'s `verify`, in the order it runs
/// them. `checks` is the comments/attributes ledger and the parse-diagnostic
/// comparison — zero for a rename, which runs neither (a rename may not change
/// the token stream at all, which is a stronger statement than the ledger's).
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct VerifyTrace {
/// Token identity over every edited file, before against after.
pub tokens: Stage,
/// How many files the resolution check watches, and their total bytes.
pub watched: usize,
/// Bytes of those watched files.
pub watched_bytes: usize,
/// The pre-edit analysis of every watched file (cached ones are free).
pub before: Stage,
/// Building the index as it would be after the edits.
pub rebuild: Stage,
/// The post-edit analysis of every watched file, and the comparison.
pub after: Stage,
/// The ledger and parse-diagnostic checks a splice runs; zero for a
/// rename.
pub checks: Stage,
}
/// Where one rename's work went: the input it was given, the time and parses
/// of each stage, and what came out.
///
/// Filled by [`crate::rename_traced`] as it runs. Every field is written
/// exactly once except the [`Stage`]s inside `verify`, which accumulate over
/// their per-file loops.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct RenameTrace {
/// Files handed to the engine, after the scope's own dedup.
pub selected: usize,
/// Their total bytes.
pub bytes: usize,
/// Building the request-local scope: the index over every selected file.
pub index: Stage,
/// Where that build's time went, by file class (review §3 item 1).
pub indexing: IndexTrace,
/// Resolving the cursor to the declaration the rename acts on.
pub locate: Stage,
/// Collecting every occurrence and turning it into edits.
pub edits: Stage,
/// The whole verification pass.
pub verify: Stage,
/// Its five sub-stages.
pub checks: VerifyTrace,
/// Describing the occurrences the rename deliberately left alone.
pub warnings: Stage,
/// Where that pass's time went, scan by scan (review §3 item 3).
pub warning_scans: WarnTrace,
/// Files the rename edits.
pub edited: usize,
/// Whole-file parses over the entire request, from the first byte of the
/// scope build to the last warning.
pub parses: u64,
}
impl RenameTrace {
/// One line, in the server's `timing:` style — the whole trace, in the
/// order the stages ran.
///
/// The two per-stage splits come last, each as its own `name{…}` group, so
/// a reader of the old fields sees exactly what it always did.
pub fn line(&self) -> String {
format!("{} {} {}", self.stages(), self.indexing.line(), self.warning_scans.line())
}
/// The stage totals — the line as it was before the splits were added.
fn stages(&self) -> String {
format!(
"selected={} bytes={} edited={} index={:.1}ms locate={:.1} edits={:.1} \
verify{{total={:.1} tokens={:.1} watched={} watched_bytes={} before={:.1} \
rebuild={:.1} after={:.1} checks={:.1}}} warnings={:.1} \
parses={} parses{{index={} locate={} edits={} before={} rebuild={} after={}}}",
self.selected,
self.bytes,
self.edited,
self.index.ms,
self.locate.ms,
self.edits.ms,
self.verify.ms,
self.checks.tokens.ms,
self.checks.watched,
self.checks.watched_bytes,
self.checks.before.ms,
self.checks.rebuild.ms,
self.checks.after.ms,
self.checks.checks.ms,
self.warnings.ms,
self.parses,
self.index.parses,
self.locate.parses,
self.edits.parses,
self.checks.before.parses,
self.checks.rebuild.parses,
self.checks.after.parses,
)
}
}