summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/File.zig101
-rw-r--r--src/Stream.zig49
-rw-r--r--src/gui/gui.zig77
-rw-r--r--src/image.zig45
-rw-r--r--src/panes.zig20
-rw-r--r--src/pardes.zig4
-rw-r--r--src/syntax.zig1
-rw-r--r--src/tty/tty.zig38
8 files changed, 262 insertions, 73 deletions
diff --git a/src/File.zig b/src/File.zig
index 798e3e84..6df49c98 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -26,6 +26,7 @@ const terminal = panes.terminal;
const Text = panes.Text;
const SYNTAX_CONTEXT_AFTER_ROWS: usize = 2;
+const long_paint: usize = 16 << 10;
/// Content and primary selection at one file edit boundary. Keeping only the
/// primary avoids putting Text.max_selections ranges in every history entry.
@@ -1724,8 +1725,23 @@ pub fn refreshHighlights(p: *Pardes) void {
f.syntax_dirty = false;
continue;
}
- const need_start = lineStart(p.gpa, f, f.scroll);
- const need_end = @max(need_start, lineStart(p.gpa, f, f.scroll + pane.rows + SYNTAX_CONTEXT_AFTER_ROWS));
+ // ponytail: a stretch over `long_paint` bytes (a minified line) is
+ // painted only around the caret, so a key typed in a 500 KB line
+ // parses 16 KB, not the line (190 ms a key in ReleaseSafe; 25 ms at
+ // 64 KB, so 16, still eight views of 80x25); its first
+ // tokens may be coloured as if the line began there. The upgrade: an
+ // incremental parse of the whole text, edited in place.
+ const caret = @min(f.content.len, lineStart(p.gpa, f, @intCast(@max(0, pane.body.cur_row))) + @as(usize, @intCast(@max(0, pane.body.cur_col))));
+ const around = struct {
+ fn clamp(a: usize, b: usize, at: usize) [2]usize {
+ if (b - a <= long_paint) return .{ a, b };
+ const lo = std.math.clamp(at -| long_paint / 2, a, b - long_paint);
+ return .{ lo, lo + long_paint };
+ }
+ };
+ const need = around.clamp(lineStart(p.gpa, f, f.scroll), @max(lineStart(p.gpa, f, f.scroll), lineStart(p.gpa, f, f.scroll + pane.rows + SYNTAX_CONTEXT_AFTER_ROWS)), caret);
+ const need_start = need[0];
+ const need_end = need[1];
if ((!f.tree_context or (f.highlight_start == 0 and f.highlights.len == f.content.len)) and
f.highlights.len > 0 and need_start >= f.highlight_start and
need_end <= f.highlight_start + f.highlights.len)
@@ -1734,7 +1750,11 @@ pub fn refreshHighlights(p: *Pardes) void {
if (!f.tree_context and pane.colorAlgo() == .source) paintAhead(p, pane, f);
continue;
}
- const slack: usize = if (f.highlights.len == 0) 0 else pane.rows;
+ // A view far from what is painted (a jump: ge, a Look) paints only
+ // itself on the frame, a third of the work; the stretch around it
+ // comes painted ahead (paintAhead) where there are workers.
+ const far = f.highlights.len == 0 or need_end <= f.highlight_start or need_start >= f.highlight_start + f.highlights.len;
+ const slack: usize = if (far and p.host.vtable.search != null) 0 else if (f.highlights.len == 0) 0 else pane.rows;
const whole = pane.colorAlgo() == .locations or f.tree_context;
// Painted ahead on a worker (Stream.paintAhead): taken, no parse.
if (!whole and pane.colorAlgo() == .source) if (Stream.takeAhead(p, pane.serial, f.revision, need_start, need_end)) |got| {
@@ -1747,11 +1767,14 @@ pub fn refreshHighlights(p: *Pardes) void {
continue;
};
if (pane.colorAlgo() == .source and !whole) p.stream.painted_on_loop += 1;
- const start = if (whole) 0 else lineStart(p.gpa, f, f.scroll -| slack);
- const end = if (whole)
+ const wide_start = if (whole) 0 else lineStart(p.gpa, f, f.scroll -| slack);
+ const wide_end = if (whole)
f.content.len
else
- @max(start, lineStart(p.gpa, f, f.scroll + pane.rows + SYNTAX_CONTEXT_AFTER_ROWS + slack));
+ @max(wide_start, lineStart(p.gpa, f, f.scroll + pane.rows + SYNTAX_CONTEXT_AFTER_ROWS + slack));
+ const window = if (whole) [2]usize{ wide_start, wide_end } else around.clamp(wide_start, wide_end, caret);
+ const start = window[0];
+ const end = window[1];
const new_highlights = (switch (pane.colorAlgo()) {
.none => @as([]u8, &.{}),
.diff => syntax.highlightDiff(p.tree_sitter_gpa, f.content, start, end),
@@ -1772,24 +1795,76 @@ pub fn refreshHighlights(p: *Pardes) void {
}
}
-/// The stretch a scroll of `pane` is heading into, two views on the way it
-/// last went, asked of a worker (Stream.paintAhead) so it is painted by the
-/// time the view gets there.
+/// The stretch a scroll of `pane` is heading into, the view and three more
+/// the way it last went, asked of a worker (Stream.paintAhead) so it is
+/// painted by the time the view gets there. From the view itself: two
+/// views out, an upward stretch stopped short of the view's own bottom
+/// rows, and a half page up (or down) missed it by a line and painted on
+/// the frame, 11-17 ms in ReleaseSafe.
fn paintAhead(p: *Pardes, pane: *Pane, f: *State) void {
const seen = &p.stream.ahead_seen;
- if (seen.serial == pane.serial and f.scroll != seen.scroll) seen.dir = if (f.scroll > seen.scroll) 1 else -1;
+ // 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
+ // of the stretch and a worker's start on the loop per key.
+ if (seen.serial == pane.serial and f.scroll == seen.scroll) return;
+ if (seen.serial == pane.serial) seen.dir = if (f.scroll > seen.scroll) 1 else -1;
if (seen.serial != pane.serial) seen.dir = 1;
seen.serial = pane.serial;
seen.scroll = f.scroll;
const rows: usize = pane.rows;
- const view = if (seen.dir > 0) f.scroll + 2 * rows else f.scroll -| 2 * rows;
- const start = lineStart(p.gpa, f, view -| rows);
- const end = @max(start, lineStart(p.gpa, f, view + 2 * rows + SYNTAX_CONTEXT_AFTER_ROWS));
+ const start = lineStart(p.gpa, f, if (seen.dir > 0) f.scroll else f.scroll -| 3 * rows);
+ const end = @max(start, lineStart(p.gpa, f, f.scroll + (if (seen.dir > 0) 4 * rows else rows) + SYNTAX_CONTEXT_AFTER_ROWS));
// Painted already, as far as that goes.
if (f.highlights.len > 0 and start >= f.highlight_start and end <= f.highlight_start + f.highlights.len) return;
Stream.paintAhead(p, pane.serial, f.revision, f.path, f.content, start, end);
}
+test "a long line is painted only around the caret" {
+ if (!syntax.enabled) return;
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ defer p.deinit();
+ const line = try std.testing.allocator.alloc(u8, 600 << 10);
+ defer std.testing.allocator.free(line);
+ for (line, 0..) |*c, i| c.* = if (i % 2 == 0) '1' else ',';
+ const pane = try p.setTestFile(line);
+ p.gpa.free(pane.file.?.path);
+ pane.file.?.path = try p.gpa.dupe(u8, "/tmp/long.json");
+ pane.body.cur_col = 300 << 10;
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ _ = try p.render(arena.allocator());
+ const f = &pane.file.?;
+ try std.testing.expect(f.highlights.len == long_paint);
+ try std.testing.expect(f.highlight_start <= 300 << 10 and f.highlight_start + f.highlights.len >= 300 << 10);
+}
+
+test "a caret deep in a long line scrolls the view sideways to it, measured from the view's edge" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ defer p.deinit();
+ p.settings.wrap = false;
+ const line = try std.testing.allocator.alloc(u8, 500 << 10);
+ defer std.testing.allocator.free(line);
+ @memset(line, 'a');
+ const pane = try p.setTestFile(line);
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ _ = try p.render(arena.allocator());
+ // Right, far: the caret ends a margin in from the right edge.
+ pane.body.cur_col = 400_000;
+ pane.ensureCursorVisible();
+ const w: i32 = @as(i32, pane.cols) - @as(i32, gutterWidth(pane));
+ const hmargin: i32 = @min(config.scroll_off, @divTrunc(w - 1, 2));
+ try std.testing.expectEqual(@as(i32, 400_000 - (w - 1 - hmargin)), pane.hscroll);
+ // Back left of the view: a margin in from the left edge.
+ pane.body.cur_col = 100_000;
+ pane.ensureCursorVisible();
+ try std.testing.expectEqual(@as(i32, 100_000 - hmargin), pane.hscroll);
+ // At the start: none.
+ pane.body.cur_col = 1;
+ pane.ensureCursorVisible();
+ try std.testing.expectEqual(@as(i32, 0), pane.hscroll);
+}
+
pub fn supportsContext(pane: *const Pane) bool {
const file = pane.file orelse return false;
return file.output == null and file.mini == null and syntax.supportsPath(file.path);
diff --git a/src/Stream.zig b/src/Stream.zig
index ace73ca4..e42d4a16 100644
--- a/src/Stream.zig
+++ b/src/Stream.zig
@@ -140,6 +140,7 @@ const PaintTask = struct { job: *PaintJob, future: ?std.Io.Future(void) = null,
/// a parse's (those count from 0 in `gen`, and it would take two billion
/// searches to reach it).
const paint_bit: u32 = 1 << 31;
+const max_paint_bytes = 512 << 10;
/// Where a worker hands a batch: false when it was turned away (the inbox
/// closed, the wait for room canceled), and the worker stops.
@@ -204,6 +205,10 @@ parse_ns: u64 = 0,
/// The paint running (one at a time), its generation's count, and the
/// painted stretch it gave, kept until the view gets there (tree_sitter_gpa).
paint: ?PaintTask = null,
+/// One told to stop and not waited for: joined when its answer comes (a
+/// paint of a long line runs on in its query; waiting for it here held the
+/// loop for half a second). No new paint starts while it is out.
+paint_retired: ?PaintTask = null,
paint_count: u32 = 0,
ahead: []u8 = &.{},
ahead_for: struct { serial: u32 = 0, revision: u32 = 0, start: usize = 0 } = .{},
@@ -237,18 +242,20 @@ pub fn cancelParse(s: *Stream) void {
t.job.deinit();
}
-/// The paint's worker told to stop and joined.
+/// The paints' workers told to stop and joined (the session's end).
pub fn cancelPaint(s: *Stream) void {
- const t = s.paint orelse return;
- s.paint = null;
- t.job.stop.store(true, .release);
- if (t.future) |f| {
- pardes.turn.yield();
- var future = f;
- future.cancel(t.io.?);
- pardes.turn.back();
+ for ([_]*?PaintTask{ &s.paint, &s.paint_retired }) |slot| {
+ const t = slot.* orelse continue;
+ slot.* = null;
+ t.job.stop.store(true, .release);
+ if (t.future) |f| {
+ pardes.turn.yield();
+ var future = f;
+ future.cancel(t.io.?);
+ pardes.turn.back();
+ }
+ t.job.deinit();
}
- t.job.deinit();
}
/// The painted stretch kept for pane `serial`'s text at `revision`, if it
@@ -271,12 +278,19 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont
if (p.host.vtable.search == null or p.fs.serving or to <= from or to > content.len) return;
const a = s.ahead_for;
if (s.ahead.len != 0 and a.serial == serial and a.revision == revision and from >= a.start and to <= a.start + s.ahead.len) return;
+ // A stretch bigger than this (a few lines of a minified file) is not
+ // painted ahead: the loop paints what it shows of it.
+ if (to - from > max_paint_bytes) return;
if (s.paint) |t| {
// One under way for the same text that reaches into the stretch
// asked for is let finish: held keys move the stretch every frame,
// and a paint restarted every frame would never land.
if (t.job.serial == serial and t.job.revision == revision and t.job.start < to and t.job.start + t.job.source.len > from) return;
- s.cancelPaint();
+ // Else it is told to stop and left to end on its own, once.
+ if (s.paint_retired != null) return;
+ t.job.stop.store(true, .release);
+ s.paint_retired = t;
+ s.paint = null;
}
if (!syntax.readyApart(path)) return;
const job = p.gpa.create(PaintJob) catch return;
@@ -297,8 +311,10 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont
}
fn paintWork(job: *PaintJob, ctx: ?*anyopaque, sink: Sink) void {
- const styles = syntax.highlightApart(job.out, job.path, job.source, &job.stop) catch return;
- if (!sink(ctx, .{ .gen = job.gen, .styles = styles, .painted = .{ .serial = job.serial, .revision = job.revision, .start = job.start }, .end = .done })) return;
+ // Stopped or failed, it still answers (with nothing), so the loop
+ // knows to join it.
+ const styles: []u8 = syntax.highlightApart(job.out, job.path, job.source, &job.stop) catch @constCast(&[_]u8{});
+ _ = sink(ctx, .{ .gen = job.gen, .styles = styles, .painted = .{ .serial = job.serial, .revision = job.revision, .start = job.start }, .end = .done });
}
/// A paint's answer: its task joined, its stretch kept for when the view
@@ -306,8 +322,9 @@ fn paintWork(job: *PaintJob, ctx: ?*anyopaque, sink: Sink) void {
fn receivePaint(p: *Pardes, b: *const Batch) void {
const s = &p.stream;
const painted = b.painted.?;
- if (s.paint) |t| if (t.job.gen == b.gen) {
- s.paint = null;
+ // Its task joined: it has sent its last, so the join is a return away.
+ for ([_]*?PaintTask{ &s.paint, &s.paint_retired }) |slot| if (slot.*) |t| if (t.job.gen == b.gen) {
+ slot.* = null;
if (t.future) |f| {
pardes.turn.yield();
var future = f;
@@ -315,6 +332,8 @@ fn receivePaint(p: *Pardes, b: *const Batch) void {
pardes.turn.back();
}
t.job.deinit();
+ // A retired one's answer is for a stretch nobody wants now.
+ if (slot == &s.paint_retired) return;
};
const id = p.paneBySerial(painted.serial) orelse return;
const f = p.panes[id].?.file orelse return;
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 1a56ecd0..5d3cbafd 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -4126,8 +4126,8 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void {
// tick, skipping (never bunching) the ticks a slow frame missed.
tick = if (g.pace_ns != 0 and now -| g.pace_ns < 2 * period) g.pace_ns + period else now;
if (tick < now) tick += (now - tick) / period * period;
- const left: c_int = @intCast(@min(std.math.maxInt(c_int), (tick -| now) / std.time.ns_per_ms));
- ms = if (ms < 0) left else @min(ms, left);
+ // Waited out polling, below.
+ ms = 0;
}
g.input_seen = false;
// The wait is the 9P connections' turn with the core.
@@ -4177,16 +4177,25 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void {
// never adds a tick to an input's latency), and the ticks after it
// run from it.
if (paced and g.input_seen) tick = c.SDL_GetTicksNS();
+ // The tick is waited out polling, not asleep, as a game's frame
+ // limiter spins: asleep between 60 Hz frames, the CPU's clock fell
+ // (intel_pstate, 4.9 GHz down toward 0.8) and every frame, an
+ // input's first among them, took 2-5x as long: input-to-present p50
+ // 1.9 ms spinning against 7-8 ms asleep (docs/next-anim.md).
+ // ponytail: a core while something moves (~250 ms a scroll); a
+ // sleep to a millisecond short of the tick if the power matters more.
while (paced and !g.input_seen) {
- const left = tick -| c.SDL_GetTicksNS();
- if (left < std.time.ns_per_ms) break;
- const wait_ms: c_int = @intCast(@min(left / std.time.ns_per_ms, @as(u64, @intCast(demoDue(g) orelse std.math.maxInt(c_int)))));
+ if (tick -| c.SDL_GetTicksNS() < 50 * std.time.ns_per_us) break;
pardes.turn.rest();
- const more = c.SDL_WaitEventTimeout(&sev, wait_ms);
+ const more = c.SDL_PollEvent(&sev);
pardes.turn.wake();
if (more) {
dispatch(g, &in, &sev);
while (c.SDL_PollEvent(&sev)) dispatch(g, &in, &sev);
+ } else {
+ // The events asked for every 100 µs, the core busy between.
+ const until = c.SDL_GetTicksNS() + 100 * std.time.ns_per_us;
+ while (c.SDL_GetTicksNS() < @min(until, tick)) std.atomic.spinLoopHint();
}
runDemo(g, &in, core);
if (g.input_seen) tick = c.SDL_GetTicksNS();
@@ -5071,7 +5080,7 @@ const Glide = struct {
/// whatever moved it.
/// When the picture was last sampled (sample).
shown_ns: u64 = 0,
- seen: [pardes.MAX_PANES]struct { serial: u32 = 0, line: i64 = 0, file: u64 = 0 } = @splat(.{}),
+ seen: [pardes.MAX_PANES]struct { serial: u32 = 0, line: i64 = 0, file: u64 = 0, revision: u32 = 0 } = @splat(.{}),
/// How far the picture may lag, in bodies: the rows kept past each
/// edge, so a jump made while one glides carries on from where it is.
@@ -5152,23 +5161,31 @@ const Glide = struct {
}
};
// The least shift under which the rows agree, but for a few: the
- // cursor's line and its number's weight move with the scroll.
- var shift: u16 = found: for (1..h) |k| {
- const kk: u16 = @intCast(k);
- const allowed = @max(2, (h - kk) / 8);
- var misses: usize = 0;
- for (0..h - kk) |r| {
- const rr: u16 = @intCast(r);
- const old_row = glide.before[@as(usize, if (glide.dir > 0) rr + kk else rr) * w ..][0..w];
- const new_row = now.row(surface, x0, glide.body_y + if (glide.dir > 0) rr else rr + kk, w);
- if (!sameRow(old_row, new_row)) {
- misses += 1;
- if (misses > allowed) continue :found;
+ // cursor's line and its number's weight move with the scroll. The
+ // lines the core moved are tried first: unwrapped, they are the
+ // rows, and one pass over the body settles it (the search over
+ // every shift was 6 ms of the first frame of a page in Debug).
+ const agrees = struct {
+ fn at(gl: *const Glide, surf: *const pardes.Surface, x: u16, width: u16, height: u16, kk: u16) bool {
+ if (kk == 0 or kk + 3 > height) return false;
+ const allowed = @max(2, (height - kk) / 8);
+ var misses: usize = 0;
+ for (0..height - kk) |r| {
+ const rr: u16 = @intCast(r);
+ const old_row = gl.before[@as(usize, if (gl.dir > 0) rr + kk else rr) * width ..][0..width];
+ const new_row = now.row(surf, x, gl.body_y + if (gl.dir > 0) rr else rr + kk, width);
+ if (!sameRow(old_row, new_row)) {
+ misses += 1;
+ if (misses > allowed) return false;
+ }
}
+ return true;
}
- // Too few rows left to tell: none in common.
- if (h - kk < 3) break :found 0;
- break :found kk;
+ };
+ const guess: u16 = @intFromFloat(@min(glide.pending_lines, @as(f32, @floatFromInt(h))));
+ // A body's lines or more: every row moved off, none in common.
+ var shift: u16 = if (guess >= h) 0 else if (agrees.at(glide, surface, x0, w, h, guess)) guess else found: for (1..h) |k| {
+ if (agrees.at(glide, surface, x0, w, h, @intCast(k))) break :found @intCast(k);
} else 0;
// None in common: a body's move from the last frame's body, if the
// lines moved are that many (rows that merely changed, a small
@@ -5285,9 +5302,14 @@ fn stepGlide(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void {
};
const line: i64 = if (pane.file) |f| @intCast(f.scroll) else if (pane.isTerminal()) pardes.panes.terminal.gridOffset(pane) else 0;
const file: u64 = if (pane.file) |f| std.hash.Wyhash.hash(0, f.path) else 0;
+ const revision: u32 = if (pane.file) |f| f.revision else 0;
const was = glide.seen[id];
- glide.seen[id] = .{ .serial = pane.serial, .line = line, .file = file };
+ glide.seen[id] = .{ .serial = pane.serial, .line = line, .file = file, .revision = revision };
if (was.serial != pane.serial or was.file != file or was.line == line or pane.collapsed or pane.hasPdf()) continue;
+ // Generated text rewritten as its view moved (a query pane's live
+ // rows, a +Search refilled, Callstack): the rows are another
+ // text's, not this one's moved; it lands.
+ if (pane.file) |f| if (f.output != null and revision != was.revision) continue;
if (pane.file == null and wheeled != id) continue;
if (g.scroll_pane == id and (g.scroll_lag != 0 or g.scroll_delta != 0)) continue;
const moved = line - was.line;
@@ -5350,8 +5372,13 @@ fn emitGlideRows(g: *Gui, instances: [*]CellInstance, base: u32, surface: *parde
const cursor_idx: u32 = if (surface.cursor) |cu| @as(u32, cu.y) * surface.cols + cu.x else std.math.maxInt(u32);
const cursor_bar = if (surface.cursor) |cu| cu.bar else false;
var n: u32 = 0;
- var row = y0 + kept.rows;
- while (row < y0 + bh) : (row += 1) {
+ // Only the rows the scissor shows: lagging a body or more, every body
+ // row is past the edge, and drawing them all was twice the frame's
+ // instances for the length of a long glide.
+ const lag: u16 = @intFromFloat(@min(@floor(@abs(glide.offset)), @as(f32, @floatFromInt(bh))));
+ var row = y0 + kept.rows + if (glide.offset > 0) lag else 0;
+ const row_end = y0 + bh - if (glide.offset < 0) lag else 0;
+ while (row < row_end) : (row += 1) {
const line = surface.cells[@as(usize, row) * surface.cols ..][0..surface.cols];
const row_cursor: ?usize = if (surface.cursor) |cu| if (cu.y == row) cu.x else null else null;
var col = x0;
diff --git a/src/image.zig b/src/image.zig
index 5797ce9e..19dbb9e4 100644
--- a/src/image.zig
+++ b/src/image.zig
@@ -1,5 +1,6 @@
//! Image decoding, pixel placement, and glyph approximation.
const std = @import("std");
+const builtin = @import("builtin");
const zstbi = @import("zstbi");
/// The 16-colour ANSI table below is the only thing this file ever wanted from
/// the emulator, and a build without one still renders images — see
@@ -767,7 +768,9 @@ pub fn readable(bytes: []const u8) bool {
/// decode + downscale to RGBA, gpa-owned. Returns null on any failure — the
/// pane then simply shows a blank body.
-pub fn decode(gpa: std.mem.Allocator, bytes: []const u8) ?struct { rgba: []u8, w: usize, h: usize } {
+const Decoded = struct { rgba: []u8, w: usize, h: usize };
+
+pub fn decode(gpa: std.mem.Allocator, bytes: []const u8) ?Decoded {
if (bytes.len == 0 or bytes.len > std.math.maxInt(c_int)) return null;
// Bounded before decoding: a few KB of PNG can name gigapixels.
var iw: c_int = 0;
@@ -812,8 +815,14 @@ pub const State = struct {
raw: []u8 = &.{},
grid: GlyphArt.Grid = .{ .cells = &.{}, .cols = 0, .rows = 0 },
grid_key: GridKey = .{},
+ /// The decode under way off the loop (ensureDecoded), and its answer.
+ decoder: ?std.Thread = null,
+ decoded: std.atomic.Value(bool) = .init(false),
+ answer: ?Decoded = null,
pub fn deinit(state: *State, gpa: std.mem.Allocator) void {
+ if (state.decoder) |t| t.join();
+ if (state.answer) |a| gpa.free(a.rgba);
gpa.free(state.path);
if (state.rgba.len > 0) gpa.free(state.rgba);
if (state.raw.len > 0) gpa.free(state.raw);
@@ -941,15 +950,41 @@ pub fn tagPrefix(arena: std.mem.Allocator, state: *const State, glyphs: bool) ![
);
}
+/// The picture decoded off the loop: the first draw starts a thread and
+/// draws the pane without it (its tag and frame at once); the frames after
+/// it look (Pardes.nextWake asks for them) and the picture lands on the
+/// first one after the decode (80 ms in ReleaseSafe, 290 in Debug, for the
+/// docs' screenshot: all of it once on the frame before).
+/// ponytail: a thread a picture; a pool if many open at once.
fn ensureDecoded(p: *pardes.Pardes, state: *State) void {
- if (state.tried) return;
- state.tried = true;
- if (state.raw.len == 0) return; // nothing was readable at open
- if (decode(p.image_gpa, state.raw)) |decoded| {
+ if (state.decoder) |t| {
+ if (!state.decoded.load(.acquire)) return;
+ t.join();
+ state.decoder = null;
+ }
+ if (state.answer) |decoded| {
+ state.answer = null;
state.rgba = decoded.rgba;
state.iw = decoded.w;
state.ih = decoded.h;
}
+ if (state.tried) return;
+ state.tried = true;
+ if (state.raw.len == 0) return; // nothing was readable at open
+ const Work = struct {
+ fn run(gpa: std.mem.Allocator, st: *State) void {
+ st.answer = decode(gpa, st.raw);
+ st.decoded.store(true, .release);
+ }
+ };
+ if (comptime pardes.hosted and !builtin.is_test) {
+ if (std.Thread.spawn(.{}, Work.run, .{ p.image_gpa, state })) |t| {
+ state.decoder = t;
+ return;
+ } else |_| {}
+ }
+ Work.run(p.image_gpa, state);
+ ensureDecoded(p, state);
}
fn ensureGrid(p: *pardes.Pardes, state: *State, cols: u16, rows: u16) void {
diff --git a/src/panes.zig b/src/panes.zig
index 20618ccc..51710d08 100644
--- a/src/panes.zig
+++ b/src/panes.zig
@@ -619,14 +619,18 @@ pub const Pane = struct {
const line = modal.lineSlice(f.content, @intCast(@max(0, pane.body.cur_row)));
const raw_cur: usize = @intCast(@max(0, pane.body.cur_col));
const raw_scroll: usize = @intCast(@max(0, pane.hscroll));
- const cur = @as(i32, @intCast(File.rawDisplayCol(line, raw_cur)));
- const visual_scroll = @as(i32, @intCast(File.rawDisplayCol(line, raw_scroll)));
- var target = visual_scroll;
- if (cur < visual_scroll + hmargin)
- target = @max(0, cur - hmargin)
- else if (cur > visual_scroll + w - 1 - hmargin)
- target = cur - (w - 1 - hmargin);
- if (target != visual_scroll) pane.hscroll = @intCast(File.rawAtDisplay(line, @intCast(target)));
+ // Measured from the view's left edge to the caret, never from the
+ // line's start: a 500 KB line was walked from byte 0 at every key
+ // (widths add, so the span alone is exact).
+ const apart = File.lineDisplayOffset(line, raw_scroll, raw_cur);
+ const back: ?i32 = if (apart < hmargin) hmargin else if (apart > w - 1 - hmargin) w - 1 - hmargin else null;
+ if (back) |cols| {
+ // The byte `cols` columns before the caret, looked for from a
+ // few bytes a column back (a grapheme is at most 4 a column).
+ const from = raw_cur -| @as(usize, @intCast(cols)) * 4;
+ const ahead = File.lineDisplayOffset(line, from, raw_cur) - cols;
+ pane.hscroll = @intCast(if (ahead <= 0) from else File.byteAtDisplayFrom(line, from, @intCast(ahead)));
+ }
}
}
diff --git a/src/pardes.zig b/src/pardes.zig
index 9177721f..0f3f87f2 100644
--- a/src/pardes.zig
+++ b/src/pardes.zig
@@ -7696,8 +7696,10 @@ pub const Pardes = struct {
// seen are stamped 0, which `advance` would count back from).
if (p.fs.batch.bytes.items.len > 0) return @max(next, p.fs.batch.last_ns + ctlfs.pane.batch_pause_ns);
if (p.chrome_animation.isActive() or p.presentation.animating()) return next;
- // Places still to find resume on the next frame.
+ // Places still to find resume on the next frame; a picture being
+ // decoded is looked for.
for (p.panes) |slot| if (slot) |pane| if (pane.places.busy(pane)) return next;
+ for (p.panes) |slot| if (slot) |pane| if (pane.image) |iv| if (iv.decoder != null) return next;
// A markdown text's tables still being read from the top (Tables.zig).
for (p.panes) |slot| if (slot) |pane| if (pane.file) |f| if (f.tables.busy()) return next;
if (panes.Query.busy(p)) return next;
diff --git a/src/syntax.zig b/src/syntax.zig
index 821754fa..985a9888 100644
--- a/src/syntax.zig
+++ b/src/syntax.zig
@@ -344,6 +344,7 @@ pub fn highlightApart(gpa: std.mem.Allocator, path: []const u8, source: []const
.{ .payload = @constCast(halt), .progress_callback = @ptrCast(&Reader.progress) },
) orelse return error.Canceled;
defer tree.destroy();
+ if (halt.load(.monotonic)) return error.Canceled;
const styles = try gpa.alloc(u8, source.len);
@memset(styles, 0);
var own = shared;
diff --git a/src/tty/tty.zig b/src/tty/tty.zig
index 264cfbef..b7bb2d54 100644
--- a/src/tty/tty.zig
+++ b/src/tty/tty.zig
@@ -1521,8 +1521,10 @@ const Shell = struct {
/// PARDES_TEST_CLOCK's virtual time (host_io.testClock), and where the
/// running-out wait moves it: the core's next wake.
test_clock: ?u64 = null,
- /// The tick the last paced frame was due at (waitInput).
+ /// The tick the last paced frame was due at (waitInput), and whether
+ /// this wait read an input (its frame is drawn at once).
pace_ns: u64 = 0,
+ input_seen: bool = false,
wake_to: u64 = 0,
fn of(ctx: ?*anyopaque) *Shell {
@@ -1574,7 +1576,6 @@ const Shell = struct {
fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void {
const s = of(ctx);
- var batch: usize = 0;
// The wait is the 9P connections' turn with the core. A timeout (the
// core's next wake) arrives through the same queue (`wakeWatch`).
// While anything moves, a frame a tick (animation.frame_ns) on a grid
@@ -1583,14 +1584,32 @@ const Shell = struct {
// ends the wait at once and its frame is drawn then; the ticks after
// it run from it.
var wait = timeout_ms;
- if (s.test_clock == null and s.core.continuous()) {
+ const paced = s.test_clock == null and s.core.continuous();
+ var tick: u64 = 0;
+ if (paced) {
const period = pardes.animation.frame_ns;
const now: u64 = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
- var tick = if (s.pace_ns != 0 and now -| s.pace_ns < 2 * period) s.pace_ns + period else now;
+ tick = if (s.pace_ns != 0 and now -| s.pace_ns < 2 * period) s.pace_ns + period else now;
if (tick < now) tick += (now - tick) / period * period;
s.pace_ns = tick;
wait = @intCast(@max(1, (tick -| now) / std.time.ns_per_ms));
}
+ s.input_seen = false;
+ // Paced, what is not an input (a frame's ack, a pty's output) is
+ // taken in and the wait goes on to the tick: the acks alone ran the
+ // loop at the terminal's round trip.
+ while (true) {
+ s.waitOnce(wait);
+ if (!paced or s.input_seen or s.core.quit) break;
+ const now: u64 = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
+ if (now + std.time.ns_per_ms > tick) break;
+ wait = @intCast((tick - now) / std.time.ns_per_ms);
+ }
+ if (paced and s.input_seen) s.pace_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
+ }
+
+ fn waitOnce(s: *Shell, wait: u32) void {
+ var batch: usize = 0;
if (wait != 0) {
s.wake_to = s.core.nextWake() orelse 0;
requestWake(s.io, wait);
@@ -1638,8 +1657,15 @@ const Shell = struct {
// A key's or a click's time, for its latency to the frame that
// answers it (FrameStats): when the loop takes it in.
switch (event) {
- .key_press => core.frames.input(pardes.FrameStats.nowNs()),
- .mouse => |m| if (m.type == .press) core.frames.input(pardes.FrameStats.nowNs()),
+ .key_press => {
+ core.frames.input(pardes.FrameStats.nowNs());
+ s.input_seen = true;
+ },
+ .mouse => |m| if (m.type == .press) {
+ core.frames.input(pardes.FrameStats.nowNs());
+ s.input_seen = true;
+ },
+ .paste, .paste_start, .winsize => s.input_seen = true,
else => {},
}
switch (event) {