Fast YAML parser for Python with C++/SIMD backend
Project description
pyfastyaml
pyfastyaml — Fast YAML parser for Python with C++/SIMD backend. 12–18× faster than PyYAML (CSafeLoader/libyaml), 100–170× faster than PyYAML (SafeLoader), 150–220× faster than ruamel.yaml. API compatible with PyYAML for typical config use cases (Kubernetes, Ansible, CI, Docker Compose).
Features
- High performance: C++17 implementation with AVX2 SIMD on x86 — orders of magnitude faster than pure-Python parsers
- PyYAML compatible API:
loads(s),load(fp),load_all(s),dump(),dump_all()— drop-in replacement for config loading - Types: dict, list, str, int, float, bool, null, nested structures
- Indentation: 2-space (preferred), 4-space, tabs
- Flow syntax:
[a, b, c],{a: 1, b: 2}(incl. nested) - Multiline strings:
|(literal),>(folded) - Anchors and aliases:
&anchor,*alias - Merge key:
<<: *anchorand<<: [*a, *b] - Document markers:
---,...,%YAMLdirective
Installation
pip install pyfastyaml
Requires Python 3.10+ and a C++ compiler with C++17 support.
Usage
import pyfastyaml
# Parse string
data = pyfastyaml.loads("""
key: value
nested:
inner: 42
items:
- a
- b
""")
# {'key': 'value', 'nested': {'inner': 42}, 'items': ['a', 'b']}
# Parse file (path or file-like object)
data = pyfastyaml.load("config.yaml")
# Serialize to YAML
yaml_str = pyfastyaml.dump(data)
pyfastyaml.dump(data, stream=open("out.yaml", "w"))
API matches PyYAML for common cases:
# Drop-in replacement
import pyfastyaml as yaml # instead of import yaml
data = yaml.loads(yaml_string)
data = yaml.load("config.yaml")
Benchmark
Parsing time (μs, lower is better):
| Size | pyfastyaml | PyYAML (C) | PyYAML | ruamel |
|---|---|---|---|---|
| Small (~200 B) | ~2.3 | ~32 | ~229 | ~376 |
| Medium (~1.5 KB) | ~11 | ~134 | ~1,014 | ~1,689 |
| Large (~15 KB) | ~83 | ~1,483 | ~11,169 | ~17,954 |
| Realworld | ~6.4 | ~98 | ~833 | ~1,388 |
PyYAML (C) = CSafeLoader (libyaml extension). PyYAML = SafeLoader (pure Python).
Run benchmarks:
pip install pytest-benchmark PyYAML ruamel.yaml
pytest tests/test_benchmark.py -v --benchmark-only
PyYAML Compatibility
pyfastyaml produces the same result as yaml.safe_load() for block-style YAML typical of configs. Compatibility tests included in tests/test_pyyaml_compat.py.
Serialization (dump)
# Single document
yaml_str = pyfastyaml.dump({"a": 1, "b": [1, 2, 3]})
# Write to file (path or stream)
pyfastyaml.dump(data, "output.yaml")
pyfastyaml.dump(data, stream=open("out.yaml", "w"))
# Deterministic output (sorted keys)
yaml_str = pyfastyaml.dump(data, sort_keys=True)
# Multiple documents
yaml_str = pyfastyaml.dump_all([{"a": 1}, {"b": 2}])
Multi-document streams
docs = pyfastyaml.load_all("""
---
a: 1
---
b: 2
""")
# [{'a': 1}, {'b': 2}]
Limitations
Not yet supported:
- Custom tags
- Complex flow keys (
?syntax)
Development
pip install -e ".[dev]"
python setup.py build_ext --inplace
pytest tests/
Benchmark commands:
pytest tests/test_benchmark.py -v --benchmark-only
pytest tests/test_benchmark.py -v --benchmark-only --benchmark-autosave
pytest tests/test_benchmark.py -v --benchmark-only --benchmark-compare
Publishing (maintainers)
pip install build twine
python -m build
python -m twine upload dist/*
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyfastyaml-0.1.0b5.tar.gz.
File metadata
- Download URL: pyfastyaml-0.1.0b5.tar.gz
- Upload date:
- Size: 27.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6d7403b1943997b44906269e10d3759ca1d82a4a2be9ffcd2c48c97db5b4ef7
|
|
| MD5 |
18085264c1771cedd577e2e5b1ef1147
|
|
| BLAKE2b-256 |
0aebeaabebc1b0141be1d2024c869ff365c31683587ac1c11de8130d09dedf26
|
File details
Details for the file pyfastyaml-0.1.0b5-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: pyfastyaml-0.1.0b5-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 90.9 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4722ddde735bab26c7e8e238bc2b4b3b66eb40c7b803060d48f38490757922d
|
|
| MD5 |
509724c0fbaa5cd3898706ca871604ad
|
|
| BLAKE2b-256 |
fe78c8552724c7c442764035e0620ce8470596b166bf96406f50b3103489482a
|