Perf: a frame counts each command once, and every frame closes its stage record (#9328)

Only the outermost run_command writes the frame's command stage, so a nested
run (file.save -> layer.smartObjects.saveContents, actions.play -> its view
steps) isn't counted twice. ui_frame's UI moves into ui_body and finish_frame
runs after it on every path, including the screen picker's wait modal; the
pass before fonts are ready closes it too.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Brennan StehlingandClaude Opus 5.5 committed 2026-10-27 23:08:27 -07:00
1 parent ce8f3e719e
commit fc6d8b0ec2
5 files changed
+89 -44

No files matched your search

+1
View File
@@ -122,6 +122,7 @@ impl PhotocraftApp {
synthetic_reinject: false,
synthetic_snapshot: None,
automation_input: false,
run_depth: 0,
paused_playbacks: Default::default(),
channel_thumbs: None,
channel_views: HashMap::new(),
+10 -1
View File
@@ -35,6 +35,13 @@ impl PhotocraftApp {
}
pub(crate) fn run_command(&mut self, id: &str, params: Value) -> Result<Value, String> {
self.run_depth += 1;
let r = self.run_command_at_depth(id, params);
self.run_depth -= 1;
r
}
fn run_command_at_depth(&mut self, id: &str, params: Value) -> Result<Value, String> {
if !vector_ui::cancel_stale(self) {
return Err(self.ui.status.clone());
}
@@ -121,7 +128,9 @@ impl PhotocraftApp {
self.sync_command_side_effects(id, &r, creates_active_mask, creates_active_vector_mask, creates_adjustment_or_fill, path_mask);
self.perf.last_command = id.to_string();
self.perf.command_ms = gpu_canvas::now_ms() - t0;
self.perf.stage.command(id, self.perf.command_ms);
if self.run_depth == 1 {
self.perf.stage.command(id, self.perf.command_ms);
}
match &r {
Ok(_) => {
if self.ui.status_error {
+57 -43
View File
@@ -167,12 +167,67 @@ impl PhotocraftApp {
self.automation_input = false;
// No editor was drawn this pass: end its IME context rather than keep a stale owner.
type_tool::sync_ime(self, &ctx);
// The frame's stage record was started with its input: close it here too.
self.finish_frame(0.0);
return;
}
let t0 = gpu_canvas::now_ms();
self.perf.stage.ui_start(t0, frame.info().cpu_usage);
// A software adapter or CPU mode chosen at startup is every document's reason.
crate::render_path::frame(self);
// Every frame that started its stage record closes it, whichever way `ui_body` returns.
let drawn = self.ui_body(ui);
let ui_ms = gpu_canvas::now_ms() - t0;
// A GPU measurement lands a frame or two after the refresh it belongs to. Refreshes
// carry them out, but the last refresh before the app settles has none following it, so
// drain the timer here too or its number is never reported. Drain it either way: a
// measurement left sitting would surface later against an unrelated refresh. Report it
// only while the canvas is still on the GPU, since a CPU refresh clears `gpu_ms`
// precisely so a GPU number is never shown as that refresh's.
if let Some(gpu) = &self.gpu
&& let Some(t) = gpu.take_gpu_timing()
&& self.perf.last_refresh.starts_with("gpu")
{
self.perf.record_gpu(t);
}
self.perf.frame(ui_ms);
self.finish_frame(ui_ms);
if !drawn {
return;
}
// This pass handled its input: the in-flight synthetic batch is delivered once and the
// next step may be injected on the next frame.
self.finish_synthetic_pass();
// Synthetic input is injected one press/release step per frame: keep frames coming until
// the queue is empty, then release control replies waiting on it.
// Only a pass that reached the end of the UI and delivered the in-flight batch counts as
// settled input; an early-returning pass keeps the batch (and the waiting replies).
if !self.synthetic.is_empty() || self.synthetic_batch.is_some() {
ctx.request_repaint();
} else if !self.input_waiters.is_empty() {
for w in self.input_waiters.drain(..) {
let _ = w.send(serde_json::json!({"ok": true, "result": null}));
}
}
// A menu or dialog can start a filter after `jobs_ui::tick` ran in this frame. Keep
// polling it even when the platform would otherwise put the event loop to sleep.
jobs_ui::keep_polling(self, &ctx);
// Menus and buttons in this frame may have asked for a file dialog.
self.poll_file_dialog(&ctx, Some(frame));
// The Type tool's IME descriptor is the last writer of this pass: every panel and the
// canvas have finished mutating the model, and an absent/focus-moved editor ends here.
type_tool::sync_ime(self, &ctx);
// Issue any due `ui.screenshot` now that this pass's state is settled: a queued request
// that has waited its passes and animation delay captures exactly this frame.
self.issue_captures(&ctx);
// Last, after everything drawn this frame: a lost window device can't draw it (#3438).
gpu_status::check_window(self, &ctx);
}
/// The frame's UI, between `ui_start` and `finish_frame`. False when the screen picker's
/// wait modal took the frame (its input, IME and window check already handled).
fn ui_body(&mut self, ui: &mut egui::Ui) -> bool {
let ctx = ui.ctx().clone();
// A text field's right-click menu, before any field is drawn (text_field_menu.rs).
text_field_menu::show(&ctx);
// The wheel goes to a focused value field before the canvas or a panel sees it (#5473).
@@ -203,7 +258,7 @@ impl PhotocraftApp {
self.automation_input = false;
type_tool::sync_ime(self, &ctx);
gpu_status::check_window(self, &ctx);
return;
return false;
}
if screen_picker::busy(&ctx) {
ui.disable();
@@ -298,47 +353,6 @@ impl PhotocraftApp {
egui::Modal::new(egui::Id::unique("screen-color-wait")).frame(egui::Frame::NONE).backdrop_color(egui::Color32::TRANSPARENT).show(&ctx, |_| {});
ctx.set_cursor_icon(egui::CursorIcon::Wait);
}
let ui_ms = gpu_canvas::now_ms() - t0;
// A GPU measurement lands a frame or two after the refresh it belongs to. Refreshes
// carry them out, but the last refresh before the app settles has none following it, so
// drain the timer here too or its number is never reported. Drain it either way: a
// measurement left sitting would surface later against an unrelated refresh. Report it
// only while the canvas is still on the GPU, since a CPU refresh clears `gpu_ms`
// precisely so a GPU number is never shown as that refresh's.
if let Some(gpu) = &self.gpu
&& let Some(t) = gpu.take_gpu_timing()
&& self.perf.last_refresh.starts_with("gpu")
{
self.perf.record_gpu(t);
}
self.perf.frame(ui_ms);
self.finish_frame(ui_ms);
// This pass handled its input: the in-flight synthetic batch is delivered once and the
// next step may be injected on the next frame.
self.finish_synthetic_pass();
// Synthetic input is injected one press/release step per frame: keep frames coming until
// the queue is empty, then release control replies waiting on it.
// Only a pass that reached the end of the UI and delivered the in-flight batch counts as
// settled input; an early-returning pass keeps the batch (and the waiting replies).
if !self.synthetic.is_empty() || self.synthetic_batch.is_some() {
ctx.request_repaint();
} else if !self.input_waiters.is_empty() {
for w in self.input_waiters.drain(..) {
let _ = w.send(serde_json::json!({"ok": true, "result": null}));
}
}
// A menu or dialog can start a filter after `jobs_ui::tick` ran in this frame. Keep
// polling it even when the platform would otherwise put the event loop to sleep.
jobs_ui::keep_polling(self, &ctx);
// Menus and buttons in this frame may have asked for a file dialog.
self.poll_file_dialog(&ctx, Some(frame));
// The Type tool's IME descriptor is the last writer of this pass: every panel and the
// canvas have finished mutating the model, and an absent/focus-moved editor ends here.
type_tool::sync_ime(self, &ctx);
// Issue any due `ui.screenshot` now that this pass's state is settled: a queued request
// that has waited its passes and animation delay captures exactly this frame.
self.issue_captures(&ctx);
// Last, after everything drawn this frame: a lost window device can't draw it (#3438).
gpu_status::check_window(self, &ctx);
true
}
}
+17
View File
@@ -42,6 +42,23 @@ fn perf_output_includes_slow_frames() {
for key in ["t", "frameMs", "inputMs", "commandMs", "damageMs", "compositeMs", "uploadMs", "gpuMs", "presentMs", "refresh", "refreshPx", "path"] {
assert!(last.get(key).is_some(), "{key} in {last}");
}
// A nested run (`actions.play` → its view steps through `App::run`) is inside the outer
// call's time: the frame counts the outer command once, not the steps again.
app.session.actions.list.push(photocraft_engine::actions_cmds::Action {
name: "Zoom".into(),
steps: vec![("view.zoomIn".into(), json!({})), ("view.zoomOut".into(), json!({}))],
disabled: Default::default(),
..Default::default()
});
let played = app.run("actions.play", json!({"action": "Zoom"})).unwrap();
assert_eq!(played["ran"], 2, "{played}");
let outer_ms = app.perf.command_ms;
app.finish_frame(1.0);
let frames = call(&mut app, &ctx, "ui.inspect", json!({}))["result"]["perf"]["slowFrames"].clone();
let last = frames.as_array().and_then(|f| f.last()).expect("a slow frame").clone();
assert_eq!(last["command"], "actions.play", "{last}");
let command_ms = last["commandMs"].as_f64().unwrap();
assert!((command_ms - outer_ms).abs() < 1e-9, "the outer run's {outer_ms} ms, once: {last}");
}
#[test]
+4
View File
@@ -505,6 +505,10 @@ pub struct PhotocraftApp {
/// makes shortcut- and dialog-triggered commands pass the same policy as
/// direct control calls.
pub(crate) automation_input: bool,
/// How many `run_command` calls are on the stack. Only the outermost one writes the frame's
/// command stage: a nested run (`file.save` → `layer.smartObjects.saveContents`,
/// `actions.play` → its steps) is already inside the outer call's time.
run_depth: u32,
/// Action playbacks paused at a step dialog, by the token in that dialog's fields
/// ([`actions::RESUME`]): what to play next and the automation gate it paused under.
pub(crate) paused_playbacks: actions::PausedPlaybacks,