PyO3 lets Python libraries run Rust, but the return trip costs more than the parse
Pydantic v2's core, pydantic-core, uses PyO3 to compile Rust into a shared library that Python imports like any package. The author walks through a JSON parser in four steps: write a Rust module, annotate with PyO3 macros, build with maturin, import the result. The key takeaway: if the function returns a scalar, the boundary cost is negligible; if it returns a large structure (e.g., a JSON tree), converting Rust values to Python objects can cost more than the parse itself. 100,000 values means 100,000 Python objects created at the boundary—the materialization loop, not the parsing, dominates end-to-end time. The post doesn't spell out specific latency numbers but suggests returning lazy Rust-backed views instead of materializing the full tree.