Skip to content

Resolve the namespace prefix without expanding the whole worksheet - #1

Merged
connorshea merged 2 commits into
connorshea:optimize-rows-generatorfrom
aktio-cc:fix-reader-expand
Sep 8, 2026
Merged

connorshea merged 2 commits into
connorshea:optimize-rows-generatorfrom
aktio-cc:fix-reader-expand

Conversation

@michaelbaudino

@michaelbaudino michaelbaudino commented Aug 10, 2026 •

Copy link
Copy Markdown

Note

This is branched off your optimize-rows-generator branch rather than off Creek's master, so it can go straight into pythonicrubyist#138 if you want it.

Short version: pythonicrubyist#138 roughly halves wall clock, but on a large sheet it leaves RSS untouched. The prefix detection still calls Reader#namespaces on the worksheet root, and that single call builds a DOM of the entire sheet.

What we measured

A real 57,895-row file: 5 MB zipped, 44.8 MB of sheet XML. Full walk of sheet.rows, two runs each, same machine, Ruby 4.0.6 / Nokogiri 1.19.4:

Creek Variant RSS Wall clock
master (2.6.3) 1 150 MB 14.8 s
pythonicrubyist#132 116 MB 29.5 s
pythonicrubyist#138 1 148 MB 7.6 s
this very PR 121 MB 8.8 s

Rows produced are identical across all four (same digest over sampled rows).

Why pythonicrubyist#138 doesn't move RSS

Reader#attributes, #attribute_hash and #namespaces all go through libxml2's xmlTextReaderExpand(), which materialises the subtree under the current node. #attribute(name) and #attribute_at(i) go through xmlTextReaderGetAttribute/GetAttributeNo, which read the current node only and never build anything.

The namespace_resolved flag in pythonicrubyist#138 cuts the number of #namespaces calls from 660,002 to 2 — which is exactly where the wall-clock win comes from — but the first of those two calls lands on the <worksheet> root, and on the root "the subtree under the current node" is the whole document. Isolated, on the same file:

node #   name          RSS
1        worksheet     1116.5 MB   (26.6 MB before the call)
2        sheetPr       1116.5 MB
3        pageSetUpPr   1116.5 MB

One call, before the first row is yielded.

Also note that the cost is linear in the size of the sheet XML, at roughly 25x:

Sheet XML 1.9 MB 7.4 MB 22.7 MB 44.8 MB
RSS 79 MB 220 MB 592 MB 1120 MB

Two things worth noting:

  • It is specifically the root that hurts. Expanding on every <row> and <c> while leaving the root alone (2.1M calls) costs 29 MB — libxml2 frees those as the cursor moves on. So attribute_hash per cell was a real allocation problem, as you found, but not a retention one.
  • It is native memory, so it appears in neither GC.stat nor allocation profiles. That's why an allocation-and-wall-clock benchmark can't see it, and why this survived several rounds of tuning. VmRSS in /proc/self/status is what shows it.

The two commits

  1. Resolve the prefix via Reader#prefix / #namespace_uri instead of #namespaces. Taking the prefix from the first node that is in the spreadsheetml namespace, rather than from the namespaces declared on it, also handles a worksheet whose root sits in another namespace while its rows are prefixed.

  2. Keep attribute_hash off the plain rows path. Your commit message explicitly keeps it on the row opener to preserve the exact meta-data row hash — that is preserved: rows_with_meta_data still goes through attribute_hash, and only the rows/simple_rows path takes node.attribute('r'), which is the only row attribute those paths use. It saves one expand per row: bounded, unlike the root one, but paid on every row of every file.

About the wall clock

These commits land at ~8.8–9.1 s against ~6.8–7.6 s for pythonicrubyist#138 alone — still ~40% faster than master, but about a second and a half slower than your branch. That looks like the honest price of actually streaming: once the root has been expanded, the reader walks a tree that is already in memory, whereas here the sheet is genuinely parsed and inflated incrementally. We haven't profiled that difference, so treat the explanation as a hypothesis; the numbers themselves are reproducible.

If that trade-off isn't one you want in pythonicrubyist#138, these two commits stand alone just as well against master, but I feel like pythonicrubyist#138 could become "the big performance-solving PR" (with both CPU/wall-time and RAM optimizations).

Context

