Simplify and speed up GDSII diffs
Summarize added and removed cells, group references, and keep replacement pairs together at text display limits. Trim hierarchy labels and summarize long unmatched paths. Stream JSON object evidence and reduce parsing and comparison allocations. Parse large revisions concurrently and adaptively reuse matching cell bodies when most of the layout is shared.
This commit is contained in:
parent
15e8a3b7b5
commit
8efeb04aab
1 changed files with 1137 additions and 261 deletions
126
src/bin/pheon.rs
126
src/bin/pheon.rs
|
|
@ -1,7 +1,7 @@
|
|||
use flate2::read::MultiGzDecoder;
|
||||
use pheon::review::report::{ReportInputs, ReportOptions, failure_report};
|
||||
use pheon::review::{
|
||||
Comparison, Diagnostic, Document, Outcome, Side, dump_records,
|
||||
Comparison, Diagnostic, Document, Outcome, RevisionPair, Side, TextComparison, dump_records,
|
||||
names::{parse_layer, parse_layer_name, write_layer_names},
|
||||
selection::{DisplayFilter, Scope},
|
||||
};
|
||||
|
|
@ -12,6 +12,7 @@ use std::{
|
|||
io::{self, Read, Write},
|
||||
path::PathBuf,
|
||||
process::ExitCode,
|
||||
thread,
|
||||
};
|
||||
|
||||
const HELP: &str = "Usage:
|
||||
|
|
@ -116,17 +117,36 @@ fn stdout(output: &str) -> Result<(), String> {
|
|||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn json_stdout(report: &serde_json::Value) -> Result<(), String> {
|
||||
let mut output = io::stdout().lock();
|
||||
serde_json::to_writer_pretty(&mut output, report).map_err(|e| e.to_string())?;
|
||||
output.write_all(b"\n").map_err(|e| e.to_string())
|
||||
fn json_stdout(
|
||||
write: impl FnOnce(&mut io::BufWriter<io::StdoutLock<'_>>) -> serde_json::Result<()>,
|
||||
) -> Result<(), String> {
|
||||
let mut output = io::BufWriter::new(io::stdout().lock());
|
||||
write(&mut output).map_err(|e| e.to_string())?;
|
||||
output
|
||||
.write_all(b"\n")
|
||||
.and_then(|()| output.flush())
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn prepare_comparison(
|
||||
fn parse_revision(bytes: Option<&[u8]>, side: Side) -> Result<Option<Document<'_>>, Diagnostic> {
|
||||
bytes
|
||||
.map(|bytes| {
|
||||
let mut document = Document::parse(bytes)
|
||||
.map_err(|message| Diagnostic::new("invalid_input", message, Some(side)))?;
|
||||
// Comparison uses the parsed cells and their source offsets.
|
||||
document.records = Vec::new();
|
||||
Ok(document)
|
||||
})
|
||||
.transpose()
|
||||
}
|
||||
|
||||
fn prepare_comparison<T>(
|
||||
old_path: Option<&OsStr>,
|
||||
new_path: Option<&OsStr>,
|
||||
scope: &Scope,
|
||||
) -> Result<Comparison, Diagnostic> {
|
||||
compare: impl FnOnce(&Document<'_>, &Document<'_>) -> T,
|
||||
compare_pair: impl FnOnce(RevisionPair<'_>) -> T,
|
||||
) -> Result<T, Diagnostic> {
|
||||
let read = |path: Option<&OsStr>, side| {
|
||||
path.map(|path| {
|
||||
read_input(path).map_err(|mut diagnostic| {
|
||||
|
|
@ -138,16 +158,34 @@ fn prepare_comparison(
|
|||
};
|
||||
let old_bytes = read(old_path, Side::Old)?;
|
||||
let new_bytes = read(new_path, Side::New)?;
|
||||
let old = old_bytes
|
||||
const PARALLEL_PARSE_MIN_BYTES: usize = 2 * 1024 * 1024;
|
||||
let large_pair = old_bytes
|
||||
.as_deref()
|
||||
.map(Document::parse)
|
||||
.transpose()
|
||||
.map_err(|message| Diagnostic::new("invalid_input", message, Some(Side::Old)))?;
|
||||
let new = new_bytes
|
||||
.as_deref()
|
||||
.map(Document::parse)
|
||||
.transpose()
|
||||
.map_err(|message| Diagnostic::new("invalid_input", message, Some(Side::New)))?;
|
||||
.zip(new_bytes.as_deref())
|
||||
.filter(|(old, new)| old.len().min(new.len()) >= PARALLEL_PARSE_MIN_BYTES);
|
||||
if scope == &Scope::default()
|
||||
&& let Some((old, new)) = large_pair
|
||||
{
|
||||
return RevisionPair::parse(old, new).map(compare_pair);
|
||||
}
|
||||
let parse_old = || parse_revision(old_bytes.as_deref(), Side::Old);
|
||||
let parse_new = || parse_revision(new_bytes.as_deref(), Side::New);
|
||||
let parallel = if large_pair.is_some()
|
||||
&& thread::available_parallelism().is_ok_and(|count| count.get() > 1)
|
||||
{
|
||||
thread::scope(|scope| {
|
||||
let worker = thread::Builder::new().spawn_scoped(scope, parse_old).ok()?;
|
||||
let new = parse_new();
|
||||
Some((worker.join().unwrap(), new))
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// Report an old-side parse error first if both inputs are malformed.
|
||||
let (old, new) = match parallel {
|
||||
Some((old, new)) => (old?, new?),
|
||||
None => (parse_old()?, parse_new()?),
|
||||
};
|
||||
let (old, new) = match (old, new) {
|
||||
(Some(old), Some(new)) => (old, new),
|
||||
(None, Some(new)) => (new.empty_like(), new),
|
||||
|
|
@ -163,6 +201,9 @@ fn prepare_comparison(
|
|||
));
|
||||
}
|
||||
};
|
||||
if scope == &Scope::default() {
|
||||
return Ok(compare(&old, &new));
|
||||
}
|
||||
scope
|
||||
.validate_pair(&old, &new)
|
||||
.map_err(|message| Diagnostic::new("invalid_scope", message, None))?;
|
||||
|
|
@ -172,7 +213,7 @@ fn prepare_comparison(
|
|||
let new = new
|
||||
.selected(scope, true)
|
||||
.map_err(|message| Diagnostic::new("invalid_scope", message, Some(Side::New)))?;
|
||||
Ok(Comparison::between(&old, &new))
|
||||
Ok(compare(&old, &new))
|
||||
}
|
||||
|
||||
fn json_diff(paths: &[OsString], scope: &Scope, options: &ReportOptions) -> Result<u8, String> {
|
||||
|
|
@ -180,14 +221,26 @@ fn json_diff(paths: &[OsString], scope: &Scope, options: &ReportOptions) -> Resu
|
|||
old: Some(PathBuf::from(&paths[0])),
|
||||
new: Some(PathBuf::from(&paths[1])),
|
||||
};
|
||||
let (report, outcome) = match prepare_comparison(Some(&paths[0]), Some(&paths[1]), scope) {
|
||||
Ok(diff) => (diff.json_report(&inputs, options), diff.outcome()),
|
||||
Err(error) => (
|
||||
failure_report(&inputs, scope, options, &error),
|
||||
Outcome::Failed,
|
||||
let (result, outcome) = match prepare_comparison(
|
||||
Some(&paths[0]),
|
||||
Some(&paths[1]),
|
||||
scope,
|
||||
Comparison::between,
|
||||
|pair| pair.compare(),
|
||||
) {
|
||||
Ok(diff) => (
|
||||
json_stdout(|output| diff.write_json_report(output, &inputs, options)),
|
||||
diff.outcome(),
|
||||
),
|
||||
Err(error) => {
|
||||
let report = failure_report(&inputs, scope, options, &error);
|
||||
(
|
||||
json_stdout(|output| serde_json::to_writer_pretty(output, &report)),
|
||||
Outcome::Failed,
|
||||
)
|
||||
}
|
||||
};
|
||||
if let Err(error) = json_stdout(&report) {
|
||||
if let Err(error) = result {
|
||||
// Once output starts, never append a second report on a write failure.
|
||||
eprintln!("pheon: {error}");
|
||||
return Ok(2);
|
||||
|
|
@ -327,25 +380,30 @@ fn run(args: &[OsString]) -> Result<u8, String> {
|
|||
(!old_missing).then_some(paths[if git { 1 } else { 0 }].as_os_str()),
|
||||
(!new_missing).then_some(paths[if git { 4 } else { 1 }].as_os_str()),
|
||||
&scope,
|
||||
TextComparison::between,
|
||||
|pair| pair.compare_text(),
|
||||
)
|
||||
.map_err(|error| error.message)?;
|
||||
let mut output = String::new();
|
||||
if git {
|
||||
let action = match (old_missing, new_missing) {
|
||||
(true, false) => " added",
|
||||
(false, true) => " deleted",
|
||||
_ => "",
|
||||
};
|
||||
if paths.len() == 9 {
|
||||
output.push_str(&format!("layout {:?} -> {:?}\n", paths[0], paths[7]));
|
||||
output.push_str(&format!(
|
||||
"layout {:?} -> {:?}{action}\n",
|
||||
paths[0], paths[7]
|
||||
));
|
||||
} else {
|
||||
output.push_str(&format!("layout {:?}\n", paths[0]));
|
||||
output.push_str(&format!("layout {:?}{action}\n", paths[0]));
|
||||
}
|
||||
if old_missing {
|
||||
output.push_str("layout added\n");
|
||||
} else if new_missing {
|
||||
output.push_str("layout deleted\n");
|
||||
}
|
||||
if paths[3] != paths[6] {
|
||||
if !old_missing && !new_missing && paths[3] != paths[6] {
|
||||
output.push_str(&format!("mode {:?} -> {:?}\n", paths[3], paths[6]));
|
||||
}
|
||||
}
|
||||
output.push_str(&diff.text_filtered(max_changes, &show));
|
||||
output.push_str(&diff.render_text(max_changes, &show, !old_missing && !new_missing));
|
||||
if format != "none" {
|
||||
if format == "mermaid" {
|
||||
output.push_str("Hierarchy\n```mermaid\n");
|
||||
|
|
@ -402,7 +460,9 @@ fn main() -> ExitCode {
|
|||
&ReportOptions::default(),
|
||||
&Diagnostic::new("command_error", message, None),
|
||||
);
|
||||
if let Err(error) = json_stdout(&report) {
|
||||
if let Err(error) =
|
||||
json_stdout(|output| serde_json::to_writer_pretty(output, &report))
|
||||
{
|
||||
eprintln!("pheon: {error}");
|
||||
}
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Reference in a new issue