diff --git a/apps/zgx/plugins-builtin/git/graph.luau b/apps/zgx/plugins-builtin/git/graph.luau index 21453ca1e..32f8ec559 100644 --- a/apps/zgx/plugins-builtin/git/graph.luau +++ b/apps/zgx/plugins-builtin/git/graph.luau @@ -22,18 +22,33 @@ export type Line = { color: number, } +export type Commit = { sha: string, parents: { string } } + export type Row = { col: number, color: number, lines: { Line }, lanes: number } local M = {} --- The rows for `commits` ({ sha, parents }), and the most lanes any row has. -function M.layout(commits: { { sha: string, parents: { string } } }): ({ Row }, number) +-- A layout cache for one History view. update accepts the accumulated history. +-- Existing rows keep their identity on append; changed/reordered/truncated +-- topology starts over. update returns rows, full width and a generation that +-- changes only on a rebuild. Inputs may be freshly decoded tables each time. +-- `max_lanes` bounds line geometry only. Keep every lane, parent and color +-- in the topology: a hidden lane can join a visible one later, and compact +-- mode still needs every commit's own column/color and the full width. +function M.new(max_lanes: number?) + local visible = max_lanes or math.huge -- (A free lane is `false`, never a hole: `#lanes` stays the count.) local lanes: { string | false } = {} local colors: { number } = {} local next_color = 0 local rows: { Row } = {} + -- Reused scratch for connections into/out of the commit. Passing lanes + -- precede them in paint order, which is known once parents are assigned. + local connections: { Line } = {} local widest = 1 + -- A generation identifies all rows until a prefix change rebuilds them. + local version = 0 + local topology: { Commit } = {} local function free_slot(avoid: number?): number for j = 1, #lanes + 1 do if not lanes[j] and j ~= avoid then @@ -47,85 +62,137 @@ function M.layout(commits: { { sha: string, parents: { string } } }): ({ Row }, next_color += 1 return c end - for _, c in commits do - local lines: { Line } = {} - -- Its lane: the leftmost waiting for it, or a new one (a branch's tip). - local col: number? = nil - local n = #lanes - for j = 1, n do - if lanes[j] == c.sha then - col = j - break - end - end - if col == nil then - col = free_slot() - colors[col :: number] = new_color() - end - local here = col :: number - -- Into the row: each lane from the top, straight down, or into the - -- commit if it waited for it. - for j = 1, math.max(n, here) do - local w = lanes[j] - if w then - if w == c.sha then - table.insert(lines, { a = j, b = here, from = 0, to = 0.5, color = colors[j] }) + local function update(commits: { Commit }): ({ Row }, number, number) + -- Validate the prefix in O(commits + parents); only the appended suffix + -- runs the lane algorithm and allocates row geometry. + local append = #commits >= #topology + if append then + for i, old in topology do + local c = commits[i] + if c.sha ~= old.sha or #c.parents ~= #old.parents then + append = false + break end - end - end - -- The lanes that met here are free now. - for j = 1, n do - if lanes[j] == c.sha then - lanes[j] = false - end - end - local color = colors[here] - -- Out of the row: its first parent down its own lane; each other - -- parent down the lane that waits for it, or a new one. - local parents = c.parents - -- (The lanes this commit opens: they start at its middle.) - local opened: { [number]: boolean } = {} - if #parents == 0 then - lanes[here] = false - else - lanes[here] = parents[1] - table.insert(lines, { a = here, b = here, from = 0.5, to = 1, color = colors[here] }) - for k = 2, #parents do - local p = parents[k] - local into: number? = nil - for j = 1, #lanes do - if j ~= here and lanes[j] == p then - into = j + for j, parent in old.parents do + if c.parents[j] ~= parent then + append = false break end end - if into == nil then - into = free_slot(here) - lanes[into :: number] = p - colors[into :: number] = new_color() - opened[into :: number] = true + if not append then + break end - table.insert(lines, { a = here, b = into :: number, from = 0.5, to = 1, color = colors[into :: number] }) end end - -- Lanes passing by, top to foot. - for j = 1, #lanes do - local w = lanes[j] - if w and j ~= here and not opened[j] then - table.insert(lines, 1, { a = j, b = j, from = 0, to = 1, color = colors[j] }) + if not append then + version += 1 + lanes = {} + colors = {} + next_color = 0 + rows = {} + topology = {} + widest = 1 + end + for i = #rows + 1, #commits do + local c = commits[i] + local lines: { Line } = {} + table.clear(connections) + -- Its lane: the leftmost waiting for it, or a new one (a branch's tip). + local col: number? = nil + local n = #lanes + for j = 1, n do + if lanes[j] == c.sha then + col = j + break + end end + if col == nil then + col = free_slot() + colors[col :: number] = new_color() + end + local here = col :: number + -- Width includes incoming lanes even when they end here and their + -- lines are omitted. It must not depend on the retained geometry. + local width = math.max(n, here) + -- Into the row: each lane from the top, straight down, or into the + -- commit if it waited for it. + for j = 1, if here <= visible then math.min(n, visible) else 0 do + local w = lanes[j] + if w then + if w == c.sha then + table.insert(connections, { a = j, b = here, from = 0, to = 0.5, color = colors[j] }) + end + end + end + -- The lanes that met here are free now. + for j = 1, n do + if lanes[j] == c.sha then + lanes[j] = false + end + end + local color = colors[here] + -- Out of the row: its first parent down its own lane; each other + -- parent down the lane that waits for it, or a new one. + local parents = c.parents + -- (The lanes this commit opens: they start at its middle.) + local opened: { [number]: boolean } = {} + if #parents == 0 then + lanes[here] = false + else + lanes[here] = parents[1] + if here <= visible then + table.insert(connections, { a = here, b = here, from = 0.5, to = 1, color = colors[here] }) + end + for k = 2, #parents do + local p = parents[k] + local into: number? = nil + for j = 1, #lanes do + if j ~= here and lanes[j] == p then + into = j + break + end + end + if into == nil then + into = free_slot(here) + lanes[into :: number] = p + colors[into :: number] = new_color() + opened[into :: number] = true + end + width = math.max(width, into :: number) + if here <= visible and into :: number <= visible then + table.insert(connections, { a = here, b = into :: number, from = 0.5, to = 1, color = colors[into :: number] }) + end + end + end + -- The original front insertions put passing lanes in descending + -- order, before connections. Append in that same final order, without + -- shifting every preceding segment for each passing lane. + for j = math.min(#lanes, visible), 1, -1 do + local w = lanes[j] + if w and j ~= here and not opened[j] then + table.insert(lines, { a = j, b = j, from = 0, to = 1, color = colors[j] }) + end + end + for _, line in connections do + table.insert(lines, line) + end + -- (Free lanes at the end go.) + while #lanes > 0 and not lanes[#lanes] do + table.remove(lanes) + end + widest = math.max(widest, width) + table.insert(rows, { col = here, color = color, lines = lines, lanes = width }) + -- Snapshot only topology, not subjects, authors or other row metadata. + topology[i] = { sha = c.sha, parents = table.clone(c.parents) } end - -- (Free lanes at the end go.) - while #lanes > 0 and not lanes[#lanes] do - table.remove(lanes) - end - local width = math.max(#lanes, here) - for _, l in lines do - width = math.max(width, l.a, l.b) - end - widest = math.max(widest, width) - table.insert(rows, { col = here, color = color, lines = lines, lanes = width }) + return rows, widest, version end + return { update = update } +end + +-- One-shot callers use exactly the same algorithm as paginated History. +function M.layout(commits: { Commit }, max_lanes: number?): ({ Row }, number) + local rows, widest = M.new(max_lanes).update(commits) return rows, widest end diff --git a/apps/zgx/plugins-builtin/git/history.luau b/apps/zgx/plugins-builtin/git/history.luau index 4c29d8800..ff6b60c01 100644 --- a/apps/zgx/plugins-builtin/git/history.luau +++ b/apps/zgx/plugins-builtin/git/history.luau @@ -18,8 +18,8 @@ -- `git.commit.head` in an opened commit's head (View on GitHub). With no -- plugin there, History is git alone. -- --- The rows are a virtualized list keyed by commit and how its lines run: --- more history, or a refresh, builds only the rows that came or changed. +-- The rows are virtualized with compact position/generation keys. Appending +-- history keeps existing rows; topology changes invalidate the generation. local Changes = require("@ds/git_changes") local git = require("@ds/git") @@ -1016,14 +1016,18 @@ return function(zeron: any, Repo: any, words: any) end -- The graph, worked out as the commits change (none while searching: -- the commits found aren't a line), and the lanes it needs at most. - local laid = ui.computed(function(): { rows: { graph.Row }, lanes: number, commits: { any } } + -- Keep pending lanes across pages. The old tail already has its outgoing + -- edges; row_key below refreshes its compact rail when a next row arrives. + local graph_cache = graph.new(MAX_LANES) + local laid = ui.computed(function(): { rows: { graph.Row }, lanes: number, commits: { any }, version: number } local h = model.history:get() local commits = if h then h.commits else {} if searching() then - return { rows = {}, lanes = 0, commits = commits } + graph_cache.update({}) + return { rows = {}, lanes = 0, commits = commits, version = -1 } end - local rows, widest = graph.layout(commits) - return { rows = rows, lanes = math.min(widest, MAX_LANES), commits = commits } + local rows, widest, version = graph_cache.update(commits) + return { rows = rows, lanes = math.min(widest, MAX_LANES), commits = commits, version = version } end) ui.effect(function() local w = width:get() @@ -1054,35 +1058,26 @@ return function(zeron: any, Repo: any, words: any) geom:set(now) end end) - -- Rows keyed by commit and how its lines run (a row whose lines - -- moved is drawn again), then "older commits" when there are more. - local keys = ui.computed(function(): { string } + -- Generate compact keys only for rows the list asks for. Within a + -- graph generation each position's geometry is immutable; a rebuild + -- changes the version. Width and the old tail's continuation can still + -- change on append. No serialized segments, key array or reverse index. + local function row_count(): number local l = laid:get() - local out: { string } = {} - for i, c in l.commits do - local r = l.rows[i] - local sig: { string } = { tostring(l.lanes) } - if r then - table.insert(sig, tostring(r.col)) - for _, line in r.lines do - table.insert(sig, `{line.a}{line.b}{line.from}{line.to}{line.color}`) - end - end - out[i] = `{c.sha}:{table.concat(sig, ",")}` - end local h = model.history:get() - if h and h.more then - table.insert(out, "more") + return #l.commits + (if h and h.more then 1 else 0) + end + local function row_key(i: number): string + local l = laid:peek() + local c = l.commits[i] + if not c then + return "more" end - return out - end) - local index = ui.computed(function(): { [string]: number } - local map: { [string]: number } = {} - for i, k in keys:get() do - map[k] = i + if l.version < 0 then + return `s:{i}:{c.sha}` end - return map - end) + return `g:{i}:{l.version}:{l.lanes}:{if i < #l.rows then 1 else 0}` + end local function find(sha: string): any? for _, x in laid:peek().commits do if x.sha == sha then @@ -1154,16 +1149,9 @@ return function(zeron: any, Repo: any, words: any) h_full = true, offset = offset, overscan = 8, - heights = function(): { number } - local hs: { number } = {} - for i = 1, #keys:get() do - hs[i] = ROW - end - return hs - end, - key = function(i: number): string - return keys:peek()[i] or `none:{i}` - end, + height = ROW, + count = row_count, + key = row_key, row = function(key: string): Node if key == "more" then return ui.row { @@ -1183,7 +1171,8 @@ return function(zeron: any, Repo: any, words: any) }, } end - local i = index:peek()[key] + local position = string.match(key, "^[gs]:(%d+):") + local i = if position then tonumber(position) else nil local l = laid:peek() local c = if i then l.commits[i] else nil if not c then @@ -1220,7 +1209,7 @@ return function(zeron: any, Repo: any, words: any) id = "git-history-rail", offset = offset, extent = function(): number - return #keys:get() * ROW + return row_count() * ROW end, view = seen, }, diff --git a/apps/zgx/src/plugins/examples_tests.rs b/apps/zgx/src/plugins/examples_tests.rs index 2222d7276..92b7a8b80 100644 --- a/apps/zgx/src/plugins/examples_tests.rs +++ b/apps/zgx/src/plugins/examples_tests.rs @@ -3123,6 +3123,200 @@ return ui.text {{ id = "out", `{{widest}}|` .. table.concat(out, " ") }} assert_eq!(app.text("out"), "2|1:1>1@0.5-1,1>2@0.5-1 1:1>1@0-0.5,1>1@0.5-1,2>2@0-1 2:1>1@0-1,2>2@0-0.5,2>2@0.5-1 1:1>1@0-0.5,2>1@0-0.5"); } +/// Compare with the frozen pre-optimization layout, including line order +/// (paint/hit-test precedence), hidden-lane joins, colors and page boundaries. +#[test] +fn git_s_graph_keeps_visible_geometry_and_full_topology() { + let source = std::fs::read_to_string(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("plugins-builtin/git/graph.luau")).unwrap(); + let script = format!("local graph = (function() {source} end)()\nlocal reference = (function() {} end)()\n{}", include_str!("fixtures/git_graph_reference.luau"), r#" +local checked = 0 +local function check(commits) + local expected, width = reference.layout(commits) + for _, cap in { 0, 1, 2, 8, 16, math.huge } do + local rows, widest = graph.layout(commits, if cap == math.huge then nil else cap) + assert(widest == width, `width {widest} != {width}, cap {cap}`) + assert(#rows == #expected) + for i, r in rows do + local e = expected[i] + assert(r.col == e.col and r.color == e.color and r.lanes == e.lanes, `topology row {i}, cap {cap}`) + local visible = {} + for _, line in e.lines do + if line.a <= cap and line.b <= cap then table.insert(visible, line) end + end + assert(#r.lines == #visible, `line count row {i}, cap {cap}`) + for j, line in r.lines do + for _, field in { "a", "b", "from", "to", "color" } do + assert(line[field] == visible[j][field], `line order/geometry row {i}, line {j}, {field}, cap {cap}`) + end + end + checked += 1 + end + end +end +check({}) +-- Grow past the cap, then consume the pending parents: lanes join, empty +-- and are reused. Check prefixes as well as the full, paginated history. +local commits = {} +for i = 1, 100 do + table.insert(commits, { sha = `c{i}`, parents = { `c{i + 1}`, `p{i}` } }) +end +for i = 1, 100 do + table.insert(commits, { sha = `p{i}`, parents = { "root" } }) +end +table.insert(commits, { sha = "c101", parents = { "root" } }) +table.insert(commits, { sha = "root", parents = {} }) +for _, n in { 1, 8, 17, 51, 100, 101, 151, #commits } do + local prefix = {} + for i = 1, n do prefix[i] = commits[i] end + check(prefix) +end +-- Deterministic DAGs exercise merges that rejoin occupied lanes, roots, +-- disconnected tips, octopus merges and parents beyond the loaded page. +local seed = 19 +local function random(n) + seed = (seed * 48271) % 2147483647 + return seed % n +end +for trial = 1, 32 do + local dag = {} + for i = 1, 60 do + local parents, seen = {}, {} + for _ = 1, random(5) do + local p = `n{i + 1 + random(70 - i)}` + if not seen[p] then table.insert(parents, p); seen[p] = true end + end + table.insert(dag, { sha = `n{i}`, parents = parents }) + end + check(dag) +end +return ui.text { id = "out", `checked {checked}` } +"#); + let mut app = TestApp::inline(&script); + assert_eq!(app.error(), None); + assert_eq!(app.text("out"), "checked 15306"); +} + +/// Appending must preserve old row objects as well as matching the frozen +/// full layout. Reloads and changes to any prefix topology invalidate the cache. +#[test] +fn git_s_graph_extends_pages_without_rebuilding_old_rows() { + let source = std::fs::read_to_string(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("plugins-builtin/git/graph.luau")).unwrap(); + let script = format!("local graph = (function() {source} end)()\nlocal reference = (function() {} end)()\n{}", include_str!("fixtures/git_graph_reference.luau"), r#" +local function same(commits, rows, widest, cap) + local expected, width = reference.layout(commits) + assert(widest == width and #rows == #expected) + for i, r in rows do + local e = expected[i] + assert(r.col == e.col and r.color == e.color and r.lanes == e.lanes, `topology {i}`) + local lines = {} + for _, line in e.lines do + if line.a <= cap and line.b <= cap then table.insert(lines, line) end + end + assert(#lines == #r.lines, `segments {i}`) + for j, line in lines do + for _, field in { "a", "b", "from", "to", "color" } do + assert(line[field] == r.lines[j][field], `geometry {i}/{j}/{field}`) + end + end + end +end +local commits = {} +for i = 1, 40 do + table.insert(commits, { sha = `c{i}`, parents = { `c{i + 1}`, `p{i}`, "root" } }) +end +for i = 1, 40 do + table.insert(commits, { sha = `p{i}`, parents = { "root" } }) +end +table.insert(commits, { sha = "c41", parents = { "root" } }) +table.insert(commits, { sha = "root", parents = {} }) +for _, cap in { 0, 1, 2, 8, math.huge } do + -- A page boundary at every row exercises hidden joins and lane reuse. + local cache = graph.new(cap) + local prior = {} + local generation = nil + local prefix = {} + for _, c in commits do + table.insert(prefix, c) + local rows, width, version = cache.update(prefix) + if generation ~= nil then assert(version == generation, "append changed generation") end + generation = version + for i, old in prior do assert(rows[i] == old, `rebuilt row {i}`) end + same(prefix, rows, width, cap) + prior = table.clone(rows) + end + -- Freshly decoded metadata and an empty page must reuse all geometry. + local decoded = {} + for i, c in prefix do decoded[i] = { sha = c.sha, parents = table.clone(c.parents), subject = "updated" } end + local rows, width, version = cache.update(decoded) + assert(version == generation, "metadata changed generation") + for i, old in prior do assert(rows[i] == old) end + same(decoded, rows, width, cap) + -- Parent changes (including in-place edits) must not reuse stale state. + decoded[2].parents[1] = "root" + rows, width, version = cache.update(decoded) + assert(version > generation, "changed prefix kept generation") + assert(rows[1] ~= prior[1]) + same(decoded, rows, width, cap) + decoded[1], decoded[2] = decoded[2], decoded[1] + rows, width = cache.update(decoded) + same(decoded, rows, width, cap) + -- Refresh to a shorter history, clear, then reload the original history. + rows, width = cache.update({ decoded[1] }) + same({ decoded[1] }, rows, width, cap) + rows, width = cache.update({}) + same({}, rows, width, cap) + rows, width = cache.update(commits) + same(commits, rows, width, cap) +end +return ui.text { id = "out", "incremental graph matches" } +"#); + let mut app = TestApp::inline(&script); + assert_eq!(app.error(), None); + assert_eq!(app.text("out"), "incremental graph matches"); +} + +/// Isolate graph allocation/layout from UI construction and Rust state +/// conversion. The reference flag runs the frozen old layout on the same VM. +#[test] +#[ignore] +fn git_graph_layout_cost() { + let count: usize = std::env::var("GIT_GRAPH_COUNT").ok().and_then(|s| s.parse().ok()).unwrap_or(100); + let parents: usize = std::env::var("GIT_GRAPH_PARENTS").ok().and_then(|s| s.parse().ok()).unwrap_or(8); + let cap: usize = std::env::var("GIT_GRAPH_LIMIT").ok().and_then(|s| s.parse().ok()).unwrap_or(8); + let reference = std::env::var_os("GIT_GRAPH_REFERENCE").is_some(); + let source = if reference { include_str!("fixtures/git_graph_reference.luau").to_string() } else { + std::fs::read_to_string(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("plugins-builtin/git/graph.luau")).unwrap() + }; + let script = format!(r#" +local graph = (function() {source} end)() +local commits = {{}} +for i = 1, {count} do + local parents = {{}} + if i < {count} then table.insert(parents, `c{{i + 1}}`) end + for j = 1, {parents} do table.insert(parents, `p{{i}}-{{j}}`) end + table.insert(commits, {{ sha = `c{{i}}`, parents = parents }}) +end +local times, segments, widest = {{}}, 0, 0 +for run = 1, 24 do + local start = os.clock() + local rows, width = graph.layout(commits, {cap}) + local elapsed = (os.clock() - start) * 1000 + if run > 3 then table.insert(times, elapsed) end + segments, widest = 0, width + for _, row in rows do segments += #row.lines end +end +table.sort(times) +return ui.text {{ id = "out", `segments={{segments}} widest={{widest}} layout_p50_ms={{times[11]}} layout_p95_ms={{times[20]}}` }} +"#); + // Several complete layouts intentionally share one load call. Give the + // old wide-graph implementation room to finish its measured iterations. + let mut app = TestApp::new(move |runtime| ScriptHost::inline(runtime, "graph-layout-bench", script).limits(zgx_script::Limits { + load: std::time::Duration::from_secs(30), call: std::time::Duration::from_secs(30), ..Default::default() + })); + assert_eq!(app.error(), None); + eprintln!("GRAPH reference={reference} commits={count} extra_parents={parents} cap={cap}: {}", app.text("out")); +} + #[test] fn git_s_empty_page_rows_open_its_tab() { let (mut app, heard, _) = git_tab(git_world(), None); @@ -3350,6 +3544,80 @@ fn history_of(app: &mut TestApp) { assert!(app.wait_until(|app| app.find("role:button \"Merge main into feature/graph\"").is_some()), "{}", app.tree()); } +#[test] +fn history_s_hidden_lanes_keep_compact_hover_and_page_continuations() { + let mut w = git_world(); + let mut commits = Vec::new(); + for i in 0..100 { + let mut c = w["history"]["commits"][0].clone(); + c["sha"] = json!(format!("{i:040x}")); + c["parents"] = json!([format!("{:040x}", 1000 + i)]); + if i > 0 { + c["subject"] = json!(format!("tip {i}")); + } + c["refs"] = json!([]); + commits.push(c); + } + w["history"] = json!({ "commits": commits, "more": true }); + let (mut app, heard, world) = git_tab(w, None); + history_of(&mut app); + app.click("role:checkbox \"Every branch\""); + assert!(app.wait_until(|_| asked_service(&heard, "history").iter().any(|c| c["args"]["all"] == true))); + git_fixture(&mut app, "width", zgx_script::Value::from(320.0)); + app.host.set_state("git.s1.history_scroll", zgx_script::Value::from(3000.0)).unwrap(); + let row = "role:button \"tip 99\""; + assert!(app.wait_until(|app| app.find(row).is_some())); + let cell = format!("{row} >> .git-graph"); + assert!((app.bounds(&cell).width - 18.0).abs() < 0.5, "hidden-lane dot uses the compact rail"); + app.hover(&format!("{row} >> \"tip 99\"")); + app.frames(30); + assert!(app.opacity(&format!("{row} >> .git-graph-lit")) > 0.99); + let tail = app.find(row).unwrap(); + // The previous tail's visible graph segments do not change, but its + // compact rail must now continue to the newly appended row. + world.borrow_mut()["history"] = json!({ "commits": [{ + "sha": format!("{:040x}", 1000), "parents": [], "author": "Ada", "email": "ada@example.com", + "at": 1790000000, "refs": [], "subject": "older parent", + }], "more": false }); + app.click("role:button \"Show older commits\""); + assert!(app.wait_until(|app| app.find("role:button \"older parent\"").is_some())); + assert_ne!(app.find(row).unwrap(), tail, "the previous tail must receive its new rail continuation"); + assert!((app.bounds(&cell).width - 18.0).abs() < 0.5); + git_fixture(&mut app, "width", zgx_script::Value::from(900.0)); + app.frames(10); + assert!((app.bounds(&cell).width - 102.0).abs() < 0.5, "the graph returns to eight full-width lanes"); + assert_eq!(app.error(), None); +} + +#[test] +fn history_s_compact_keys_reuse_rows_and_invalidate_same_size_graph_changes() { + let (mut app, heard, world) = git_tab(git_world(), None); + history_of(&mut app); + let row = "role:button \"Draw the lanes\""; + let original = app.find(row).unwrap(); + let graph_width = app.bounds(&format!("{row} >> .git-graph")).width; + // Decoded tables with identical topology keep their mounted rows. + app.click("role:checkbox \"Every branch\""); + assert!(app.wait_until(|_| asked_service(&heard, "history").iter().any(|c| c["args"]["all"] == true))); + app.frames(10); + assert_eq!(app.find(row), Some(original)); + // Same commits, positions and width, but the merge's parent order now + // changes their lanes/colors. A generation change must rebuild the rows. + world.borrow_mut()["history"]["commits"][0]["parents"] = json!(["4".repeat(40), "2".repeat(40)]); + let before = asked_service(&heard, "history").len(); + app.click("role:checkbox \"Every branch\""); + assert!(app.wait_until(|_| asked_service(&heard, "history").len() > before)); + assert!(app.wait_until(|app| app.find(row).is_some_and(|node| node != original))); + assert_eq!(app.bounds(&format!("{row} >> .git-graph")).width, graph_width); + // Index-bearing keys still open the commit at the intended position. + app.click(row); + app.frames(5); + let opened = heard.borrow().iter().filter(|(kind, _)| kind == "plugin.git.open_tab") + .map(|(_, value)| serde_json::from_str::(value).unwrap()).last().unwrap(); + assert_eq!(opened["data"]["commit"]["sha"], "2".repeat(40)); + assert_eq!(app.error(), None); +} + #[test] fn history_s_refs_keep_to_their_room_and_fold_the_rest() { // The merge carries five refs (Zeron's rule: 45% of the commit's room, @@ -3698,12 +3966,28 @@ fn git_scroll_cost() { let world: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap(); let only = std::env::var("GIT_PERF").unwrap_or_else(|_| "history,files,diff,split,wrap".into()); let width: f64 = std::env::var("GIT_PERF_WIDTH").ok().and_then(|w| w.parse().ok()).unwrap_or(0.0); + // Compare the same workload under different plugin budgets to identify + // collection pressure without changing the application's limits. + let memory_mb: usize = std::env::var("GIT_PERF_MEMORY_MB").ok().and_then(|m| m.parse().ok()).unwrap_or(32); + let steps: usize = std::env::var("GIT_PERF_STEPS").ok().and_then(|s| s.parse().ok()).unwrap_or(240); + let bounce = std::env::var_os("GIT_PERF_BOUNCE").is_some(); + let github = world.get("github").cloned(); + let memory = |app: &TestApp, stage: &str| { + let mut report = zgx::memory::Report::default(); + app.host.memory(&mut report); + for line in report.lines.iter().filter(|l| l.area == "luau" || (l.area == "state" && l.name == "all named states")) { + eprintln!("MEM {stage}: {} {} {} bytes", line.area, line.name, line.bytes.unwrap_or(0)); + } + }; let pct = |v: &mut Vec, p: f64| { v.sort_by(|a, b| a.partial_cmp(b).unwrap()); v[((v.len() as f64 - 1.0) * p).round() as usize] }; for surface in only.split(',') { - let (mut app, _heard, _) = git_tab(world.clone(), None); + let limits = zgx_script::Limits { plugin_memory: memory_mb * (1 << 20), ..Default::default() }; + let (mut app, _heard, _) = git_tab_within(world.clone(), github.clone(), limits); + memory(&app, "before-history"); + let load_started = std::time::Instant::now(); if width > 0.0 { git_fixture(&mut app, "width", zgx_script::Value::from(width)); } @@ -3732,6 +4016,8 @@ fn git_scroll_cost() { assert_eq!(app.error(), None, "{surface}"); } app.frames(30); + eprintln!("PERF {surface}: open and settle {:.2} ms; {} commits; github {}; bounce {bounce}; steps {steps}", load_started.elapsed().as_secs_f64() * 1e3, world["history"]["commits"].as_array().map_or(0, Vec::len), github.is_some()); + memory(&app, "at-rest"); if let Ok(out) = std::env::var("GIT_PERF_TREE") { std::fs::write(out, app.tree()).unwrap(); } @@ -3745,6 +4031,7 @@ fn git_scroll_cost() { let mut seen: std::collections::HashSet = app.texts().into_iter().collect(); let first_seen = seen.len(); let at_rest = count(&mut app); + let heap_at_rest = app.host.script_heap(); zgx::trace::set_enabled(true); let mut times = Vec::new(); let mut phases: std::collections::BTreeMap<&'static str, Vec> = Default::default(); @@ -3752,7 +4039,8 @@ fn git_scroll_cost() { // (Hovering: the rows in view, one a frame, each lighting its path // as the last fades.) let subjects: Vec = world["history"]["commits"].as_array().map(|c| c.iter().take(18).filter_map(|c| c["subject"].as_str().map(str::to_string)).collect()).unwrap_or_default(); - for step in 0..240 { + let mut peak_heaps: std::collections::BTreeMap = Default::default(); + for step in 0..steps { let start = std::time::Instant::now(); if surface == "hover" { let s = &subjects[step % subjects.len().max(1)]; @@ -3761,7 +4049,7 @@ fn git_scroll_cost() { app.hover("ed); } } else { - app.scroll(target, 48.0); + app.scroll(target, if bounce && (step / 64) % 2 == 1 { -48.0 } else { 48.0 }); } app.frame(); times.push(start.elapsed().as_secs_f64() * 1e3); @@ -3773,9 +4061,23 @@ fn git_scroll_cost() { if step % 40 == 0 { most = most.max(count(&mut app)); seen.extend(app.texts()); + // Sample outside the timed frame. These are VM heaps only; + // Rust-backed state also counts against each plugin budget. + let mut report = zgx::memory::Report::default(); + app.host.memory(&mut report); + for line in report.lines.into_iter().filter(|l| l.area == "luau") { + let peak = peak_heaps.entry(line.name).or_default(); + *peak = (*peak).max(line.bytes.unwrap_or(0)); + } } } zgx::trace::set_enabled(false); + let heap_after_scroll = app.host.script_heap(); + memory(&app, "after-scroll"); + let live_after_scroll = app.host.script_memory(); + memory(&app, "collected"); + eprintln!("MEM sampled peak VM heaps: {peak_heaps:?}"); + eprintln!("PERF {surface}: plugin budget {memory_mb} MiB; all script heaps at rest {heap_at_rest}, after scroll {heap_after_scroll}, collected {live_after_scroll} bytes"); let (p50, p95, max) = (pct(&mut times, 0.5), pct(&mut times, 0.95), pct(&mut times, 1.0)); let split: Vec = phases.iter_mut().map(|(k, v)| format!("{k} {:.2}/{:.2}", pct(v, 0.5), pct(v, 0.95))).collect(); eprintln!("PERF {surface}: frame p50 {p50:.2} ms, p95 {p95:.2}, max {max:.2}; texts mounted at rest {at_rest}, scrolling ≤{most}, distinct texts passed {} (at rest {first_seen}); phases p50/p95 ms: {}", seen.len(), split.join(", ")); diff --git a/apps/zgx/src/plugins/fixtures/git_graph_reference.luau b/apps/zgx/src/plugins/fixtures/git_graph_reference.luau new file mode 100644 index 000000000..43672a7d2 --- /dev/null +++ b/apps/zgx/src/plugins/fixtures/git_graph_reference.luau @@ -0,0 +1,134 @@ +--!strict +-- Frozen pre-optimization oracle for geometry/topology and layout benchmarks. +-- Keep this independent of production graph.luau; do not optimize it in sync. +-- The branch graph beside History's rows: which lane each commit is in, +-- and the lines through its row, worked out once for the commits in +-- order (newest first, a parent never before its child: `git log +-- --topo-order`) and drawn a row at a time. +-- +-- Lanes are columns, each waiting for the commit it leads to. A commit +-- takes the lane that waits for it (the leftmost, when two do: the others +-- join it), hands that lane to its first parent, and opens a lane for each +-- other parent (or joins one that waits for it already). A lane never +-- moves sideways, so every line through a row is straight, but for those +-- that meet or leave the row's commit. Each lane keeps its colour from +-- where it starts. + +export type Line = { + -- From lane `a` to lane `b`; `from`/`to` are 0 (the row's top), 0.5 + -- (its commit's middle) or 1 (its foot). + a: number, + b: number, + from: number, + to: number, + color: number, +} + +export type Row = { col: number, color: number, lines: { Line }, lanes: number } + +local M = {} + +-- The rows for `commits` ({ sha, parents }), and the most lanes any row has. +function M.layout(commits: { { sha: string, parents: { string } } }): ({ Row }, number) + -- (A free lane is `false`, never a hole: `#lanes` stays the count.) + local lanes: { string | false } = {} + local colors: { number } = {} + local next_color = 0 + local rows: { Row } = {} + local widest = 1 + local function free_slot(avoid: number?): number + for j = 1, #lanes + 1 do + if not lanes[j] and j ~= avoid then + return j + end + end + return #lanes + 1 + end + local function new_color(): number + local c = next_color + next_color += 1 + return c + end + for _, c in commits do + local lines: { Line } = {} + -- Its lane: the leftmost waiting for it, or a new one (a branch's tip). + local col: number? = nil + local n = #lanes + for j = 1, n do + if lanes[j] == c.sha then + col = j + break + end + end + if col == nil then + col = free_slot() + colors[col :: number] = new_color() + end + local here = col :: number + -- Into the row: each lane from the top, straight down, or into the + -- commit if it waited for it. + for j = 1, math.max(n, here) do + local w = lanes[j] + if w then + if w == c.sha then + table.insert(lines, { a = j, b = here, from = 0, to = 0.5, color = colors[j] }) + end + end + end + -- The lanes that met here are free now. + for j = 1, n do + if lanes[j] == c.sha then + lanes[j] = false + end + end + local color = colors[here] + -- Out of the row: its first parent down its own lane; each other + -- parent down the lane that waits for it, or a new one. + local parents = c.parents + -- (The lanes this commit opens: they start at its middle.) + local opened: { [number]: boolean } = {} + if #parents == 0 then + lanes[here] = false + else + lanes[here] = parents[1] + table.insert(lines, { a = here, b = here, from = 0.5, to = 1, color = colors[here] }) + for k = 2, #parents do + local p = parents[k] + local into: number? = nil + for j = 1, #lanes do + if j ~= here and lanes[j] == p then + into = j + break + end + end + if into == nil then + into = free_slot(here) + lanes[into :: number] = p + colors[into :: number] = new_color() + opened[into :: number] = true + end + table.insert(lines, { a = here, b = into :: number, from = 0.5, to = 1, color = colors[into :: number] }) + end + end + -- Lanes passing by, top to foot. + for j = 1, #lanes do + local w = lanes[j] + if w and j ~= here and not opened[j] then + table.insert(lines, 1, { a = j, b = j, from = 0, to = 1, color = colors[j] }) + end + end + -- (Free lanes at the end go.) + while #lanes > 0 and not lanes[#lanes] do + table.remove(lanes) + end + local width = math.max(#lanes, here) + for _, l in lines do + width = math.max(width, l.a, l.b) + end + widest = math.max(widest, width) + table.insert(rows, { col = here, color = color, lines = lines, lanes = width }) + end + return rows, widest +end + +return M diff --git a/docs/research/zgx-git-history-graph-results.csv b/docs/research/zgx-git-history-graph-results.csv new file mode 100644 index 000000000..b99733de2 --- /dev/null +++ b/docs/research/zgx-git-history-graph-results.csv @@ -0,0 +1,109 @@ +stage,kind,case,rep,exit,segments,widest,p50,p95,max,open_ms,peak_git_vm_bytes,collected_git +baseline,layout,0,1,0,198,1,0.07322104647755623,0.11790095595642924,,,, +baseline,layout,1,1,0,5248,100,1.9032210111618042,2.0527829765342176,,,, +baseline,layout,4,1,0,20398,400,12.409305025357753,14.015682972967625,,,, +baseline,layout,8,1,0,40598,800,39.888674044050276,41.0661959904246,,,, +baseline,scroll,linear,1,0,,,1.09,2.02,5.28,26.45,13159158,6467726 +baseline,scroll,real,1,0,,,1.11,2.07,3.07,27.96,13911838,10017966 +baseline,scroll,github,1,0,,,1.69,3.09,6.69,44.0,13993638,6402406 +baseline,scroll,pending1,1,0,,,1.03,2.03,11.81,34.88,19345678,8204278 +baseline,scroll,pending4,1,0,,,0.99,1.98,9.74,68.26,20696134,16000694 +baseline,scroll,pending8,1,101,,,0.01,0.01,0.07,92.67,21456742,21455142 +baseline,scroll,real1000,1,101,,,0.01,0.01,0.07,61.0,17351446,17334390 +baseline,layout,0,2,0,198,1,0.06881094304844737,0.12348097516223788,,,, +baseline,layout,1,2,0,5248,100,1.9238910172134638,1.9864910282194614,,,, +baseline,layout,4,2,0,20398,400,12.358064006548375,13.222064008004963,,,, +baseline,layout,8,2,0,40598,800,39.30204699281603,41.60671203862876,,,, +baseline,scroll,linear,2,0,,,1.08,2.01,6.79,27.11,13093718,6467726 +baseline,scroll,real,2,0,,,1.13,2.1,3.08,27.19,13960918,10328806 +baseline,scroll,github,2,0,,,1.67,3.11,6.78,44.92,13993638,6369686 +baseline,scroll,pending1,2,0,,,1.11,2.11,14.68,46.35,19345678,8220638 +baseline,scroll,pending4,2,0,,,1.03,2.02,11.94,68.92,20696134,16000694 +baseline,scroll,pending8,2,101,,,0.01,0.01,0.07,93.18,21456742,21455142 +baseline,scroll,real1000,2,101,,,0.01,0.01,0.07,56.84,17351446,17334390 +baseline,layout,0,3,0,198,1,0.07056101458147168,0.11803198140114546,,,, +baseline,layout,1,3,0,5248,100,1.914871041662991,2.046511974185705,,,, +baseline,layout,4,3,0,20398,400,12.208772997837514,12.843149015679955,,,, +baseline,layout,8,3,0,40598,800,39.07943400554359,40.74868303723633,,,, +baseline,scroll,linear,3,0,,,1.08,2.07,4.43,26.32,13208238,6385926 +baseline,scroll,real,3,0,,,1.18,2.28,3.22,27.57,13911838,10001606 +baseline,scroll,github,3,0,,,1.79,3.37,7.17,44.41,14009998,6369686 +baseline,scroll,pending1,3,0,,,1.01,2.04,12.11,35.07,19345678,8204278 +baseline,scroll,pending4,3,0,,,0.98,1.93,9.18,68.04,20696134,15967974 +baseline,scroll,pending8,3,101,,,0.01,0.01,0.07,98.56,21456742,21455142 +baseline,scroll,real1000,3,101,,,0.01,0.01,0.07,58.9,17351446,17334390 +baseline,layout_full,8,1,0,40598,800,39.73508201306686,42.42122097639367,,,, +baseline,layout_full,8,2,0,40598,800,38.79843099275604,40.85427394602448,,,, +baseline,layout_full,8,3,0,40598,800,39.29325699573383,41.81972501100972,,,, +step1,layout,0,1,0,198,1,0.06234098691493273,0.11275103315711021,,,, +step1,layout,1,1,0,878,100,0.5749069969169796,0.934810028411448,,,, +step1,layout,4,1,0,896,400,3.210315015166998,3.3688059775158763,,,, +step1,layout,8,1,0,899,800,11.03239000076428,11.232092045247555,,,, +step1,scroll,linear,1,0,,,1.16,2.12,4.98,26.58,13111110,6436038 +step1,scroll,real,1,0,,,1.09,2.07,3.05,27.21,14027390,11131478 +step1,scroll,github,1,0,,,1.69,3.09,7.22,43.9,13961950,6468878 +step1,scroll,pending1,1,0,,,1.09,2.07,3.22,27.71,14338230,6861518 +step1,scroll,pending4,1,0,,,1.11,2.16,3.14,31.1,14403670,6845158 +step1,scroll,pending8,1,0,,,1.11,2.08,3.22,38.42,14894590,6943414 +step1,scroll,real1000,1,0,,,1.46,2.18,15.32,48.49,19630406,11433830 +step1,layout,0,2,0,198,1,0.06215099710971117,0.11051102774217725,,,, +step1,layout,1,2,0,878,100,0.5380360526032746,0.6309269811026752,,,, +step1,layout,4,2,0,896,400,3.2061049714684486,3.2217249972745776,,,, +step1,layout,8,2,0,899,800,11.033579998183995,11.193912010639906,,,, +step1,scroll,linear,2,0,,,1.09,2.05,4.23,26.63,13176550,6452398 +step1,scroll,real,2,0,,,1.11,2.07,3.04,27.27,13945590,10362558 +step1,scroll,github,2,0,,,1.7,3.12,6.75,43.74,13945590,6403438 +step1,scroll,pending1,2,0,,,1.08,2.04,3.16,28.01,14338230,6861518 +step1,scroll,pending4,2,0,,,1.11,2.07,3.09,30.2,14469110,6877878 +step1,scroll,pending8,2,0,,,1.03,2.05,3.17,39.83,14780046,6943414 +step1,scroll,real1000,2,0,,,1.46,2.19,13.81,48.21,19597686,11433830 +step1,layout,0,3,0,198,1,0.06315100472420454,0.11320202611386776,,,, +step1,layout,1,3,0,878,100,0.5342860240489244,0.624706968665123,,,, +step1,layout,4,3,0,896,400,3.2265549525618553,3.3245959784835577,,,, +step1,layout,8,3,0,899,800,11.02511998033151,11.087010032497346,,,, +step1,scroll,linear,3,0,,,1.08,2.04,3.12,26.8,13192910,6370598 +step1,scroll,real,3,0,,,1.09,2.06,3.19,27.56,14011030,11164198 +step1,scroll,github,3,0,,,1.83,3.59,6.55,43.17,13945590,6452518 +step1,scroll,pending1,3,0,,,1.07,2.06,3.03,28.07,14305510,6894238 +step1,scroll,pending4,3,0,,,1.07,2.03,3.13,30.17,14354590,6861518 +step1,scroll,pending8,3,0,,,1.09,2.04,3.13,38.92,14845510,6959774 +step1,scroll,real1000,3,0,,,1.44,2.15,13.86,48.19,19614046,11433830 +step1,layout_full,8,1,0,40598,800,38.316897000186145,42.21320798387751,,,, +step1,layout_full,8,2,0,40598,800,38.09565398842096,40.36634904332459,,,, +step1,layout_full,8,3,0,40598,800,38.26388600282371,40.25011701742187,,,, +step2,layout,0,1,0,198,1,0.07147103315219283,0.1327010104432702,,,, +step2,layout,1,1,0,878,100,0.4862049827352166,0.581555999815464,,,, +step2,layout,4,1,0,896,400,3.1714450451545417,3.4605670371092856,,,, +step2,layout,8,1,0,899,800,10.989839967805892,11.063679994549602,,,, +step2,scroll,linear,1,0,,,1.1,2.04,5.31,26.43,13111186,6468834 +step2,scroll,real,1,0,,,1.09,2.06,3.0,26.83,13962026,10199034 +step2,scroll,github,1,0,,,1.64,3.04,6.79,43.74,14027466,6436234 +step2,scroll,pending1,1,0,,,1.1,2.04,3.13,28.01,14272866,6943394 +step2,scroll,pending4,1,0,,,1.1,2.05,3.15,30.77,14469186,6796154 +step2,scroll,pending8,1,0,,,1.09,2.07,3.11,38.99,14747402,7041650 +step2,scroll,real1000,1,0,,,1.44,2.16,16.76,47.66,19614098,11352106 +step2,layout,0,2,0,198,1,0.07251999340951443,0.1306509948335588,,,, +step2,layout,1,2,0,878,100,0.48330496065318584,0.5793359596282244,,,, +step2,layout,4,2,0,896,400,3.155924961902201,3.1988740083761513,,,, +step2,layout,8,2,0,899,800,11.011188966222107,12.76425801916048,,,, +step2,scroll,linear,2,0,,,1.1,2.05,3.05,27.07,13176626,6419754 +step2,scroll,real,2,0,,,1.08,2.06,3.03,28.01,13962026,10640754 +step2,scroll,github,2,0,,,1.67,3.06,6.69,44.3,14027466,6419874 +step2,scroll,pending1,2,0,,,1.14,2.11,3.12,28.16,14371026,6845234 +step2,scroll,pending4,2,0,,,1.06,2.04,3.03,30.31,14452826,6861594 +step2,scroll,pending8,2,0,,,1.03,2.0,3.11,38.41,14861922,6992570 +step2,scroll,real1000,2,0,,,1.45,2.18,14.84,47.43,19630482,11303026 +step2,layout,0,3,0,198,1,0.0683909747749567,0.12799096293747425,,,, +step2,layout,1,3,0,878,100,0.4945949767716229,0.5715060397051275,,,, +step2,layout,4,3,0,896,400,3.1551450374536216,3.306746017187834,,,, +step2,layout,8,3,0,899,800,11.014789983164519,11.208721029106528,,,, +step2,scroll,linear,3,0,,,1.09,2.07,3.05,26.58,13225706,6387034 +step2,scroll,real,3,0,,,1.1,2.1,3.1,27.4,14027466,11033394 +step2,scroll,github,3,0,,,1.65,3.05,6.83,45.08,14027466,6452594 +step2,scroll,pending1,3,0,,,1.12,2.08,3.13,28.13,14403746,6861594 +step2,scroll,pending4,3,0,,,1.09,2.05,3.11,30.34,14371026,6828874 +step2,scroll,pending8,3,0,,,1.09,2.02,3.32,38.83,14780122,6959850 +step2,scroll,real1000,3,0,,,1.44,2.14,13.35,47.33,19646842,11417546 +step2,layout_full,8,1,0,40598,800,18.04344600532204,20.438531995750964,,,, +step2,layout_full,8,2,0,40598,800,17.85618398571387,20.297671027947217,,,, +step2,layout_full,8,3,0,40598,800,18.051966035272926,20.84577700588852,,,, diff --git a/docs/research/zgx-git-history-scroll.md b/docs/research/zgx-git-history-scroll.md new file mode 100644 index 000000000..797ccce8e --- /dev/null +++ b/docs/research/zgx-git-history-scroll.md @@ -0,0 +1,319 @@ +# Git History scroll investigation (2026-11-15) + +The investigation sections record the behavior before optimization. The +implementation and repeated before/after measurements are documented below. + +The Git plugin can exhaust its default 32 MiB budget with 1,000 commits. +That count is fixture-specific, not a threshold: a later synthetic graph +test also exhausted it with only 100 commits (see below). +This is reproducible with the current pinned zgx revision +`d391ea7276f0de5cb3d65c191bfd66c02a4c358d`. It is not yet proof of the +reported persistent slowdown of the entire native window. + +History virtualizes its rows (`plugins-builtin/git/history.luau`, `ui.list`, +overscan 8). The framework removes rows outside the current window. Its +data is not bounded: `repo.luau` appends pages of 200 commits and republishes +the accumulated history. The `laid` computed holds the graph and commits; +`keys` and `index` hold additional derived collections. Each append rebuilds +these over the full history. `MAX_LANES = 8` bounds drawing, not graph layout +or storage. + +The framework meters Rust state values together with the plugin's Luau +heap. Near its budget, `zgx-script/src/interrupt.rs` runs a full Luau +collection synchronously at an interrupt safepoint. This can cause UI pauses, +but collection time was not isolated in this experiment. + +## Measurements + +Ran the existing ignored `git_scroll_cost` release benchmark with real +commits from this checkout (`git log --topo-order`, newest first), 240 scroll +steps of 48 pixels. The fake Git service returned the full selected history +in one response; this measures loaded history, not incremental pagination. +GitHub decorations were disabled. These are headless CPU measurements, not +native presentation times or FPS. + +| Commits | Plugin budget | Scroll p95 | Result | +| --- | --- | --- | --- | +| 200 | 32 MiB | 4.99 ms | Pass | +| 600 | 32 MiB | 2.67 ms | Pass | +| 800 | 32 MiB | 2.54 ms | Pass | +| 1,000 | 32 MiB | Invalid | Memory exhaustion while building graph keys | +| 1,000 | 128 MiB | 3.09 ms | Pass | +| 3,135 | 128 MiB | 3.19 ms | Pass | + +The failure names `history.luau:1060`, inside the `keys` computed: +`git: memory error: not enough memory`. The failed run displays only three +texts and must not be interpreted as a fast scroll result. + +Successful runs mounted 132 texts at rest and at most 150 during the longer +scrolls, despite thousands of commits. This supports working virtualization; +text count is not a count of every scene node. Hover p95 was about 1.1 ms +with 600 and 800 commits. Persistent global scroll degradation was not +reproduced by these workloads. + +## Diagnostic changes and follow-up + +`git_scroll_cost` now reports total script heap bytes before and after +scroll, and after collection. `GIT_PERF_MEMORY_MB` overrides only the test +plugin budget; the application still uses its existing limits. Example: + +```sh +GIT_PERF_JSON=/path/to/git-world.json GIT_PERF=history,hover \ + GIT_PERF_MEMORY_MB=128 cargo test --manifest-path apps/zgx/Cargo.toml \ + --release --bin zeron-zgx git_scroll_cost -- --ignored --nocapture +``` + +The JSON fixture uses the shape documented by `git_world` in +`apps/zgx/src/plugins/examples_tests.rs`. Measurement logs from this session +are `/tmp/zgx-history-*.log`; generated fixtures are `/tmp/zgx-history-*.json`. + +The next native trace should include repeated pagination, GitHub enabled, +and scrolling another panel before and after closing History. Inspect plugin +memory, GC pauses and GPU/compositor work separately. A production fix should +reduce full-history derived state and repeated conversion; increasing the +budget alone only moves the failure point. + +## Follow-up: 100 commits and graph complexity + +The first experiment did not establish a minimum failing commit count. +Follow-up workloads kept 100 commits and varied their parent topology. +The real fixture is the newest 100 commits of this checkout; the synthetic +variants keep their metadata but replace parents. All use 32 MiB unless +otherwise specified. + +The benchmark now supports `GIT_PERF_BOUNCE=1` (reverse every 64 steps), +`GIT_PERF_STEPS=1200`, and a top-level `github` mock response in the input +JSON. It reports each VM heap before History, at rest, after scrolling, +after collection, and sampled every 40 steps outside the timed frames. +An additional real-history run traversed the list for 12,000 steps. + +| 100-commit fixture | Graph segments | Sampled peak Git VM heap | CPU scroll p95 | +| --- | --- | --- | --- | +| Synthetic linear chain | 198 | 12.5 MiB | 2.05 ms | +| Real current history | 640 | 13.4 MiB | 2.06 ms | +| Real `git log --all --topo-order` | Not counted | 13.2 MiB | 2.25 ms | +| Real current history + mocked GitHub | 640 | 13.3 MiB | 3.15 ms | +| Synthetic chain + one distinct pending parent per commit | 5,248 | 18.4 MiB | 2.05 ms | +| Synthetic chain + four distinct pending parents per commit | 20,398 | 19.7 MiB | 1.99 ms | +| Synthetic chain + eight distinct pending parents per commit | 40,598 | Memory error at 32 MiB budget | Invalid | +| Same eight-parent graph, 128 MiB budget | 40,598 | 50.1 MiB | 2.13 ms | + +Segment counts come from a reference calculation of the layout algorithm +over the fixture topology. The synthetic graphs intentionally leave many +parents beyond the loaded page; the eight-parent case is an extreme stress +case, not an observed shape of the user's repository. These cases demonstrate +why commit count alone cannot predict graph memory. + +For the real history, 12,000 scroll steps peaked at 14.7 MiB of Git VM heap, +with p95 2.09 ms and max 5.29 ms, without an error. The GitHub fixture's +separate VM peaked at 9.1 MiB during 1,200 steps. That is a separate plugin +budget and must not be added to Git's consumption to claim Git exceeded +32 MiB. GitHub API replies were mocked; native image downloading, decoding +and GPU presentation were not measured. + +### Memory accounting limits + +The reported VM heap is **not the plugin's complete metered footprint**. +The budget also counts Rust-backed state, including anonymous computed +state holding the graph. `host.memory()` reports all named states but does +not expose every anonymous state or the exact per-plugin meter total. +The failed eight-parent run reported only about 20.5 MiB of Git VM heap +after the error; that is compatible with a 32 MiB combined limit and is not +evidence that the limit was ignored. Its failures name `history.luau:1030` +and `history.luau:1060`. + +The Git plugin budget is shared by its session models and caches, not +renewed for each History tab. `repo.luau` retains models in its `models` +table. These measurements start from a fresh host and one session; they +do not measure accumulated histories/diffs from other sessions. The app +also requests 200 commits per page: visible commits and loaded commits +are different counts. + +The native global slowdown with a small ordinary history remains +unreproduced. These results justify investigating graph storage and shared +plugin pressure, but do not establish GC as the cause of that native symptom. +Follow-up logs and fixtures are `/tmp/zgx-100-*.log` and +`/tmp/zgx-100-*.json`; the 32 MiB stress failure is intentional. + +## Implementation, step 1: retain only visible graph segments + +`graph.layout(commits, max_lanes)` now keeps every pending parent, lane and +color, but emits a segment only when both endpoints fit the requested lane +limit. History passes its existing eight-lane drawing limit. Omitting the +argument preserves the uncapped API. Width is tracked independently of +retained segments, including incoming lanes that end on a row. This preserves +compact-mode decisions, dot colors, hidden-lane joins and later page layout. + +History's row keys also include the commit's color, previous row color and +whether another row follows. Compact rails depend on these even when the +commit's ordinary segments are all hidden; appending a page must update the +previous tail's continuation. + +Regression checks compare the new layout against a frozen original +implementation. Across page prefixes and 32 deterministic DAGs, 15,306 row +comparisons check column, color, full width and the exact visible segment +sequence for limits 0, 1, 2, 8, 16 and no limit. Existing History tests cover +responsive geometry and path highlighting. A functional wide-graph test +also covers a hidden-lane commit's compact hover, page append and return to +full-width geometry. + +### Repeated measurement protocol + +Three fresh processes per case and implementation, run sequentially on Linux +with the same release test executable and pinned framework. Tables report +the median of the three results. No production memory budget was increased. +Baseline runs use the frozen original graph with the same UI/benchmark +harness, including the explicit compact-row key dependencies. + +The isolated layout benchmark performs 24 layouts, discards the first three, +and reports p50/p95 of the remaining 21. Its script-call budget is raised to +30 seconds so the old algorithm can complete the repeated layouts; this does +not change the plugin's production time/memory limits. These timings include +Luau allocation/collection, but exclude UI construction and Rust state +conversion. Luau assertions are enabled in this build. + +| 100-commit topology | Original segments | Step 1 segments | Original layout p50 | Step 1 layout p50 | +| --- | ---: | ---: | ---: | ---: | +| Linear | 198 | 198 | 0.071 ms | 0.062 ms | +| One extra pending parent per commit | 5,248 | 878 | 1.915 ms | 0.538 ms | +| Four extra pending parents | 20,398 | 896 | 12.358 ms | 3.210 ms | +| Eight extra pending parents | 40,598 | 899 | 39.302 ms | 11.032 ms | + +The extreme case stores 97.8% fewer segments, with no loss of the full +800-lane topology. Lane/parent searches are still over the full topology; +this change does not make the entire algorithm independent of graph width. + +UI runs perform 1,200 scroll steps of 48 pixels, reversing every 64 steps. +Heap figures are sampled Git VM peaks, not the complete metered plugin +footprint (Rust state is additional). Opening includes 30 settling frames +and service/UI work, rather than only graph layout. + +| Fixture | Original peak Git VM heap | Step 1 peak | Original open | Step 1 open | Original scroll p95 | Step 1 scroll p95 | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | +| Real 100 commits | 13.27 MiB | 13.36 MiB | 27.57 ms | 27.27 ms | 2.10 ms | 2.07 ms | +| Real 100 + mocked GitHub | 13.35 MiB | 13.30 MiB | 44.41 ms | 43.74 ms | 3.11 ms | 3.12 ms | +| Synthetic 100, one extra parent | 18.45 MiB | 13.67 MiB | 35.07 ms | 28.01 ms | 2.04 ms | 2.06 ms | +| Synthetic 100, four extra parents | 19.74 MiB | 13.74 MiB | 68.26 ms | 30.20 ms | 1.98 ms | 2.07 ms | +| Synthetic 100, eight extra parents | Memory error | 14.16 MiB | Invalid | 38.92 ms | Invalid | 2.05 ms | +| Real 1,000 commits | Memory error | 18.71 MiB | Invalid | 48.21 ms | Invalid | 2.18 ms | + +All three runs of each formerly failing fixture pass at 32 MiB after step 1. +Ordinary-history scroll times are effectively unchanged. This is evidence +of reduced graph allocation and resolved tested memory failures, not proof +that the reported global native-window FPS problem is fixed. + +### Reproduction + +```sh +python3 scripts/zgx-git-history-fixture.py /tmp/graph-wide.json \ + --rev 68e460af --count 100 --shape pending --extra-parents 8 +GIT_PERF_JSON=/tmp/graph-wide.json GIT_PERF=history \ + GIT_PERF_BOUNCE=1 GIT_PERF_STEPS=1200 \ + cargo test --manifest-path apps/zgx/Cargo.toml --release --bin zeron-zgx \ + git_scroll_cost -- --ignored --nocapture --test-threads=1 +GIT_GRAPH_PARENTS=8 cargo test --manifest-path apps/zgx/Cargo.toml \ + --release --bin zeron-zgx git_graph_layout_cost -- --ignored --nocapture +# Add GIT_GRAPH_REFERENCE=1 for the frozen original layout. +``` + +Use `--shape real`, `--shape linear`, `--all` or `--github` to generate the +other fixture types. Keep the generated JSON fixed across implementations. +Per-run measurements are in `zgx-git-history-graph-results.csv`; detailed +logs for this session are `/tmp/zgx-graph-{baseline,step1}-*.log`. + +## Implementation, step 2: append geometry in final paint order + +Passing lanes are now emitted in descending lane order, followed by the +commit's incoming/outgoing connections. This exactly preserves the original +front-insertion sequence, including hit-test precedence, without repeatedly +shifting the existing line array. Connections use one scratch array per +layout, cleared and reused between rows; segment records are not copied. + +The geometry/topology oracle and all 12 History-related tests pass for both +steps independently, including compact hover, hidden-lane page continuation, +responsive widths and GitHub decoration. Graph lint passes. Static Luau type +checking could not run because `luau-lsp` is not installed; the release build +and runtime tests passed. No framework source or production budgets changed. + +### Final results + +Same protocol and three repetitions as step 1; every previously failing +32 MiB fixture still passes in all three runs. + +| 100-commit topology, eight visible lanes | Original layout p50 | Step 1 | Final | +| --- | ---: | ---: | ---: | +| Linear | 0.071 ms | 0.062 ms | 0.071 ms | +| One extra pending parent | 1.915 ms | 0.538 ms | 0.486 ms | +| Four extra pending parents | 12.358 ms | 3.210 ms | 3.156 ms | +| Eight extra pending parents | 39.302 ms | 11.032 ms | 11.011 ms | + +In the extreme capped workload, the combined changes reduce graph layout +time by about 72% and retained segments by 97.8% (40,598 → 899). The first +step accounts for most of that improvement. With only eight lanes emitted, +removing front insertions has little additional effect in the widest case; +full-topology lane/parent searches now dominate. Do not attribute the entire +combined gain to step 2. + +To isolate step 2, an additional run sets `GIT_GRAPH_LIMIT=10000`, so all +800 lanes and 40,598 segments are emitted by every version: + +| Same complete graph, all lanes emitted | Layout p50 | Layout p95 | +| --- | ---: | ---: | +| Original | 39.293 ms | 41.820 ms | +| Step 1, limit above full width | 38.264 ms | 40.366 ms | +| Step 2, limit above full width | 18.043 ms | 20.439 ms | + +This demonstrates roughly 53% less layout time from removing repeated shifts +when many segments are actually emitted. History itself uses the capped +path; this uncapped result is not a native-window FPS claim. + +| UI fixture | Final sampled peak Git VM heap | Final open | Final scroll p95 | +| --- | ---: | ---: | ---: | +| Real 100 commits | 13.32 MiB | 27.40 ms | 2.06 ms | +| Real 100 + mocked GitHub | 13.38 MiB | 44.30 ms | 3.05 ms | +| Synthetic 100, one extra parent | 13.71 MiB | 28.13 ms | 2.08 ms | +| Synthetic 100, four extra parents | 13.78 MiB | 30.34 ms | 2.05 ms | +| Synthetic 100, eight extra parents | 14.10 MiB | 38.83 ms | 2.02 ms | +| Real 1,000 commits | 18.72 MiB | 47.43 ms | 2.16 ms | + +Ordinary-history CPU scroll performance remains effectively unchanged. +The measured wins are lower graph allocation, faster wide-graph construction +and elimination of the tested memory failures. Incremental pagination, +full-history reactive conversion, session-cache lifetime and native GPU +profiling remain separate work. + +`zgx-git-history-graph-results.csv` preserves every repetition. `kind=layout` +uses eight visible lanes, `layout_full` emits the complete graph, and +`scroll` measures UI frames. Rows with `exit=101` are expected baseline +memory failures: their apparent frame times are invalid. Final logs are +`/tmp/zgx-graph-step2-*.log`. + + +## Follow-up: compact History keys (2026-11-16) + +History now uses a graph generation and row position instead of serializing +all visible segments into a retained key for every commit. The generation +stays constant on append and metadata-only updates; changed, reordered or +truncated topology increments it. Keys also include visible width and tail +continuation, so pagination still refreshes the previous tail. Search keys +use position and SHA, with a separate prefix from graph keys. + +Keys are generated on demand. Their position resolves the commit directly, +removing both the key array and reverse index map. Fixed `height`/`count` +also replace the array of identical heights. Commit metadata stays in the +model's existing collection; graph geometry and topology validation remain. + +A single headless smoke comparison against `cec0fc6d`, using the same 1,000 +real commits, 32 MiB plugin budget and 240 scroll steps, measured Git VM heap: + +| Stage | Before | Compact keys | +| --- | ---: | ---: | +| At rest | 13,610,626 bytes | 12,956,046 bytes | +| After scroll and collection | 14,201,210 bytes | 13,988,222 bytes | + +Both runs mounted 124 texts at rest and at most 150 while scrolling. This +is a memory smoke comparison, not evidence of a native scrolling speedup. +The existing `git_scroll_cost` benchmark reproduces it using a fixture from +`scripts/zgx-git-history-fixture.py --count 1000`. Regression tests cover +stable generations on append, invalidation on topology changes with unchanged +commit count and width, commit opening, search and compact tail continuation. diff --git a/scripts/zgx-git-history-fixture.py b/scripts/zgx-git-history-fixture.py new file mode 100644 index 000000000..248b8e3e2 --- /dev/null +++ b/scripts/zgx-git-history-fixture.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +"""Write git_scroll_cost input from real commits, optionally stressing topology.""" + +import argparse +import json +import subprocess +from pathlib import Path + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("output", type=Path) + parser.add_argument("--count", type=int, default=100) + parser.add_argument("--rev", default="HEAD") + parser.add_argument("--all", action="store_true", help="Read all refs instead of --rev") + parser.add_argument("--shape", choices=("real", "linear", "pending"), default="real") + parser.add_argument("--extra-parents", type=int, default=8, help="Synthetic pending parents per commit") + parser.add_argument("--github", action="store_true", help="Include mocked GitHub author/PR replies") + args = parser.parse_args() + if args.count < 1 or args.extra_parents < 0: + parser.error("count must be positive and extra-parents nonnegative") + + cmd = ["git", "log", "--topo-order", f"-{args.count}", "--format=%H%x1f%P%x1f%an%x1f%ae%x1f%at%x1f%D%x1f%s"] + cmd += ["--all"] if args.all else [args.rev] + cmd += ["--"] + commits = [] + for line in subprocess.check_output(cmd).decode().splitlines(): + sha, parents, author, email, at, refs, subject = line.split("\x1f", 6) + commits.append(dict(sha=sha, parents=parents.split(), author=author, email=email, + at=int(at), refs=refs.split(", ") if refs else [], subject=subject)) + if not commits: + parser.error("the selected revision has no commits") + if args.shape != "real": + for i, commit in enumerate(commits): + commit["parents"] = [commits[i + 1]["sha"]] if i + 1 < len(commits) else [] + if args.shape == "pending": + commit["parents"] += [f"{30000 + i * args.extra_parents + j:040x}" for j in range(args.extra_parents)] + + world = dict( + status=dict(branch="main", oid=commits[0]["sha"], conflicts=0, changes=0, + tree="t1", index="i1", device="devbox", remote="origin", + remote_url="git@github.com:zeronsh/zeron.git" if args.github else "", who="Ada"), + changes={scope: dict(scope=scope, a="a", b="b", files=[]) for scope in ("working", "branch", "turn", "commit")}, + history=dict(more=False, commits=commits), diff=dict(text=""), fail={}, + ) + if args.github: + world["github"] = dict(viewer="ada", repository="zeronsh/zeron", branches={}, commits={ + c["sha"]: dict( + author=dict(user=dict(login=c["author"], avatarUrl="https://avatars.githubusercontent.com/u/1")), + associatedPullRequests=dict(nodes=[dict(number=i + 1, title=c["subject"], + url=f"https://github.com/zeronsh/zeron/pull/{i + 1}", state="MERGED", merged=True)]), + ) for i, c in enumerate(commits) + }) + args.output.write_text(json.dumps(world) + "\n") + + +if __name__ == "__main__": + main()