Skip to content

Sheets: a formula whose ranges add up past the budget stops before it reads a cell - #2

Merged
jamescalam merged 1 commit into
mainfrom
sheets-222
Nov 15, 2026
Merged

jamescalam merged 1 commit into
mainfrom
sheets-222

Conversation

@jamescalam

@jamescalam jamescalam commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

=SUMPRODUCT(A1:T10000, …) with 16 ranges of 200,000 cells read every range before SUMPRODUCT ran. The step budget charged each read, so the formula ended in #CALC! only at the 16th range, after about 3 million cell reads. A sheet holding it froze for that long, and the hostile test "element-wise arrays are capped" timed out at 3 s on CI's runners (https://github.com/spacesheep-dev/skill-sheets/actions/runs/43671424929).

What changed

  • Before a formula is worked out, the engine adds up the cells of every range it is sure to read. That is all of them, except the parts of IF, IFERROR, IFNA and IFS after the first, which may never be worked out.
  • Each of those reads would be charged its cells anyway (mk), so a total past the budget is #CALC! at once, with no cell read. The result for that cell is the same as before; it just arrives without the work.
  • A highlight's test (ruleAt) gets the same check.
  • refRange's span moves into refSpan, which sureReads shares, so both count the same cells.

Timing

The four formulas in the hostile test, fastest of 3 runs on an Apple M-series laptop:

Formula Before After
SUMPRODUCT((A1:A2000=1)*(A1:GR1=1)) 4 ms 4 ms
SUMPRODUCT((A1:A10000=1)*(A1:ALL1=1)) 16 ms 19 ms
SUM(-(-(…16 deep…A1:T10000))) 0.30–0.46 s 0.36–0.42 s
SUMPRODUCT(A1:T10000, …16 ranges) 2.1–2.6 s 0 ms

On CI's runner, the whole "element-wise arrays are capped" test took 2.6 s on main's last green run (https://github.com/spacesheep-dev/skill-sheets/actions/runs/43668981314), and over 3 s for one formula on the two failed runs. With this change it took 0.82 s, 0.37 s and 0.55 s on three runs of this PR's check (https://github.com/spacesheep-dev/skill-sheets/actions/runs/43673088741), all green.

The nested minus is unchanged on purpose. It really makes 15 arrays of 200,000 cells, and the budget stops it after them.

Testing

  • New test in tests/hostile.js: the 16-range formula is #CALC! and reads no cell but its own. On the old engine it reads 2,800,001 cells and the test fails. A formula within budget reads what it names. IF/IFERROR/IFS with the big formula in an unused part still give their value. A highlight's test reads nothing.
  • node tests/run.js: 72 passed (Node 22 and 26).
  • npx spacesheep skills lint: well-formed, hash c6e7202d466e.

Worth a close look

  • sureReads must never count a range that evaluation might skip. Otherwise a cell that used to work would show #CALC!. The only lazy paths in evaluate are the four in LAZY, and every other call evaluates all of its arguments (n.a.map(ev)).

… reads a cell

=SUMPRODUCT(A1:T10000, ...) with 16 ranges of 200,000 cells read each
range before SUMPRODUCT ran. The budget charged each read, so it ended
in #CALC! only at the 16th range, after 3 million cell reads: about
2 s on a laptop, and over 3 s on CI's runners, where the hostile test
timed out. A person's sheet with such a formula froze for as long.

Now, before a formula is worked out, the engine adds up the cells of
every range it is sure to read (all of them, except the parts of IF,
IFERROR, IFNA and IFS past the first, which may never be worked out).
Each of those reads would be charged its cells anyway, so a total past
the budget ends in #CALC! at once, with no cell read. A highlight's test
gets the same check. refRange's span moves into refSpan, which both use.

Co-authored-by: Pointman <noreply@pointman.ai>
Co-authored-by: Claude <noreply@anthropic.com>
@jamescalam
jamescalam merged commit 9160a54 into main Nov 15, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant