Comparison
Comparison with Other Libraries¶
pyrs-yaml compared against the two most popular Python YAML libraries, and against
yaml-rs and ryaml, the Rust-backed readers.
Performance Comparison¶
Parse Speed (Large YAML, ~2 KB)¶
| Library | Time | Speedup |
|---|---|---|
| pyrs-yaml | 69 µs | — |
| PyYAML | 2.3 ms | 34× slower |
| ruamel.yaml | 5.5 ms | 80× slower |
| yaml-rs | 26 µs | 2.6× faster |
| ryaml | 74 µs | 1.1× slower |
Serialize Speed (Large YAML, ~2 KB)¶
| Library | Time | Speedup |
|---|---|---|
| pyrs-yaml | 4.8 µs | — |
| PyYAML | 1.3 ms | 276× slower |
| ruamel.yaml | 3.0 ms | 634× slower |
| yaml-rs | 16 µs | 3.4× slower |
| ryaml | 42 µs | 8.8× slower |
Round-Trip Speed (Large YAML, ~2 KB)¶
| Library | Time | Speedup |
|---|---|---|
| pyrs-yaml | 77 µs | — |
| PyYAML | 3.9 ms | 51× slower |
| ruamel.yaml | 8.2 ms | 107× slower |
| yaml-rs | 47 µs | 1.6× faster |
| ryaml | 118 µs | 1.5× slower |
Trade-off vs. native-object Rust parsers¶
pyrs-yaml is the fastest YAML library that also keeps a round-trip AST
(comments, anchors, tags, and source spans survive a load→edit→dump). When
compared against Rust parsers that deserialize straight to plain Python
objects and do not offer round-trip editing — yaml-rs in the tables
above — the picture is split:
- Serialize: pyrs-yaml wins by about 3.2–4.1× on the same fixtures (see the tables above).
- Eager parse into a
YamlDocument: pyrs-yaml is about 2.5× slower thanyaml-rs, because it builds the richer round-trip structureyaml-rsnever produces. This is a deliberate design trade — the extra work is what buys comment/anchor/order preservation, not a regression to optimize away.
Choose a native-only parser like yaml-rs when you only ever read YAML,
need the last microsecond on parse, and never need to write changes back
preserving the original formatting. Choose pyrs-yaml when round-trip
fidelity matters — and note that even then it parses 27–40× faster than
PyYAML and serializes 276–316× faster.
Feature Comparison¶
| Feature | pyrs-yaml | PyYAML | ruamel.yaml |
|---|---|---|---|
| YAML 1.2 compliance | |||
| Comments (standalone) | |||
| Comments (inline) | |||
| Anchors/aliases | |||
| Tags (explicit) | |||
| Block scalars | |||
| Flow collections | |||
| Merge keys (<<) | |||
| Complex keys | |||
| Round-trip preservation | |||
| Python bindings | |||
| ABI3 (py3.8+) | |||
| Type stubs (.pyi) | |||
| i18n error messages | |||
| Rust backend | |||
| Performance | Fastest | Slow | Slow |
Summary¶
Choose pyrs-yaml when¶
Recommended for most use cases
- Performance matters — 27–40× faster parsing and 276–316× faster serialization than PyYAML
- Round-trip preservation is critical — preserves comments, anchors, tags
- You want PyYAML compatibility — drop-in replacement API
- You need type hints — full
.pyistubs - You want a single wheel — ABI3 works across Python 3.8–3.15
Choose PyYAML when¶
Legacy compatibility only
- You're already using it and don't need round-trip preservation
- You need maximum compatibility with existing code
- Performance is not a concern
Choose ruamel.yaml when¶
Slowest option
- You need the most feature-complete YAML parser
- You're doing complex YAML manipulation
- Performance is not a concern (it's the slowest option)
Migration Path¶
# Step 1: Install
pip install pyrs-yaml
# Step 2: Replace import
# Before:
import yaml
# After:
import pyrs_yaml as yaml
# Step 3: Test
# Run your existing tests to verify compatibility
Most code will work without changes. The main differences:
- Round-trip output will preserve comments and formatting
- Error messages are more detailed and can be localized
- Performance will be significantly better