This looks like the root cause behind pythonicrubyist#122, and it is the same thing @alx75 fixed in pythonicrubyist#132 (8 GB on a 500k-row file); the difference here is only that the prefix is resolved without a per-node namespace_uri check and the cell attributes keep your cheaper reads, so the memory fix doesn't cost the wall clock that pythonicrubyist#132 pays.

`Reader#namespaces` and `Reader#attributes` both go through
`xmlTextReaderExpand()`, which materialises the subtree under the current node.
The prefix detection runs on every node until it latches, so it runs on the
worksheet root -- where "the subtree under the current node" is the entire
document. One call therefore builds a DOM of the whole worksheet and the
streaming parse is lost before the first row is yielded.

The cost is ~25x the size of the sheet XML, in native memory: 1.9 MB -> 79 MB,
7.4 MB -> 220 MB, 22.7 MB -> 592 MB, 44.8 MB -> 1120 MB. Being native, it
appears in neither `GC.stat` nor allocation profiles, which is why it survived
several rounds of allocation-focused tuning: on a 44.8 MB sheet, pythonicrubyist#138 alone
cuts wall clock in half but leaves RSS at 1149 MB, and pythonicrubyist#132 brings RSS down to
113 MB but doubles wall clock by calling `namespace_uri` on every node and
keeping `attributes` on every cell.

`Reader#prefix` and `Reader#namespace_uri` map to `xmlTextReaderConstPrefix`
and `xmlTextReaderConstNamespaceUri`, which read the current node only. Taking
the prefix from the first node that is *in* the spreadsheetml namespace, rather
than from the namespaces declared *on* it, also handles a worksheet whose root
is in another namespace while its rows are prefixed.

Measured on a real 57895-row FEC export (44.8 MB of sheet XML, 5 MB zipped),
on top of pythonicrubyist#138: 1149 MB -> 113 MB RSS and 13.2 s -> 8.7 s, with byte-identical
rows.

Claude-Session: https://claude.ai/code/session_01BNG66yvb43CB5x3mAWY64h
Row attributes are only exposed by rows_with_meta_data; every other caller
needs nothing but 'r'. Since attribute_hash expands the row's subtree, reading
that one attribute directly saves an expand per row -- bounded, unlike the one
on the root, but paid on every row of every file.

Claude-Session: https://claude.ai/code/session_01BNG66yvb43CB5x3mAWY64h
@michaelbaudino

michaelbaudino commented Aug 12, 2026 •

Copy link
Copy Markdown
Author

Disclaimer

We didn't want to rely on this very PR which — even if you merge it — would only be present in a PR for upstream creek, which I'm not sure is even maintained anymore 😞

So we decided to actually switch to simple_xlsx_reader rather than creek: it's not quite as fast as creek with all the patches proposed in this PR, but it uses far less memory.


For the records, here is the benchmark we relied on to take our decision with our use case (lazily parsing the same 60k rows XLSX file described in the PR description, but this benchmark also creates an ActiveRecord record for roughly 8k of them — many rows are ignored in our use case):

Reader Max RSS RSS delta
(compared to "RSS after boot")
Wall-clock time
creek (2.6.3) 1 476 MB +1 224 MB 81.9 seconds
creek (this PR) 381 MB +120 MB 59.7 seconds
simple_xlsx_reader (5.1.0) 301 MB +49 MB 67.2 seconds

I hope this might help, but feel free to close this PR if you don't think it belongs to pythonicrubyist#138 👌

@connorshea

Copy link
Copy Markdown
Owner

Sorry, I don't think I got a notification about this when it was sent!

Based on my testing, this actually has no downside vs the PR I opened, as best as I can tell. It's actually faster on all of the Excel fixtures we have in our codebase.

Variant file1 wall file2 wall file3 wall peak RSS file1 allocs file2 allocs file3 allocs
2.6.3 vanilla 4.282s 0.904s 0.700s 829.8 MB 37,194,339 7,141,486 5,468,852
2.6.3 + PR138 2.461s 0.600s 0.476s 826.8 MB 9,026,592 1,468,082 1,236,285
2.6.3 + PR138 + PR1 1.695s 0.383s 0.313s 102.4 MB 8,835,940 1,447,369 1,219,787

I'm going to merge this, thanks!

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