pjfanning opened a new pull request, #1508:
URL: https://github.com/apache/poi/pull/1508

   Fixes #1506.
   
   `XSSFSheet` kept array formula ranges in a `List` and scanned all of them in 
`isCellInArrayFormulaContext`, `getFirstCellInArrayFormula` and 
`removeArrayFormula`. `setArrayFormula` also did a linear `contains()`. 
`XSSFCell.isFormulaCell()` calls `isCellInArrayFormulaContext`, so 
`getCellType()`, `getCachedFormulaResultType()`, `getCellFormula()` and 
`isPartOfArrayFormulaGroup()` each cost O(number of array formulas). Visiting 
every cell of a sheet with many array formulas is therefore quadratic. Adding 
them with `setArrayFormula` is quadratic too.
   
   The issue suggests a per-cell `Map<CellAddress, …>`. That makes lookups 
O(1), but it needs one entry per covered cell, so a single `A1:A1000000` array 
formula would need a million map entries. This PR adds a package-private 
`ArrayFormulaRanges` instead:
   
   - Each range is registered against every 64-row block it spans 
(`HashMap<Integer, List<CellRangeAddress>>`). A lookup only checks the ranges 
in the cell's block. Memory grows with rows spanned divided by 64, not with 
cells covered.
   - A `HashSet` gives O(1) duplicate detection for `setArrayFormula` and 
`onReadCell`.
   - Ranges are copied on add, so a caller who later mutates the 
`CellRangeAddress` it passed to `setArrayFormula` can't corrupt the index. The 
old list stored the caller's object.
   
   When ranges overlap, which Excel doesn't allow, lookups still return the 
earliest-added match, as before.
   
   Tests: there's a new `TestArrayFormulaRanges`, and 
`TestXSSFSheetUpdateArrayFormulas.testManyArrayFormulas` covers 500 two-row 
formulas plus a tall one across many blocks, a write/read round trip, and 
removal.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to