diff options
Diffstat (limited to 'src/Tables.zig')
| -rw-r--r-- | src/Tables.zig | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/src/Tables.zig b/src/Tables.zig index 2e65a716..44918559 100644 --- a/src/Tables.zig +++ b/src/Tables.zig @@ -95,8 +95,13 @@ pending: ?Resume = null, /// and whether the line before ended a block. const Resume = struct { at: usize, n: u32, fence: ?Fence, prev_blank: bool }; -/// What one frame spends reading a big text from the top. +/// What one frame spends reading a big text from the top: on the session's +/// tick, by the clock (src/tape.zig keeps a replica's reading in step). pub const frame_ns: u64 = 4 * std.time.ns_per_ms; +/// What a new text's first look reads at once, wherever it is asked for +/// (the render, or a step reading the view's geometry): a count, never the +/// clock, so whoever asks first finds the same tables. +pub const first_lines: u32 = 4096; pub fn deinit(t: *Tables, gpa: std.mem.Allocator) void { t.list.deinit(gpa); @@ -130,10 +135,7 @@ pub fn find(t: *const Tables, line: usize) ?*const Table { /// touch shift and only the block around it is read again (`reflow`): a /// keystroke costs its table, not the file. pub fn ensure(t: *Tables, gpa: std.mem.Allocator, f: *File.State) void { - if (t.current(f)) { - if (t.pending != null) t.go(gpa, f.content, frame_ns); - return; - } + if (t.current(f)) return; defer { t.revision = f.revision; t.content = @intFromPtr(f.content.ptr); @@ -157,13 +159,20 @@ pub fn ensure(t: *Tables, gpa: std.mem.Allocator, f: *File.State) void { // read: it goes on. if (lineHolding(starts, f.splice.head) >= r.n) { t.lines = starts.len; - t.go(gpa, f.content, frame_ns); + t.go(gpa, f.content, 0, first_lines); return; } } else if (t.reflow(gpa, f)) |_| return else |_| {} } t.begin(starts.len); - t.go(gpa, f.content, frame_ns); + t.go(gpa, f.content, 0, first_lines); +} + +/// A frame's more of a reading under way: on the session's tick, so a core +/// that draws reads exactly as far as one that does not. +pub fn step(t: *Tables, gpa: std.mem.Allocator, f: *File.State) void { + t.ensure(gpa, f); + if (t.pending != null) t.go(gpa, f.content, frame_ns, std.math.maxInt(u32)); } /// Whether a reading of the text is still under way (Pardes.nextWake). @@ -183,11 +192,11 @@ fn begin(t: *Tables, lines: usize) void { t.pending = .{ .at = 0, .n = 0, .fence = null, .prev_blank = true }; } -/// Read on for about `budget_ns` (0: to the end). On no memory there are -/// no tables (the text is shown as it is). -fn go(t: *Tables, gpa: std.mem.Allocator, content: []const u8, budget_ns: u64) void { +/// Read on for about `budget_ns` (0: no clock) or `max_lines`, whichever +/// ends first. On no memory there are no tables (the text is shown as it is). +fn go(t: *Tables, gpa: std.mem.Allocator, content: []const u8, budget_ns: u64, max_lines: u32) void { var at = t.pending orelse return; - const done = t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, budget_ns) catch { + const done = t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, budget_ns, max_lines) catch { t.list.clearRetainingCapacity(); t.pending = null; return; @@ -290,7 +299,7 @@ fn reflow(t: *Tables, gpa: std.mem.Allocator, f: *File.State) !void { const prev_blank = r0 == 0 or blank(prev) or closesAny(prev) or std.mem.startsWith(u8, prev[@min(indentOf(prev), prev.len)..], "#"); const head = lineOf(content, starts, r0); var at: Resume = .{ .at = head.start, .n = head.n, .fence = null, .prev_blank = prev_blank }; - _ = try t.scanFrom(gpa, content, &at, @intCast(r1), false, 0); + _ = try t.scanFrom(gpa, content, &at, @intCast(r1), false, 0, std.math.maxInt(u32)); const fresh = try gpa.dupe(Table, t.list.items[before..]); defer gpa.free(fresh); t.list.shrinkRetainingCapacity(before); @@ -500,7 +509,7 @@ pub fn scan(t: *Tables, gpa: std.mem.Allocator, content: []const u8) !void { t.aligns.clearRetainingCapacity(); t.begin(std.mem.count(u8, content, "\n") + 1); var at = t.pending.?; - _ = try t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, 0); + _ = try t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, 0, std.math.maxInt(u32)); t.pending = null; } @@ -508,7 +517,7 @@ pub fn scan(t: *Tables, gpa: std.mem.Allocator, content: []const u8) !void { /// appending them; with `fences`, keep the fences. With a budget, stop /// when it is spent, `at` where to go on from. Whether it reached the end /// (or `stop`). -fn scanFrom(t: *Tables, gpa: std.mem.Allocator, content: []const u8, at: *Resume, stop: u32, fences: bool, budget_ns: u64) !bool { +fn scanFrom(t: *Tables, gpa: std.mem.Allocator, content: []const u8, at: *Resume, stop: u32, fences: bool, budget_ns: u64, max_lines: u32) !bool { var fence = at.fence; var prev_blank = at.prev_blank; var line = lineAt(content, at.at, at.n); @@ -516,7 +525,7 @@ fn scanFrom(t: *Tables, gpa: std.mem.Allocator, content: []const u8, at: *Resume var lines: usize = 0; while (line) |l| : (lines += 1) { if (l.n > stop) break; - if (budget_ns > 0 and lines % 256 == 255 and panes.Places.nowNs() -| t0 > budget_ns) { + if (lines % 256 == 255 and (lines >= max_lines or (budget_ns > 0 and pardes.tape.over(t0, budget_ns)))) { at.* = .{ .at = l.start, .n = l.n, .fence = fence, .prev_blank = prev_blank }; return false; } @@ -1049,6 +1058,7 @@ test "tables: a big text's tables are read a frame's budget at a time from the t File.setContent(p, f, new); edits += 1; } + p.update(.tick); _ = frame.reset(.retain_capacity); _ = try p.render(frame.allocator()); try testing.expect(frames < 10_000); @@ -1077,6 +1087,7 @@ test "tables: a table wider than a wrapping pane is shown as its source, laid ou // Source: a pipe is a pipe, at its source column. try testing.expectEqual(@as(i32, 7), File.displayOffset(pane, 5, 0, 7)); try testing.expect(p.executeBuiltinLine(0, "Wrap off")); + p.sync(); _ = frame.reset(.retain_capacity); _ = try p.render(frame.allocator()); try testing.expect(!pane.body_wrapped); |
