Skip to content

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

Open
aoright wants to merge 2 commits into
firecrawl:mainfrom
aoright:fix/xlsx-stream-and-whitespace-extent
Open

aoright wants to merge 2 commits into
firecrawl:mainfrom
aoright:fix/xlsx-stream-and-whitespace-extent

Conversation

@aoright

@aoright aoright commented Nov 18, 2026 •

Copy link
Copy Markdown

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" } (xlsx: resource limits hit on real workbooks with excess whitespace #156 shape 2) and fragmented per-thread ptmalloc arenas in multi-worker hosts such as Node's libuv threadpool (Node binding retains large XLSX RSS high-water per libuv worker #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 (xlsx: resource limits hit on real workbooks with excess whitespace #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.

Written for commit ab1fe0b. Summary will update on new commits.

View guided diff

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 5 files

Click Fix with cubic on a comment, or reply @cubic-dev-ai fix this, and cubic writes the fix.

View guided diff | Re-trigger cubic

Comment thread src/formats/sheet/xlsb.rs Resolved
Comment thread src/formats/sheet/xlsx.rs Resolved
@matiacone

matiacone commented Nov 20, 2026 •

Copy link
Copy Markdown

We hit the xlsx memory limits in production and built this PR to try it. Sharing our numbers in case they help it land.

Setup: dense invented workbooks (one sheet, numeric and shared-string cells), anydoc v0.2.4 vs this branch, built locally, CLI, peak RSS.

Sheet XML v0.2.4 peak memory #229 peak memory
10 MB 425 MB 79 MB
50 MB refused (2M-node limit) 399 MB
100 MB refused (2M-node limit) 782 MB, 5 s
  • Markdown output was identical to v0.2.4 on our plain, namespace-prefixed (<x:c>/<x:v>) and attributed (<v xml:space="preserve">) variants, including CDATA.
  • A 200 MB, 4-sheet workbook is still refused, at the 4M-cell workbook limit. That's expected; it's a separate limit.
  • Memory still grows linearly (~8 MB per MB of sheet XML vs ~42 MB per MB before). That's a big enough drop that most workbooks we currently have to pre-split ourselves would convert directly.

Without this, we split large workbooks into row blocks with our own code before calling anydoc. It works, but it's a lot of XML handling to maintain outside the parser. We'd love to see this merged. Happy to run more cases if useful.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants