diff: Add timings with debug flag
72b1e35cfde4c92199910fbfbc6881e46c79cb9c
1 parent
58a0c1b3
crates/diff/src/bin/rustik-diff.rs
+26 -4
| 38 | 38 | } |
|
| 39 | 39 | } |
|
| 40 | 40 | ||
| 41 | 41 | /// Runs the binary with forwarded Git arguments. |
|
| 42 | 42 | fn run(args: Vec<OsString>) -> Result<(), Error> { |
|
| 43 | + | let mut debug = false; |
|
| 44 | + | let mut paths = false; |
|
| 45 | + | let mut git_args = Vec::new(); |
|
| 46 | + | for arg in args { |
|
| 47 | + | if !paths && arg == "--debug" { |
|
| 48 | + | debug = true; |
|
| 49 | + | continue; |
|
| 50 | + | } |
|
| 51 | + | paths |= arg == "--"; |
|
| 52 | + | git_args.push(arg); |
|
| 53 | + | } |
|
| 54 | + | ||
| 43 | 55 | let cwd = env::current_dir()?; |
|
| 44 | 56 | let input_from_git = io::stdin().is_terminal(); |
|
| 45 | 57 | let input = if input_from_git { |
|
| 46 | - | let output = git_diff(&args, &cwd)?; |
|
| 58 | + | let output = git_diff(&git_args, &cwd)?; |
|
| 47 | 59 | if !output.status.success() { |
|
| 48 | 60 | io::stderr().write_all(&output.stderr)?; |
|
| 49 | 61 | return Err(Error::Git(output.status.code().unwrap_or(1))); |
|
| 50 | 62 | } |
|
| 51 | 63 | output.stdout |
| 53 | 65 | let mut buf = Vec::new(); |
|
| 54 | 66 | io::stdin().read_to_end(&mut buf)?; |
|
| 55 | 67 | buf |
|
| 56 | 68 | }; |
|
| 57 | 69 | ||
| 58 | - | run_or_forward(&input, &cwd, input_from_git.then_some(args.as_slice())) |
|
| 70 | + | run_or_forward( |
|
| 71 | + | &input, |
|
| 72 | + | &cwd, |
|
| 73 | + | input_from_git.then_some(git_args.as_slice()), |
|
| 74 | + | debug, |
|
| 75 | + | ) |
|
| 59 | 76 | } |
|
| 60 | 77 | ||
| 61 | 78 | /// Renders diff input or passes non-diff data through unchanged. |
|
| 62 | - | fn run_or_forward(input: &[u8], cwd: &Path, args: Option<&[OsString]>) -> Result<(), Error> { |
|
| 79 | + | fn run_or_forward( |
|
| 80 | + | input: &[u8], |
|
| 81 | + | cwd: &Path, |
|
| 82 | + | args: Option<&[OsString]>, |
|
| 83 | + | debug: bool, |
|
| 84 | + | ) -> Result<(), Error> { |
|
| 63 | 85 | let document = match DiffDocument::decode_bytes(input) { |
|
| 64 | 86 | Ok(document) if !document.is_empty() => document, |
|
| 65 | 87 | Ok(_) => { |
|
| 66 | 88 | io::stdout().write_all(input)?; |
|
| 67 | 89 | return Ok(()); |
| 98 | 120 | ||
| 99 | 121 | terminal.clear()?; |
|
| 100 | 122 | terminal.set_cursor_visible(false)?; |
|
| 101 | 123 | ||
| 102 | 124 | let repo = args.map(|args| GitRepo::new(cwd, args)); |
|
| 103 | - | let result = session::run(&mut terminal, &mut viewer, highlight_document, repo); |
|
| 125 | + | let result = session::run(&mut terminal, &mut viewer, highlight_document, repo, debug); |
|
| 104 | 126 | let cursor = terminal.set_cursor_visible(true); |
|
| 105 | 127 | ||
| 106 | 128 | result?; |
|
| 107 | 129 | cursor.map_err(Error::Io) |
|
| 108 | 130 | } |
crates/diff/src/bin/rustik-diff/session.rs
+140 -32
| 2 | 2 | ||
| 3 | 3 | use std::collections::{BTreeMap, BTreeSet}; |
|
| 4 | 4 | use std::io; |
|
| 5 | 5 | use std::sync::mpsc::{self, SyncSender, TrySendError}; |
|
| 6 | 6 | use std::thread; |
|
| 7 | + | use std::time::{Duration, Instant}; |
|
| 7 | 8 | ||
| 8 | 9 | use rustik_diff::{ |
|
| 9 | 10 | BuiltinHighlighter, ContextChange, ContextRequest, ContextUpdate, Decode, DiffDocument, |
|
| 10 | 11 | DiffHighlighter, DiffSide, DiffViewer, FileId, HighlightRequest, HighlightResult, |
|
| 11 | 12 | HighlightSource, |
| 37 | 38 | pub(super) fn run( |
|
| 38 | 39 | terminal: &mut DiffTerminal, |
|
| 39 | 40 | viewer: &mut DiffViewer, |
|
| 40 | 41 | document: DiffDocument, |
|
| 41 | 42 | repo: Option<GitRepo>, |
|
| 43 | + | debug: bool, |
|
| 42 | 44 | ) -> Result<(), Error> { |
|
| 43 | 45 | let (events_tx, events_rx) = mpsc::channel(); |
|
| 44 | 46 | TerminalEvent::spawn_input_worker(events_tx.clone()); |
|
| 45 | 47 | ||
| 46 | 48 | let worker_tx = TerminalEvent::spawn_worker(events_tx, document, repo); |
|
| 47 | 49 | let mut session = TerminalSession { |
|
| 48 | 50 | terminal, |
|
| 49 | 51 | viewer, |
|
| 50 | 52 | worker_tx, |
|
| 51 | 53 | context_pending: false, |
|
| 54 | + | debug, |
|
| 55 | + | debug_status: None, |
|
| 52 | 56 | }; |
|
| 53 | 57 | session.redraw()?; |
|
| 54 | 58 | ||
| 55 | 59 | while let Ok(event) = events_rx.recv() { |
|
| 56 | 60 | let mut redraw = false; |
| 77 | 81 | viewer: &'a mut DiffViewer, |
|
| 78 | 82 | /// Background worker command channel. |
|
| 79 | 83 | worker_tx: SyncSender<WorkerCommand>, |
|
| 80 | 84 | /// Whether a context refetch is already in flight. |
|
| 81 | 85 | context_pending: bool, |
|
| 86 | + | /// Whether debug footer diagnostics are shown. |
|
| 87 | + | debug: bool, |
|
| 88 | + | /// Latest highlight timing shown in the debug footer. |
|
| 89 | + | debug_status: Option<String>, |
|
| 82 | 90 | } |
|
| 83 | 91 | ||
| 84 | 92 | impl TerminalSession<'_> { |
|
| 85 | 93 | /// Applies one event and records whether the session should redraw. |
|
| 86 | 94 | fn update(&mut self, event: TerminalEvent, redraw: &mut bool) -> Result<Control, Error> { |
| 97 | 105 | if control == Control::Redraw || (resized && control != Control::Quit) { |
|
| 98 | 106 | *redraw = true; |
|
| 99 | 107 | } |
|
| 100 | 108 | Ok(control) |
|
| 101 | 109 | } |
|
| 102 | - | TerminalEvent::Highlights(results) => { |
|
| 103 | - | *redraw |= self.viewer.apply_highlights(results); |
|
| 110 | + | TerminalEvent::Highlights(update) => { |
|
| 111 | + | if self.debug { |
|
| 112 | + | self.debug_status = |
|
| 113 | + | Some(update.timings.status(update.timings.started.elapsed())); |
|
| 114 | + | } |
|
| 115 | + | if self.viewer.apply_highlights(update.results) || self.debug { |
|
| 116 | + | self.draw_frame()?; |
|
| 117 | + | } |
|
| 104 | 118 | Ok(Control::Continue) |
|
| 105 | 119 | } |
|
| 106 | 120 | TerminalEvent::Context(update) => { |
|
| 107 | 121 | self.context_pending = false; |
|
| 108 | 122 | if let Some(update) = update { |
| 115 | 129 | } |
|
| 116 | 130 | } |
|
| 117 | 131 | ||
| 118 | 132 | /// Draws a frame and requests highlights for visible rows. |
|
| 119 | 133 | fn redraw(&mut self) -> io::Result<()> { |
|
| 120 | - | self.draw_frame()?; |
|
| 121 | 134 | self.request_visible_highlights(); |
|
| 135 | + | self.draw_frame()?; |
|
| 122 | 136 | Ok(()) |
|
| 123 | 137 | } |
|
| 124 | 138 | ||
| 125 | 139 | /// Requests highlight work for currently visible rows. |
|
| 126 | 140 | fn request_visible_highlights(&mut self) { |
|
| 141 | + | let mut timings = Timings { |
|
| 142 | + | started: Instant::now(), |
|
| 143 | + | view: Duration::ZERO, |
|
| 144 | + | context: Duration::ZERO, |
|
| 145 | + | syntax: Duration::ZERO, |
|
| 146 | + | }; |
|
| 127 | 147 | let area = viewer_area(self.terminal.area()); |
|
| 128 | 148 | let requests = self.viewer.highlight_requests(area); |
|
| 129 | - | if !requests.is_empty() { |
|
| 130 | - | match self.worker_tx.try_send(WorkerCommand::Highlight(requests)) { |
|
| 131 | - | Ok(()) => {} |
|
| 132 | - | Err( |
|
| 133 | - | TrySendError::Full(WorkerCommand::Highlight(requests)) |
|
| 134 | - | | TrySendError::Disconnected(WorkerCommand::Highlight(requests)), |
|
| 135 | - | ) => { |
|
| 136 | - | self.viewer.cancel_highlights(requests); |
|
| 137 | - | } |
|
| 138 | - | Err( |
|
| 139 | - | TrySendError::Full(WorkerCommand::Context(_)) |
|
| 140 | - | | TrySendError::Disconnected(WorkerCommand::Context(_)), |
|
| 141 | - | ) => {} |
|
| 149 | + | timings.view = timings.started.elapsed(); |
|
| 150 | + | if requests.is_empty() { |
|
| 151 | + | if self.debug { |
|
| 152 | + | self.debug_status = Some(format!( |
|
| 153 | + | "view {} highlight {} (cached)", |
|
| 154 | + | format_ms(timings.view), |
|
| 155 | + | format_ms(timings.started.elapsed()) |
|
| 156 | + | )); |
|
| 142 | 157 | } |
|
| 158 | + | return; |
|
| 159 | + | } |
|
| 160 | + | ||
| 161 | + | match self |
|
| 162 | + | .worker_tx |
|
| 163 | + | .try_send(WorkerCommand::Highlight(HighlightWork { |
|
| 164 | + | requests, |
|
| 165 | + | timings, |
|
| 166 | + | })) { |
|
| 167 | + | Ok(()) => {} |
|
| 168 | + | Err( |
|
| 169 | + | TrySendError::Full(WorkerCommand::Highlight(work)) |
|
| 170 | + | | TrySendError::Disconnected(WorkerCommand::Highlight(work)), |
|
| 171 | + | ) => { |
|
| 172 | + | self.viewer.cancel_highlights(work.requests); |
|
| 173 | + | } |
|
| 174 | + | Err( |
|
| 175 | + | TrySendError::Full(WorkerCommand::Context(_)) |
|
| 176 | + | | TrySendError::Disconnected(WorkerCommand::Context(_)), |
|
| 177 | + | ) => {} |
|
| 143 | 178 | } |
|
| 144 | 179 | } |
|
| 145 | 180 | ||
| 146 | 181 | /// Requests context refetch work for one context change. |
|
| 147 | 182 | fn request_context(&mut self, change: ContextChange) { |
| 160 | 195 | /// Draws one terminal frame without scheduling highlight work. |
|
| 161 | 196 | fn draw_frame(&mut self) -> io::Result<()> { |
|
| 162 | 197 | self.terminal.draw(|frame| { |
|
| 163 | 198 | let body = viewer_area(frame.area); |
|
| 164 | 199 | frame.render_widget(&mut *self.viewer, body); |
|
| 165 | - | draw_status(frame); |
|
| 200 | + | draw_status(frame, self.debug_status.as_deref()); |
|
| 166 | 201 | }) |
|
| 167 | 202 | } |
|
| 168 | 203 | } |
|
| 169 | 204 | ||
| 170 | 205 | /// Event delivered to the terminal loop. |
|
| 171 | 206 | enum TerminalEvent { |
|
| 172 | 207 | /// Terminal input event. |
|
| 173 | 208 | Input(io::Result<Event<()>>), |
|
| 174 | - | /// Background highlight results. |
|
| 175 | - | Highlights(Vec<HighlightResult>), |
|
| 209 | + | /// Background highlight results with timings. |
|
| 210 | + | Highlights(HighlightUpdate), |
|
| 176 | 211 | /// Background context update result. |
|
| 177 | 212 | Context(Option<ContextUpdate>), |
|
| 178 | 213 | } |
|
| 179 | 214 | ||
| 180 | 215 | /// Background worker command. |
|
| 181 | 216 | enum WorkerCommand { |
|
| 182 | 217 | /// Highlight visible source rows. |
|
| 183 | - | Highlight(Vec<HighlightRequest>), |
|
| 218 | + | Highlight(HighlightWork), |
|
| 184 | 219 | /// Refetch and decode a context update. |
|
| 185 | 220 | Context(ContextRequest), |
|
| 186 | 221 | } |
|
| 187 | 222 | ||
| 223 | + | /// Highlight work requested by the terminal session. |
|
| 224 | + | struct HighlightWork { |
|
| 225 | + | /// Rows that need highlighting. |
|
| 226 | + | requests: Vec<HighlightRequest>, |
|
| 227 | + | /// Timings collected before the worker receives the request. |
|
| 228 | + | timings: Timings, |
|
| 229 | + | } |
|
| 230 | + | ||
| 231 | + | /// Highlight result produced by the background worker. |
|
| 232 | + | struct HighlightUpdate { |
|
| 233 | + | /// Highlighted visible rows. |
|
| 234 | + | results: Vec<HighlightResult>, |
|
| 235 | + | /// Timings collected while producing the results. |
|
| 236 | + | timings: Timings, |
|
| 237 | + | } |
|
| 238 | + | ||
| 239 | + | /// Debug timings for one highlight batch. |
|
| 240 | + | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
|
| 241 | + | struct Timings { |
|
| 242 | + | /// Time at which the visible highlight batch was requested. |
|
| 243 | + | started: Instant, |
|
| 244 | + | /// Time spent collecting visible highlight requests. |
|
| 245 | + | view: Duration, |
|
| 246 | + | /// Time spent loading repo-backed highlight context. |
|
| 247 | + | context: Duration, |
|
| 248 | + | /// Time spent syntax-highlighting rows. |
|
| 249 | + | syntax: Duration, |
|
| 250 | + | } |
|
| 251 | + | ||
| 252 | + | impl Timings { |
|
| 253 | + | /// Formats this timing breakdown after the batch lands in the UI. |
|
| 254 | + | fn status(&self, landed: Duration) -> String { |
|
| 255 | + | let measured = self.view + self.context + self.syntax; |
|
| 256 | + | let wait = landed.saturating_sub(measured); |
|
| 257 | + | format!( |
|
| 258 | + | "wait {} view {} ctx {} syn {} highlight {}", |
|
| 259 | + | format_ms(wait), |
|
| 260 | + | format_ms(self.view), |
|
| 261 | + | format_ms(self.context), |
|
| 262 | + | format_ms(self.syntax), |
|
| 263 | + | format_ms(landed) |
|
| 264 | + | ) |
|
| 265 | + | } |
|
| 266 | + | } |
|
| 267 | + | ||
| 188 | 268 | /// Background worker for blocking repo and syntax-highlighting work. |
|
| 189 | 269 | struct HighlightWorker { |
|
| 190 | 270 | /// Built-in syntax highlighter. |
|
| 191 | 271 | highlighter: BuiltinHighlighter, |
|
| 192 | 272 | /// Git-backed repo used for direct Git invocations. |
| 204 | 284 | repo, |
|
| 205 | 285 | context_sources: BTreeMap::new(), |
|
| 206 | 286 | } |
|
| 207 | 287 | } |
|
| 208 | 288 | ||
| 209 | - | /// Highlights one batch after enriching it with repo-backed context. |
|
| 210 | - | fn highlight(&mut self, mut requests: Vec<HighlightRequest>) -> Vec<HighlightResult> { |
|
| 289 | + | /// Highlights one batch with context and syntax timings. |
|
| 290 | + | fn highlight( |
|
| 291 | + | &mut self, |
|
| 292 | + | mut requests: Vec<HighlightRequest>, |
|
| 293 | + | mut timings: Timings, |
|
| 294 | + | ) -> (Vec<HighlightResult>, Timings) { |
|
| 295 | + | let context = Instant::now(); |
|
| 211 | 296 | self.load_context_sources(&requests); |
|
| 297 | + | timings.context = context.elapsed(); |
|
| 298 | + | let syntax = Instant::now(); |
|
| 212 | 299 | for request in &mut requests { |
|
| 213 | 300 | if let Some(source) = self.context_source_for(request) { |
|
| 214 | 301 | request.source = source.clone(); |
|
| 215 | 302 | } |
|
| 216 | 303 | } |
|
| 217 | - | self.highlighter.highlight_batch(requests) |
|
| 304 | + | let results = self.highlighter.highlight_batch(requests); |
|
| 305 | + | timings.syntax = syntax.elapsed(); |
|
| 306 | + | (results, timings) |
|
| 218 | 307 | } |
|
| 219 | 308 | ||
| 220 | 309 | /// Loads bounded source context for uncached request paths. |
|
| 221 | 310 | fn load_context_sources(&mut self, requests: &[HighlightRequest]) { |
|
| 222 | 311 | let Some(repo) = self.repo.as_ref() else { |
| 314 | 403 | let (requests_tx, requests_rx) = mpsc::sync_channel::<WorkerCommand>(4); |
|
| 315 | 404 | thread::spawn(move || { |
|
| 316 | 405 | let mut worker = HighlightWorker::new(&document, repo); |
|
| 317 | 406 | while let Ok(command) = requests_rx.recv() { |
|
| 318 | 407 | match command { |
|
| 319 | - | WorkerCommand::Highlight(requests) => { |
|
| 320 | - | if requests.is_empty() { |
|
| 408 | + | WorkerCommand::Highlight(work) => { |
|
| 409 | + | if work.requests.is_empty() { |
|
| 321 | 410 | continue; |
|
| 322 | 411 | } |
|
| 323 | - | let results = worker.highlight(requests); |
|
| 324 | - | if tx.send(Self::Highlights(results)).is_err() { |
|
| 412 | + | let (results, timings) = worker.highlight(work.requests, work.timings); |
|
| 413 | + | if tx |
|
| 414 | + | .send(Self::Highlights(HighlightUpdate { results, timings })) |
|
| 415 | + | .is_err() |
|
| 416 | + | { |
|
| 325 | 417 | break; |
|
| 326 | 418 | } |
|
| 327 | 419 | } |
|
| 328 | 420 | WorkerCommand::Context(request) => { |
|
| 329 | 421 | if tx.send(Self::Context(worker.context(request))).is_err() { |
| 368 | 460 | area.rows.saturating_sub(footer_rows), |
|
| 369 | 461 | ) |
|
| 370 | 462 | } |
|
| 371 | 463 | ||
| 372 | 464 | /// Draws the bottom status row. |
|
| 373 | - | fn draw_status(frame: &mut tui::Frame<'_>) { |
|
| 465 | + | fn draw_status(frame: &mut tui::Frame<'_>, debug: Option<&str>) { |
|
| 374 | 466 | let area = frame.area; |
|
| 375 | 467 | if area.rows == 0 { |
|
| 376 | 468 | return; |
|
| 377 | 469 | } |
|
| 378 | 470 | let row = Rect::new(area.x, area.bottom().saturating_sub(1), area.cols, 1); |
|
| 379 | 471 | ||
| 380 | - | frame.render_widget(status_line(area.cols), row); |
|
| 472 | + | frame.render_widget(status_line(area.cols, debug), row); |
|
| 381 | 473 | } |
|
| 382 | 474 | ||
| 383 | 475 | /// Returns the padded bottom status line. |
|
| 384 | - | fn status_line(cols: usize) -> Line { |
|
| 385 | - | Line::new(Label::new("rustik-diff").fg(FOOTER_FG)) |
|
| 476 | + | fn status_line(cols: usize, debug: Option<&str>) -> Line { |
|
| 477 | + | let text = match debug { |
|
| 478 | + | Some(debug) if debug.len() < cols => { |
|
| 479 | + | format!( |
|
| 480 | + | "{:<width$}{debug}", |
|
| 481 | + | "rustik-diff", |
|
| 482 | + | width = cols - debug.len() |
|
| 483 | + | ) |
|
| 484 | + | } |
|
| 485 | + | _ => "rustik-diff".to_owned(), |
|
| 486 | + | }; |
|
| 487 | + | Line::new(Label::new(&text).fg(FOOTER_FG)) |
|
| 386 | 488 | .style(Style::default().bg(FOOTER_BG)) |
|
| 387 | 489 | .padded(cols) |
|
| 490 | + | .truncated(cols, "") |
|
| 491 | + | } |
|
| 492 | + | ||
| 493 | + | /// Formats debug timing in milliseconds. |
|
| 494 | + | fn format_ms(duration: Duration) -> String { |
|
| 495 | + | format!("{:.1}ms", duration.as_secs_f64() * 1_000.0) |
|
| 388 | 496 | } |
|
| 389 | 497 | ||
| 390 | 498 | #[cfg(test)] |
|
| 391 | 499 | #[allow(clippy::unwrap_used)] |
|
| 392 | 500 | mod tests { |
| 401 | 509 | assert_eq!(viewer_area(Rect::new(0, 0, 80, 1)), Rect::new(0, 0, 80, 0)); |
|
| 402 | 510 | } |
|
| 403 | 511 | ||
| 404 | 512 | #[test] |
|
| 405 | 513 | fn status_line_fills_footer_width() { |
|
| 406 | - | assert_eq!(status_line(24).width(), 24); |
|
| 514 | + | assert_eq!(status_line(24, None).width(), 24); |
|
| 407 | 515 | } |
|
| 408 | 516 | ||
| 409 | 517 | #[test] |
|
| 410 | 518 | fn resize_requests_redraw_even_when_viewer_continues() { |
|
| 411 | 519 | let resized = true; |