Skip to content

Benchmarks

Benchmarks

Benchmark environment

CodSpeed CI runs in simulation mode (mode: simulation), which counts instructions rather than wall-clock time. The wall-clock figures below come from scripts/bench_snapshot.py, which times batches of calls and records the fastest of nine batches; its readings are committed to .ci/perf-snapshot.json together with the interpreter, library versions and fixture sizes they were measured on. Treat the ratios as indicative — they depend on the interpreter, hardware, and document.

Performance benchmarks for pyrs-yaml. CodSpeed's simulation numbers track relative trends; the wall-clock tables below are batch-timed readings from one machine, reproducible with scripts/bench_snapshot.py.

Methodology

  • Tool: codspeed-divan-compat (Rust) and pytest-codspeed (Python) in CI; scripts/bench_snapshot.py for the wall-clock tables
  • Rounds: nine batches per case, each calibrated to at least 0.1 s; the fastest batch is reported
  • Metric: microseconds (Python wall-clock and Rust instrumented); CodSpeed reports instruction counts, not time

Parse Performance

YAML Size pyrs-yaml PyYAML ruamel.yaml yaml-rs ryaml Speedup vs PyYAML
Small (~100 B) 5.3 µs 144 µs 338 µs 1.9 µs 5.4 µs 27×
Medium (~690 B) 25 µs 994 µs 2.2 ms 10 µs 31 µs 40×
Large (~2 KB) 69 µs 2.3 ms 5.5 ms 26 µs 74 µs 34×

Serialize Performance

YAML Size pyrs-yaml PyYAML ruamel.yaml yaml-rs ryaml Speedup vs PyYAML
Small (~100 B) 0.28 µs 88 µs 220 µs 0.91 µs 3.6 µs 311×
Medium (~690 B) 1.4 µs 450 µs 1.3 ms 5.9 µs 15 µs 316×
Large (~2 KB) 4.8 µs 1.3 ms 3.0 ms 16 µs 42 µs 276×

Round-Trip Performance

YAML Size pyrs-yaml PyYAML ruamel.yaml yaml-rs ryaml Speedup vs PyYAML
Small (~100 B) 5.6 µs 252 µs 567 µs 2.8 µs 10 µs 45×
Medium (~690 B) 26 µs 1.3 ms 3.6 ms 17 µs 48 µs 50×
Large (~2 KB) 77 µs 3.9 ms 8.2 ms 47 µs 118 µs 51×

JSON

The same JSON text through three readers. orjson is the faster one on the large document; the row is published because it is the measurement, not because it flatters us.

Document pyrs-yaml json orjson Speedup vs json
Config (~123 B) 0.56 µs 1.3 µs 0.43 µs 2.3×
Large (~1.9 KB) 6.7 µs 9.4 µs 4.7 µs 1.4×

TOML

TOML text through pyrs_yaml.load_toml, the standard library's tomllib and rtoml — the toml-rs binding. rtoml decodes straight into a dictionary and still reads about 1.5× faster on both fixtures. The node model is no longer what we pay for: load_toml now walks the scanner's own table tree into Python, and what is left of the row is that tree. Measured on this host, the scan plus the tree already costs what rtoml's entire parse does, so the remaining target is to build no tree at all.

Document pyrs-yaml tomllib rtoml Speedup vs rtoml
Hot (~1.8 KB) 26 µs 204 µs 18 µs 0.7×
Config (~2.1 KB) 41 µs 361 µs 30 µs 0.7×

JSON5

One small JSON5 document against pyjson5, which is the only independent JSON5 reader in the ecosystem. We are the slower reader here, and that is why the JSON5 read path stays an open optimisation target.

Document pyrs-yaml pyjson5 Speedup vs pyjson5
Config (~83 B) 1.9 µs 0.65 µs 0.3×

Rust-Side Benchmarks (divan)

Measured by the Rust CodSpeed leg (cargo codspeed, simulation mode): these are instrumented times, not native wall-clock. The fixtures come from pyrs_yaml_core::bench_inputs, whose "small" document is 19 B — not the 95 B document used by the Python tables — so rows with the same name are not comparable.

Operation Time
Parse (small) 85.6 µs
Parse (medium) 277.4 µs
Parse (large) 840.6 µs
Parse (anchors) 254.3 µs
Parse (comments) 164.9 µs
Parse (block scalars) 123.8 µs
Serialize (small) 7.9 µs
Serialize (medium) 32.2 µs
Serialize (large) 76.1 µs
Serialize (anchors) 27.0 µs
Serialize (block scalars) 15.4 µs
Round-trip (small) 91.7 µs
Round-trip (medium) 303.1 µs
Round-trip (large) 910.0 µs

Key Takeaways

  1. pyrs-yaml parses 27–40× faster and serializes 276–316× faster than PyYAML
  2. pyrs-yaml is 64–89× faster at parsing and 634–888× faster at serializing than ruamel.yaml while matching its round-trip features
  3. Round-trip is 45–51× faster than PyYAML and 101–135× faster than ruamel.yaml
  4. The Rust-backed readers split the result — yaml-rs decodes plain objects rather than a round-trip tree, and parses 2.5–2.8× faster and round-trips 1.6–2.0× faster; pyrs-yaml serializes 3.2–4.1× faster in return
  5. Rust-side parsing avoids the Python call overhead; the simulation leg decomposes parse cost by operation
  6. Serialization stays well below parsing cost in the same simulation leg
  7. The speed advantage holds across all document sizes

Notes

  • Python tables: batch-timed wall-clock readings recorded 2026-11-04 by scripts/bench_snapshot.py; the snapshot (.ci/perf-snapshot.json) records the environment, library versions and fixture sizes
  • CodSpeed CI runs in simulation mode and reports instruction counts; use it for relative trends
  • Ratios are indicative and vary with interpreter, hardware, and document size
  • PyYAML benchmarks use safe_load/safe_dump (same safety guarantees)
  • ruamel.yaml benchmarks use default settings
  • yaml-rs and ryaml hand back plain Python objects, so their rows are not the same job as pyrs-yaml's, which builds the document a later edit can format-preserve
  • Python fixtures are 95 B / 687 B / 1949 B (tests/data/yaml_samples.py)