Skip to content

feat!: overhaul rxml for shared-reference performance and strict XML correctness - #15

Merged
nephi-dev merged 4 commits into
mainfrom
maintenance-update
Aug 5, 2026
Merged

nephi-dev merged 4 commits into
mainfrom
maintenance-update

Conversation

@nephi-dev

@nephi-dev nephi-dev commented Aug 5, 2026 •

Copy link
Copy Markdown
Owner

Performance and correctness overhaul of the rxml PyO3 extension, bumping the crate from 2.4.1 to 3.0.0. Reviewed by a full code-review pass (approved, no outstanding findings); 20/20 Rust tests passing; cargo clippy --all-targets -D warnings clean; cargo fmt clean.

Python-visible behavior changes (call these out to consumers):

  • Node.children and search results now share references (is/id() identity is observable across accesses) instead of each access returning an independent deep copy.
  • Malformed input now raises ValueError/FileNotFoundError instead of a Rust PanicException.
  • Previously-tolerated malformed XML now fails to parse (stricter correctness, see below).

Changes

  • Shared references instead of deep copies: Node.children and search results now return Vec<Py<Node>> internally instead of deep-copying subtrees on every Python attribute access. Benchmarked on a 1.72 MB / ~36k-node XML document (median of 5 runs): Python-side tree traversal 60 ms → 17.5 ms (~3.4x), search 22 ms → 0.5 ms (~40x).
  • GIL released during parse/write: parsing and serialization now use py.detach, so multithreaded callers can parse/write in parallel. Parsing builds a plain RawNode tree off-GIL and converts it to Python objects once.
  • Streaming I/O: the read path uses quick-xml's borrowed read_event() instead of allocating a buffer per recursion level; the write path drops a full-buffer copy and streams through BufWriter instead of building the entire output string in memory.
  • Release profile tuning: lto = "thin", codegen-units = 1, strip = true. Parse/write times are unchanged (±5%) despite the correctness work below.
  • Typed error handling: malformed XML (mismatched/unclosed tags, invalid attributes, unknown entity references) now raises ValueError with a byte position via a typed ParseError enum, instead of a Rust panic. A missing file raises FileNotFoundError; a missing root tag raises ValueError instead of silently returning an empty node.
  • Entity handling fixed: entities are now unescaped on read (text via GeneralRef event accumulation, attributes via spec XML attribute-value normalization). The writer escapes tab/CR/LF in attribute values as character references so documents round-trip losslessly (previously these characters silently corrupted to spaces).
  • Nested same-named elements fixed: elements nested inside a same-named ancestor now parse correctly (the old parser collapsed them; test_read_file encoded this bug and has been corrected).
  • Text/root handling fixed: multiple text fragments now concatenate instead of last-fragment-wins; <root/> as a root element works; root_tag matches at any depth (subtree extraction) — documented and tested.
  • rxml.pyi: Node.text corrected to str | None.
  • CHANGELOG.md removed deliberately — release notes now live in GitHub releases, which pyproject.toml's Changelog URL already points to. (The notable-changes list above can be reused as release notes.)
  • pyproject.toml: build-system maturin requirement raised to >=1.14.1 (dev keeps <2.0), verified with an isolated uv build and wheel smoke test.
  • README.md refreshed to match this branch: Node.text shown as str | None, a note on shared-reference semantics for children/search, and new feature bullets for GIL-released parse/write and the new error types.
  • Test count: 11 → 20.

How to Test

cargo fmt --check
cargo clippy --all-targets -- -D warnings
cargo test --release

All 20 Rust tests should pass, clippy should be clean, and formatting should be clean.

Notes

  • The performance and correctness changes touch the same files and are interdependent (e.g. the GIL-release path relies on the new off-GIL RawNode tree, which also underpins the shared-reference change).
  • The shared-reference and stricter-parsing changes are breaking for any caller relying on independent copies from repeated attribute access, or on the old parser silently tolerating malformed XML — hence the 3.0.0 version bump.

Checklist

  • Tests pass (cargo test)
  • Code is formatted (cargo fmt and ruff format)
  • Lints pass (cargo clippy)
  • Updated documentation (if applicable)
  • Added/updated tests (if applicable)

…correctness

Bump crate to 2.5.0. Node.children and search results now return
shared references (Vec<Py<Node>> internally) instead of deep-copying
subtrees on every Python attribute access, and parsing/serialization
release the GIL (py.detach) so multithreaded callers can parse/write
in parallel. Benchmarked on a 1.72 MB / ~36k-node document (median of
5): tree traversal 60 ms -> 17.5 ms (~3.4x), search 22 ms -> 0.5 ms
(~40x); parse/write times unchanged (+/-5%) despite the correctness
work below.

Parsing now builds a plain RawNode tree off-GIL and converts it to
Python objects once. The read path uses quick-xml's borrowed
read_event() instead of per-recursion-level buffers; the write path
drops a full-buffer copy and streams through BufWriter instead of
building the whole output string. Release profile adds
lto = "thin", codegen-units = 1, strip = true.

Correctness fixes:
- Malformed XML (mismatched/unclosed tags, invalid attributes, unknown
  entity refs) now raises ValueError with a byte position via a typed
  ParseError enum, instead of a Rust panic. Missing file raises
  FileNotFoundError; missing root tag raises ValueError instead of
  returning an empty node.
- Entities are unescaped on read (text via GeneralRef event
  accumulation, attributes via spec XML attribute-value
  normalization). The writer escapes tab/CR/LF in attribute values as
  character references so documents round-trip losslessly (previously
  they silently corrupted to spaces).
- Elements nested inside a same-named ancestor now parse correctly
  (the old parser collapsed them; test_read_file encoded this bug and
  has been corrected).
- Multiple text fragments concatenate instead of last-wins, <root/> as
  root works, and root_tag now matches at any depth (subtree
  extraction) - documented and tested.
- rxml.pyi: Node.text corrected to `str | None`.

CHANGELOG.md is removed; release notes now live in GitHub releases,
which pyproject.toml's Changelog URL already points to.

Test count 11 -> 20 (20/20 passing), clippy --all-targets -D warnings
clean, cargo fmt clean.

BREAKING CHANGE: Node.children and search results now expose shared
node references instead of independent deep copies, so `is`/`id()`
identity semantics are observable across accesses. Malformed input
raises ValueError/FileNotFoundError instead of a Rust
PanicException, and previously-tolerated malformed XML now fails to
parse.
Raise the maturin build/dev requirement from >=1.9.6 / >=1.0,<2.0 to
>=1.14.1 (dev keeps the <2.0 ceiling). Verified with an isolated
`uv build` honoring the new pin, followed by a wheel smoke test.

Update README.md to match this branch's behavior changes: Node.text
corrected to `str | None`, a note that children/search results are
shared references (cheap repeated access), and new feature bullets
for GIL-released parse/write (thread parallelism) and ValueError with
position info / FileNotFoundError on malformed or missing input.
@nephi-dev nephi-dev self-assigned this Aug 5, 2026
@nephi-dev
nephi-dev merged commit e44b0ca into main Aug 5, 2026
17 checks passed
@nephi-dev
nephi-dev deleted the maintenance-update branch August 5, 2026 04:29
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.

1 participant