Expanded empty elements allocate a String per element

Aperta
#1,036 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
rust

Direzione di ricerca

Start in src/reader/state.rs at close_expanded_empty and trace how opened_buffer supplies the name and how the resulting BytesEnd<'static> is used by the reader API. Check the existing tests for expand_empty_elements(true) and add a focused allocation or behavior check if the project has a suitable pattern. Done means expanded empty elements preserve the expected End event behavior while avoiding a per-element String allocation, or the issue explains why the API requires ownership.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

optimization question

I think expand_empty_elements(true) may cost more than it needs to on input with many empty elements, because each expanded end tag allocates.

In src/reader/state.rs, close_expanded_empty takes the name with self.opened_buffer.split_off(...) and wraps it in Cow::Owned, so every expanded <a/> makes a new String for its End event.

What I measured, on quick-xml 0.42.0, in a release build on Linux arm64, walking a synthetic 100 MB part made of about 25 million <a/> elements with the same reader settings:

  • with expand_empty_elements(true): about 25 million allocations and 25 million frees for the walk;
  • with it set to false, handling Event::Empty instead: no allocation per element, and about 41 % fewer instructions for the walk (callgrind, on a 5 MB sample of the same input).

This matters most for callers that turn the option on and cannot turn it off. The case I met is reading XLSX parts through a library that sets it.

I may well be missing a reason for this. close_expanded_empty returns BytesEnd<'static>, so the owned name may be needed by the current API. I have not tried a patch, and I don't know how small a fix could be. If the name could be borrowed from opened_buffer for the life of the event, I think the allocation would go away, but that may not be possible without an API change.

If this is useful I'm happy to test a patch on the same input. If it's by design, please feel free to close this.

Lingua principale
Rust
Stelle
1.6k
Fork
299
Merge medio
2g 4h
PR unite (30g)
9

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di tafia/quick-xml

Tutte le issue di tafia/quick-xml

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.