Repository navigation
Resolve the namespace prefix without expanding the whole worksheet - #1
Conversation
`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
DisclaimerWe didn't want to rely on this very PR which — even if you merge it — would only be present in a PR for upstream So we decided to actually switch to simple_xlsx_reader rather than 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):
I hope this might help, but feel free to close this PR if you don't think it belongs to pythonicrubyist#138 👌 |
|
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.
I'm going to merge this, thanks! |
Note
This is branched off your
optimize-rows-generatorbranch 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#namespaceson 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:Rows produced are identical across all four (same digest over sampled rows).
Why pythonicrubyist#138 doesn't move RSS
Reader#attributes,#attribute_hashand#namespacesall go through libxml2'sxmlTextReaderExpand(), which materialises the subtree under the current node.#attribute(name)and#attribute_at(i)go throughxmlTextReaderGetAttribute/GetAttributeNo, which read the current node only and never build anything.The
namespace_resolvedflag in pythonicrubyist#138 cuts the number of#namespacescalls 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: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:
Two things worth noting:
<row>and<c>while leaving the root alone (2.1M calls) costs 29 MB — libxml2 frees those as the cursor moves on. Soattribute_hashper cell was a real allocation problem, as you found, but not a retention one.GC.statnor allocation profiles. That's why an allocation-and-wall-clock benchmark can't see it, and why this survived several rounds of tuning.VmRSSin/proc/self/statusis what shows it.The two commits
Resolve the prefix via
Reader#prefix/#namespace_uriinstead 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.Keep
attribute_hashoff the plainrowspath. Your commit message explicitly keeps it on the row opener to preserve the exact meta-data row hash — that is preserved:rows_with_meta_datastill goes throughattribute_hash, and only therows/simple_rowspath takesnode.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_uricheck and the cell attributes keep your cheaper reads, so the memory fix doesn't cost the wall clock that pythonicrubyist#132 pays.