From 0045636495bf2baf77c8b0eb63e45feeffa016ac Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 18 Nov 2026 02:43:11 -0300 Subject: file watch: a text's watch armed over its open (file_watch.applyEffect) no longer reads and hashes the file again to know the disk is still what the open read: readHost keeps, for each of its last eight whole reads, the path, the buffer it returned and the file's identity (inode, size, times) before reading (fs.last_reads), and an arm whose pane's content is that very buffer, unedited, compares the file as it is now with it, reading again (reloadPane) when it changed, or the content came from elsewhere (a slim dump's Restore, which the watch fills from disk); strace: medium.zig opened 3 -> 2 times by a start; tui medium.zig (3.2 MB), perf stat to the kill at the first frame, best of ten interleaved: 55.4M -> 53.6M instructions:u, 10.1 -> 8.8 ms (load 4); tui core-test 819/958 (139 skipped), shell-test 993/996, behaviour-test 269/273 (the four addon ABI tests the base fails), pane-test 180/181, addon-test 5/5, process-test 2 of 144 failed (lspcomplete and lspcompletemoved, the base's); gui unit-test 1582/1590 (the base's), the frame corpus 77/77 against mbase; test/daemon.py, --upgrade (tui and gui, from the base build), --games and --window-clients (its PDF window case run) pass Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/file_watch.zig | 12 ++++++++++++ src/fs.zig | 13 +++++++++++++ 2 files changed, 25 insertions(+) (limited to 'src') diff --git a/src/file_watch.zig b/src/file_watch.zig index 788bcae1..9bb59683 100644 --- a/src/file_watch.zig +++ b/src/file_watch.zig @@ -561,6 +561,18 @@ pub fn applyEffect( watched.existed = std.hash.Wyhash.hash(0, file.path); return false; }; + // A text armed over its open's bytes, read just now (its content that + // read's very buffer, unedited): the file still what it was before that + // read (the same inode, size and times) needs no reading again. + if (on) if (watches[id]) |*watched| if (core.panes[id]) |pane| if (pane.file) |file| if (filesystem.localPath(file.path)) |native| { + const at = std.hash.Wyhash.hash(0, native); + for (filesystem.last_reads) |r| if (r.path == at and r.bytes == file.content.ptr and r.len == file.content.len and file.revision == 0) { + const now = identify(io, file.path) catch break; + if (!now.eql(r.file)) break; + watched.existed = std.hash.Wyhash.hash(0, file.path); + return false; + }; + }; return on and watches[id] != null and reloadPane(core, io, watches, id, false); } diff --git a/src/fs.zig b/src/fs.zig index 47ee0856..dab7c8dd 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -738,6 +738,8 @@ pub fn readHost(gpa: std.mem.Allocator, path_z: [:0]const u8, limit: usize) ![]u else => error.OpenFailed, }; defer _ = libc.close(fd); + // What it is before its bytes are read, for a watch armed over them. + const before = (std.Io.File{ .handle = fd, .flags = .{ .nonblocking = true } }).stat(std.Io.Threaded.global_single_threaded.io()) catch null; // A device (/dev/zero, a tty) is no file to read whole: said so, not // read to the stream limit and called too large. @@ -791,9 +793,20 @@ pub fn readHost(gpa: std.mem.Allocator, path_z: [:0]const u8, limit: usize) ![]u len += @intCast(n); } if (len != buf.len) buf = try gpa.realloc(buf, len); + if (before) |st| if (st.kind == .file) { + last_reads[last_read % last_reads.len] = .{ .path = std.hash.Wyhash.hash(0, path_z), .bytes = buf.ptr, .len = buf.len, .file = .{ .inode = st.inode, .size = st.size, .mtime_ns = st.mtime.nanoseconds, .ctime_ns = st.ctime.nanoseconds } }; + last_read +%= 1; + }; return buf; } +/// The last files `readHost` read whole, each its path's hash, the buffer +/// it returned and what the file was before the read: a watch armed over a +/// text whose content is that very buffer (file_watch.applyEffect) finds the +/// disk the same without reading it again. +pub var last_reads: [8]struct { path: u64 = 0, bytes: [*]const u8 = undefined, len: usize = 0, file: @import("file_watch.zig").Identity = undefined } = @splat(.{}); +var last_read: usize = 0; + /// A 9P write of an Edit held while its commands run (Namespace.edit_hold). pub const EditHold = struct { /// The listener's: the connection that asked, and its park. -- cgit v1.3