Calc: index former spill areas by 64-cell squares, not by cell, on full recalc

A crafted XLSX may declare up to 16 million dynamic-range cells per sheet, and
opening it runs a full recalculation: the former-area cell set grew with every
cell (841 MB peak, 2.4 s for 16 one-million-cell areas). The index now lists the
areas over each 64 x 64 square they cover (12 MB, 13 ms for the same sheet).
This commit is contained in:
quantforge-br
2026-11-02 16:12:40 -03:00
parent 88310f3f4d
commit 113e312809
2 changed files with 45 additions and 13 deletions
+28 -13
View File
@@ -562,21 +562,27 @@ impl Calc {
#[derive(Default)]
struct Former {
areas: HashMap<Key, RangeRef>,
/// The cells of `areas`, built when an array first needs more cells than its own former area
/// (so each check costs what checking the cells themselves does, however many areas there are).
cells: Option<HashSet<Key>>,
/// The areas over each [`FORMER_TILE`]-cell square of a sheet, built when an array first needs
/// more cells than its own former area. A check looks only at the areas near the array, and the
/// index grows with the squares the areas cover, not with their cells: a file's `ref`s may
/// declare millions of them.
tiles: Option<HashMap<(usize, u32, u32), Vec<Key>>>,
}
/// The side of [`Former`]'s index squares, in cells.
const FORMER_TILE: u32 = 64;
/// The index squares `range` overlaps, as (row, column) square numbers.
fn former_tiles(range: RangeRef) -> impl Iterator<Item = (u32, u32)> {
let cols = range.start.col / FORMER_TILE..=range.end.col / FORMER_TILE;
(range.start.row / FORMER_TILE..=range.end.row / FORMER_TILE).flat_map(move |r| cols.clone().map(move |c| (r, c)))
}
impl Former {
/// Frees `key`'s former area, as it is laid out now.
/// Frees `key`'s former area, as it is laid out now. The index keeps its key; checks skip
/// keys whose area is gone.
fn take(&mut self, key: &Key) -> Option<RangeRef> {
let area = self.areas.remove(key)?;
if let Some(cells) = &mut self.cells {
for cell in area.iter() {
cells.remove(&(key.0, cell));
}
}
Some(area)
self.areas.remove(key)
}
/// Whether `range` on `sheet` reaches a former area that is still taken.
@@ -585,8 +591,17 @@ impl Former {
return false;
}
let areas = &self.areas;
let cells = self.cells.get_or_insert_with(|| areas.iter().flat_map(|(k, area)| area.iter().map(move |cell| (k.0, cell))).collect());
range.iter().any(|cell| cells.contains(&(sheet, cell)))
let tiles = self.tiles.get_or_insert_with(|| {
let mut tiles: HashMap<(usize, u32, u32), Vec<Key>> = HashMap::default();
for (key, area) in areas {
for (r, c) in former_tiles(*area) {
tiles.entry((key.0, r, c)).or_default().push(*key);
}
}
tiles
});
former_tiles(range)
.any(|(r, c)| tiles.get(&(sheet, r, c)).is_some_and(|keys| keys.iter().any(|k| areas.get(k).is_some_and(|area| area.intersects(&range)))))
}
}
+17
View File
@@ -737,6 +737,23 @@ fn a_full_recalc_keeps_a_spill_from_an_array_that_grew_into_it() {
assert_eq!([t.num("C14"), t.num("F14"), t.num("G14"), t.num("I14")], [1.0, 4.0, 5.0, 7.0]);
}
#[test]
fn a_full_recalc_keeps_a_former_area_where_it_crosses_into_far_cells() {
// As above, with one array at BI14:BO14 and the other at BN13: the former area runs past
// column BL, and the array grows into the part beyond it (former areas are indexed by squares
// of cells, so the overlap must be found away from the area's first cells too).
let mut t = T::new();
t.set("J1", "1");
t.set("BI14", "=SEQUENCE(1,7)");
t.set("BN13", "=SEQUENCE(J1,2)");
assert_eq!([t.num("BN13"), t.num("BO13")], [1.0, 2.0]);
t.wb.calc.mode = gridcraft_model::CalcMode::Manual;
t.set("J1", "5");
t.calc.recalc_all(&mut t.wb);
assert_eq!(t.get("BN13"), Value::Error(CellError::Spill));
assert_eq!([t.num("BI14"), t.num("BN14"), t.num("BO14")], [1.0, 6.0, 7.0]);
}
#[test]
fn a_full_recalc_lays_out_arrays_as_an_automatic_one_when_one_shrinks_for_another() {
// C1 grows into C3, which A3's array fills, and A3 grows into E3, which E1's array leaves.