Skip to content

Commit b747261

Browse files
WillLillisamaanq
authored andcommitted
feat(cli): display parse and edit times separately for parse --time
1 parent 5d9870e commit b747261

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

cli/src/parse.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ pub fn parse_file_at_path(parser: &mut Parser, opts: &ParseFileOptions) -> Resul
221221
})));
222222
}
223223

224-
let time = Instant::now();
224+
let parse_time = Instant::now();
225225

226226
#[inline(always)]
227227
fn is_utf16_le_bom(bom_bytes: &[u8]) -> bool {
@@ -315,6 +315,7 @@ pub fn parse_file_at_path(parser: &mut Parser, opts: &ParseFileOptions) -> Resul
315315
Some(parse_opts),
316316
),
317317
};
318+
let parse_duration = parse_time.elapsed();
318319

319320
let stdout = io::stdout();
320321
let mut stdout = stdout.lock();
@@ -324,6 +325,7 @@ pub fn parse_file_at_path(parser: &mut Parser, opts: &ParseFileOptions) -> Resul
324325
println!("BEFORE:\n{}", String::from_utf8_lossy(&source_code));
325326
}
326327

328+
let edit_time = Instant::now();
327329
for (i, edit) in opts.edits.iter().enumerate() {
328330
let edit = parse_edit_flag(&source_code, edit)?;
329331
perform_edit(&mut tree, &mut source_code, &edit)?;
@@ -333,11 +335,12 @@ pub fn parse_file_at_path(parser: &mut Parser, opts: &ParseFileOptions) -> Resul
333335
println!("AFTER {i}:\n{}", String::from_utf8_lossy(&source_code));
334336
}
335337
}
338+
let edit_duration = edit_time.elapsed();
336339

337340
parser.stop_printing_dot_graphs();
338341

339-
let duration = time.elapsed();
340-
let duration_ms = duration.as_micros() as f64 / 1e3;
342+
let parse_duration_ms = parse_duration.as_micros() as f64 / 1e3;
343+
let edit_duration_ms = edit_duration.as_micros() as f64 / 1e3;
341344
let mut cursor = tree.walk();
342345

343346
if opts.output == ParseOutput::Normal {
@@ -548,11 +551,12 @@ pub fn parse_file_at_path(parser: &mut Parser, opts: &ParseFileOptions) -> Resul
548551
}
549552

550553
if first_error.is_some() || opts.print_time {
554+
let path = opts.path.to_string_lossy();
551555
write!(
552556
&mut stdout,
553-
"{:width$}\t{duration_ms:>7.2} ms\t{:>6} bytes/ms",
554-
opts.path.to_str().unwrap(),
555-
(source_code.len() as u128 * 1_000_000) / duration.as_nanos(),
557+
"{:width$}\tParse: {parse_duration_ms:>7.2} ms\t{:>6} bytes/ms",
558+
path,
559+
(source_code.len() as u128 * 1_000_000) / parse_duration.as_nanos(),
556560
width = opts.max_path_length
557561
)?;
558562
if let Some(node) = first_error {
@@ -578,23 +582,31 @@ pub fn parse_file_at_path(parser: &mut Parser, opts: &ParseFileOptions) -> Resul
578582
start.row, start.column, end.row, end.column
579583
)?;
580584
}
585+
if !opts.edits.is_empty() {
586+
write!(
587+
&mut stdout,
588+
"\n{:width$}\tEdit: {edit_duration_ms:>7.2} ms",
589+
" ".repeat(path.len()),
590+
width = opts.max_path_length,
591+
)?;
592+
}
581593
writeln!(&mut stdout)?;
582594
}
583595

584596
return Ok(ParseResult {
585597
successful: first_error.is_none(),
586598
bytes: source_code.len(),
587-
duration: Some(duration),
599+
duration: Some(parse_duration),
588600
});
589601
}
590602
parser.stop_printing_dot_graphs();
591603

592604
if opts.print_time {
593-
let duration = time.elapsed();
605+
let duration = parse_time.elapsed();
594606
let duration_ms = duration.as_micros() as f64 / 1e3;
595607
writeln!(
596608
&mut stdout,
597-
"{:width$}\t{duration_ms:>7.2} ms\t(timed out)",
609+
"{:width$}\tParse: {duration_ms:>7.2} ms\t(timed out)",
598610
opts.path.to_str().unwrap(),
599611
width = opts.max_path_length
600612
)?;

0 commit comments

Comments
 (0)