GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add opt-in streaming (SXSSF) Excel write by mathiasmuller4sh · Pull Request #23 · 4sh/retable

Conversation

@mathiasmuller4sh

@mathiasmuller4sh mathiasmuller4sh commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Why

Excel writes always build the whole workbook in memory (XSSFWorkbook). On large exports this takes several GB of heap: in C-Fluide production it led to an OutOfMemoryError and a JVM stuck in GC for several minutes.

What

New opt-in options on ExcelReadOptions:

  • useStreamingWorkbook (default false): writes with POI streaming API (SXSSFWorkbook). Only the last rows are kept in memory, the others are flushed to a temporary file, deleted (dispose()) once the workbook is written, even on failure.
  • streamingRowAccessWindowSize (default SXSSFWorkbook.DEFAULT_WINDOW_SIZE, i.e. 100).

Applies to write and writeWorkbook. Default behaviour is unchanged.

Changes needed to support SXSSF:

  • the write path uses the ss.usermodel interfaces (Workbook, Row, Cell, CellStyle) instead of the XSSF classes.
  • POI autosize (useExperimentalFasterAutoSizeColumn = false): trackAllColumnsForAutoSizing() is called on streaming sheets, since flushed rows can no longer be measured afterwards.
  • sheet based value constraints: the hidden _constraints_ sheet is now written row by row (each row created once, in ascending order) instead of column by column, because SXSSF cannot go back to a flushed row. Same output as before with XSSFWorkbook.
  • withValueConstraints now extends Sheet instead of XSSFSheet (source compatible; it is public but only used inside retable).

Two small fixes found along the way:

  • useExperimentalFasterAutoSizeColumn = true with no record threw UnsupportedOperationException: Empty sequence can't be reduced.
  • with POI autosize, the computed column lengths were collected in a list (toList()) for nothing, they are now just consumed.

Tests

  • Same content (strings, ints, dates, hyperlinks) read back from a classic export and from streaming exports with a window of 10 rows and 1,000 records, with both autosize modes. Column widths are set.
  • Sheet based value constraints with 500 and 300 allowed values in streaming mode (the column by column code fails there with IllegalArgumentException).
  • writeWorkbook with several sheets in streaming mode.
  • Empty export with the faster autosize (fails without the fix).

Full suite: 58 tests, all passing (JDK 11).

Memory check, 200,000 rows x 6 columns, test JVM limited to 256 MB of heap:

Mode Result
useStreamingWorkbook = true OK in 5.4 s
default (XSSFWorkbook) OutOfMemoryError after 4 min 29 s of GC

Not in this PR

writeLocalDateCell passes value.month.value (1 to 12) to Calendar.set, whose month is 0 based: LocalDate cells look shifted by one month. Not changed here as it changes existing outputs, worth a separate issue.

🤖 Generated with Claude Code

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mathiasmuller4sh @claude
Add opt-in streaming (SXSSF) Excel write
f849665
New ExcelReadOptions.useStreamingWorkbook / streamingRowAccessWindowSize
write xlsx with POI SXSSFWorkbook, keeping only a window of rows in
memory instead of the whole workbook. Default behaviour is unchanged.

- write path uses ss.usermodel interfaces instead of XSSF classes
- track columns for POI autosize on streaming sheets
- hidden constraints sheet written row by row (required by SXSSF)
- fix faster autosize on empty records (empty sequence reduce)
- don't collect column lengths when using POI autosize

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mathiasmuller4sh
mathiasmuller4sh requested review from SaraPossard and walien October 28, 2026 16:50
mathiasmuller4sh
mathiasmuller4sh commented Oct 29, 2026
View reviewed changes

@mathiasmuller4sh mathiasmuller4sh left a comment

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, well-scoped change: opt-in, default path untouched, dispose() in a finally, and the row-by-row _constraints_ sheet is cleaner than before. The reduceOrNull fix and dropping the toList() on maxColumnLength are good catches (the latter also saves memory in classic mode). Full suite passes locally on the branch (58 tests).

