Skip to content

Optimize ReferenceHelper to filter cells by range before iterating#4826

Draft
kemo wants to merge 5 commits into
PHPOffice:masterfrom
kemo:perf/reference-helper-cell-index
Draft

Optimize ReferenceHelper to filter cells by range before iterating#4826
kemo wants to merge 5 commits into
PHPOffice:masterfrom
kemo:perf/reference-helper-cell-index

Conversation

@kemo
Copy link
Copy Markdown
Contributor

@kemo kemo commented Mar 10, 2026

Summary

  • Adds getCoordinatesInRange() and getCoordinatesOutsideRange() methods to Cells.php that filter cells using the internal index array
  • Refactors insertNewBefore() in ReferenceHelper.php from a single loop over ALL cells to two targeted passes: one for affected cells (move) and one for outside cells (formula update only)
  • Reduces the work from O(all cells) to O(affected cells) + O(formula cells outside range)

Changes

  • src/PhpSpreadsheet/Collection/Cells.php: Added getCoordinatesInRange() and getCoordinatesOutsideRange()
  • src/PhpSpreadsheet/ReferenceHelper.php: Two-pass refactor of insertNewBefore()
  • tests/PhpSpreadsheetTests/ReferenceHelper6Test.php: 14 new tests covering row/column insert, formula updates, edge cases
  • tests/PhpSpreadsheetTests/Benchmark/ReferenceHelperBenchmark.php: Benchmark with 10,000 cells, indexed vs full iteration comparison

Test plan

  • Verify inserting rows correctly updates formula references
  • Verify inserting columns correctly updates formula references
  • Verify edge cases: insert at row 1, insert at last column, Z/AA boundary
  • Run benchmark: vendor/bin/phpunit --group benchmark --filter ReferenceHelperBenchmark --stderr

@kemo
Copy link
Copy Markdown
Contributor Author

kemo commented Mar 11, 2026

Benchmark Results (reference-helper-cell-index)

Targeted benchmark: insert rows/columns into worksheets of increasing size, with formulas.

Benchmark Master Branch Delta
10k cells, insert rows 807ms 798ms -1%
10k cells, insert cols 825ms 816ms -1%
60k cells, insert rows 4,886ms 4,965ms +2%
60k cells, insert cols 5,567ms 5,382ms -3%
100k cells, insert rows 8,098ms 7,963ms -2%
100k cells, insert cols 8,728ms 8,231ms -6%

Marginal improvement (2–6%) at larger sheet sizes. Within noise for smaller sheets. Memory usage unchanged.

@oleibman
Copy link
Copy Markdown
Collaborator

Thank you for the summaries. For purposes of prioritization, I am going to put the PRs with less demonstrable improvement, including this one, in draft status for now. This does not mean that I will not return to them.

@oleibman oleibman marked this pull request as draft March 11, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants