summaryrefslogtreecommitdiff
path: root/core/File.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-23 15:42:33 -0300
committerGabriel Schneider <[email protected]>2026-11-23 16:31:13 -0300
commit6f9a8b907a504c28aef6c68b923508419b54f0ee (patch)
tree4a9411cf4caffb6b7886f52dd67392a623915338 /core/File.zig
parentfa3550eedec44e624384b478fc19a40d94fb7a82 (diff)
downloadpardes-6f9a8b907a504c28aef6c68b923508419b54f0ee.tar.gz
pardes-6f9a8b907a504c28aef6c68b923508419b54f0ee.zip
changes layer: a file with no layer pays for it what tree6 paid, found by measure (instructions by symbol and by source line, Intel PT, on the ReleaseFast pardes-perf, tree6 against round 9's base): perfops against tree6 from 12 of 121 over 0.1% to 1 (the base's `body-appends` +0.44%, `open` +0.235%, `client-key-down` +0.13..0.16%, `client-delete-undo`, `client-edit-char`, `deep-sel` +0.10..0.14%, every frame +3.8k); (1) `File.fillBody`'s row walk compiled worse with the gutter number's layer lookups (`readOnlyRow`, `fileRow`) inlined beside it: PT by source line, tree6's lines matched to the base's by their text, the same bytes walked, the inlined `fitEnd` byte loop +98k an op with no line and its checks reshuffled (body-appends +86k an op): `fillBody(comptime layered)`, as tree6's `comptime ghosted` was, chosen by `mapRows` and `bodyText` (a length test); (2) the 9P path asked the file's text and its offsets through calls: `bodyOf`, `fileDot`, `setFileDot`, `showFileOffset` answer with no layer by a length test, inline (core/ninep/pane.zig); (3) `open` (+8.8k since tree6, step 1's): not the change-id rule's regexp (it stands after the file rules and is never reached for a file) but its PARSE: pardes-perf's open boots a core each time and each core reads the rules at its first plumb (`Plumb.Parser.take` +4.0k, `expand` +1.4k, `feed` +1.2k, `endSet`, `word`, `append`, `stringToEnum`: +8.6k): the rule is deleted from core/plumbing and Look's own unresolved path takes a change id (core/look.zig `lookUnresolved`: eight letters or more, k to z, in a pane neither a raw terminal's nor a kind's, the rule's `src is pardes`; `jj.diffChange`; one naming no change searched for as before), no rule and no parse anywhere; (4) a client's key (+2.0..2.4k): the digest of File.State's two new fields, two small hash updates a pane a digest (Wyhash's buffered update and its memcpy): one field, `layer: Layer` (`owner`, `lines`: what were `layer` and `read_only`), digested by hand (`Layer.digest`: nothing hashed with no layer shown, a length test), its line in replica.zig's `hand_walked` (the test joining a client over a pane showing the layer); LEFT FOR THE COORDINATOR'S RULING: `terminal on all` +0.141% (+147k an op over the 18 terminal cells, +7.8k a terminal frame, the same in-run by an in-process counter around each cell): PT by line in the terminal's render cell: core/ansi.zig `recolor`'s per-cell style loop (source unchanged) compiled worse inlined into `body_layer.renderBody` after round 8's deletions there (`w.ghost`'s skips, `Pane.wrapAt`'s `ghost` field): what it buys is those deletions; `@call(.never_inline, ansi.recolor)` made it worse (+0.227%, and `deep-sel` +0.285%), not kept; perfops (the gate's malloc setting, load 2-9): v4 against tree6 120 of 121 within 0.1%, largest under the budget `client-key-down` on medium +0.065%; core-test 952/1118 (166 skipped) Debug tui
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/File.zig')
-rw-r--r--core/File.zig118
1 files changed, 67 insertions, 51 deletions
diff --git a/core/File.zig b/core/File.zig
index d36af5c2..b2df6c0f 100644
--- a/core/File.zig
+++ b/core/File.zig
@@ -199,13 +199,28 @@ pub const State = struct {
/// In its twin group's Follow chain (builtins.Follow), whose panes
/// show the text as consecutive pages (followChain).
follow: bool = false,
- /// Its `Changes` layer's lines while it is shown (`ReadOnly`, jj.zig):
- /// read-only lines of the buffer, in order, gpa-owned (a twin holds its
- /// own copy); none while it is hidden.
- read_only: []ReadOnly = &.{},
- /// Whose layer they are: the serial of the pane whose `Changes` answer
- /// they are the ghosts of, in its order (jj.answered); 0, none shown.
- layer: u32 = 0,
+ /// Its `Changes` layer while it is shown (`Layer`); none while hidden.
+ layer: Layer = .{},
+};
+
+/// A buffer's `Changes` layer (jj.zig): whose, and its lines.
+pub const Layer = struct {
+ /// The serial of the pane whose `Changes` answer its lines are the
+ /// ghosts of, in its order (jj.answered); 0, none shown.
+ owner: u32 = 0,
+ /// Its lines: read-only lines of the buffer, in order, gpa-owned (a
+ /// twin holds its own copy).
+ lines: []ReadOnly = &.{},
+
+ /// By hand, so that a file with no layer shown pays a length test:
+ /// nothing hashed (the walk's two fields cost two small hash updates
+ /// a pane a digest, a client's key 0.1% more). A shown one: its owner,
+ /// its lines.
+ pub fn digest(l: *const Layer, h: *pardes.walk.Hasher) void {
+ if (l.owner == 0 and l.lines.len == 0) return;
+ h.update(std.mem.asBytes(&l.owner));
+ pardes.walk.hash([]ReadOnly, &l.lines, h);
+ }
};
/// A read-only line of a pane's buffer: one of its `Changes` layer's (a
@@ -232,7 +247,7 @@ pub const ReadOnly = struct {
/// the buffer itself while it has none (a file with no layer pays nothing),
/// else a copy in `arena`.
pub fn fileText(f: *const State, arena: std.mem.Allocator) error{OutOfMemory}![]const u8 {
- if (f.read_only.len == 0) return f.content;
+ if (f.layer.lines.len == 0) return f.content;
const out = try arena.alloc(u8, fileLen(f));
var parts: FileParts = .{ .f = f };
var n: usize = 0;
@@ -246,7 +261,7 @@ pub fn fileText(f: *const State, arena: std.mem.Allocator) error{OutOfMemory}![]
/// The file's text's length (`fileText`).
pub fn fileLen(f: *const State) usize {
var n = f.content.len;
- for (f.read_only) |r| n -= r.len;
+ for (f.layer.lines) |r| n -= r.len;
return n;
}
@@ -260,7 +275,7 @@ pub const FileParts = struct {
pub fn next(it: *FileParts) ?[]const u8 {
if (it.done) return null;
- const ro = it.f.read_only;
+ const ro = it.f.layer.lines;
if (it.i == ro.len) {
it.done = true;
return it.f.content[it.at..];
@@ -287,7 +302,7 @@ pub fn fileEql(f: *const State, bytes: []const u8) bool {
/// The file's text hashed (Wyhash, as `std.hash.Wyhash.hash(0, text)`):
/// what the dirty check compares, never a copy.
pub fn fileHash(f: *const State) u64 {
- if (f.read_only.len == 0) return std.hash.Wyhash.hash(0, f.content);
+ if (f.layer.lines.len == 0) return std.hash.Wyhash.hash(0, f.content);
var h = std.hash.Wyhash.init(0);
var parts: FileParts = .{ .f = f };
while (parts.next()) |part| h.update(part);
@@ -299,7 +314,7 @@ pub fn fileHash(f: *const State) u64 {
/// it stands before starts).
pub fn fileOffset(f: *const State, at: usize) usize {
var less: usize = 0;
- for (f.read_only) |r| {
+ for (f.layer.lines) |r| {
if (r.lead) {
if (at <= r.at) break;
if (at <= r.at + r.len) return r.at - less;
@@ -318,7 +333,7 @@ pub fn fileOffset(f: *const State, at: usize) usize {
/// them (a range of the file's lines holds none of the layer's after it).
pub fn bufferOffset(f: *const State, at: usize, end: bool) usize {
var more: usize = 0;
- for (f.read_only) |r| {
+ for (f.layer.lines) |r| {
const place = r.at - more;
if (if (r.lead or end) place >= at else place > at) break;
more += r.len;
@@ -330,7 +345,7 @@ pub fn bufferOffset(f: *const State, at: usize, end: bool) usize {
/// before.
pub fn fileRow(f: *const State, row: usize) usize {
var less: usize = 0;
- for (f.read_only, 0..) |r, k| {
+ for (f.layer.lines, 0..) |r, k| {
const at = r.line + k;
if (at > row) break;
if (at == row) return r.line;
@@ -342,7 +357,7 @@ pub fn fileRow(f: *const State, row: usize) usize {
/// A file row as the buffer's: after the read-only lines standing before it.
pub fn bufferRow(f: *const State, row: usize) usize {
var more: usize = 0;
- for (f.read_only) |r| {
+ for (f.layer.lines) |r| {
if (r.line > row) break;
more += 1;
}
@@ -352,7 +367,7 @@ pub fn bufferRow(f: *const State, row: usize) usize {
/// Which read-only line buffer row `row` is (its place in `read_only`),
/// null for a line of the file.
pub fn readOnlyRow(f: *const State, row: usize) ?usize {
- for (f.read_only, 0..) |r, k| {
+ for (f.layer.lines, 0..) |r, k| {
const at = r.line + k;
if (at == row) return k;
if (at > row) return null;
@@ -366,7 +381,7 @@ pub fn readOnlyRow(f: *const State, row: usize) ?usize {
/// where one starts must leave a line's end before it: a line deleted
/// whole above one is the file's edit; a `J` into one is not).
fn touchesReadOnly(f: *const State, off: usize, len: usize, bytes: []const u8) bool {
- for (f.read_only) |r| {
+ for (f.layer.lines) |r| {
const lo = r.at;
const hi = r.at + r.len;
if (lo > off + len) break;
@@ -557,7 +572,7 @@ pub fn gutterWidth(pane: *const Pane) u16 {
// A query pane's rows carry their own line numbers (Query.zig).
if (pane.query != null) return 0;
// The file's lines: the layer's have no number.
- var lines = (if (file.line_starts.len > 0) file.line_starts.len else lineCount(file.content)) - file.read_only.len;
+ var lines = (if (file.line_starts.len > 0) file.line_starts.len else lineCount(file.content)) - file.layer.lines.len;
var digits: u16 = 1;
while (lines >= 10) : (lines /= 10) digits += 1;
return @max(config.PREFIX_W, digits + 1) + laneCells(pane) + pane.layers.heat;
@@ -813,7 +828,7 @@ pub fn deinit(p: *Pardes, pane: *Pane, file: *State) void {
if (file.line_starts.len > 0 and !index_shared) pardes.memory.freeRaw(p.gpa, indexRoom(file));
if (file.highlights.len > 0) p.tree_sitter_gpa.free(file.highlights);
p.gpa.free(file.pager_spans);
- p.gpa.free(file.read_only);
+ p.gpa.free(file.layer.lines);
locations.freeRows(p.gpa, file.location_rows);
file.context.deinit();
if (file.node_tree) |tree| syntax.freeTree(tree);
@@ -869,7 +884,7 @@ pub fn setRows(p: *Pardes, f: *State, rows: []locations.Row) error{RowsMismatch}
/// caller made anyway, as the text's buffer.
pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void {
var span = span_in orelse spanOf(f.content, new);
- if (f.read_only.len > 0) span = clearOfReadOnly(f, new, span);
+ if (f.layer.lines.len > 0) span = clearOfReadOnly(f, new, span);
if (edit(p, f, span.head, f.content.len - span.head - span.tail, new[span.head .. new.len - span.tail], new, true, null, .edit)) return;
// Refused (read-only text, said) or no memory: `new` kept until the
// next such, as the editor keeps a text it made (edit.zig
@@ -895,7 +910,7 @@ fn clearOfReadOnly(f: *const State, new: []const u8, span: Span) Span {
// Never back across a read-only line: an edit made on one (a paste
// below it) is no edit of the line above it.
var floor: usize = 0;
- for (f.read_only) |r| {
+ for (f.layer.lines) |r| {
if (r.at + r.len > span.head) break;
floor = r.at + r.len;
}
@@ -928,7 +943,7 @@ pub const How = enum { edit, load, layer };
/// and come back where the file's lines they stood before went (jj.edited
/// carries them).
pub fn load(p: *Pardes, f: *State, new: []u8) void {
- const layer = f.layer;
+ const layer = f.layer.owner;
if (layer != 0) jj.hideLayer(p, f);
defer if (layer != 0) jj.showLayer(p, f, layer);
const span = spanOf(f.content, new);
@@ -938,7 +953,7 @@ pub fn load(p: *Pardes, f: *State, new: []u8) void {
/// `replace` of the file's `len` bytes at `off` (its own offsets, `body`
/// over 9P's), as `load` is a new text: the layer's lines out and back.
pub fn replaceFile(p: *Pardes, f: *State, off: usize, len: usize, bytes: []const u8) bool {
- const layer = f.layer;
+ const layer = f.layer.owner;
if (layer != 0) jj.hideLayer(p, f);
defer if (layer != 0) jj.showLayer(p, f, layer);
return replace(p, f, off, len, bytes);
@@ -1002,14 +1017,14 @@ pub fn relayer(p: *Pardes, f: *State, new: []u8, spans: []ReadOnly, layer: u32)
for (copies[0..ntwins]) |c| p.gpa.free(c);
return false;
}
- p.gpa.free(f.read_only);
- f.read_only = spans;
- f.layer = layer;
+ p.gpa.free(f.layer.lines);
+ f.layer.lines = spans;
+ f.layer.owner = layer;
for (twins[0..ntwins], copies[0..ntwins]) |id, c| {
const tf = &p.panes[id].?.file.?;
- p.gpa.free(tf.read_only);
- tf.read_only = c;
- tf.layer = layer;
+ p.gpa.free(tf.layer.lines);
+ tf.layer.lines = c;
+ tf.layer.owner = layer;
}
for (places.items) |place| {
const pos = positionIn(p.gpa, f, bufferOffset(f, place.at, false));
@@ -1028,7 +1043,7 @@ pub fn relayer(p: *Pardes, f: *State, new: []u8, spans: []ReadOnly, layer: u32)
fn readOnly(f: *const State, off: usize, len: usize, bytes: []const u8, how: How) bool {
if (how == .layer) return false;
if (f.output) |o| if (o.from == .revision) return true;
- return how == .edit and f.read_only.len > 0 and touchesReadOnly(f, off, len, bytes);
+ return how == .edit and f.layer.lines.len > 0 and touchesReadOnly(f, off, len, bytes);
}
/// Said on each pane of `f` (`readOnly`).
@@ -1037,7 +1052,7 @@ fn refuse(p: *Pardes, f: *const State) void {
const pane = slot orelse continue;
if (pane.file) |*state| if (state == f) {
pane.body.refused = true;
- p.setMessage(.at(id), if (f.read_only.len > 0) read_only_said else "a file at a revision is read-only");
+ p.setMessage(.at(id), if (f.layer.lines.len > 0) read_only_said else "a file at a revision is read-only");
};
}
}
@@ -1068,7 +1083,7 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who
// edits come with the layer out (undoStep, `load`).
const told = how != .layer;
const file_off = fileOffset(f, off);
- const lines_taken = if (f.read_only.len > 0) std.mem.count(u8, f.content[off..][0..len], "\n") else 0;
+ const lines_taken = if (f.layer.lines.len > 0) std.mem.count(u8, f.content[off..][0..len], "\n") else 0;
const was_clean = f.revision == f.saved_revision;
const old_len = f.content.len;
const new_len = old_len - len + bytes_in.len;
@@ -1082,7 +1097,7 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who
const bytes = if (own) p.scratch.allocator().dupe(u8, bytes_in) catch return false else bytes_in;
// The journal keeps the file's text: never a buffer holding the
// layer's lines.
- const swapping = journaled and len >= swap_min and len * 2 >= old_len and f.read_only.len == 0;
+ const swapping = journaled and len >= swap_min and len * 2 >= old_len and f.layer.lines.len == 0;
// A text a join is still sending stays as it is (memory.zig `pin`).
const keep = whole == null and (swapping or pardes.memory.isPinned(f.content));
const fresh: ?[]u8 = whole orelse if (keep) copySpliced(p.gpa, f.content, off, len, bytes) catch return false else null;
@@ -1140,7 +1155,7 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who
// copy's tree under it, so that one is left as it is and parsed anew;
// nor with the `Changes` layer's lines in or going (the tree is of the
// file's text, the edit of the buffer's): parsed anew.
- if (f.node_tree) |tree| if (syntax.held_trees == null and f.node_tree_edits == f.revision and told and f.read_only.len == 0) {
+ if (f.node_tree) |tree| if (syntax.held_trees == null and f.node_tree_edits == f.revision and told and f.layer.lines.len == 0) {
syntax.editTree(tree, f.content[0..off], f.content[off..][0..len], bytes);
f.node_tree_edits = f.revision +% 1;
};
@@ -1169,9 +1184,9 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who
}
// The read-only lines after the edit move with what it put in and took
// out, and the layer's ghosts with them (none is in it: refused).
- if (told and f.read_only.len > 0) {
+ if (told and f.layer.lines.len > 0) {
const lines_put = std.mem.count(u8, bytes, "\n");
- for (f.read_only) |*r| if (r.at >= off + len) {
+ for (f.layer.lines) |*r| if (r.at >= off + len) {
r.at = r.at - len + bytes.len;
r.line = @intCast(r.line + lines_put - lines_taken);
};
@@ -1210,7 +1225,7 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who
if (!told and tf.context.revision == f.revision -% 1) tf.context.revision = f.revision;
// Its copy of the read-only lines, moved as this one's were (the
// layer's own edit sets them after, jj.showLayer).
- if (told and tf.read_only.len == f.read_only.len) @memcpy(tf.read_only, f.read_only);
+ if (told and tf.layer.lines.len == f.layer.lines.len) @memcpy(tf.layer.lines, f.layer.lines);
tf.revision = f.revision;
tf.saved_revision = f.saved_revision;
tf.saved_hash = f.saved_hash;
@@ -1383,13 +1398,12 @@ pub fn zerox(p: *Pardes, from: usize, to: usize) !*Pane {
}
// The layer's lines are the buffer's: a twin shows them too, a copy of
// their places its own.
- const read_only = try p.gpa.dupe(ReadOnly, f.read_only);
+ const read_only = try p.gpa.dupe(ReadOnly, f.layer.lines);
errdefer p.gpa.free(read_only);
const pane = try p.newDocPane(to);
pane.layers.lanes = src.layers.lanes;
pane.file = .{
- .read_only = read_only,
- .layer = f.layer,
+ .layer = .{ .owner = f.layer.owner, .lines = read_only },
.path = path,
.content = f.content,
.cap = f.cap,
@@ -1752,7 +1766,7 @@ pub fn undoStep(p: *Pardes, pane: *Pane, back: bool) error{OutOfMemory}!void {
if (if (back) j.done == 0 else j.done == j.steps.items.len) return;
const at = if (back) j.done - 1 else j.done;
const now = placeOf(pane, f);
- const layer = f.layer;
+ const layer = f.layer.owner;
if (layer != 0) jj.hideLayer(p, f);
defer if (layer != 0) jj.showLayer(p, f, layer);
if (!turn(p, f, at, back)) return error.OutOfMemory;
@@ -1991,7 +2005,7 @@ pub fn refreshHighlights(p: *Pardes) void {
if (!f.tree_context and pane.colorAlgo() == .source) paintAhead(p, pane, f);
continue;
}
- if (f.read_only.len > 0) {
+ if (f.layer.lines.len > 0) {
highlightLayered(p, pane, f);
continue;
}
@@ -2048,7 +2062,7 @@ pub fn refreshHighlights(p: *Pardes) void {
/// the frame, 11-17 ms in ReleaseSafe.
fn paintAhead(p: *Pardes, pane: *Pane, f: *State) void {
// A worker paints the buffer: with the layer's lines in it, never.
- if (f.read_only.len > 0) return;
+ if (f.layer.lines.len > 0) return;
const seen = &p.stream.ahead_seen;
// A view standing still (typing in it) needs nothing ahead: each edit
// is a new revision, and a paint asked at every keystroke was a copy
@@ -2086,7 +2100,7 @@ fn highlightLayered(p: *Pardes, pane: *Pane, f: *State) void {
// The file's bytes between the read-only lines, a stretch at a time.
var from: usize = 0;
var at = start;
- for (f.read_only) |r| {
+ for (f.layer.lines) |r| {
if (r.at + r.len <= start) continue;
if (r.at >= end) break;
const n = @min(r.at -| at, got.len -| from);
@@ -2155,7 +2169,7 @@ pub fn bodyText(tab: u16, arena: std.mem.Allocator, pane: *Pane, f: *State) ![]c
geometry(tab, pane);
const out = try arena.alloc(u8, pane.geometry_len);
const width = wrapWidth(pane, pane.view.wrap);
- const filled = fillBody(tab, out, pane, f, width, false);
+ const filled = if (f.layer.lines.len == 0) fillBody(false, tab, out, pane, f, width, false) else fillBody(true, tab, out, pane, f, width, false);
std.debug.assert(filled == out.len);
return out;
}
@@ -2199,7 +2213,7 @@ fn mapRows(tab: u16, pane: *Pane, f: *State) usize {
pane.body_wrapped = width > 0;
f.tables.ensure(tab, pane.gpa, f);
prepareContextRows(pane, f);
- return fillBody(tab, null, pane, f, width, true);
+ return if (f.layer.lines.len == 0) fillBody(false, tab, null, pane, f, width, true) else fillBody(true, tab, null, pane, f, width, true);
}
/// Everything the geometry is made of, hashed: the text (its revision and
@@ -2227,8 +2241,10 @@ fn geometryKey(pane: *const Pane, f: *const State) u64 {
/// Run the file-body row walk. With no destination it is the exact sizing
/// pass; with one it fills that allocation and records the wrapping map.
/// A read-only line (the `Changes` layer's) is a line as any, wrapped as
-/// any; the numbers are the file's, and it has none.
-fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_wrap: bool) usize {
+/// any; the numbers are the file's, and it has none (`layered`: a body with
+/// none walks without asking: beside that walk its loop compiled worse, a
+/// row walk of a file with no layer 2% dearer).
+fn fillBody(comptime layered: bool, tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_wrap: bool) usize {
if (record_wrap) pane.wrap_n = 0;
const prefix_width = gutterWidth(pane);
// Where a number ends: a blank, then the heat lane and the lanes after it.
@@ -2276,9 +2292,9 @@ fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_w
const prefix = out[written..][0..prefix_width];
@memset(prefix, ' ');
// A read-only line has no number; the others, the file's.
- const numbered = f.read_only.len == 0 or readOnlyRow(f, row) == null;
+ const numbered = !layered or readOnlyRow(f, row) == null;
if (at == 0 and prefix.len > 0 and numbered) {
- var lineno = (if (f.read_only.len == 0) row else fileRow(f, row)) + 1;
+ var lineno = (if (layered) fileRow(f, row) else row) + 1;
var digit: usize = number_end;
if (row == end_slot) {
prefix[digit - 1] = '~';
@@ -2340,7 +2356,7 @@ fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_w
/// lane each of the pane's pinned changes after the line number (the
/// gutter `lanes` cells wider), a line one added whole green across
/// it, a changed line's words, and the ghost lines (the buffer's
-/// read-only lines, `State.read_only`) inked (jj.paint).
+/// read-only lines, `State.layer.lines`) inked (jj.paint).
/// 4. churn (`Churn`): a heat lane after the number and a strip by the
/// scroll thumb.
///