Two points I'd like addressed or at least documented before merging (hyperlinks still kept in memory, out-of-order records failing depending on the window size), plus a few minor remarks inline.

Agreed on keeping the writeLocalDateCell month shift out of this PR; it is a real bug (Calendar months are 0-based), worth its own issue.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread README.md
By default the whole workbook is built in memory before being written (POI `XSSFWorkbook`), which can take
several GB for a few hundred thousand rows. With `useStreamingWorkbook` the POI streaming API (`SXSSFWorkbook`) is used:
only the last `streamingRowAccessWindowSize` rows (100 by default) are kept in memory, the others are flushed to a
temporary file which is deleted once the workbook is written.

@mathiasmuller4sh mathiasmuller4sh Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hyperlinks are not streamed. In POI 5.2.3 SXSSFCell.setHyperlink calls getSheet()._sh.addHyperlink(xssfobj) (SXSSFCell.java:671): every hyperlink is kept on the backing XSSFSheet until the end, it is not flushed with the rows. So on an export with a writeUrlAsHyperlink column, memory still grows linearly with the number of rows even with useStreamingWorkbook = true.

Could you mention it here, and say whether the 200,000-row memory check included a URL column? If it did not, it would be worth re-running it with one.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mathiasmuller4sh mathiasmuller4sh Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented in 03a443f (README + ExcelReadOptions comment). The memory check has not been re-run with a URL column yet.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread README.md Outdated
)) to outputStream)
```

Records must be written in ascending line order, which is always the case when the data is provided through `data(...)`.

@mathiasmuller4sh mathiasmuller4sh Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out-of-order records fail depending on the window size. In classic mode, createRow(record.lineNumber - 1) silently reorders rows by lineNumber. In streaming mode POI throws IllegalArgumentException: Attempting to write a row[n] in the range [0,m] that is already written to disk (SXSSFSheet.java:136), but only once the row has left the window. So something like records.sortedBy { ... } works in tests on small data and breaks in production on large exports.

Suggestion: track the last written row index in writeSheet and throw an explicit error as soon as a record comes out of order (whatever the volume), with a test for it. That makes the constraint deterministic instead of data-size dependent.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mathiasmuller4sh mathiasmuller4sh Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b3efe65: in streaming mode any out of order record now fails right away with an explicit IllegalArgumentException, whatever the data volume (classic mode unchanged). Test added with 2 rows that still fit in the window, where POI alone would not fail.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/main/kotlin/io/retable/RetableSheetValueConstraints.kt Outdated
*/

fun XSSFSheet.withValueConstraints(options: ExcelReadOptions, columns: RetableColumns): XSSFSheet {
fun Sheet.withValueConstraints(options: ExcelReadOptions, columns: RetableColumns): Sheet {

@mathiasmuller4sh mathiasmuller4sh Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: this is not fully source compatible, the return type changes from XSSFSheet to Sheet, so a caller using the result as an XSSFSheet no longer compiles (and the JVM signature changes, so binaries compiled against the old one break). A generic receiver keeps both:

fun <S : Sheet> S.withValueConstraints(options: ExcelReadOptions, columns: RetableColumns): S {

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mathiasmuller4sh mathiasmuller4sh Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c611892 with fun <S : Sheet> S.withValueConstraints(...): S. Source compatible again; the JVM signature still changes (S erases to Sheet), so compiled callers need a rebuild.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/main/kotlin/io/retable/RetableExcelSupport.kt Outdated
// write xlsx with POI streaming API (SXSSF): only the last `streamingRowAccessWindowSize` rows are kept in
// memory, the others are flushed to a temporary file. Records must be written in ascending line order.
val useStreamingWorkbook: Boolean = false,
val streamingRowAccessWindowSize: Int = SXSSFWorkbook.DEFAULT_WINDOW_SIZE

@mathiasmuller4sh mathiasmuller4sh Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: adding constructor parameters changes the synthetic $default constructor Kotlin generates, so a library already compiled against a previous retable version calling ExcelReadOptions(...) would get a NoSuchMethodError. Not a big deal with jitpack consumers recompiling, but worth a line in the release notes.

(Nit: write options living in ExcelReadOptions reads a bit odd, though useExperimentalFasterAutoSizeColumn already set the precedent.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mathiasmuller4sh mathiasmuller4sh Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changed in code: to be mentioned in the release notes (compiled callers of ExcelReadOptions(...) need a rebuild).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/main/kotlin/io/retable/RetableExcelSupport.kt Outdated

private fun withWorkbook(block: (Workbook) -> Unit) {
val workbook =
if (options.useStreamingWorkbook) SXSSFWorkbook(options.streamingRowAccessWindowSize)

@mathiasmuller4sh mathiasmuller4sh Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: SXSSF temp files are written uncompressed to java.io.tmpdir. On large exports in a container with small ephemeral storage this can become the next limit. Maybe expose setCompressTempFiles(true) as an option (or enable it by default in streaming mode), or at least document it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mathiasmuller4sh mathiasmuller4sh Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an opt-in compressStreamingTempFiles option in 846f657 (default false, as in POI), documented in the README, with a test.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/main/kotlin/io/retable/RetableExcelSupport.kt
} else {
maxColumnLength.toList()
// consumes the records without keeping the computed lengths
maxColumnLength.forEach { }

@mathiasmuller4sh mathiasmuller4sh Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the intent (forcing the lazy sequence so rows get written) is a bit hidden behind an empty forEach { }. A comment saying the sequence is lazy and must be consumed to create the rows would make it clearer.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mathiasmuller4sh mathiasmuller4sh Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment reworded in b2a1f3a.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/test/kotlin/io/retable/RetableExcelTest.kt
XSSFWorkbook(File(pathTo("export_streaming.xlsx"))).use { workbook ->
val sheet = workbook.getSheetAt(0)
expectThat(sheet.getRow(1000).getCell(3).hyperlink?.address).isEqualTo("https://4sh.fr/1000")
expectThat(sheet.getColumnWidth(0)).isNotEqualTo(sheet.defaultColumnWidth * 256)

@mathiasmuller4sh mathiasmuller4sh Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this only checks that some width was set. Comparing column widths between export_classic.xlsx and export_streaming.xlsx (POI autosize, tracked columns) would actually verify trackAllColumnsForAutoSizing() gives the same result. Nothing checks temp files are removed either, though that one is harder to test.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mathiasmuller4sh mathiasmuller4sh Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in f787a30: widths are now compared with the classic export (they match within 1/256 of a character, POI rounds slightly differently in SXSSF). Also added a test checking SXSSF temp files exist while writing and are deleted afterwards.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
mathiasmuller4sh and others added 8 commits October 29, 2026 12:04
@mathiasmuller4sh @claude
Reject out of order records in streaming mode
b3efe65
POI only fails on an out of order row once the previous one has been
flushed to disk, so the error depended on the data volume. Fail on any
out of order record instead, with an explicit message.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mathiasmuller4sh @claude
Document that hyperlinks are not streamed
03a443f
POI SXSSF keeps every hyperlink on the backing XSSFSheet until the
workbook is written, so memory still grows with URL columns.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mathiasmuller4sh @claude
Keep sheet type returned by withValueConstraints
c611892
Generic receiver so that callers passing an XSSFSheet still get an
XSSFSheet back (source compatible with the previous signature; the JVM
signature still changes since S erases to Sheet).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mathiasmuller4sh @claude
Add compressStreamingTempFiles option
846f657
SXSSF temporary files are written uncompressed to java.io.tmpdir, which
can become the next limit on large exports in containers with little
disk space. Opt-in gzip compression, at the cost of some CPU.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mathiasmuller4sh @claude
Clarify why the row sequence is consumed with POI autosize
b2a1f3a
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mathiasmuller4sh @claude
Test streaming column widths and temp files cleanup
f787a30
- compare POI autosize widths of the streaming export with the classic
  one instead of only checking a width was set
- check SXSSF temporary files exist while writing and are deleted after

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mathiasmuller4sh @claude
Compute export columns once per sheet instead of once per record
f7eec79
columns.list() goes through Kotlin reflection; calling it for each record
cost about 25% of the streaming write time (200,000 rows: 3.97 s -> 3.03 s).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mathiasmuller4sh @claude
Add streaming non-regression and performance tests
f5a0c4c
- non-regression: same cells/styles in classic and streaming, inline and
  sheet based value constraints, temp files deleted on failure, line number
  gaps, window of 1, empty export, classic reordering, faster autosize widths
- performance: new perfTest task (256 MB heap, tag "perf", excluded from
  test) covering 200,000 rows, POI autosize, hyperlinks, streaming vs
  classic and compressed temp files

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mathiasmuller4sh

mathiasmuller4sh commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator Author

Second review pass, with the fixes

The fixes address the first review well. The full suite passes on JDK 11 and on JDK 8 (the CI JDK): 71 tests, 0 failures.

Pushed in this pass

  • f7eec79, perf fix: columns.list() goes through Kotlin reflection and was called for every record in writeSheet. It is now computed once per sheet. About 25% faster: 200,000 rows in streaming went from 3.97 s to 3.03 s.
  • f5a0c4c, tests:
    • 11 non-regression tests in RetableExcelTest:
      • classic and streaming exports produce the same cells: types, values, date format, wrap, underline, hyperlinks;
      • inline value constraints in streaming;
      • sheet based constraints with columns of unequal sizes (the classic output is unchanged), and on several sheets in streaming;
      • temp files are deleted when writing fails;
      • gaps in line numbers, a window of 1 row, an empty export with POI autosize;
      • classic mode still reorders out of order records;
      • faster autosize gives the same widths in both modes.
    • A new perfTest Gradle task (256 MB heap, tag perf, excluded from test) with RetableStreamingPerformanceTest:
Scenario Result
streaming, 200,000 rows, faster autosize, no URL 3.0 s
streaming, 50,000 rows, POI autosize 26 s
streaming, 50,000 rows, with hyperlinks 1.8 s
20,000 rows, streaming vs classic 0.48 s vs 2.0 s
temp files on disk, 50,000 rows, compressed vs uncompressed 1.9 MB vs 19.6 MB

Remaining remarks

  1. Hyperlinks, measured: streaming 200,000 rows with a URL column in a 256 MB heap ends in OutOfMemoryError after about 280 s of GC, the same symptom as the original production issue. 50,000 rows still fit. writeUrlAsHyperlink defaults to true on string columns, so any value starting with http becomes a hyperlink. Callers with large exports should check for URL columns, or disable writeUrlAsHyperlink on them.
  2. Streaming with POI autosize is very slow: about 35x slower per row than useExperimentalFasterAutoSizeColumn, because tracked columns measure every cell with font metrics. The README already recommends faster autosize. Worth making it more prominent, or enabling it by default in streaming mode.
  3. Retable.data(...) builds all records eagerly in a list. The memory gain is only on the workbook side. For end to end streaming, callers have to pass a lazy Sequence to Retable(columns, records).
  4. Nit: a record with lineNumber = 1 silently overwrites the header row. Same behaviour as classic mode, not blocking.
  5. CI only publishes on tags and does not run tests, so ./gradlew perfTest has to be run by hand. The older RetablePerformanceTest (40,000 rows, about 20 s) still runs in test. It could get the perf tag too.

🤖 Generated with Claude Code

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mathiasmuller4sh
mathiasmuller4sh merged commit 480bcb2 into master Oct 30, 2026
@mathiasmuller4sh
mathiasmuller4sh deleted the feat/sxssf-streaming-write branch October 30, 2026 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@SaraPossard SaraPossard Awaiting requested review from SaraPossard
@walien walien Awaiting requested review from walien

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@mathiasmuller4sh
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.