summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/file_watch.zig12
-rw-r--r--src/fs.zig13
2 files changed, 25 insertions, 0 deletions
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.