diff --git a/crates/vlog_refactor/src/ports/offer.rs b/crates/vlog_refactor/src/ports/offer.rs index 516cccb..cfc8c0d 100644 --- a/crates/vlog_refactor/src/ports/offer.rs +++ b/crates/vlog_refactor/src/ports/offer.rs @@ -47,7 +47,7 @@ use super::list::{append_to_list, conn_list_open, elem_spans}; use super::propagate::{connections_of, instance_at, missing_ports}; use super::remove::{body_uses_in, disconnections_in, header_removal, port_at}; use super::{port_action_modules_in, sites_phrase, Plan}; -use crate::scope::name_among; +use crate::scope::BaseNames; /// Which of the four port refactorings an offer names. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -149,6 +149,8 @@ struct Files<'a> { /// Index of the buffer the cursor is in. here: usize, parsed: Vec>, + /// How many sources share each file name, for naming them in messages. + base_names: BaseNames, } impl<'a> Files<'a> { @@ -161,7 +163,8 @@ impl<'a> Files<'a> { sources.dedup_by(|a, b| a.0 == b.0); let here = sources.iter().position(|(path, _)| *path == file)?; let parsed = sources.iter().map(|_| None).collect(); - Some(Files { sources, here, parsed }) + let base_names = BaseNames::of(sources.iter().map(|(p, _)| *p)); + Some(Files { sources, here, parsed, base_names }) } fn len(&self) -> usize { @@ -248,7 +251,7 @@ impl<'a> Files<'a> { if !text.contains(module) { return Ok((Vec::new(), 0)); } - let shown = name_among(self.sources.iter().map(|(p, _)| *p), path); + let shown = self.base_names.name(path); if i == self.here { return disconnections_in(text, &shown, buffer.tree, module, port, warnings); } diff --git a/crates/vlog_refactor/src/scope.rs b/crates/vlog_refactor/src/scope.rs index a3a1849..280150b 100644 --- a/crates/vlog_refactor/src/scope.rs +++ b/crates/vlog_refactor/src/scope.rs @@ -25,6 +25,7 @@ //! [`KnownFacts`]; `docs/features/perf5r-scope-reuse/README.md` has the rest. use std::collections::HashMap; +use std::ffi::OsString; use std::path::{Path, PathBuf}; use vlog_parser::parse_source_tolerant; @@ -170,6 +171,8 @@ pub(crate) struct Scope<'a> { /// [`Analysis`] that does not depend on the finished index. Moved into /// `cache` the first time that file's analysis is asked for. kept: HashMap, + /// How many sources share each file name, for [`Scope::name_of`]. + base_names: BaseNames, } impl<'a> Scope<'a> { @@ -262,7 +265,8 @@ impl<'a> Scope<'a> { .map(|(i, path)| (path.as_path(), FileId(i as u32))) .collect(); let file_ids = sources.iter().map(|(path, _)| ids.get(path).copied()).collect(); - Scope { sources, index, file_ids, cache: HashMap::new(), kept } + let base_names = BaseNames::of(sources.iter().map(|(p, _)| *p)); + Scope { sources, index, file_ids, cache: HashMap::new(), kept, base_names } } pub(crate) fn len(&self) -> usize { @@ -339,7 +343,7 @@ impl<'a> Scope<'a> { /// `pwrmgr_if.sv` in one answer must name two findable files (§65.1 /// re-check R1). A front end makes the path relative as it does any other. pub(crate) fn name_of(&self, path: &Path) -> String { - name_among(self.sources.iter().map(|(p, _)| *p), path) + self.base_names.name(path) } /// [`Scope::name_of`] for the file at source index `i`. @@ -354,16 +358,33 @@ pub(crate) fn display(path: &Path) -> String { path.file_name().and_then(|n| n.to_str()).unwrap_or("?").to_string() } -/// [`display`] made unambiguous within a set of files: the file name when no -/// other path in `all` shares it, else the path as given. The one rule -/// [`Scope::name_of`] and a buffer-level planner (the port offers) both name -/// files by, so an offer and the refactoring it offers say the same thing. -pub(crate) fn name_among<'p>(all: impl IntoIterator, path: &Path) -> String { - let base = path.file_name(); - let shared = all.into_iter().filter(|p| p.file_name() == base).nth(1).is_some(); - match shared { - true => path.display().to_string(), - false => display(path), +/// How many files of a set share each file name, counted once so that +/// naming every file of a large set is linear, not quadratic (review M-2). +pub(crate) struct BaseNames(HashMap); + +impl BaseNames { + /// The counts for the files at `all`. + pub(crate) fn of<'p>(all: impl IntoIterator) -> BaseNames { + let mut counts: HashMap = HashMap::new(); + for path in all { + if let Some(base) = path.file_name() { + *counts.entry(base.to_os_string()).or_default() += 1; + } + } + BaseNames(counts) + } + + /// [`display`] made unambiguous within the counted set: the file name + /// when no other path shares it, else the path as given. The one rule + /// [`Scope::name_of`] and a buffer-level planner (the port offers) both + /// name files by, so an offer and the refactoring it offers say the same + /// thing. + pub(crate) fn name(&self, path: &Path) -> String { + let shared = path.file_name().and_then(|b| self.0.get(b)).is_some_and(|&n| n > 1); + match shared { + true => path.display().to_string(), + false => display(path), + } } } @@ -574,4 +595,14 @@ mod tests { assert_eq!(scope.index().fingerprint(), built.fingerprint()); assert!(scope.analysis(0).is_some(), "and every file still analyses on demand"); } + + #[test] + fn a_file_name_another_file_shares_is_named_by_its_path() { + let paths = [Path::new("/a/x/if.sv"), Path::new("/a/y/if.sv"), Path::new("/a/top.sv")]; + let names = BaseNames::of(paths); + assert_eq!(names.name(Path::new("/a/x/if.sv")), "/a/x/if.sv"); + assert_eq!(names.name(Path::new("/a/y/if.sv")), "/a/y/if.sv"); + assert_eq!(names.name(Path::new("/a/top.sv")), "top.sv"); + assert_eq!(names.name(Path::new("/elsewhere/new.sv")), "new.sv", "outside the set"); + } } diff --git a/oct_review.md b/oct_review.md index c1fff57..e60a3b8 100644 --- a/oct_review.md +++ b/oct_review.md @@ -75,7 +75,7 @@ It is a read-only review. The only file written is this one. - **Effect:** it misleads users and agents, and it fires for modules, packages (`p::X`) and every include-wrapper layout. - **Suggested fix:** skip tokens covered by this rename's edits. -### M-2. Naming files in warnings is quadratic: about 0.5 s per rename on opentitan +### M-2. Naming files in warnings is quadratic: about 0.5 s per rename on opentitan — **FIXED 2026-10-07** - **Where:** - `vlog_refactor/src/scope.rs:~341-345` (`name_of`) scans every source on each call.