1616//!
1717//! This module provides the same interface as the C extension (bson._cbson)
1818//! but implemented in Rust using PyO3 and the bson library.
19+ //!
20+ //! # Implementation History
21+ //!
22+ //! This implementation was developed as part of PYTHON-5683 to investigate
23+ //! using Rust as an alternative to C for Python extension modules.
24+ //!
25+ //! See PR #2695 for the complete implementation history, including:
26+ //! - Initial implementation with 100% test compatibility
27+ //! - Performance optimizations (type caching, fast paths, direct conversions)
28+ //! - Architectural analysis comparing Rust vs C extension approaches
29+ //!
30+ //! # Performance
31+ //!
32+ //! Current performance: ~0.21x (5x slower than C extension)
33+ //! Root cause: Architectural difference (Python ↔ Bson ↔ bytes vs Python ↔ bytes)
34+ //! See README.md for detailed performance analysis and optimization opportunities.
1935
2036#![ allow( clippy:: useless_conversion) ]
2137
@@ -398,7 +414,8 @@ fn str_flags_to_int(options: &str) -> i32 {
398414 flags
399415}
400416
401- /// Test function for POC validation
417+ /// Test function to verify Rust extension is loaded
418+ /// Used by test suite to confirm PYMONGO_USE_RUST environment variable is working
402419#[ pyfunction]
403420fn _test_rust_extension ( py : Python ) -> PyResult < PyObject > {
404421 let result = PyDict :: new ( py) ;
@@ -805,8 +822,9 @@ fn _dict_to_bson(
805822) -> PyResult < Py < PyBytes > > {
806823 let codec_options = Some ( _codec_options) ;
807824
808- // COPILOT POC APPROACH: Use python_mapping_to_bson_doc for better performance
825+ // Use python_mapping_to_bson_doc for efficient encoding
809826 // This uses items() method and efficient tuple extraction
827+ // See PR #2695 for implementation details and performance analysis
810828 let doc = python_mapping_to_bson_doc ( obj, check_keys, codec_options, true )
811829 . map_err ( |e| {
812830 // Match C extension behavior: TypeError for non-mapping types, InvalidDocument for encoding errors
@@ -831,7 +849,7 @@ fn _dict_to_bson(
831849 }
832850 } ) ?;
833851
834- // Use to_writer() to write directly to buffer (like Copilot POC)
852+ // Use to_writer() to write directly to buffer
835853 // This is faster than bson::to_vec() which creates an intermediate Vec
836854 let mut buf = Vec :: new ( ) ;
837855 doc. to_writer ( & mut buf)
@@ -1444,7 +1462,7 @@ fn _bson_to_dict(
14441462}
14451463
14461464/// Process a single item from a mapping's items() iterator
1447- /// COPILOT POC APPROACH: Efficient tuple extraction
1465+ /// Uses efficient tuple extraction for performance
14481466fn process_mapping_item (
14491467 item : & Bound < ' _ , PyAny > ,
14501468 doc : & mut Document ,
@@ -1503,7 +1521,8 @@ fn process_mapping_item(
15031521}
15041522
15051523/// Convert a Python mapping (dict, SON, OrderedDict, etc.) to a BSON Document
1506- /// HYBRID APPROACH: Fast path for PyDict, Copilot POC approach for other mappings
1524+ /// HYBRID APPROACH: Fast path for PyDict, items() method for other mappings
1525+ /// See PR #2695 for implementation details and performance benchmarks
15071526fn python_mapping_to_bson_doc (
15081527 obj : & Bound < ' _ , PyAny > ,
15091528 check_keys : bool ,
@@ -1583,7 +1602,7 @@ fn python_mapping_to_bson_doc(
15831602 }
15841603
15851604 // SLOW PATH: Fall back to mapping protocol for SON, OrderedDict, etc.
1586- // Use Copilot POC approach with items() method
1605+ // Use items() method for efficient iteration
15871606 if let Ok ( items_method) = obj. getattr ( "items" ) {
15881607 if let Ok ( items_result) = items_method. call0 ( ) {
15891608 // Try to downcast to PyList or PyTuple first for efficient iteration
0 commit comments