Skip to content

Replace SortedDictionary with Dictionary in HSSFRow, XSSFRow and SXSSFRow#1753

Merged
tonyqus merged 3 commits into
nissl-lab:masterfrom
tonyqus:sxssf_optimization
Mar 23, 2026
Merged

Replace SortedDictionary with Dictionary in HSSFRow, XSSFRow and SXSSFRow#1753
tonyqus merged 3 commits into
nissl-lab:masterfrom
tonyqus:sxssf_optimization

Conversation

@tonyqus
Copy link
Copy Markdown
Member

@tonyqus tonyqus commented Mar 23, 2026

This PR is suggested by OpenCode. It can avoid O(log(n)) scan due to SortedDictionary

Changes

  • NPOI won't sort by column number in XXXRow.GetEnumerator anymore for the sake of performance. This can be a breaking change for existing code based on NPOI
  • Use a larger buffer in SXSSFWorkbook.CopyStreamAndInjectWorksheet

XSSFWorkbook Benchmark (RowCellAccessBenchmark)

Method SortedDictionary Dictionary Perf Increase
CreateCell_AppendOrder 57,350 ns 39,785 ns 30.6% ⬆️
CreateCell_RandomOrder 70,614 ns 38,459 ns 45.5% ⬆️
GetCell_Existing 38,116 ns 12,290 ns 67.8% ⬆️
IterateAllCells 7,505,841 ns 148,753 ns 98.0% ⬆️
IterateAllCellsWithGetCell 39,033 ns 12,176 ns 68.8% ⬆️
IterateRowCells 498 ns 126 ns 74.7% ⬆️
DeleteCell 62,804 ns 41,003 ns 34.7% ⬆️
ReadCellValues 590,989 ns 501,610 ns 15.1% ⬆️
GetRow_ByIndex 1,744 ns 1,637 ns 6.1% ⬆️
IterateAllRows 3,482 ns 3,534 ns Same

@tonyqus tonyqus added this to the NPOI 2.8.0 milestone Mar 23, 2026
@tonyqus tonyqus merged commit 1fd31e2 into nissl-lab:master Mar 23, 2026
3 checks passed
ken-swyfft added a commit to swyfft-insurance/npoi that referenced this pull request Mar 24, 2026
PR nissl-lab#1753 replaced SortedDictionary with Dictionary in XSSFRow for O(1)
insert/lookup, but GetEnumerator() and Cells no longer returned cells
in ascending column order — a breaking change for existing consumers.

Add a lazily-built sorted cell cache (List<ICell>) that is populated on
first read and invalidated on mutation (add/remove/rebuild). This
preserves the Dictionary performance win while restoring the sorted
iteration contract. Applied consistently to XSSFRow, HSSFRow, and
SXSSFRow.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant