Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

Add Files UI performance profiling

Mele21d96bc2b2e
diff --git a/src/files/app_listing.zig b/src/files/app_listing.zig
index eaf8b0b..38db399 100644
--- a/src/files/app_listing.zig
+++ b/src/files/app_listing.zig
@@ -1,6 +1,7 @@
//! Files listing: navigation, history moves, snapshot sync and the filtered, sorted view.
const std = @import("std");
+const ui_profile = @import("ui_profile.zig");
const i18n = @import("i18n");
const tr = i18n.tr;
const App = @import("app.zig").App;
@@ -137,6 +138,9 @@ fn damageFolderProgressBatch(
paths: []const []const u8,
indices: []const usize,
) void {
+ const profile_started = ui_profile.start();
+ defer ui_profile.finish(profile_started, "progress_damage_lookup", paths.len);
+
var results: [64]?f64 = undefined;
var matched: [64]bool = undefined;
runner.progressForPaths(paths, results[0..paths.len], matched[0..paths.len]) catch return;
@@ -260,6 +264,9 @@ fn damageJobProgress(
}
pub fn sync(self: *App) void {
+ const profile_started = ui_profile.start();
+ defer ui_profile.finish(profile_started, "sync", self.items.items.len);
+
if (self.worker.takeApplicationNotice()) |notice| self.setStatusNotice(notice.message, notice.failed);
if (self.worker.takeThemeNotice()) |notice| {
defer self.worker.allocator.free(notice);
@@ -534,6 +541,9 @@ pub fn lessThan(mode: Sort, a: ViewItem, b: ViewItem) bool {
pub fn rebuildView(self: *App) void {
const snap = self.snapshot orelse return;
+ const profile_started = ui_profile.start();
+ defer ui_profile.finish(profile_started, "listing_rebuild", self.items.items.len);
+
self.torrents.rows_changed = false;
// Keep old storage alive until names, selection and focus have been copied.
// Both a worker refresh and a local filter can rebuild this view.
diff --git a/src/files/app_paint.zig b/src/files/app_paint.zig
index a1a5cb7..fcb29e9 100644
--- a/src/files/app_paint.zig
+++ b/src/files/app_paint.zig
@@ -1,6 +1,7 @@
//! Files painting: header, toolbar, location bar, rename card and the frame.
const std = @import("std");
+const ui_profile = @import("ui_profile.zig");
const i18n = @import("i18n");
const tr = i18n.tr;
const App = @import("app.zig").App;
@@ -145,6 +146,9 @@ pub fn renderGitViewToggle(self: *App, cr: *c.cairo_t) void {
// Cairo Rendering
pub fn render(self: *App, cr: *c.cairo_t) void {
+ const profile_started = ui_profile.start();
+ defer ui_profile.finish(profile_started, "app_render", self.items.items.len);
+
const w_f = @as(f64, @floatFromInt(self.w));
const h_f = @as(f64, @floatFromInt(self.h));
diff --git a/src/files/app_sidebar_paint.zig b/src/files/app_sidebar_paint.zig
index 3831532..f56ae74 100644
--- a/src/files/app_sidebar_paint.zig
+++ b/src/files/app_sidebar_paint.zig
@@ -1,6 +1,7 @@
//! Files sidebar painting.
const std = @import("std");
+const ui_profile = @import("ui_profile.zig");
const i18n = @import("i18n");
const tr = i18n.tr;
const App = @import("app.zig").App;
@@ -123,6 +124,9 @@ fn drawSidebarRowProximity(
/// The shared accordion geometry. Every expanded visible card receives
/// its body, with all cards stacked in section order.
pub fn sidebarLayout(self: *App) app_model.SidebarLayout {
+ const profile_started = ui_profile.start();
+ defer ui_profile.finish(profile_started, "sidebar_layout", self.items.items.len);
+
const top = self.contentTop() - self.sidebar_scroll;
const pin_bottom = self.pinRowY(self.pins.items.items.len);
const old_places_top = self.contentTop() - self.sidebar_scroll;
@@ -703,6 +707,13 @@ pub fn renderTransferTree(self: *App, cr: *c.cairo_t) void {
const area = transferTreeBox(self);
if (area.w < 100 or area.h <= 0) return;
+ const profile_started = ui_profile.start();
+ defer ui_profile.finish(
+ profile_started,
+ "sidebar_tree_paint",
+ tree.nodes.items.len,
+ );
+
const progress = if (self.job_runner) |runner| runner.pollProgress() else null;
const active_source = if (progress) |value|
(if (value.current_file_path_len > 0) value.current_file_path[0..value.current_file_path_len] else "")
diff --git a/src/files/app_transfer_rows.zig b/src/files/app_transfer_rows.zig
index 51fbac5..818bb33 100644
--- a/src/files/app_transfer_rows.zig
+++ b/src/files/app_transfer_rows.zig
@@ -1,6 +1,7 @@
//! Merge active file-operation manifest entries into the current folder view.
const std = @import("std");
+const ui_profile = @import("ui_profile.zig");
const App = @import("app.zig").App;
const ViewItem = @import("app_model.zig").ViewItem;
const transfer_tree = @import("transfer_tree.zig");
@@ -17,10 +18,23 @@ pub fn appendTransferRows(
self.job_state != .waiting_conflict) return;
const runner = self.job_runner orelse return;
- var manifest = runner.snapshotTransferManifest(self.allocator) catch return;
+ const profile_started = ui_profile.start();
+ defer ui_profile.finish(profile_started, "append_transfer_rows", self.items.items.len);
+
+ const snapshot_started = ui_profile.start();
+ var manifest = runner.snapshotTransferManifest(self.allocator) catch {
+ ui_profile.finish(snapshot_started, "rows_manifest_snapshot", 0);
+ return;
+ };
+ ui_profile.finish(snapshot_started, "rows_manifest_snapshot", manifest.entries.items.len);
defer manifest.deinit();
- var tree = transfer_tree.build(mem, manifest.entries.items) catch return;
+ const build_started = ui_profile.start();
+ var tree = transfer_tree.build(mem, manifest.entries.items) catch {
+ ui_profile.finish(build_started, "rows_tree_build", manifest.entries.items.len);
+ return;
+ };
+ ui_profile.finish(build_started, "rows_tree_build", tree.nodes.items.len);
defer tree.deinit();
appendTreeRows(self, mem, &tree, old);
}
@@ -34,6 +48,9 @@ pub fn appendTreeRows(
tree: *const transfer_tree.Tree,
old: *const std.StringHashMap(ViewItem),
) void {
+ const profile_started = ui_profile.start();
+ defer ui_profile.finish(profile_started, "append_tree_rows", tree.nodes.items.len);
+
for (tree.nodes.items) |node| {
if (!std.mem.eql(u8, std.fs.path.dirname(node.path) orelse "", self.history.current)) continue;
diff --git a/src/files/app_transfer_tree.zig b/src/files/app_transfer_tree.zig
index edd7d4d..6c0975f 100644
--- a/src/files/app_transfer_tree.zig
+++ b/src/files/app_transfer_tree.zig
@@ -1,6 +1,7 @@
//! Cached UI state for the transfer destination tree.
const std = @import("std");
+const ui_profile = @import("ui_profile.zig");
const Allocator = std.mem.Allocator;
const ops = @import("ops.zig");
const projection = @import("transfer_tree.zig");
@@ -39,10 +40,19 @@ pub const State = struct {
/// Replaces the cached projection from one consistent manifest snapshot.
pub fn rebuild(self: *State, runner: *ops.JobRunner) !void {
+ var profiled_nodes = self.treeSize();
+ const profile_started = ui_profile.start();
+ defer ui_profile.finish(profile_started, "sidebar_tree_rebuild", profiled_nodes);
+
+ const snapshot_started = ui_profile.start();
var snapshot = try runner.snapshotTransferManifest(self.allocator);
+ ui_profile.finish(snapshot_started, "sidebar_manifest_snapshot", snapshot.entries.items.len);
defer snapshot.deinit();
+ const build_started = ui_profile.start();
var next = try projection.build(self.allocator, snapshot.entries.items);
+ profiled_nodes = next.nodes.items.len;
+ ui_profile.finish(build_started, "sidebar_tree_projection", next.nodes.items.len);
errdefer next.deinit();
try self.aggregate_cache.ensureTotalCapacity(self.allocator, next.nodes.items.len);
@@ -89,6 +99,9 @@ pub const State = struct {
}
pub fn visibleCount(self: *const State, tree: *const projection.Tree) usize {
+ const profile_started = ui_profile.start();
+ defer ui_profile.finish(profile_started, "tree_visible_count", tree.nodes.items.len);
+
var count: usize = 0;
for (tree.nodes.items, 0..) |_, index| {
if (self.nodeVisible(tree, index)) count += 1;
@@ -106,6 +119,9 @@ pub const State = struct {
active_bytes: u64,
active_size: u64,
) !void {
+ const profile_started = ui_profile.start();
+ defer ui_profile.finish(profile_started, "tree_aggregate", tree.nodes.items.len);
+
const count = tree.nodes.items.len;
try self.aggregate_cache.ensureTotalCapacity(self.allocator, count);
try self.child_counts.ensureTotalCapacity(self.allocator, count);
@@ -176,6 +192,10 @@ pub const State = struct {
return self.aggregate_cache.items[index];
}
+ fn treeSize(self: *const State) usize {
+ return if (self.tree) |*tree| tree.nodes.items.len else 0;
+ }
+
pub fn toggleExpanded(self: *State, path: []const u8) !void {
if (self.expanded_paths.fetchRemove(path)) |removed| {
self.allocator.free(removed.key);
diff --git a/src/files/client_render.zig b/src/files/client_render.zig
index 4a4a636..8dfc8a2 100644
--- a/src/files/client_render.zig
+++ b/src/files/client_render.zig
@@ -1,6 +1,7 @@
//! Rendering: shm buffers, the frame and the client-side titlebar.
const std = @import("std");
+const ui_profile = @import("ui_profile.zig");
const c = @import("c.zig").api;
const app_mod = @import("app.zig");
const header = @import("header.zig");
@@ -71,6 +72,9 @@ fn frameDone(callback: *wl.Callback, _: wl.Callback.Event, self: *Client) void {
pub fn render(self: *Client) !void {
if (!self.configured or !self.app.dirty or self.outstanding >= 2) return;
+ const profile_started = ui_profile.start();
+ defer ui_profile.finish(profile_started, "client_frame", self.app.items.items.len);
+
// Partial repaints (hover, proximity glow, fades) follow the display,
// not the pointer rate: wait for the last commit's frame callback.
if ((self.app.paint_damage != null or self.app.progress_damage_count > 0) and
diff --git a/src/files/ui_profile.zig b/src/files/ui_profile.zig
new file mode 100644
index 0000000..b0fea48
--- /dev/null
+++ b/src/files/ui_profile.zig
@@ -0,0 +1,28 @@
+//! Opt-in timing for Files UI hot paths.
+//! Enable with REDIWM_FILES_UI_PROFILE=1.
+
+const std = @import("std");
+
+pub fn start() ?u64 {
+ if (std.c.getenv("REDIWM_FILES_UI_PROFILE") == null) return null;
+ return nowNs();
+}
+
+pub fn finish(started: ?u64, stage: []const u8, item_count: usize) void {
+ const start_ns = started orelse return;
+ const elapsed_ns = nowNs() - start_ns;
+ // Avoid flooding stderr with insignificant calls.
+ if (elapsed_ns < 50_000) return;
+ std.debug.print(
+ "FILES_UI_PROFILE,{s},{d},{d}\n",
+ .{ stage, elapsed_ns, item_count },
+ );
+}
+
+fn nowNs() u64 {
+ var ts: std.c.timespec = undefined;
+ const result = std.c.clock_gettime(std.c.CLOCK.MONOTONIC, &ts);
+ std.debug.assert(result == 0);
+ return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s +
+ @as(u64, @intCast(ts.nsec));
+}