fix(xlsx): stream worksheet XML without DOM allocation and skip whitespace-only cells
AO
2d ago+522+522+012345678901234567890123456789-87-87-01234567890123456789
Summary
Resolves two coupled failure/memory modes on real-world Excel workbooks (#156, #155):
- Stream
xl/worksheets/sheet*.xmlviaquick_xml::NsReaderwithout building a DOM tree (src/formats/sheet/xlsx.rs)- Previously,
xlsx::parseloaded every worksheet throughPackage::optional_xml_part(parse_xml), allocating hundreds of thousands of smallElement/Attr/Nodeheap 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) trippedConvertError::ResourceLimit { limit: "max_xml_nodes" }(#156 shape 2) and fragmented per-threadptmallocarenas in multi-worker hosts such as Node'slibuvthreadpool (#155). read_sheetnow streams worksheet events directly with reused cell buffers while preservinglimits::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.
- Previously,
- Evict one-shot worksheet buffers after parsing (
Package::evictinsrc/package/archive.rs)- Drops cached raw worksheet bytes in
xlsxandxlsbonce streamed so multi-sheet workbooks do not retain every decompressedsheetN.xml/sheetN.binsimultaneously, while keepingPackage::total_readmonotonic soMAX_TOTAL_BYTESenforcement is unchanged.
- Drops cached raw worksheet bytes in
- Skip whitespace-only cell text across
xlsx,xlsb, andxls(!text.trim().is_empty())Cell::is_empty()andrender_cellalready treat whitespace-only text (spaces, NBSP\u{a0}normalized byclean_text) as empty, yetSheetContent::cellspreviously admitted!text.is_empty()entries, inflatingboundsto millions of empty grid slots and trippingmax_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_workbookinvariant preserved).
Verification
- Added regression tests in
src/formats/sheet/xlsx.rs:whitespace_only_cells_do_not_widen_the_grid_extentspace_fill_dragged_past_the_dom_node_cap_streams_without_errordeeply_nested_worksheet_xml_still_trips_max_xml_depthinline_phonetic_runs_are_omitted_and_strict_ooxml_worksheets_stream
- Ran full Rust CI suite locally:
cargo fmt --all --checkcargo clippy --workspace --all-targets --all-features -- -D warningscargo test --locked(290 unit + 2 header_collision + 1 robustness + 9 corpus snapshot tests pass)
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
xlsxandxlsb, 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, andxls, matching howCell::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.