Rust-accelerated backend for markdown-it-py
markdown-it-accel provides a drop-in Rust-accelerated backend for markdown-it-py, offering significant performance improvements for markdown rendering while maintaining full compatibility with the original Python implementation.
- Blazing fast performance: Up to 80x speedup on typical documents, 200x+ on large documents
- Drop-in acceleration: Simply import and apply to existing markdown-it-py instances
- Automatic fallback: Gracefully falls back to Python implementation when needed
- Full CommonMark support: Powered by pulldown-cmark
- Cross-platform: Pre-built wheels for Linux, macOS, and Windows
- Python 3.8+ support: Compatible with modern Python versions
Install from PyPI:
pip install markdown-it-accelFor development or building from source:
# Clone the repository
git clone https://github.com/ChungNYCU/markdown-it-accel.git
cd markdown-it-accel
# Install Rust (if not already installed)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Install in development mode
pip install maturin
maturin developfrom markdown_it import MarkdownIt
from markdown_it_accel import use_rust_core
# Create a markdown-it-py instance
md = MarkdownIt()
# Apply Rust acceleration
use_rust_core(md)
# Use as normal - now with Rust speed!
html = md.render("# Hello, *World*!")
print(html) # <h1>Hello, <em>World</em>!</h1>Based on comprehensive benchmarking, markdown-it-accel delivers significant performance improvements:
Overall Speedup: 67x faster on average
| Document Type | Size | Python Time | Rust Time | Speedup |
|---|---|---|---|---|
| BIG.md | 8.0KB | 5.69ms | 71.3μs | 79.88x |
| CODE_HEAVY.md | 8.7KB | 1.28ms | 27.6μs | 46.56x |
| HUGE.md | 434KB | 230.80ms | 3.57ms | 64.68x |
| TABLE_HEAVY.md | 2.1KB | 3.55ms | 46.5μs | 76.36x |
Compared to other Python markdown libraries:
| Library | Time | Throughput | Relative Performance |
|---|---|---|---|
| markdown-it-accel (Rust) | 80.1μs | 100.5M chars/sec | Fastest |
| markdown-it-py (Python) | 5.72ms | 1.4M chars/sec | 71x slower |
| python-markdown | 25.00ms | 322K chars/sec | 312x slower |
Performance scales excellently with document size:
- 1x size: 38x speedup
- 10x size: 132x speedup
- 50x size: 201x speedup
Run benchmarks on your system:
# Comprehensive benchmark suite
python bench/scripts/benchmark.py
# Library comparison
python bench/scripts/compare_libraries.pyMARKDOWN_IT_ACCEL=0: Disable Rust acceleration (fallback to Python)MARKDOWN_IT_ACCEL=1: Enable Rust acceleration (default)
# Disable Rust acceleration
MARKDOWN_IT_ACCEL=0 python your_script.py
# Enable Rust acceleration (default behavior)
MARKDOWN_IT_ACCEL=1 python your_script.pyfrom markdown_it_accel import is_available, get_version
print(f"Rust acceleration available: {is_available()}")
print(get_version())- Headers (
#,##, etc.) - Emphasis (
*italic*,**bold**) - Links (
[text](url)) - Images (
) - Lists (ordered and unordered)
- Code blocks (fenced and indented)
- Blockquotes (
>) - Tables (GFM extension)
- Strikethrough (
~~text~~) - Line breaks and paragraphs
The library automatically falls back to the Python implementation when:
- Rust extension is not available (installation without binary wheels)
- Unsupported syntax is detected
- Rust renderer encounters an error
This ensures your code continues to work regardless of the environment.
Currently, some advanced markdown-it-py features may trigger fallback:
- Custom plugins and renderers
- Complex HTML blocks
- Math expressions (
$$...$$) - Mermaid diagrams
- Python 3.8+
- Rust 1.70+
- maturin
# Clone and enter directory
git clone https://github.com/ChungNYCU/markdown-it-accel.git
cd markdown-it-accel
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # or .venv\Scripts\activate on Windows
# Install dependencies
pip install -e .[dev]# Development build
maturin develop
# Release build
maturin develop --release
# Build wheels
maturin build --release# Run tests
pytest tests/
# Run tests with coverage
pytest tests/ --cov=markdown_it_accel
# Run benchmarks
python bench/bench_render.py# Format Python code
black .
# Lint Python code
ruff check .
# Format Rust code
cargo fmt
# Lint Rust code
cargo clippyThe library consists of three main components:
- Rust Core (
src/lib.rs): PyO3 module using pulldown-cmark for fast HTML generation - Python Shim (
markdown_it_accel/_shim.py): Monkey-patches markdown-it-py instances - Fallback Logic: Automatic detection and graceful degradation
# Before acceleration
md = MarkdownIt()
html = md.render(text) # Pure Python
# After acceleration
use_rust_core(md)
html = md.render(text) # Rust + Python fallbackThe use_rust_core() function replaces the render method with a wrapper that:
- Checks if content is supported by Rust implementation
- Attempts fast Rust rendering via
pulldown-cmark - Falls back to original Python implementation on any issue
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for new functionality
- Ensure tests pass (
pytest) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- markdown-it-py - The Python markdown parser this library accelerates
- pulldown-cmark - The fast Rust markdown parser powering the acceleration
- PyO3 - Rust bindings for Python
- maturin - Build system for Rust-based Python extensions
- Initial release
- Basic CommonMark support with Rust acceleration
- Automatic fallback mechanism
- Cross-platform binary wheels
- Comprehensive test suite and benchmarks