Skip to content

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

Optimize file transfer progress rendering

Melc6430ea6d6ca
diff --git a/src/files/app_items.zig b/src/files/app_items.zig
index 431ad36..660c03f 100644
--- a/src/files/app_items.zig
+++ b/src/files/app_items.zig
@@ -74,6 +74,25 @@ pub fn renderFileItems(self: *App, cr: *c.cairo_t, viewport_h: i32) void {
const cell = self.cellWidth();
const first = @divTrunc(self.scroll_y, self.rowHeight()) * cols;
const last = @min(@as(i32, @intCast(self.items.items.len)), (@divTrunc(self.scroll_y + viewport_h, self.rowHeight()) + 1) * cols);
+ var progress_arena = std.heap.ArenaAllocator.init(self.allocator);
+ defer progress_arena.deinit();
+ var directory_progress: []?f64 = &.{};
+ if (self.job_runner) |runner| {
+ const count: usize = @intCast(last - first);
+ const scratch = progress_arena.allocator();
+ if (scratch.alloc([]const u8, count)) |paths| {
+ for (paths, 0..) |*path, offset| {
+ const item = self.items.items[@as(usize, @intCast(first)) + offset];
+ path.* = if (item.is_dir and !item.is_symlink) item.path else "";
+ }
+ if (scratch.alloc(?f64, count)) |results| {
+ if (scratch.alloc(bool, count)) |matched| {
+ runner.progressForPaths(paths, results, matched) catch {};
+ directory_progress = results;
+ } else |_| {}
+ } else |_| {}
+ } else |_| {}
+ }
// The hover glide sits under every item, since mid-glide it straddles two.
const glide = self.item_hover.frame;
if (glide.alpha > 0) {
@@ -163,8 +182,9 @@ pub fn renderFileItems(self: *App, cr: *c.cairo_t, viewport_h: i32) void {
}
}
if (item.is_dir and !item.is_symlink) {
- if (self.job_runner) |runner| {
- if (runner.progressForPath(item.path)) |progress| {
+ const progress_index: usize = @intCast(index - first);
+ if (progress_index < directory_progress.len) {
+ if (directory_progress[progress_index]) |progress| {
const ring_size = @min(26.0, icon.w);
shell_ui.drawProgressRing(cr, .{
.x = icon.x + (icon.w - ring_size) / 2,
diff --git a/src/files/app_listing.zig b/src/files/app_listing.zig
index 62fbc70..eaf8b0b 100644
--- a/src/files/app_listing.zig
+++ b/src/files/app_listing.zig
@@ -139,7 +139,7 @@ fn damageFolderProgressBatch(
) void {
var results: [64]?f64 = undefined;
var matched: [64]bool = undefined;
- runner.progressForPaths(paths, results[0..paths.len], matched[0..paths.len]);
+ runner.progressForPaths(paths, results[0..paths.len], matched[0..paths.len]) catch return;
for (results[0..paths.len], matched[0..paths.len], indices) |result, was_matched, index| {
if (!was_matched or result == null) continue;
diff --git a/src/files/app_sidebar_paint.zig b/src/files/app_sidebar_paint.zig
index 956663e..3831532 100644
--- a/src/files/app_sidebar_paint.zig
+++ b/src/files/app_sidebar_paint.zig
@@ -712,6 +712,13 @@ pub fn renderTransferTree(self: *App, cr: *c.cairo_t) void {
const active_size = if (progress) |value| value.current_file_size else 0;
const palette = ui_theme.shellPalette();
+ self.transfer_tree_state.recomputeAggregates(
+ tree,
+ active_source,
+ active_bytes,
+ active_size,
+ ) catch return;
+
c.cairo_save(cr);
defer c.cairo_restore(cr);
c.cairo_rectangle(cr, area.x, area.y, area.w, area.h);
@@ -730,13 +737,7 @@ pub fn renderTransferTree(self: *App, cr: *c.cairo_t) void {
parent = tree.nodes.items[parent_index].parent_index;
}
const indent = @as(f64, @floatFromInt(@min(depth, 8))) * 12;
- const aggregate = self.transfer_tree_state.aggregateProgress(
- tree,
- index,
- active_source,
- active_bytes,
- active_size,
- );
+ const aggregate = self.transfer_tree_state.aggregateProgress(index);
const active = aggregate.active;
const glyph: []const u8 = if (node.kind == .directory)
(if (self.transfer_tree_state.isExpanded(node.path)) "▾" else "▸")
diff --git a/src/files/app_transfer_tree.zig b/src/files/app_transfer_tree.zig
index 27ce6d7..edd7d4d 100644
--- a/src/files/app_transfer_tree.zig
+++ b/src/files/app_transfer_tree.zig
@@ -9,6 +9,9 @@ pub const State = struct {
allocator: Allocator,
tree: ?projection.Tree = null,
expanded_paths: std.StringHashMap(void),
+ aggregate_cache: std.ArrayList(AggregateProgress) = .empty,
+ child_counts: std.ArrayList(usize) = .empty,
+ aggregate_queue: std.ArrayList(usize) = .empty,
dirty: bool = true,
pub fn init(allocator: Allocator) State {
@@ -21,6 +24,10 @@ pub const State = struct {
pub fn deinit(self: *State) void {
if (self.tree) |*tree| tree.deinit();
+ self.aggregate_cache.deinit(self.allocator);
+ self.child_counts.deinit(self.allocator);
+ self.aggregate_queue.deinit(self.allocator);
+
var paths = self.expanded_paths.keyIterator();
while (paths.next()) |path| self.allocator.free(path.*);
self.expanded_paths.deinit();
@@ -38,6 +45,10 @@ pub const State = struct {
var next = try projection.build(self.allocator, snapshot.entries.items);
errdefer next.deinit();
+ try self.aggregate_cache.ensureTotalCapacity(self.allocator, next.nodes.items.len);
+ try self.child_counts.ensureTotalCapacity(self.allocator, next.nodes.items.len);
+ try self.aggregate_queue.ensureTotalCapacity(self.allocator, next.nodes.items.len);
+
if (self.tree) |*old| old.deinit();
self.tree = next;
self.dirty = false;
@@ -85,46 +96,84 @@ pub const State = struct {
return count;
}
- fn isWithinNode(tree: *const projection.Tree, candidate: usize, ancestor: usize) bool {
- var parent = tree.nodes.items[candidate].parent_index;
- while (parent) |parent_index| {
- if (parent_index == ancestor) return true;
- parent = tree.nodes.items[parent_index].parent_index;
- }
- return candidate == ancestor;
- }
-
- /// Computes file-byte progress for a node and all descendant files.
- /// The active path is the manifest source path.
- pub fn aggregateProgress(
- _: *const State,
+ /// Computes every node's subtree progress in one bottom-up pass.
+ /// The active path is the manifest source path. The queue processes
+ /// leaves before parents, independent of manifest/node ordering.
+ pub fn recomputeAggregates(
+ self: *State,
tree: *const projection.Tree,
- index: usize,
active_source_path: []const u8,
active_bytes: u64,
active_size: u64,
- ) AggregateProgress {
- var result: AggregateProgress = .{};
- for (tree.nodes.items, 0..) |node, candidate| {
- if (!isWithinNode(tree, candidate, index) or
- node.kind == .directory or hiddenState(node.state)) continue;
-
- result.total_bytes +|= node.size;
- if (node.state == .failed) result.failed = true;
- if (node.state == .completed) {
- result.completed_bytes +|= node.size;
- } else if (active_source_path.len > 0 and
- std.mem.eql(u8, node.source_path, active_source_path))
- {
- result.active = true;
- result.completed_bytes +|= @min(
- active_bytes,
- if (node.size > 0) node.size else active_size,
- );
+ ) !void {
+ const count = tree.nodes.items.len;
+ try self.aggregate_cache.ensureTotalCapacity(self.allocator, count);
+ try self.child_counts.ensureTotalCapacity(self.allocator, count);
+ try self.aggregate_queue.ensureTotalCapacity(self.allocator, count);
+
+ self.aggregate_cache.clearRetainingCapacity();
+ self.child_counts.clearRetainingCapacity();
+ self.aggregate_queue.clearRetainingCapacity();
+
+ for (tree.nodes.items, 0..) |node, index| {
+ self.aggregate_cache.appendAssumeCapacity(.{});
+ self.child_counts.appendAssumeCapacity(0);
+
+ if (node.kind != .directory and !hiddenState(node.state)) {
+ var aggregate = &self.aggregate_cache.items[index];
+ aggregate.total_bytes = node.size;
+ if (node.state == .failed) {
+ aggregate.failed = true;
+ } else if (node.state == .completed) {
+ aggregate.completed_bytes = node.size;
+ } else if (active_source_path.len > 0 and
+ std.mem.eql(u8, node.source_path, active_source_path))
+ {
+ aggregate.active = true;
+ aggregate.completed_bytes = @min(
+ active_bytes,
+ if (node.size > 0) node.size else active_size,
+ );
+ }
+ }
+
+ if (node.parent_index) |parent| {
+ self.child_counts.items[parent] += 1;
}
}
- result.completed_bytes = @min(result.completed_bytes, result.total_bytes);
- return result;
+
+ for (self.child_counts.items, 0..) |children, index| {
+ if (children == 0) self.aggregate_queue.appendAssumeCapacity(index);
+ }
+
+ var head: usize = 0;
+ while (head < self.aggregate_queue.items.len) : (head += 1) {
+ const child_index = self.aggregate_queue.items[head];
+ const parent_index = tree.nodes.items[child_index].parent_index orelse continue;
+ const child = self.aggregate_cache.items[child_index];
+ const parent = &self.aggregate_cache.items[parent_index];
+
+ parent.total_bytes +|= child.total_bytes;
+ parent.completed_bytes +|= child.completed_bytes;
+ parent.active = parent.active or child.active;
+ parent.failed = parent.failed or child.failed;
+
+ self.child_counts.items[parent_index] -= 1;
+ if (self.child_counts.items[parent_index] == 0) {
+ self.aggregate_queue.appendAssumeCapacity(parent_index);
+ }
+ }
+
+ for (self.aggregate_cache.items) |*aggregate| {
+ aggregate.completed_bytes = @min(
+ aggregate.completed_bytes,
+ aggregate.total_bytes,
+ );
+ }
+ }
+
+ pub fn aggregateProgress(self: *const State, index: usize) AggregateProgress {
+ return self.aggregate_cache.items[index];
}
pub fn toggleExpanded(self: *State, path: []const u8) !void {
@@ -181,7 +230,8 @@ test "transfer tree aggregates descendant file progress" {
var state = State.init(allocator);
defer state.deinit();
- const aggregate = state.aggregateProgress(&tree, 0, "/source/active.bin", 4, 10);
+ try state.recomputeAggregates(&tree, "/source/active.bin", 4, 10);
+ const aggregate = state.aggregateProgress(0);
try std.testing.expectEqual(@as(u64, 30), aggregate.total_bytes);
try std.testing.expectEqual(@as(u64, 24), aggregate.completed_bytes);
try std.testing.expect(aggregate.active);
@@ -221,7 +271,8 @@ test "transfer tree aggregates deeply nested progress without mixing roots or ou
// Root includes every visible descendant, but excludes skipped/cancelled
// payloads. Failed payload remains in the denominator.
- const root = state.aggregateProgress(&tree, 0, "/source-a/one/two/active.bin", 4, 10);
+ try state.recomputeAggregates(&tree, "/source-a/one/two/active.bin", 4, 10);
+ const root = state.aggregateProgress(0);
try std.testing.expectEqual(@as(u64, 100), root.total_bytes);
try std.testing.expectEqual(@as(u64, 24), root.completed_bytes);
try std.testing.expect(root.active);
@@ -229,7 +280,7 @@ test "transfer tree aggregates deeply nested progress without mixing roots or ou
try std.testing.expectApproxEqAbs(@as(f64, 0.24), root.fraction(), 0.0001);
// The middle directory aggregates only its own subtree.
- const middle = state.aggregateProgress(&tree, 1, "/source-a/one/two/active.bin", 4, 10);
+ const middle = state.aggregateProgress(1);
try std.testing.expectEqual(@as(u64, 30), middle.total_bytes);
try std.testing.expectEqual(@as(u64, 24), middle.completed_bytes);
try std.testing.expect(middle.active);
@@ -237,14 +288,14 @@ test "transfer tree aggregates deeply nested progress without mixing roots or ou
try std.testing.expectApproxEqAbs(@as(f64, 0.8), middle.fraction(), 0.0001);
// The deepest directory sees only the active file.
- const deepest = state.aggregateProgress(&tree, 2, "/source-a/one/two/active.bin", 4, 10);
+ const deepest = state.aggregateProgress(2);
try std.testing.expectEqual(@as(u64, 10), deepest.total_bytes);
try std.testing.expectEqual(@as(u64, 4), deepest.completed_bytes);
try std.testing.expect(deepest.active);
try std.testing.expectApproxEqAbs(@as(f64, 0.4), deepest.fraction(), 0.0001);
// A second selected root never inherits the first root's active progress.
- const other_root = state.aggregateProgress(&tree, 9, "/source-a/one/two/active.bin", 4, 10);
+ const other_root = state.aggregateProgress(9);
try std.testing.expectEqual(@as(u64, 70), other_root.total_bytes);
try std.testing.expectEqual(@as(u64, 70), other_root.completed_bytes);
try std.testing.expect(!other_root.active);
diff --git a/src/files/client_render.zig b/src/files/client_render.zig
index 370c5ef..4a4a636 100644
--- a/src/files/client_render.zig
+++ b/src/files/client_render.zig
@@ -163,6 +163,12 @@ pub fn render(self: *Client) !void {
c.cairo_save(cr);
c.cairo_translate(cr, 0, @floatFromInt(tbh));
+ // FIXME(progress-only rendering): Cairo clipping limits pixel writes, but
+ // App.render() still traverses the full UI (including layout, visible-item
+ // iteration, text measurement and sidebar rendering) for a tiny progress
+ // update. Add a dedicated progress-only render path or cached static
+ // layers, while preserving independent damage regions, retained-buffer
+ // comparisons and the existing Wayland damage/commit behavior.
self.app.render(cr);
c.cairo_restore(cr);
diff --git a/src/files/ops.zig b/src/files/ops.zig
index ed7886b..31026d0 100644
--- a/src/files/ops.zig
+++ b/src/files/ops.zig
@@ -1235,11 +1235,27 @@ pub const JobRunner = struct {
paths: []const []const u8,
results: []?f64,
matched: []bool,
- ) void {
+ ) !void {
std.debug.assert(paths.len == results.len and paths.len == matched.len);
@memset(results, null);
@memset(matched, false);
+ // Index requested directories so each manifest file can update all
+ // requested ancestors by walking its destination path only once.
+ var path_indices = std.StringHashMap(usize).init(self.allocator);
+ defer path_indices.deinit();
+ try path_indices.ensureTotalCapacity(@intCast(paths.len));
+ for (paths, 0..) |path, index| {
+ if (path.len > 0) try path_indices.put(path, index);
+ }
+
+ const totals = try self.allocator.alloc(u64, paths.len);
+ defer self.allocator.free(totals);
+ const completed = try self.allocator.alloc(u64, paths.len);
+ defer self.allocator.free(completed);
+ @memset(totals, 0);
+ @memset(completed, 0);
+
_ = c.pthread_mutex_lock(&self.mutex);
defer _ = c.pthread_mutex_unlock(&self.mutex);
@@ -1275,11 +1291,56 @@ pub const JobRunner = struct {
}
}
- for (paths, 0..) |path, path_idx| {
- if (matched[path_idx]) continue;
- results[path_idx] = self.manifestSubtreeProgressLocked(
- path,
- &matched[path_idx],
+ const direct_matched = try self.allocator.dupe(bool, matched);
+ defer self.allocator.free(direct_matched);
+
+ // One manifest traversal serves every requested directory. Walking
+ // destination ancestors makes this O(manifest entries × path depth),
+ // rather than rescanning the manifest once per visible directory.
+ for (self.manifest.entries.items) |entry| {
+ if (entry.kind != .file) continue;
+ const destination = entry.destination_path orelse continue;
+ var ancestor = std.fs.path.dirname(destination) orelse continue;
+
+ while (true) {
+ if (path_indices.get(ancestor)) |path_idx| {
+ if (!direct_matched[path_idx]) {
+ matched[path_idx] = true;
+ totals[path_idx] +|= entry.size;
+
+ switch (entry.state) {
+ .completed => completed[path_idx] +|= entry.size,
+ .copying => {
+ if (self.current_file_path_len > 0 and
+ std.mem.eql(
+ u8,
+ self.current_file_path[0..self.current_file_path_len],
+ destination,
+ ))
+ {
+ completed[path_idx] +|= @min(
+ self.current_file_bytes,
+ entry.size,
+ );
+ }
+ },
+ .pending, .skipped, .failed, .cancelled => {},
+ }
+ }
+ }
+
+ const parent = std.fs.path.dirname(ancestor) orelse break;
+ if (parent.len == ancestor.len) break;
+ ancestor = parent;
+ }
+ }
+
+ for (paths, 0..) |_, path_idx| {
+ if (results[path_idx] != null or totals[path_idx] == 0) continue;
+ results[path_idx] = @min(
+ 1.0,
+ @as(f64, @floatFromInt(completed[path_idx])) /
+ @as(f64, @floatFromInt(totals[path_idx])),
);
}
}