Skip to main content

Fast YAML parser for Python with C++/SIMD backend

Project description

pyfastyaml

PyPI version

pyfastyaml — Fast YAML parser for Python with C++/SIMD backend. 100–300× faster than PyYAML and 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: <<: *anchor and <<: [*a, *b]
  • Document markers: ---, ..., %YAML directive

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 ruamel pyfastyaml vs PyYAML
Small (~200 B) ~2 ~229 ~377 ~112× faster
Medium (~1.5 KB) ~7 ~1,034 ~1,777 ~141× faster
Large (~15 KB) ~66 ~11,153 ~19,040 ~170× faster
Realworld ~5 ~841 ~1,544 ~161× faster

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyfastyaml-0.1.0b4.tar.gz (26.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyfastyaml-0.1.0b4-cp312-cp312-win_amd64.whl (90.8 kB view details)

Uploaded CPython 3.12Windows x86-64

File details

Details for the file pyfastyaml-0.1.0b4.tar.gz.

File metadata

  • Download URL: pyfastyaml-0.1.0b4.tar.gz
  • Upload date:
  • Size: 26.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for pyfastyaml-0.1.0b4.tar.gz
Algorithm Hash digest
SHA256 9536e647a8cd35558d767b7793a7f1c9744f23c2c8eae43a1fdff7bf32a27832
MD5 30fdd41aefbff4fbdb829bb849f61f1a
BLAKE2b-256 e0b802d36c324b6a630d2b247e32a338989537368ed46508cd871128bc5bdd65

See more details on using hashes here.

File details

Details for the file pyfastyaml-0.1.0b4-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for pyfastyaml-0.1.0b4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6264040dbf0b9f189bdb7c6026cf295501d30ec3c9501e475fac95448ab4c69b
MD5 6821eb0e25969aff840f7cfcd56bd99f
BLAKE2b-256 79177ab6fc6c21c602a289da2656211d50685e6019f46a1f50e5f06e1718139b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page