fix(xlsx): stream worksheet XML without DOM allocation and skip whitespace-only cells

AO
2d ago
+522-87

Summary

Resolves two coupled failure/memory modes on real-world Excel workbooks (#156, #155):

  1. Stream xl/worksheets/sheet*.xml via quick_xml::NsReader without building a DOM tree (src/formats/sheet/xlsx.rs)
    • Previously, xlsx::parse loaded every worksheet through Package::optional_xml_part (parse_xml), allocating hundreds of thousands of small Element/Attr/Node heap nodes (~240–400 B each).
    • Worksheets with dragged fill columns (e.g., 550k+ rows of <row><c t="s"><v>0</v></c></row>, >2M XML nodes) tripped ConvertError::ResourceLimit { limit: "max_xml_nodes" } (#156 shape 2) and fragmented per-thread ptmalloc arenas in multi-worker hosts such as Node's libuv threadpool (#155).
    • read_sheet now streams worksheet events directly with reused cell buffers while preserving limits::MAX_XML_DEPTH, BOM/encoding transcoding (to_utf8), entity/CDATA decoding (resolve_entity), inline <rPh> phonetic stripping, Transitional + Strict OOXML SpreadsheetML namespaces, hidden rows/columns, merge regions, and legacy VML <legacyDrawing> lookups.
  2. Evict one-shot worksheet buffers after parsing (Package::evict in src/package/archive.rs)
    • Drops cached raw worksheet bytes in xlsx and xlsb once streamed so multi-sheet workbooks do not retain every decompressed sheetN.xml / sheetN.bin simultaneously, while keeping Package::total_read monotonic so MAX_TOTAL_BYTES enforcement is unchanged.
  3. Skip whitespace-only cell text across xlsx, xlsb, and xls (!text.trim().is_empty())
    • Cell::is_empty() and render_cell already treat whitespace-only text (spaces, NBSP \u{a0} normalized by clean_text) as empty, yet SheetContent::cells previously admitted !text.is_empty() entries, inflating bounds to millions of empty grid slots and tripping max_grid_slots (#156).
    • Non-whitespace cells still keep their original untrimmed padding (e.g. " padded "), and interior coordinates between real non-empty cells remain intact (the_grid_budget_spans_the_whole_workbook invariant preserved).

Verification

  • Added regression tests in src/formats/sheet/xlsx.rs:
    • whitespace_only_cells_do_not_widen_the_grid_extent
    • space_fill_dragged_past_the_dom_node_cap_streams_without_error
    • deeply_nested_worksheet_xml_still_trips_max_xml_depth
    • inline_phonetic_runs_are_omitted_and_strict_ooxml_worksheets_stream
  • Ran full Rust CI suite locally:
    • cargo fmt --all --check
    • cargo clippy --workspace --all-targets --all-features -- -D warnings
    • cargo test --locked (290 unit + 2 header_collision + 1 robustness + 9 corpus snapshot tests pass)

Refs #156, #155


Summary by cubic

Streams worksheet XML in xlsx via quick_xml::NsReader instead of building a DOM tree, so workbooks with hundreds of thousands of rows no longer trip max_xml_nodes or fragment heap arenas. The previous parser materialized every <row>/<c> as heap-allocated Element nodes; it now keeps only the current row and cell state in memory.

  • Cached worksheet buffers are evicted from the package cache after streaming in xlsx and xlsb, reference-counted so a part shared by multiple sheet entries is evicted only after the last one consumes it.
  • Whitespace-only cells (spaces, NBSP) are now skipped across xlsx, xlsb, and xls, matching how Cell::is_empty() renders them; padded text like " padded " keeps its padding.
  • Streaming preserves hidden rows/columns, merge regions, inline phonetic run stripping, strict OOXML namespaces, legacy VML checkbox lookups, and rejects mismatched closing tags.
  • Added regression tests for streaming past the old DOM node cap, whitespace-only extents, depth limits, strict OOXML worksheets, and shared worksheet parts.