You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update Rust extension documentation to indicate experimental status
- Add prominent warning that this is NOT PRODUCTION READY
- Document test status: 86 core tests passing, ~85 tests skipped
- List unimplemented features:
- Custom type encoders (TypeEncoder, TypeRegistry, FallbackEncoder)
- RawBSONDocument codec options
- Some DBRef edge cases
- Complete type checking support
- Document skip mechanism using @skip_if_rust_bson marker
- Update module structure documentation (6 modules)
- Change status from 'production-ready' to 'experimental'
- Clarify performance limitations (~5x slower than C)
- Add clear recommendation to use C extension for production
⚠️ **NOT PRODUCTION READY** - This is an experimental implementation with incomplete feature support and performance limitations. See [Test Status](#test-status) and [Performance Analysis](#performance-analysis) sections below.
4
+
3
5
This directory contains a Rust-based implementation of BSON encoding/decoding for PyMongo, developed as part of [PYTHON-5683](https://jira.mongodb.org/browse/PYTHON-5683).
4
6
5
7
## Overview
6
8
7
-
The Rust extension (`_rbson`) provides the same interface as the C extension (`_cbson`) but is implemented in Rust using:
9
+
The Rust extension (`_rbson`) provides a **partial implementation** of the C extension (`_cbson`) interface, implemented in Rust using:
8
10
-**PyO3**: Python bindings for Rust
9
11
-**bson crate**: MongoDB's official Rust BSON library
10
12
-**Maturin**: Build tool for Rust Python extensions
11
13
14
+
## Test Status
15
+
16
+
### ✅ Core BSON Tests: 86 passed, 2 skipped
17
+
The basic BSON encoding/decoding functionality works correctly (`test/test_bson.py`).
18
+
19
+
### ⏭️ Skipped Tests: ~85 tests across multiple test files
20
+
The following features are **not implemented** and tests are skipped when using the Rust extension:
21
+
22
+
#### Custom Type Encoders (test/test_custom_types.py)
23
+
-**`TypeEncoder` and `TypeRegistry`** - Custom type encoding/decoding
24
+
-**`FallbackEncoder`** - Fallback encoding for unknown types
25
+
-**Tests skipped**: All tests in `TestBSONFallbackEncoder`, `TestCustomPythonBSONTypeToBSONMonolithicCodec`, `TestCustomPythonBSONTypeToBSONMultiplexedCodec`
26
+
-**Reason**: Rust extension doesn't support custom type encoders or fallback encoders
27
+
28
+
#### RawBSONDocument (test/test_raw_bson.py)
29
+
-**`RawBSONDocument` codec options** - Raw BSON document handling
30
+
-**Tests skipped**: All tests in `TestRawBSONDocument`
-**Reason**: Incomplete DBRef handling in Rust extension
37
+
38
+
#### Type Checking (test/test_typing.py)
39
+
-**Type hints and mypy validation**
40
+
-**Tests skipped**: Some typing tests
41
+
-**Reason**: Type checking issues with Rust extension
42
+
43
+
### Skip Mechanism
44
+
Tests are skipped using the `@skip_if_rust_bson` pytest marker defined in `test/__init__.py`:
45
+
```python
46
+
skip_if_rust_bson = pytest.mark.skipif(
47
+
_use_rust_bson(), reason="Rust BSON extension does not support this feature"
48
+
)
49
+
```
50
+
51
+
This marker is applied to test classes and methods that use unimplemented features.
52
+
12
53
## Implementation History
13
54
14
55
This implementation was developed through [PR #2695](https://github.com/mongodb/mongo-python-driver/pull/2695) to investigate using Rust as an alternative to C for Python extension modules.
15
56
16
57
### Key Milestones
17
58
18
-
1.**Initial Implementation** - Complete BSON type support with 100% test compatibility (88/88 tests passing)
59
+
1.**Initial Implementation** - Basic BSON type support with core functionality
19
60
2.**Performance Optimizations** - Type caching, fast paths for common types, direct byte operations
20
-
3.**Architectural Analysis** - Identified fundamental performance differences between Rust and C approaches
61
+
3.**Modular Refactoring** - Split monolithic lib.rs into 6 well-organized modules
62
+
4.**Test Integration** - Added skip markers for unimplemented features (~85 tests skipped)
**Missing Features** (see [Test Status](#test-status)):
220
+
- ❌ **Custom type encoders** (`TypeEncoder`, `TypeRegistry`, `FallbackEncoder`)
221
+
- ❌ **RawBSONDocument** codec options
222
+
- ❌ **Some DBRef edge cases**
223
+
- ❌ **Complete type checking support**
224
+
175
225
**Performance Reality**: ~0.21x (5x slower than C) - see Performance Analysis section
176
226
177
227
**Key Insights**:
178
228
1.**Same Architecture, Different Results**: Both implementations use the same Rust architecture (PyO3 + bson crate with intermediate `Bson` enum), so the build system (cargo vs maturin) is not the cause of the performance difference.
179
-
2.**Incomplete vs Complete**: The POC's speed claims were based on incomplete functionality (60% test pass rate). Achieving 100% compatibility revealed the true performance cost of:
- BSON validation (size checks, null terminators, extra bytes)
182
-
- Production-ready error handling
183
-
- Edge case handling for all 88 tests
184
-
3.**The Fundamental Issue**: Both implementations suffer from the same architectural limitation (Python → Bson enum → bytes), but it only becomes a significant bottleneck when you implement all the features required for production use.
229
+
2.**Incomplete Implementation**: The current implementation has ~85 tests skipped due to unimplemented features (custom type encoders, RawBSONDocument, etc.). This is an experimental implementation, not production-ready.
230
+
3.**The Fundamental Issue**: The Rust architecture (Python → Bson enum → bytes) has inherent performance limitations compared to the C extension's direct byte-writing approach.
185
231
186
232
## Direct Byte-Writing Performance Results
187
233
@@ -317,34 +363,70 @@ maturin develop --release
317
363
318
364
## Testing
319
365
320
-
Run the test suite with the Rust extension:
366
+
Run the core BSON test suite with the Rust extension:
1. ✅ **Rust can provide a complete, production-ready BSON implementation**
334
-
2.✅**100% compatibility with existing tests and APIs is achievable**
403
+
1. ✅ **Rust can provide basic BSON encoding/decoding functionality**
404
+
2.❌**Complete feature parity with C extension is not achieved** (~85 tests skipped)
335
405
3. ❌ **Performance parity with C requires bypassing the `bson` crate**
336
406
4. ❌ **The engineering effort may not justify the benefits**
337
407
338
408
### Recommendation
339
409
340
-
The Rust extension is **production-ready** from a correctness standpoint but **not recommended** for performance-critical applications. The C extension remains the better choice for performance.
410
+
⚠️ **NOT PRODUCTION READY** - The Rust extension is **experimental** and has significant limitations:
411
+
412
+
**Missing Features:**
413
+
- Custom type encoders (`TypeEncoder`, `TypeRegistry`, `FallbackEncoder`)
414
+
- RawBSONDocument codec options
415
+
- Some DBRef edge cases
416
+
- Complete type checking support
417
+
418
+
**Performance Issues:**
419
+
-~5x slower than C extension (0.21x performance)
420
+
- Even with direct byte-writing optimizations, still ~2.3x slower (0.43x performance)
341
421
342
422
**Use Cases for Rust Extension:**
343
-
-Platforms where C compilation is difficult (e.g., WebAssembly)
344
-
-Development environments without C toolchain
345
-
-Testing and validation purposes
423
+
-**Experimental/research purposes only**
424
+
-Testing Rust-Python interop with PyO3
425
+
-Platforms where C compilation is difficult (with caveats about missing features)
346
426
- Future exploration if `bson` crate performance improves
347
427
428
+
**For production use, the C extension (`_cbson`) is strongly recommended.**
0 commit comments