From b98a7c43de25b452568daeab39e20fb640217fa9 Mon Sep 17 00:00:00 2001 From: nihilist-narwhal <240798405+nihilist-narwhal@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:29:36 -0500 Subject: [PATCH 1/8] cargo xtask bench: calculation benchmarks on fixed in-code workbooks Ten scenarios (crates/engine/examples/bench.rs, replacing the ad-hoc perf example) build their workbooks in code, warm up, and time edits, formula entry, Calculate Now, sort and XLSX save/open through Session::execute, checking each result so a scenario that stops recalculating fails instead of getting fast. A failed scenario is reported and the rest still run. `cargo xtask bench` runs them in release, writes /bench/latest.json and compares with a baseline saved by --save. Baseline (M2 Pro, 100k rows): a 100k-dependent edit recalculates in 90-106 ms; 100 whole-column SUMIFS take ~0.5 s per edit. xlsx.open fails on main: the #2413 element cap drops the last ~9,000 rows of the 100k-row model on reopen (fixed by #2527). Parity docs, gaps and roadmap carry the measured numbers. Co-Authored-By: Claude Opus 5.5 --- ROADMAP.md | 4 +- crates/engine/examples/bench.rs | 327 ++++++++++++++++++++++++++++++++ crates/engine/examples/perf.rs | 60 ------ docs/egui-037-migration-plan.md | 4 +- docs/gaps.md | 11 +- docs/roadmap.md | 6 +- docs/target-app-parity.md | 20 +- xtask/Cargo.toml | 2 +- xtask/src/bench.rs | 103 ++++++++++ xtask/src/main.rs | 5 + 10 files changed, 468 insertions(+), 74 deletions(-) create mode 100644 crates/engine/examples/bench.rs delete mode 100644 crates/engine/examples/perf.rs create mode 100644 xtask/src/bench.rs diff --git a/ROADMAP.md b/ROADMAP.md index fd27ee9eb..460511046 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~25 points of "ready for real work" and ~160–240 h away -> **Last reviewed:** 2026-11-01 · **Last updated:** 2026-11-05 · **Change:** minor (CJK fallback text shares the Latin baseline; earlier: form controls: buttons, sliders and spin buttons; earlier: RefEdit reference boxes in dialogs: Goal Seek, Solver and Data Analysis take ranges by selection on the grid; earlier: Python mode in the formula bar; earlier: Python cells stored and kept in files; earlier: equations: Insert › Equation, editor, gridcraft-math; earlier: What-if and analysis: Solver Sensitivity report for Simplex LP and GRG Nonlinear; earlier: floating objects: one selection, eight resize handles, keyboard delete; earlier: Chart data tables: live source values beneath column, bar, line, area and compatible combo charts; legend keys and borders; XLSX round-trip and print/PDF; earlier: Formulas › Function Library as labelled buttons with category menus; Recently Used and Insert Function categories; XLSX workbook-view selection, bounded tab ratios and sheet-edit regressions; readiness estimates unchanged; earlier: CSV and text open detects each column's date order, so day-first dates are not silently swapped) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-11-01 · **Last updated:** 2026-11-14 · **Change:** minor (`cargo xtask bench`: measured recalc baselines; earlier: CJK fallback text shares the Latin baseline; earlier: form controls: buttons, sliders and spin buttons; earlier: RefEdit reference boxes in dialogs: Goal Seek, Solver and Data Analysis take ranges by selection on the grid; earlier: Python mode in the formula bar; earlier: Python cells stored and kept in files; earlier: equations: Insert › Equation, editor, gridcraft-math; earlier: What-if and analysis: Solver Sensitivity report for Simplex LP and GRG Nonlinear; earlier: floating objects: one selection, eight resize handles, keyboard delete; earlier: Chart data tables: live source values beneath column, bar, line, area and compatible combo charts; legend keys and borders; XLSX round-trip and print/PDF; earlier: Formulas › Function Library as labelled buttons with category menus; Recently Used and Insert Function categories; XLSX workbook-view selection, bounded tab ratios and sheet-edit regressions; readiness estimates unchanged; earlier: CSV and text open detects each column's date order, so day-first dates are not silently swapped) · **Target:** Microsoft Excel (Microsoft 365) GridCraft aims at full Microsoft Excel parity, and at being better in places: faster, open (XLSX is the native format), fully scriptable by agents (MCP, control channel, CLI), and on the web. @@ -151,6 +151,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | 2026-10-31 | System and embedded CJK fallback text shares the Latin baseline in cells, editors and window titles (#1265, #2621). Symbol and emoji alignment, manual row heights and clipping are unchanged | | 2026-10-26 | Arabic bidi display/editing preserve logical source text; per-cell reading direction is editable and survives undo, XLSX and conditional-format priority | | 2026-10-26 | Arabic text shows in cells and the interface through a system, fontconfig or craft-fonts Arabic face; workbook fonts keep their script fallbacks | +| 2026-10-26 | `cargo xtask bench`: ten fixed in-code workbooks (100k rows) time edits, formula entry, Calculate Now, sort and XLSX save/open, check their results and compare with a saved baseline. On an M2 Pro a 100k-dependent edit recalculates in 90–106 ms and 300k formulas in 395–455 ms | | 2026-10-25 | AutoSum over a selection does what Excel does: a selected row gets one total to its right (in its last cell if that is empty), not a one-cell total under each cell; a block whose last column is empty gets each row's total there, one whose last row is empty each column's, and with both the corner gets the total of the block; otherwise totals go under each column as before | | 2026-10-24 | Macro interface ([macros.md](docs/macros.md#in-the-window)): an Automate › Macros group (Record Macro, which turns into Stop Recording, Use Relative References, Run Macro with recent macros); "Recording · N steps" with Stop in the status bar; a Record Macro dialog (checked name, free Ctrl+Shift shortcut, description); the Macros dialog lists macros beside Automate scripts and a VBA project, with Run, Step Into, Edit, Create, Delete and Options; Ctrl+Shift+letter macro shortcuts; questions that offer Run, Overwrite, Shift Down or Shift Right; a step editor with a breakpoint gutter, drag to reorder, a command picker, reference boxes that switch between relative tokens and absolute references, Inputs and Output tabs, a debug bar (Check, Run, Step, Continue, Run to Here, Stop with keep or undo) and grid overlays for inputs, writes, conflicts and the current step | | 2026-10-24 | Go To lists the named ranges of every sheet (`Sheet2!Name` for a name scoped to another sheet; hidden names, constants and formulas left out) and the references entered before, a double-click goes to an entry, and the Reference box takes several names or references separated by commas (`Price, Type`, `B14:C22,F19:G30`) | @@ -288,6 +289,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | 2026-10-31 | minor | Progress log: shared CJK/Latin baselines in cells, editors and window titles | | 2026-10-26 | minor | Progress log: Arabic bidi display and editing | | 2026-10-26 | minor | Progress log: Arabic font fallback | +| 2026-10-26 | minor | Progress log: `cargo xtask bench` | | 2026-10-25 | minor | Progress log: AutoSum over a selected row, and a selection's empty last row and column | | 2026-10-24 | minor | Macro interface on the Automate tab; progress log row | | 2026-10-24 | minor | Macro engine and `macro.*` commands; catalog 275 / 291 → 276 / 292 (View › Macros › Record Macro); progress log row | diff --git a/crates/engine/examples/bench.rs b/crates/engine/examples/bench.rs new file mode 100644 index 000000000..c07872ea9 --- /dev/null +++ b/crates/engine/examples/bench.rs @@ -0,0 +1,327 @@ +//! Calculation benchmarks on fixed workbooks built in code (release build). Run through +//! `cargo xtask bench`, which builds this in release and compares against a saved baseline, or +//! directly: `cargo run --release -p gridcraft-engine --example bench -- [--quick] [--rows N] +//! [--reps N] [--filter NAME] [--json PATH]`. +//! +//! Every scenario builds its workbook untimed, runs one warm-up, then times `reps` runs of one +//! operation through `Session::execute` (so a timing includes what an edit costs the user: undo +//! history, graph update and recalculation). Results are checked so a scenario that silently +//! stopped recalculating fails instead of getting fast. + +#![allow(clippy::disallowed_methods)] // a native-only benchmark: wasm never runs it + +use std::time::{Duration, Instant}; + +use gridcraft_core::{CellRef, Value}; +use gridcraft_engine::Session; +use serde_json::{Value as Json, json}; + +struct Cfg { + rows: usize, + reps: usize, + filter: Option, +} + +struct Outcome { + name: &'static str, + what: String, + times: Vec, + cells: usize, +} + +type R = Result; + +fn main() -> std::process::ExitCode { + match run() { + Ok(()) => std::process::ExitCode::SUCCESS, + Err(e) => { + eprintln!("bench: {e}"); + std::process::ExitCode::FAILURE + } + } +} + +fn run() -> R<()> { + let args: Vec = std::env::args().skip(1).collect(); + let flag = |name: &str| args.iter().position(|a| a == name).and_then(|i| args.get(i + 1)).cloned(); + let quick = args.iter().any(|a| a == "--quick"); + let cfg = Cfg { + rows: flag("--rows").and_then(|s| s.parse().ok()).unwrap_or(if quick { 10_000 } else { 100_000 }).max(200), + reps: flag("--reps").and_then(|s| s.parse().ok()).unwrap_or(if quick { 3 } else { 7 }).max(1), + filter: flag("--filter"), + }; + let scenarios: [(&'static str, fn(&Cfg) -> R); 10] = [ + ("edit.chain", edit_chain), + ("edit.fanout", edit_fanout), + ("edit.sliding", edit_sliding), + ("edit.sumifs", edit_sumifs), + ("edit.lookup", edit_lookup), + ("enter.formulas", enter_formulas), + ("recalc.full", recalc_full), + ("data.sort", data_sort), + ("xlsx.save", xlsx_save), + ("xlsx.open", xlsx_open), + ]; + println!("GridCraft calc bench: {} rows, {} reps (median / min)\n", cfg.rows, cfg.reps); + println!("{:<16} {:>10} {:>10} {:>9} workload", "scenario", "median", "min", "cells"); + let mut results = Vec::new(); + // A failed scenario is reported and the others still run, so one regression doesn't hide + // the numbers for everything else. + let mut failed = Vec::new(); + for (name, f) in scenarios { + if cfg.filter.as_deref().is_some_and(|p| !name.contains(p)) { + continue; + } + let o = match f(&cfg) { + Ok(o) => o, + Err(e) => { + println!("{name:<16} {:>10} {e}", "FAILED"); + failed.push(name); + continue; + } + }; + let (med, min) = stats(&o.times); + let cells = if o.cells == 0 { "-".to_string() } else { o.cells.to_string() }; + println!("{:<16} {:>10} {:>10} {:>9} {}", o.name, ms(med), ms(min), cells, o.what); + results.push(json!({"name": o.name, "what": o.what, "median_ms": med, "min_ms": min, "cells": o.cells})); + } + if let Some(path) = flag("--json") { + let out = json!({"rows": cfg.rows, "reps": cfg.reps, "results": results, "failed": failed}); + let text = serde_json::to_string_pretty(&out).map_err(|e| e.to_string())?; + std::fs::write(&path, text).map_err(|e| format!("{path}: {e}"))?; + } + if failed.is_empty() { Ok(()) } else { Err(format!("{} scenario(s) failed: {}", failed.len(), failed.join(", "))) } +} + +fn stats(times: &[Duration]) -> (f64, f64) { + let mut v: Vec = times.iter().map(|d| d.as_secs_f64() * 1000.0).collect(); + v.sort_by(f64::total_cmp); + let med = v.get(v.len() / 2).copied().unwrap_or(0.0); + (med, v.first().copied().unwrap_or(0.0)) +} + +fn ms(v: f64) -> String { + if v >= 100.0 { format!("{v:.0} ms") } else { format!("{v:.2} ms") } +} + +fn exec(s: &mut Session, cmd: &str, p: Json) -> R { + s.execute(cmd, p).map_err(|e| format!("{cmd}: {e}")) +} + +fn session() -> R { + let mut s = Session::new(); + s.new_workbook(); + Ok(s) +} + +/// Writes `rows` rows starting at `col` row 1, `make(i)` giving row i (0-based), in chunks. +fn fill(s: &mut Session, col: &str, rows: usize, make: impl Fn(usize) -> Json) -> R<()> { + let chunk = 10_000; + let mut r = 0; + while r < rows { + let n = chunk.min(rows - r); + let vals: Vec = (r..r + n).map(&make).collect(); + exec(s, "range.setValues", json!({"range": format!("{col}{}", r + 1), "values": vals}))?; + r += n; + } + Ok(()) +} + +fn num(s: &Session, at: &str) -> R { + let c = CellRef::parse(at).ok_or_else(|| format!("bad ref {at}"))?; + let d = s.doc().map_err(|e| e.to_string())?; + match d.wb.sheet(0).map(|sh| sh.value(c)) { + Some(Value::Number(n)) => Ok(n), + other => Err(format!("{at} = {other:?}, expected a number")), + } +} + +fn expect(s: &Session, at: &str, want: f64) -> R<()> { + let got = num(s, at)?; + if (got - want).abs() <= 1e-6 * want.abs().max(1.0) { Ok(()) } else { Err(format!("{at} = {got}, expected {want}")) } +} + +fn recalc_cells(s: &Session) -> usize { + s.doc().map(|d| d.calc.last_recalc_cells).unwrap_or(0) +} + +/// Runs a warm-up then `reps` timed calls of `op(rep)`, then `check(rep)` (untimed) after each. +fn time( + cfg: &Cfg, + s: &mut Session, + mut op: impl FnMut(&mut Session, usize) -> R<()>, + check: impl Fn(&Session, usize) -> R<()>, +) -> R<(Vec, usize)> { + let mut times = Vec::with_capacity(cfg.reps); + let mut cells = 0; + for rep in 0..=cfg.reps { + let t = Instant::now(); + op(s, rep)?; + let dt = t.elapsed(); + check(s, rep)?; + cells = recalc_cells(s); + if rep > 0 { + times.push(dt); + } + } + Ok((times, cells)) +} + +/// Value typed into A1 on rep `r`: alternates so every rep changes something. +fn a1(r: usize) -> f64 { + (r % 2 + 2) as f64 +} + +/// A running sum `B[n] = B[n-1] + A[n]`: one edit at the top recalculates a dependency chain +/// `rows` deep. +fn edit_chain(cfg: &Cfg) -> R { + let n = cfg.rows; + let mut s = session()?; + fill(&mut s, "A", n, |_| json!([1]))?; + fill(&mut s, "B", n, |i| json!([if i == 0 { "=A1".to_string() } else { format!("=B{i}+A{}", i + 1) }]))?; + let (times, cells) = time( + cfg, + &mut s, + |s, r| exec(s, "cell.set", json!({"cell": "A1", "input": a1(r).to_string()})).map(drop), + |s, r| expect(s, &format!("B{n}"), (n - 1) as f64 + a1(r)), + )?; + Ok(Outcome { name: "edit.chain", what: format!("edit A1 under a {n}-deep running sum"), times, cells }) +} + +/// `rows` formulas that each read `$A$1`: one edit, a wide and shallow dirty set. +fn edit_fanout(cfg: &Cfg) -> R { + let n = cfg.rows; + let mut s = session()?; + exec(&mut s, "cell.set", json!({"cell": "A1", "input": "1"}))?; + fill(&mut s, "B", n, |i| json!([format!("=$A$1*{}", i + 1)]))?; + let (times, cells) = time( + cfg, + &mut s, + |s, r| exec(s, "cell.set", json!({"cell": "A1", "input": a1(r).to_string()})).map(drop), + |s, r| expect(s, &format!("B{n}"), a1(r) * n as f64), + )?; + Ok(Outcome { name: "edit.fanout", what: format!("edit $A$1 read by {n} formulas"), times, cells }) +} + +/// `rows` overlapping 100-row window sums; an edit in the middle dirties ~100 of them, so this +/// measures the range index lookup, not evaluation. +fn edit_sliding(cfg: &Cfg) -> R { + let n = cfg.rows; + let mut s = session()?; + fill(&mut s, "A", n + 99, |_| json!([1]))?; + fill(&mut s, "B", n, |i| json!([format!("=SUM(A{}:A{})", i + 1, i + 100)]))?; + let mid = n / 2; + let (times, cells) = time( + cfg, + &mut s, + |s, r| exec(s, "cell.set", json!({"cell": format!("A{mid}"), "input": a1(r).to_string()})).map(drop), + |s, r| expect(s, &format!("B{mid}"), 99.0 + a1(r)), + )?; + Ok(Outcome { name: "edit.sliding", what: format!("edit one cell under {n} 100-row SUM windows"), times, cells }) +} + +/// 100 whole-column SUMIFS over `rows` rows of data, all dirtied by one data edit. +fn edit_sumifs(cfg: &Cfg) -> R { + let n = cfg.rows; + let mut s = session()?; + fill(&mut s, "A", n, |i| json!([1, (i % 100) as f64]))?; + fill(&mut s, "D", 100, |k| json!([format!("=SUMIFS(A:A,B:B,{k})")]))?; + let per = (n / 100) as f64; + let (times, cells) = time( + cfg, + &mut s, + |s, r| exec(s, "cell.set", json!({"cell": "A1", "input": a1(r).to_string()})).map(drop), + |s, r| expect(s, "D1", per - 1.0 + a1(r)), + )?; + Ok(Outcome { name: "edit.sumifs", what: format!("edit data under 100 SUMIFS(A:A,B:B,k) over {n} rows"), times, cells }) +} + +/// 100 exact-match XLOOKUPs over whole columns of `rows` keys, all dirtied by one key edit. +fn edit_lookup(cfg: &Cfg) -> R { + let n = cfg.rows; + let mut s = session()?; + fill(&mut s, "A", n, |i| json!([(i + 1) as f64, (2 * (i + 1)) as f64]))?; + let step = n / 100; + fill(&mut s, "D", 100, |k| json!([format!("=XLOOKUP({},A:A,B:B)", (k + 1) * step)]))?; + let (times, cells) = time( + cfg, + &mut s, + // A1's key alternates between 1 and -1, so A:A changes but the lookups' answers don't. + |s, r| exec(s, "cell.set", json!({"cell": "A1", "input": if r % 2 == 0 { "-1" } else { "1" }})).map(drop), + |s, _| expect(s, "D100", (2 * n) as f64), + )?; + Ok(Outcome { name: "edit.lookup", what: format!("edit a key under 100 XLOOKUPs over {n} rows"), times, cells }) +} + +/// Entering `rows` formulas in one command: parse, graph insertion and first evaluation. +fn enter_formulas(cfg: &Cfg) -> R { + let n = cfg.rows; + let mut s = session()?; + fill(&mut s, "A", n, |i| json!([(i % 97) as f64]))?; + let vals: Vec = (0..n).map(|i| json!([format!("=A{}*2+1", i + 1)])).collect(); + let (times, cells) = time( + cfg, + &mut s, + |s, _| exec(s, "range.setValues", json!({"range": "B1", "values": vals})).map(drop), + |s, _| expect(s, &format!("B{n}"), ((n - 1) % 97) as f64 * 2.0 + 1.0), + )?; + Ok(Outcome { name: "enter.formulas", what: format!("enter {n} formulas =An*2+1 in one paste"), times, cells }) +} + +/// A model with 3 formulas per row (arithmetic and a row SUM), recalculated in full. +fn model(n: usize) -> R { + let mut s = session()?; + fill(&mut s, "A", n, |i| json!([(i % 97) as f64, format!("=A{}*2", i + 1), format!("=B{0}+A{0}", i + 1), format!("=SUM(A{0}:C{0})", i + 1)]))?; + Ok(s) +} + +fn recalc_full(cfg: &Cfg) -> R { + let n = cfg.rows; + let mut s = model(n)?; + let last = ((n - 1) % 97) as f64 * 6.0; + let (times, cells) = time(cfg, &mut s, |s, _| exec(s, "formulas.calculateNow", json!({})).map(drop), |s, _| expect(s, &format!("D{n}"), last))?; + Ok(Outcome { name: "recalc.full", what: format!("Calculate Now, {} formulas (3 per row)", 3 * n), times, cells }) +} + +fn data_sort(cfg: &Cfg) -> R { + let n = cfg.rows; + let mut s = session()?; + fill(&mut s, "A", n, |i| json!([format!("Item {i}"), (i % 97) as f64, ((i * 7919) % 1000) as f64]))?; + let (times, _) = time( + cfg, + &mut s, + |s, r| { + let cmd = if r % 2 == 0 { "data.sortDescending" } else { "data.sortAscending" }; + exec(s, cmd, json!({"range": format!("A1:C{n}"), "column": "B"})).map(drop) + }, + |s, r| expect(s, "B1", if r % 2 == 0 { 96.0 } else { 0.0 }), + )?; + // A sort with no formulas recalculates nothing; 0 shows as "-". + Ok(Outcome { name: "data.sort", what: format!("sort {n} rows x 3 columns by a numeric key"), times, cells: 0 }) +} + +fn save_bytes(s: &mut Session) -> R { + let r = exec(s, "file.saveBytes", json!({"format": "xlsx"}))?; + r.get("base64").and_then(Json::as_str).map(str::to_string).ok_or_else(|| "file.saveBytes: no base64".to_string()) +} + +fn xlsx_save(cfg: &Cfg) -> R { + let n = cfg.rows; + let mut s = model(n)?; + let (times, _) = time(cfg, &mut s, |s, _| save_bytes(s).map(drop), |_, _| Ok(()))?; + Ok(Outcome { name: "xlsx.save", what: format!("save the {n}-row model as XLSX"), times, cells: 0 }) +} + +fn xlsx_open(cfg: &Cfg) -> R { + let n = cfg.rows; + let mut s = model(n)?; + let b64 = save_bytes(&mut s)?; + let last = ((n - 1) % 97) as f64 * 6.0; + let (times, cells) = time( + cfg, + &mut s, + |s, _| exec(s, "file.open", json!({"name": "bench.xlsx", "base64": b64})).map(drop), + |s, _| expect(s, &format!("D{n}"), last), + )?; + Ok(Outcome { name: "xlsx.open", what: format!("open the {n}-row model from XLSX (with full recalc)"), times, cells }) +} diff --git a/crates/engine/examples/perf.rs b/crates/engine/examples/perf.rs deleted file mode 100644 index 61ecf5626..000000000 --- a/crates/engine/examples/perf.rs +++ /dev/null @@ -1,60 +0,0 @@ -//! Performance scenarios on large workbooks (release build): -//! `cargo run --release -p gridcraft-engine --example perf -- [rows]` - -#![allow(clippy::disallowed_methods)] // a native-only benchmark: wasm never runs it - -use std::time::Instant; - -use gridcraft_engine::Session; -use serde_json::json; - -fn main() { - let rows: usize = std::env::args().nth(1).and_then(|s| s.parse().ok()).unwrap_or(100_000); - let mut s = Session::new(); - s.new_workbook(); - // Data: rows × 6 numeric columns, built through range.setValues in chunks. - let t = Instant::now(); - let chunk = 10_000; - let mut r = 0; - while r < rows { - let n = chunk.min(rows - r); - let vals: Vec = (0..n) - .map(|i| json!([format!("Item {}", r + i), (r + i) as f64, ((r + i) % 97) as f64, ((r + i) % 13) as f64 * 1.5, 1.0, 2.0])) - .collect(); - s.execute("range.setValues", json!({"range": format!("A{}", r + 1), "values": vals})).expect("set"); - r += n; - } - println!("fill {rows}x6 values: {:?}", t.elapsed()); - // Formulas: G = B*C+D, H = running sum of G (chain). - let t = Instant::now(); - s.execute("cell.set", json!({"cell": "G1", "input": "=B1*C1+D1"})).expect("g1"); - s.execute("edit.autoFill", json!({"source": "G1", "target": format!("G1:G{rows}")})).expect("fill g"); - println!("fill {rows} formulas G=B*C+D: {:?}", t.elapsed()); - let t = Instant::now(); - s.execute("cell.set", json!({"cell": "H1", "input": "=G1"})).expect("h1"); - s.execute("cell.set", json!({"cell": "H2", "input": "=H1+G2"})).expect("h2"); - s.execute("edit.autoFill", json!({"source": "H2", "target": format!("H2:H{rows}")})).expect("fill h"); - println!("fill {rows} chained running-sum formulas: {:?}", t.elapsed()); - let t = Instant::now(); - s.execute("cell.set", json!({"cell": "J1", "input": format!("=SUM(G1:G{rows})")})).expect("sum"); - s.execute("cell.set", json!({"cell": "J2", "input": "=SUMIFS(B:B,C:C,\">50\")"})).expect("sumifs"); - s.execute("cell.set", json!({"cell": "J3", "input": "=XLOOKUP(\"Item 77777\",A:A,B:B)"})).expect("xlookup"); - println!("aggregate formulas: {:?}", t.elapsed()); - // Edit at the top: everything below recalculates. - let t = Instant::now(); - s.execute("cell.set", json!({"cell": "B1", "input": "5"})).expect("edit"); - println!("edit B1 → recalc {} cells: {:?}", s.doc().map(|d| d.calc.last_recalc_cells).unwrap_or(0), t.elapsed()); - let t = Instant::now(); - s.execute("data.sortDescending", json!({"range": format!("A1:F{rows}"), "column": "C"})).expect("sort"); - println!("sort {rows} rows: {:?}", t.elapsed()); - let t = Instant::now(); - let r = s.execute("file.saveBytes", json!({"format": "xlsx"})).expect("save"); - let b64 = r["base64"].as_str().unwrap_or("").to_string(); - println!("save xlsx ({} bytes): {:?}", r["bytes"], t.elapsed()); - let t = Instant::now(); - s.execute("file.open", json!({"name": "big.xlsx", "base64": b64})).expect("open"); - println!("open xlsx + full recalc: {:?}", t.elapsed()); - let t = Instant::now(); - s.execute("edit.undo", json!({})).ok(); - println!("undo: {:?}", t.elapsed()); -} diff --git a/docs/egui-037-migration-plan.md b/docs/egui-037-migration-plan.md index 2f59c863f..12199a1cc 100644 --- a/docs/egui-037-migration-plan.md +++ b/docs/egui-037-migration-plan.md @@ -1013,8 +1013,8 @@ Fixtures are built in code and never committed as large binaries: - `Session::new()` + `file.new {sample: "sales"|"budget"|"grades"}` for the three samples. - A 100,000-row × 6-column workbook built through `range.setValues` in 10,000-row chunks, plus - formulas `G=B*C+D`, a chained running sum, `SUM`/`SUMIFS`/`XLOOKUP` and a sort, mirroring - `crates/engine/examples/perf.rs`. + formulas `G=B*C+D`, a chained running sum, `SUM`/`SUMIFS`/`XLOOKUP` and a sort, like the + workloads in `crates/engine/examples/bench.rs` (`cargo xtask bench`). - A two-sheet workbook with a formula being pointed across sheets, a right-to-left sheet, frozen panes, merged cells, a table, a chart, comments/notes, validation and a cell picture. - Password-protected and corrupt-font fixtures through `gridcraft-xlsx`'s `test-support` feature diff --git a/docs/gaps.md b/docs/gaps.md index d2c89d86a..842136e36 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Gaps -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-11-02 · **Change:** minor (§17: equations; earlier: §13: Solver Sensitivity report; earlier: §4: chart resizing is no longer a gap; earlier: §13: Analysis ToolPak has all 19 tools; earlier: §4: chart data tables implemented with XLSX round-trip and print/PDF; earlier: §13: Solver Options; earlier: §13: Solver Load/Save Model and Reset All) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-11-09 · **Change:** minor (`cargo xtask bench`: measured recalc baselines; earlier: §17: equations; earlier: §13: Solver Sensitivity report; earlier: §4: chart resizing is no longer a gap; earlier: §13: Analysis ToolPak has all 19 tools; earlier: §4: chart data tables implemented with XLSX round-trip and print/PDF; earlier: §13: Solver Options; earlier: §13: Solver Load/Save Model and Reset All) · **Target:** Microsoft Excel (Microsoft 365) Every known shortfall against Excel, one entry each, ranked by user impact (what blocks someone using GridCraft for real work first). This is the work list: pick from the top, check the open @@ -26,9 +26,11 @@ No alpha blockers: every core workflow passes the [alpha gate](roadmap.md#alpha- - **Estimate:** 15–25 h, plus owner time in Excel. [file-format-parity.md](file-format-parity.md) ### 2. Large workbooks don't open; recalc is single-threaded (P, H) -- **Missing:** streaming read for ~100 MB XLSX; multi-threaded recalc; 100k-dependent edit under - 100 ms (was ~0.4 s); whole-sheet operations bounded. -- **Evidence:** #175; no threading in `crates/calc`; PR #152 (whole-sheet Fill/Remove Duplicates +- **Missing:** streaming read for ~100 MB XLSX; multi-threaded recalc; faster whole-column + SUMIFS and lookups (100 of either over 100k rows take 0.16–0.59 s per edit); whole-sheet + operations bounded. A 100k-dependent edit measures 90–106 ms, around the 100 ms target + (`cargo xtask bench`). +- **Evidence:** #175; `cargo xtask bench`; no threading in `crates/calc`; PR #152 (whole-sheet Fill/Remove Duplicates out of memory), PR #151 (current region past 10,000 rows). - **Impact:** finance and data users routinely have big workbooks. - **Estimate:** 25–40 h. [target-app-parity.md](target-app-parity.md#performance), [hardware-parity.md](hardware-parity.md) @@ -202,6 +204,7 @@ No alpha blockers: every core workflow passes the [alpha gate](roadmap.md#alpha- | 2026-10-31 | minor | §13: Solver Load/Save Model (`data.solverSaveModel`, `data.solverLoadModel`) and Reset All (`data.solverReset`), with buttons in the Solver Parameters dialog | | 2026-10-26 | minor | Arabic shared bidi display/editing and persisted cell reading direction; other script and PDF work remain separate | | 2026-10-26 | minor | §5: Arabic text renders in cells and the interface: a system Arabic face (Tahoma, Arial or Segoe UI on Windows, Geeza Pro on macOS, Noto Sans or Naskh Arabic on Linux) joins the script fallbacks, fontconfig's Arabic choice, when it has Arabic letters, leads the Arabic fallbacks and draws Arabic wherever the UI or cell font has none, such as the default cell fonts (part of #229; a UI font with Arabic of its own, such as DejaVu Sans in the Flatpak, still draws interface Arabic, left for a follow-up), and craft-fonts' Noto Sans Arabic is a required face allowed on the web. Workbook fonts without Arabic keep every script fallback. Shaping and bidirectional layout remain separate work | +| 2026-10-26 | minor | #2: measured with `cargo xtask bench`; 100k-dependent edit around 100 ms, whole-column SUMIFS/XLOOKUP added | | 2026-10-24 | minor | §8: PivotTable Enable show details: `pivot.options` `enableShowDetails` turns Show Details off for a PivotTable, read and written in XLSX as `enableDrill` | | 2026-10-23 | minor | §8: PivotTable Show Details: double-clicking a value cell lists its source records on a new sheet (#2364) | | 2026-10-22 | minor | §20: a native window per workbook window, New Window on the same workbook and Arrange All on the desktop; left: Arrange All across monitors, the web's one window, pen pressure (estimate 10–14 h → 5–8 h) | diff --git a/docs/roadmap.md b/docs/roadmap.md index 458af936a..8c0d279db 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -1,6 +1,6 @@ # Roadmap detail -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-23 · **Change:** minor (test command) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-26 · **Change:** minor (`cargo xtask bench`: measured recalc baselines) · **Target:** Microsoft Excel (Microsoft 365) Forward-looking plan. The one-page summary is [`ROADMAP.md`](../ROADMAP.md); the ranked work list is [`gaps.md`](gaps.md); numbers and method are in @@ -107,12 +107,14 @@ to check our files, and Windows machines with the reported GPU drivers. - Function coverage: the script method in [function-parity.md](function-parity.md). A `cargo xtask functions` that diffs the registry against a checked-in Excel name list would make it a CI number (1–2 h). -- No `cargo xtask scorecard` or `bench` yet. +- `cargo xtask bench`: recalc, entry, sort and XLSX timings on fixed in-code workbooks, compared + with a local baseline (`--save`). No `cargo xtask scorecard` yet. ## Revision history | Date | Change | Summary | |---|---|---| +| 2026-10-26 | minor | Measuring: `cargo xtask bench` | | 2026-10-23 | minor | Measuring: tests run through `cargo xtask test` | | 2026-10-11 | minor | PivotTable calculated fields landed | | 2026-10-10 | minor | Current focus and M15 updated for the 10-10 merges | diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index a2243bc92..2fe722694 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1,6 +1,6 @@ # Target-app parity: GridCraft vs Microsoft Excel -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-11-02 · **Change:** minor (form controls; catalog 281 / 296; earlier: insert objects: equations; earlier: what-if and analysis: Solver Sensitivity report; no score change; earlier: chart resizing no longer listed as missing; no score change; earlier: what-if and analysis: Analysis ToolPak has all 19 tools; earlier: page layout and print: Page Setup's page order; no score change; earlier: charts: data tables with XLSX round-trip and print/PDF; no score change; earlier: what-if and analysis: Solver Options; the Insert Function dialog is named as in Excel; no score change; earlier: Solver Load/Save Model and Reset All; automation: macro interface; catalog count 276 / 292, as `cargo xtask parity` measures; automation: typed macros in the engine; several windows: a native window per workbook window, New Window on the same workbook, Arrange All; no score change) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-11-09 · **Change:** minor (`cargo xtask bench`: measured recalc baselines; earlier: form controls; catalog 281 / 296; earlier: insert objects: equations; earlier: what-if and analysis: Solver Sensitivity report; no score change; earlier: chart resizing no longer listed as missing; no score change; earlier: what-if and analysis: Analysis ToolPak has all 19 tools; earlier: page layout and print: Page Setup's page order; no score change; earlier: charts: data tables with XLSX round-trip and print/PDF; no score change; earlier: what-if and analysis: Solver Options; the Insert Function dialog is named as in Excel; no score change; earlier: Solver Load/Save Model and Reset All; automation: macro interface; catalog count 276 / 292, as `cargo xtask parity` measures; automation: typed macros in the engine; several windows: a native window per workbook window, New Window on the same workbook, Arrange All; no score change) · **Target:** Microsoft Excel (Microsoft 365) This is the authoritative parity assessment. [`ROADMAP.md`](../ROADMAP.md) summarizes it, [`gaps.md`](gaps.md) lists every shortfall, and the area checklists hold the detail: @@ -203,14 +203,26 @@ Excel recalculates on every core (multi-threaded recalculation), streams large f |---|---|---|---| | Sheet size limits | 1,048,576 × 16,384 | same | measured (`crates/core/src/addr.rs`) | | Recalc threads | 1 | all cores | measured (no threading in `crates/calc`) | -| Edit → recalc, 100k dependents | ~0.4 s (2026-10-07), target < 100 ms | tens of ms | measured on the owner's machine; #66 since made range-formula recalc linear, not re-timed | +| Edit → recalc, 100k-deep chain | 90–106 ms (target < 100 ms) | tens of ms | measured (`cargo xtask bench`, `edit.chain`) | +| Edit → recalc, 100k formulas reading one cell | 71–87 ms | tens of ms | measured (`edit.fanout`) | +| Edit → 100 whole-column SUMIFS / XLOOKUP over 100k rows | 420–590 ms / 160–250 ms | not measured | measured (`edit.sumifs`, `edit.lookup`) | +| Calculate Now, 300k formulas | 395–455 ms | not measured | measured (`recalc.full`) | +| Save the 100k-row model (300k formulas) as XLSX | ~530 ms | not measured | measured (`xlsx.save`) | +| Reopen that file | drops the last ~9,000 rows (element cap from #2413; fix in PR #2527, then ~1.3 s with full recalc) | opens | measured (`xlsx.open`) | | Grid scrolling | virtualised, O(log n) geometry with custom row heights | smooth | measured in code | | Open a ~100 MB XLSX | fails (#175) | opens | user report | | Whole-sheet operations | Fill and Remove Duplicates on a whole-sheet selection run out of memory or time (fix in PR #152) | fine | open PR | Work: streaming XLSX reader with shared-string and style dedup, multi-threaded recalc of independent chains (native only; WASM stays single-threaded), whole-column reference -clamping, a `cargo xtask bench` with fixed workbooks so the numbers become measured. +clamping, and faster whole-column SUMIFS and lookups (each formula rescans the column on every +edit). + +Benchmarks: `cargo xtask bench` builds ten fixed workbooks in code, times each operation through +`Session::execute` (median of 7 after a warm-up, results checked) and compares with a baseline +saved by `--save`. The numbers above are ranges over several runs on an Apple M2 Pro, release +build, 100,000 rows, 2026-10-26; recalculation is single-threaded, so core count doesn't change +them. ## Platforms @@ -327,7 +339,6 @@ The inventory carried over from the 2026-10-07 ROADMAP.md, updated for what land - Pixel comparison against Excel screenshots (planned; screenshots stay in `plan/excel/`). - Function results against Excel-computed oracles (a corpus repo is the planned home). -- Recalc and open-time benchmarks on fixed workbooks (no `cargo xtask bench` yet). - Whether Excel opens our XLSX output without repair, across features. ## Revision history @@ -344,6 +355,7 @@ The inventory carried over from the 2026-10-07 ROADMAP.md, updated for what land | 2026-10-31 | minor | The Insert Function dialog is named as in Excel (it was called Formula Builder); no score change | | 2026-10-31 | minor | What-if and analysis: Solver Options (no score change) | | 2026-10-31 | minor | What-if and analysis: Solver Load/Save Model and Reset All; Forecast Sheet moved from Missing to the list of tools, as gaps.md §13 records (no score change) | +| 2026-10-26 | minor | Performance: `cargo xtask bench` measures recalc, entry, sort and XLSX save/open on fixed in-code workbooks; the 100k-dependent edit is 90–106 ms (was ~0.4 s, an old timing), around the 100 ms target for simple formulas; a 100k-row model doesn't reopen whole until PR #2527 lands (no score change) | | 2026-10-25 | minor | Automation: macro interface (Automate › Macros, the Macros and Record Macro dialogs, the step editor, shortcuts) (no score change) | | 2026-10-25 | trivial | Catalog count 276 / 292 in the summary table and the method, as `cargo xtask parity` measures (no score change) | | 2026-10-22 | minor | Several windows on the desktop: a native window per workbook window, New Window on the same workbook, Switch Windows and Arrange All; "one window" removed from UI/UX fidelity, Hardware and the interaction-fidelity discount; no score change | diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index c40c97697..7e62711d9 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -4,7 +4,7 @@ version.workspace = true edition.workspace = true license.workspace = true publish = false -description = "GridCraft workspace tooling: cargo xtask ci | layers | assets | wasm | parity | i18n-report | version | stats | ico" +description = "GridCraft workspace tooling: cargo xtask ci | layers | assets | wasm | parity | i18n-report | bench | version | stats | ico" [lints] workspace = true diff --git a/xtask/src/bench.rs b/xtask/src/bench.rs new file mode 100644 index 000000000..42f2d5439 --- /dev/null +++ b/xtask/src/bench.rs @@ -0,0 +1,103 @@ +//! `cargo xtask bench`: runs the calculation benchmarks (`crates/engine/examples/bench.rs`) in a +//! release build, writes the results to `/bench/latest.json` and compares them with a +//! baseline (`/bench/baseline.json` unless `--baseline PATH`). `--save` makes this run +//! the baseline. Timings are machine-specific, so baselines stay in the target directory; the +//! reference numbers in docs/target-app-parity.md name the machine they came from. + +use std::path::{Path, PathBuf}; + +use serde_json::Value; + +/// Flags passed through to the bench binary (the others are xtask's own). +const PASS: [&str; 3] = ["--rows", "--reps", "--filter"]; + +pub fn run(root: &Path, args: &[&str]) -> Result<(), String> { + let target = std::env::var_os("CARGO_TARGET_DIR").map(PathBuf::from).unwrap_or_else(|| root.join("target")); + let dir = target.join("bench"); + std::fs::create_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?; + let latest = dir.join("latest.json"); + let mut baseline = dir.join("baseline.json"); + let mut save = false; + let mut pass: Vec = Vec::new(); + let mut it = args.iter(); + while let Some(&a) = it.next() { + match a { + "--save" => save = true, + "--quick" => pass.push(a.into()), + "--baseline" => baseline = it.next().map(PathBuf::from).ok_or("--baseline needs a path")?, + _ if PASS.contains(&a) => { + let v = it.next().ok_or_else(|| format!("{a} needs a value"))?; + pass.extend([a.to_string(), v.to_string()]); + } + other => return Err(format!("bench: unknown flag `{other}`")), + } + } + let mut cmd = crate::cargo(); + cmd.args(["run", "--release", "--quiet", "-p", "gridcraft-engine", "--example", "bench", "--"]).args(&pass).arg("--json").arg(&latest); + // A stale file from an earlier run must not pass for this one's results. + let _ = std::fs::remove_file(&latest); + // The bench exits non-zero when a scenario fails but still writes the others' results: + // compare those, then report the failure. + let status = crate::run(cmd, "cargo run --release -p gridcraft-engine --example bench"); + let now = match read(&latest) { + Ok(v) => v, + Err(e) => return status.and(Err(e)), + }; + if save && status.is_err() { + return status.map_err(|e| format!("{e}\nnot saved as the baseline: a scenario failed")); + } + if save { + std::fs::copy(&latest, &baseline).map_err(|e| format!("{}: {e}", baseline.display()))?; + println!("\nSaved as the baseline: {}", baseline.display()); + return Ok(()); + } + match read(&baseline) { + Ok(base) => compare(&base, &now), + Err(_) => println!("\nNo baseline at {} (run `cargo xtask bench --save` to make one).", baseline.display()), + } + status +} + +fn read(path: &Path) -> Result { + let text = std::fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?; + serde_json::from_str(&text).map_err(|e| format!("{}: {e}", path.display())) +} + +fn results(v: &Value) -> Vec<(String, f64)> { + v.get("results") + .and_then(Value::as_array) + .map(|a| a.iter().filter_map(|r| Some((r.get("name")?.as_str()?.to_string(), r.get("median_ms")?.as_f64()?))).collect()) + .unwrap_or_default() +} + +/// Prints the median change per scenario; a different row count makes the numbers incomparable. +fn compare(base: &Value, now: &Value) { + if base.get("rows") != now.get("rows") { + println!("\nBaseline was run with {} rows, this run with {}: not compared.", base["rows"], now["rows"]); + return; + } + let old = results(base); + println!("\nAgainst the baseline (median):"); + for (name, ms) in results(now) { + match old.iter().find(|(n, _)| *n == name) { + Some((_, was)) if *was > 0.0 => { + let pct = (ms - was) / was * 100.0; + println!(" {name:<16} {was:>10.2} ms -> {ms:>10.2} ms {pct:+6.1}%"); + } + _ => println!(" {name:<16} {:>13} -> {ms:>10.2} ms", "new"), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn reads_results() { + let v = json!({"rows": 10, "results": [{"name": "edit.chain", "median_ms": 1.5}, {"name": "bad"}]}); + assert_eq!(results(&v), vec![("edit.chain".to_string(), 1.5)]); + assert!(results(&json!({})).is_empty()); + } +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index fe54b0f4e..26e3a7770 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -6,6 +6,7 @@ #![cfg_attr(test, allow(clippy::unwrap_used, clippy::expect_used, clippy::panic))] mod assets; +mod bench; mod i18n_report; mod ico; mod keytips; @@ -36,6 +37,9 @@ commands: regenerate crates/ui-egui/src/keytips/generated.rs from keytips.json (--check: fail if stale) version [set X.Y.Z[-pre]] print or set the workspace version ([workspace.package] in Cargo.toml) + bench [--quick] [--rows N] [--reps N] [--filter NAME] [--save] [--baseline PATH] + release-build calculation benchmarks on fixed in-code workbooks; compares + with /bench/baseline.json (--save: make this run the baseline) stats [--exact] count tests and lines per crate (--exact: ask the test harness via `-- --list`) ico ... pack square PNGs (<= 256 px) into a Windows .ico (see packaging/icons.sh) @@ -51,6 +55,7 @@ fn main() -> ExitCode { Some("wasm") => cmd_wasm(), Some("test") => cmd_test().and_then(|()| cmd_doctest()), Some("ci") => cmd_ci(), + Some("bench") => bench::run(&root(), &rest), Some("stats") => stats::run(&root(), rest.contains(&"--exact")), Some("parity") => parity::run(&root(), &rest), Some("keytips") => keytips::run(&root(), &rest), From 815627264fbfd20fcae4d7a048c491237d51a436 Mon Sep 17 00:00:00 2001 From: nihilist-narwhal <240798405+nihilist-narwhal@users.noreply.github.com> Date: Mon, 26 Oct 2026 11:06:29 -0500 Subject: [PATCH 2/8] bench: address review (empty filter, row-count-exact checks, fresh formula entry) - A --filter that selects no scenario is an error, so --save can't write an empty baseline. - edit.sumifs and edit.lookup derive their expected values from the rows actually generated, so any --rows passes. - edit.lookup edits the key of a checked lookup (XLOOKUP falls back to 0 when it is gone), so a lookup that stops recalculating fails the check. - enter.formulas clears B1:Bn untimed before each run, so it times entry into empty cells, not an overwrite of the previous run's formulas. - xtask: a relative CARGO_TARGET_DIR resolves against the workspace root, as cargo does for the bench; an existing baseline that doesn't parse is an error instead of "no baseline". Co-Authored-By: Claude Opus 5.5 --- crates/engine/examples/bench.rs | 45 +++++++++++++++++++++++++-------- xtask/src/bench.rs | 12 ++++++--- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/crates/engine/examples/bench.rs b/crates/engine/examples/bench.rs index c07872ea9..731d5c683 100644 --- a/crates/engine/examples/bench.rs +++ b/crates/engine/examples/bench.rs @@ -68,10 +68,12 @@ fn run() -> R<()> { // A failed scenario is reported and the others still run, so one regression doesn't hide // the numbers for everything else. let mut failed = Vec::new(); - for (name, f) in scenarios { - if cfg.filter.as_deref().is_some_and(|p| !name.contains(p)) { - continue; - } + let selected: Vec<_> = scenarios.into_iter().filter(|(name, _)| cfg.filter.as_deref().is_none_or(|p| name.contains(p))).collect(); + if selected.is_empty() { + // An empty run would otherwise save as a baseline with nothing in it. + return Err(format!("--filter {} matches no scenario", cfg.filter.as_deref().unwrap_or(""))); + } + for (name, f) in selected { let o = match f(&cfg) { Ok(o) => o, Err(e) => { @@ -149,12 +151,24 @@ fn recalc_cells(s: &Session) -> usize { fn time( cfg: &Cfg, s: &mut Session, + op: impl FnMut(&mut Session, usize) -> R<()>, + check: impl Fn(&Session, usize) -> R<()>, +) -> R<(Vec, usize)> { + time_prepared(cfg, s, |_| Ok(()), op, check) +} + +/// [`time`] with `prep` run (untimed) before each call, to put back the state `op` starts from. +fn time_prepared( + cfg: &Cfg, + s: &mut Session, + mut prep: impl FnMut(&mut Session) -> R<()>, mut op: impl FnMut(&mut Session, usize) -> R<()>, check: impl Fn(&Session, usize) -> R<()>, ) -> R<(Vec, usize)> { let mut times = Vec::with_capacity(cfg.reps); let mut cells = 0; for rep in 0..=cfg.reps { + prep(s)?; let t = Instant::now(); op(s, rep)?; let dt = t.elapsed(); @@ -226,7 +240,8 @@ fn edit_sumifs(cfg: &Cfg) -> R { let mut s = session()?; fill(&mut s, "A", n, |i| json!([1, (i % 100) as f64]))?; fill(&mut s, "D", 100, |k| json!([format!("=SUMIFS(A:A,B:B,{k})")]))?; - let per = (n / 100) as f64; + // Rows whose category is 0 (i % 100 == 0), A1 among them. + let per = n.div_ceil(100) as f64; let (times, cells) = time( cfg, &mut s, @@ -242,13 +257,21 @@ fn edit_lookup(cfg: &Cfg) -> R { let mut s = session()?; fill(&mut s, "A", n, |i| json!([(i + 1) as f64, (2 * (i + 1)) as f64]))?; let step = n / 100; - fill(&mut s, "D", 100, |k| json!([format!("=XLOOKUP({},A:A,B:B)", (k + 1) * step)]))?; + fill(&mut s, "D", 100, |k| json!([format!("=XLOOKUP({},A:A,B:B,0)", (k + 1) * step)]))?; + let last = (2 * 100 * step) as f64; let (times, cells) = time( cfg, &mut s, - // A1's key alternates between 1 and -1, so A:A changes but the lookups' answers don't. - |s, r| exec(s, "cell.set", json!({"cell": "A1", "input": if r % 2 == 0 { "-1" } else { "1" }})).map(drop), - |s, _| expect(s, "D100", (2 * n) as f64), + // D1 looks up the key in A{step}: removing it (-step) makes D1 fall back to 0, putting it + // back finds it again. Every lookup reads A:A, so all 100 recalculate either way. + |s, r| { + let key = if r % 2 == 0 { format!("-{step}") } else { step.to_string() }; + exec(s, "cell.set", json!({"cell": format!("A{step}"), "input": key})).map(drop) + }, + |s, r| { + expect(s, "D1", if r % 2 == 0 { 0.0 } else { (2 * step) as f64 })?; + expect(s, "D100", last) + }, )?; Ok(Outcome { name: "edit.lookup", what: format!("edit a key under 100 XLOOKUPs over {n} rows"), times, cells }) } @@ -259,9 +282,11 @@ fn enter_formulas(cfg: &Cfg) -> R { let mut s = session()?; fill(&mut s, "A", n, |i| json!([(i % 97) as f64]))?; let vals: Vec = (0..n).map(|i| json!([format!("=A{}*2+1", i + 1)])).collect(); - let (times, cells) = time( + let (times, cells) = time_prepared( cfg, &mut s, + // Each run enters the formulas into empty cells, not over the previous run's. + |s| exec(s, "edit.clearAll", json!({"range": format!("B1:B{n}")})).map(drop), |s, _| exec(s, "range.setValues", json!({"range": "B1", "values": vals})).map(drop), |s, _| expect(s, &format!("B{n}"), ((n - 1) % 97) as f64 * 2.0 + 1.0), )?; diff --git a/xtask/src/bench.rs b/xtask/src/bench.rs index 42f2d5439..4030f8e19 100644 --- a/xtask/src/bench.rs +++ b/xtask/src/bench.rs @@ -12,7 +12,9 @@ use serde_json::Value; const PASS: [&str; 3] = ["--rows", "--reps", "--filter"]; pub fn run(root: &Path, args: &[&str]) -> Result<(), String> { - let target = std::env::var_os("CARGO_TARGET_DIR").map(PathBuf::from).unwrap_or_else(|| root.join("target")); + // Cargo resolves a relative CARGO_TARGET_DIR against the directory it runs in, which for the + // bench is the workspace root; resolve it the same way here. + let target = std::env::var_os("CARGO_TARGET_DIR").map(|d| root.join(d)).unwrap_or_else(|| root.join("target")); let dir = target.join("bench"); std::fs::create_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?; let latest = dir.join("latest.json"); @@ -51,9 +53,11 @@ pub fn run(root: &Path, args: &[&str]) -> Result<(), String> { println!("\nSaved as the baseline: {}", baseline.display()); return Ok(()); } - match read(&baseline) { - Ok(base) => compare(&base, &now), - Err(_) => println!("\nNo baseline at {} (run `cargo xtask bench --save` to make one).", baseline.display()), + if baseline.exists() { + // A baseline that is there but unreadable is an error, not a missing one. + compare(&read(&baseline)?, &now); + } else { + println!("\nNo baseline at {} (run `cargo xtask bench --save` to make one).", baseline.display()); } status } From a4aee78391ad310b66ba539931198c9276bf2c00 Mon Sep 17 00:00:00 2001 From: nihilist-narwhal <240798405+nihilist-narwhal@users.noreply.github.com> Date: Thu, 29 Oct 2026 08:39:25 -0500 Subject: [PATCH 3/8] M-perf: bench: Scenario type alias (clippy type_complexity) Co-Authored-By: Claude Opus 5.5 --- crates/engine/examples/bench.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/engine/examples/bench.rs b/crates/engine/examples/bench.rs index 731d5c683..2011f1ce1 100644 --- a/crates/engine/examples/bench.rs +++ b/crates/engine/examples/bench.rs @@ -30,6 +30,8 @@ struct Outcome { } type R = Result; +/// A scenario: its name and the function that runs it. +type Scenario = (&'static str, fn(&Cfg) -> R); fn main() -> std::process::ExitCode { match run() { @@ -50,7 +52,7 @@ fn run() -> R<()> { reps: flag("--reps").and_then(|s| s.parse().ok()).unwrap_or(if quick { 3 } else { 7 }).max(1), filter: flag("--filter"), }; - let scenarios: [(&'static str, fn(&Cfg) -> R); 10] = [ + let scenarios: [Scenario; 10] = [ ("edit.chain", edit_chain), ("edit.fanout", edit_fanout), ("edit.sliding", edit_sliding), From f17f730887c21e3c45e93cb2778f4eb0410f1fb4 Mon Sep 17 00:00:00 2001 From: nihilist-narwhal <240798405+nihilist-narwhal@users.noreply.github.com> Date: Thu, 29 Oct 2026 08:51:26 -0500 Subject: [PATCH 4/8] M-perf: bench: median of an even count averages the middle two Co-Authored-By: Claude Opus 5.5 --- crates/engine/examples/bench.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/engine/examples/bench.rs b/crates/engine/examples/bench.rs index 2011f1ce1..9c8f96aab 100644 --- a/crates/engine/examples/bench.rs +++ b/crates/engine/examples/bench.rs @@ -100,7 +100,13 @@ fn run() -> R<()> { fn stats(times: &[Duration]) -> (f64, f64) { let mut v: Vec = times.iter().map(|d| d.as_secs_f64() * 1000.0).collect(); v.sort_by(f64::total_cmp); - let med = v.get(v.len() / 2).copied().unwrap_or(0.0); + // An even count averages the two middle samples. + let mid = v.len() / 2; + let med = match (v.len() % 2, v.get(mid.wrapping_sub(1)), v.get(mid)) { + (0, Some(a), Some(b)) => (a + b) / 2.0, + (_, _, Some(m)) => *m, + _ => 0.0, + }; (med, v.first().copied().unwrap_or(0.0)) } From d56e3638fdd41289e56573c2384cfcb617834de4 Mon Sep 17 00:00:00 2001 From: nihilist-narwhal <240798405+nihilist-narwhal@users.noreply.github.com> Date: Thu, 29 Oct 2026 09:05:22 -0500 Subject: [PATCH 5/8] M-perf: bench: recalc.full recalculates a stale result; malformed --rows/--reps is an error Co-Authored-By: Claude Opus 5.5 --- crates/engine/examples/bench.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/crates/engine/examples/bench.rs b/crates/engine/examples/bench.rs index 9c8f96aab..e17d94975 100644 --- a/crates/engine/examples/bench.rs +++ b/crates/engine/examples/bench.rs @@ -48,8 +48,8 @@ fn run() -> R<()> { let flag = |name: &str| args.iter().position(|a| a == name).and_then(|i| args.get(i + 1)).cloned(); let quick = args.iter().any(|a| a == "--quick"); let cfg = Cfg { - rows: flag("--rows").and_then(|s| s.parse().ok()).unwrap_or(if quick { 10_000 } else { 100_000 }).max(200), - reps: flag("--reps").and_then(|s| s.parse().ok()).unwrap_or(if quick { 3 } else { 7 }).max(1), + rows: count(flag("--rows"), "--rows")?.unwrap_or(if quick { 10_000 } else { 100_000 }).max(200), + reps: count(flag("--reps"), "--reps")?.unwrap_or(if quick { 3 } else { 7 }).max(1), filter: flag("--filter"), }; let scenarios: [Scenario; 10] = [ @@ -118,6 +118,11 @@ fn exec(s: &mut Session, cmd: &str, p: Json) -> R { s.execute(cmd, p).map_err(|e| format!("{cmd}: {e}")) } +/// A count flag's value; one that isn't a number is an error, not the default. +fn count(v: Option, name: &str) -> R> { + v.map(|s| s.parse().map_err(|_| format!("{name}: `{s}` is not a count"))).transpose() +} + fn session() -> R { let mut s = Session::new(); s.new_workbook(); @@ -311,8 +316,20 @@ fn model(n: usize) -> R { fn recalc_full(cfg: &Cfg) -> R { let n = cfg.rows; let mut s = model(n)?; - let last = ((n - 1) % 97) as f64 * 6.0; - let (times, cells) = time(cfg, &mut s, |s, _| exec(s, "formulas.calculateNow", json!({})).map(drop), |s, _| expect(s, &format!("D{n}"), last))?; + // Manual calculation, and A[n] changed (untimed) before each run, so D[n] is stale until + // Calculate Now recalculates: a Calculate Now that did nothing fails the check. + exec(&mut s, "formulas.calculationOptions", json!({"mode": "manual"}))?; + let mut rep = 0; + let (times, cells) = time_prepared( + cfg, + &mut s, + |s| { + rep += 1; + exec(s, "cell.set", json!({"cell": format!("A{n}"), "input": (100 + rep).to_string()})).map(drop) + }, + |s, _| exec(s, "formulas.calculateNow", json!({})).map(drop), + |s, r| expect(s, &format!("D{n}"), (101 + r) as f64 * 6.0), + )?; Ok(Outcome { name: "recalc.full", what: format!("Calculate Now, {} formulas (3 per row)", 3 * n), times, cells }) } From 35996c6e2ea6e0c0ebe25e1784b32ef6247a492b Mon Sep 17 00:00:00 2001 From: David Ardila Date: Mon, 9 Nov 2026 21:44:01 -0600 Subject: [PATCH 6/8] M-perf: bench: the egui 0.37 plan's Recalc scene points at cargo xtask bench, not the removed perf example Co-Authored-By: Claude Opus 5.5 --- docs/egui-037-migration-plan.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/egui-037-migration-plan.md b/docs/egui-037-migration-plan.md index 12199a1cc..39a76f355 100644 --- a/docs/egui-037-migration-plan.md +++ b/docs/egui-037-migration-plan.md @@ -1111,7 +1111,7 @@ Never report compile duration as UI performance. | Startup | fresh process → first painted frame / control channel ready | wall time, split font setup, wgpu renderer creation, first frame | | Grid scroll | `sales`/100k-row synthetic workbook, 1,000 frames of vertical/horizontal scroll | frame distribution, `grid_ms`, tessellation time, vertices/indices, uploads | | Cell edit | type/commit 1,000 cells via control channel | command latency, frame CPU, undo memory | -| Recalc | `crates/engine/examples/perf` on the same workbook | engine-only latency, recalc cell count (not a UI number) | +| Recalc | `cargo xtask bench` (`crates/engine/examples/bench.rs`, fixed in-code workbooks; `recalc.full` and the edit scenarios) | engine-only latency, recalc cell count (not a UI number) | | Dialog/ribbon | open/close Format Cells, switch tabs, type filter | CPU frame time, paint counts | | Text tessellation | fixed 120-ASCII-label scene + mixed color-glyph row | per-tessellate time/instructions; reproduce #8731 | | Fonts | 20 cold starts with the same font set | discovery/build/set_fonts time, peak RSS | From f32fefddcf6961da5bc101090dfac4154542e006 Mon Sep 17 00:00:00 2001 From: nihilist-narwhal <240798405+nihilist-narwhal@users.noreply.github.com> Date: Mon, 2 Nov 2026 07:34:22 -0600 Subject: [PATCH 7/8] Tables never hold merged cells, which Excel removes the table for on open A table with a merged cell in it opened in Excel with the repair prompt, and Excel removed the table and its AutoFilter ("Removed Feature: Table from /xl/tables/table1.xml"). GridCraft let merged cells into a table four ways; each now does what Excel does: - Merge Cells, Merge & Center and Merge Across refuse a range that touches a table ("You can't merge cells in a table."). Unmerging is still allowed, so a file that has one can be fixed. - Insert Table / Format as Table and Resize Table unmerge the merged cells the table covers (observed: Excel creates the table and A2:B2 is no longer merged). - Paste brings merged areas along, except into a table, where the cells arrive unmerged. The command-sequence fuzzer checks the invariant (it found the paste case, seed 230). --- crates/engine/src/cmd/edit/paste_special.rs | 5 ++ crates/engine/src/cmd/extra/data.rs | 2 + crates/engine/src/cmd/format.rs | 5 ++ crates/engine/src/cmd/insert.rs | 7 +++ crates/engine/src/tests.rs | 60 +++++++++++++++++++ .../engine/tests/it/command_sequence_fuzz.rs | 6 +- crates/engine/tests/it/sheet_copy_spill.rs | 2 +- 7 files changed, 85 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/edit/paste_special.rs b/crates/engine/src/cmd/edit/paste_special.rs index c90dd9ec8..0c2f17a3d 100644 --- a/crates/engine/src/cmd/edit/paste_special.rs +++ b/crates/engine/src/cmd/edit/paste_special.rs @@ -361,6 +361,11 @@ pub(super) fn paste_special(s: &mut Session, p: &Json) -> Result { if let (Some(a), Some(b)) = (m.start.offset(dr, dc), m.end.offset(dr, dc)) { let nm = RangeRef::new(a, b); let sh = cx.sheet_mut(dest_sheet)?; + // A table can't hold merged cells (Excel removes the table on open): the + // cells are pasted there unmerged. + if sh.tables.iter().any(|t| t.range.intersects(&nm)) { + continue; + } sh.merges.retain(|x| !x.intersects(&nm)); sh.merges.push(nm); } diff --git a/crates/engine/src/cmd/extra/data.rs b/crates/engine/src/cmd/extra/data.rs index 5af7df0a8..9940ffc45 100644 --- a/crates/engine/src/cmd/extra/data.rs +++ b/crates/engine/src/cmd/extra/data.rs @@ -37,6 +37,8 @@ pub(super) fn table_resize(s: &mut Session, p: &Json) -> Result { } t.columns = cols; t.range = r; + // A table can't hold merged cells: those it now covers are unmerged, as by Insert Table. + sheet.merges.retain(|m| !m.intersects(&r)); cx.structural = true; Ok(Json::Null) }) diff --git a/crates/engine/src/cmd/format.rs b/crates/engine/src/cmd/format.rs index 90213a5e1..d512b7a0c 100644 --- a/crates/engine/src/cmd/format.rs +++ b/crates/engine/src/cmd/format.rs @@ -637,6 +637,11 @@ fn merge(s: &mut Session, p: &Json, how: &str) -> Result { let ranges = target_ranges(s, p)?; let how = if how == "center" && s.doc()?.wb.sheet(sheet).is_some_and(|sh| ranges.iter().all(|r| sh.merges.contains(r))) { "unmerge" } else { how }; + // Excel has no merged cells in a table (a file with one loses the table when Excel opens it): + // merging there is refused, unmerging is still allowed. + if how != "unmerge" && s.doc()?.wb.sheet(sheet).is_some_and(|sh| sh.tables.iter().any(|t| ranges.iter().any(|r| t.range.intersects(r)))) { + return Err(EngineError::Other("You can't merge cells in a table.".into())); + } edit(s, |cx| { let mut touched = Vec::new(); // Areas whose merging changes: a merged cell blocks a dynamic array's spill. diff --git a/crates/engine/src/cmd/insert.rs b/crates/engine/src/cmd/insert.rs index 4bd564db3..f88c6040f 100644 --- a/crates/engine/src/cmd/insert.rs +++ b/crates/engine/src/cmd/insert.rs @@ -334,6 +334,13 @@ fn insert_table(s: &mut Session, p: &Json) -> Result { RangeRef::new(r.start, CellRef::new((r.end.row + 1).min(MAX_ROWS - 1), r.end.col)) }; let sh = cx.sheet_mut(sheet)?; + // As in Excel, the table unmerges the merged cells it covers: a table can't hold them. + let merged = sh.merges.len(); + sh.merges.retain(|m| !m.intersects(&r)); + if sh.merges.len() != merged { + cx.structural = true; + } + let sh = cx.sheet_mut(sheet)?; let mut names: Vec = Vec::new(); // Lower-case names taken, and the next suffix for each header, so that thousands of equal // headers don't take cubic time. diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index f000eb9fe..5cc071d5a 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -6728,3 +6728,63 @@ fn history_charges_the_storage_of_empty_comment_replies() { // 500 replies of no text are still 500 elements. assert!(crate::sheet_text_bytes(&sheet) >= 500 * std::mem::size_of::<(String, String)>()); } + +#[test] +fn tables_never_hold_merged_cells() { + // Excel removes a table that contains a merged cell when it opens the file. Merging in a + // table is refused; making or resizing a table over merged cells unmerges them; pasting into + // a table skips the merges. + let base = || { + let mut s = s(); + s.execute("range.setValues", json!({"range": "A1:D4", "values": [["A", "B", "C", "D"], [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]})) + .unwrap(); + s + }; + let merges = |s: &Session| s.doc().unwrap().wb.active().unwrap().merges.clone(); + + let mut s = base(); + s.execute("insert.table", json!({"range": "A1:C4"})).unwrap(); + for id in ["home.mergeCells", "home.mergeCenter", "home.mergeAcross"] { + for range in ["A2:B2", "A1:B1", "C3:D3"] { + let e = s.execute(id, json!({"range": range})).unwrap_err().to_string(); + assert!(e.contains("can't merge cells in a table"), "{id} {range}: {e}"); + } + } + assert!(merges(&s).is_empty()); + s.execute("home.mergeCells", json!({"range": "E2:F2"})).unwrap(); + assert_eq!(merges(&s), vec![gridcraft_core::RangeRef::parse("E2:F2").unwrap()]); + + // A table over merged cells unmerges them; undo brings them back. + let mut s = base(); + s.execute("home.mergeCells", json!({"range": "A2:B2"})).unwrap(); + s.execute("home.mergeCells", json!({"range": "C3:D4"})).unwrap(); + s.execute("home.mergeCells", json!({"range": "F1:G1"})).unwrap(); + s.execute("insert.table", json!({"range": "A1:C4"})).unwrap(); + assert_eq!(merges(&s), vec![gridcraft_core::RangeRef::parse("F1:G1").unwrap()]); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(merges(&s).len(), 3); + s.execute("edit.redo", json!({})).unwrap(); + + // Resizing a table over merged cells unmerges them too. + let mut s = base(); + s.execute("insert.table", json!({"range": "A1:B4", "name": "T"})).unwrap(); + s.execute("home.mergeCells", json!({"range": "C2:D2"})).unwrap(); + s.execute("table.resize", json!({"table": "T", "range": "A1:C4"})).unwrap(); + assert!(merges(&s).is_empty()); + + // Merged cells pasted into a table arrive unmerged; elsewhere they stay merged. + s.execute("home.mergeCells", json!({"range": "E2:F3"})).unwrap(); + s.execute("edit.copy", json!({"range": "E2:F3"})).unwrap(); + s.execute("edit.paste", json!({"at": "A2"})).unwrap(); + s.execute("edit.paste", json!({"at": "H2"})).unwrap(); + assert_eq!(merges(&s), vec![gridcraft_core::RangeRef::parse("E2:F3").unwrap(), gridcraft_core::RangeRef::parse("H2:I3").unwrap()]); + + // The saved file has no merge inside the table. + let saved = s.execute("file.saveBytes", json!({"format": "xlsx"})).unwrap(); + let bytes = crate::io::base64_decode(saved["base64"].as_str().unwrap()).unwrap(); + let (back, _) = crate::io::open_bytes("t.xlsx", &bytes).unwrap(); + let sh = back.active().unwrap(); + assert_eq!(sh.tables.len(), 1); + assert_eq!(sh.tables[0].range, gridcraft_core::RangeRef::parse("A1:C4").unwrap()); + assert!(sh.merges.iter().all(|m| sh.tables.iter().all(|t| !t.range.intersects(m)))); +} diff --git a/crates/engine/tests/it/command_sequence_fuzz.rs b/crates/engine/tests/it/command_sequence_fuzz.rs index 89b3e9186..949f63753 100644 --- a/crates/engine/tests/it/command_sequence_fuzz.rs +++ b/crates/engine/tests/it/command_sequence_fuzz.rs @@ -12,7 +12,7 @@ //! and saving, reloading or recalculating never panics either; //! - a non-undoable command never changes cell content (it would be lost to undo); //! - the structure stays valid: active sheet in range, sheet and table names valid and unique, -//! merges and tables on the sheet and not overlapping, legacy arrays anchored at their +//! merges and tables on the sheet and not overlapping (nor a merge inside a table), legacy arrays anchored at their //! top-left cell, the selection on the sheet; //! - a full recalculation from scratch gives the values the incremental recalculation left //! (catches stale dependency graphs); @@ -408,6 +408,10 @@ fn structure_problem(wb: &Workbook, sel: &gridcraft_engine::Selection) -> Option if let Some(o) = sh.tables.iter().skip(k + 1).find(|o| overlaps(&t.range, &o.range)) { return Some(format!("{}: tables `{}` ({}) and `{}` ({}) overlap", sh.name, t.name, t.range, o.name, o.range)); } + // Excel removes a table that holds a merged cell when it opens the file. + if let Some(m) = sh.merges.iter().find(|m| overlaps(&t.range, m)) { + return Some(format!("{}: table `{}` ({}) holds merged cells {m}", sh.name, t.name, t.range)); + } } } if sel.ranges.is_empty() || !sel.active.is_valid() || sel.ranges.iter().any(|r| !r.start.is_valid() || !r.end.is_valid()) { diff --git a/crates/engine/tests/it/sheet_copy_spill.rs b/crates/engine/tests/it/sheet_copy_spill.rs index aed68baf9..481f9889e 100644 --- a/crates/engine/tests/it/sheet_copy_spill.rs +++ b/crates/engine/tests/it/sheet_copy_spill.rs @@ -17,7 +17,7 @@ fn value(s: &Session, sheet: &str, cell: &str) -> Value { fn copied_sheet_keeps_spill_values_through_full_recalc() { let mut s = Session::new(); run(&mut s, "file.new", json!({"sample": "grades"})); - run(&mut s, "home.mergeCells", json!({"range": "F:H"})); + run(&mut s, "home.mergeCells", json!({"range": "F15:H20"})); run(&mut s, "cell.set", json!({"cell": "J54", "input": "=SEQUENCE(5)*C6"})); run(&mut s, "selection.set", json!({"range": "15:15"})); run(&mut s, "sheet.move", json!({"copy": true, "sheet": 0, "to": 1})); From 91f9b870704600eaf6e2ccafb0d58c33192e549d Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Thu, 26 Nov 2026 21:15:09 +0100 Subject: [PATCH 8/8] Resize Table keeps main's refusal over merged cells (Excel's behaviour); test follows --- crates/engine/src/cmd/extra/data.rs | 2 -- crates/engine/src/tests.rs | 6 ++++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/cmd/extra/data.rs b/crates/engine/src/cmd/extra/data.rs index 37f383642..cc4490e7f 100644 --- a/crates/engine/src/cmd/extra/data.rs +++ b/crates/engine/src/cmd/extra/data.rs @@ -93,8 +93,6 @@ pub(super) fn table_resize(s: &mut Session, p: &Json) -> Result { let t = sheet.tables.get_mut(ti).ok_or_else(|| bad("table", "gone"))?; t.columns = cols; t.range = r; - // A table can't hold merged cells: those it now covers are unmerged, as by Insert Table. - sheet.merges.retain(|m| !m.intersects(&r)); cx.structural = true; Ok(Json::Null) }) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 829b15368..c1668a003 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -8208,12 +8208,14 @@ fn tables_never_hold_merged_cells() { assert_eq!(merges(&s).len(), 3); s.execute("edit.redo", json!({})).unwrap(); - // Resizing a table over merged cells unmerges them too. + // Resizing a table over merged cells is refused, as in Excel; the merge stays. let mut s = base(); s.execute("insert.table", json!({"range": "A1:B4", "name": "T"})).unwrap(); s.execute("home.mergeCells", json!({"range": "C2:D2"})).unwrap(); + let e = s.execute("table.resize", json!({"table": "T", "range": "A1:C4"})).unwrap_err().to_string(); + assert!(e.contains("can't overlap merged cells"), "{e}"); + s.execute("home.unmergeCells", json!({"range": "C2:D2"})).unwrap(); s.execute("table.resize", json!({"table": "T", "range": "A1:C4"})).unwrap(); - assert!(merges(&s).is_empty()); // Merged cells pasted into a table arrive unmerged; elsewhere they stay merged. s.execute("home.mergeCells", json!({"range": "E2:F3"})).unwrap();