PYTHON-5737 - BSON encoding/decoding performance improvements#2715
Conversation
There was a problem hiding this comment.
Pull request overview
This PR implements three performance optimizations to the BSON C extension to improve encode/decode performance by up to ~25% on Python 3.14, bringing it close to Python 3.11 performance levels.
Changes:
- Skip expensive
_type_markerattribute lookups for common built-in types that are known not to have this attribute - Use direct
PyDict_New()API instead of genericPyObject_CallObject()when document_class is dict - Use direct
PyDict_SetItem()API instead of genericPyObject_SetItem()when document_class is dict
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2715 +/- ##
==========================================
+ Coverage 87.55% 87.57% +0.01%
==========================================
Files 141 141
Lines 24098 24182 +84
Branches 4118 4146 +28
==========================================
+ Hits 21100 21177 +77
- Misses 2108 2114 +6
- Partials 890 891 +1 Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
sleepyStick
left a comment
There was a problem hiding this comment.
lol this sent me straight to reading docs land so it took me a hot minute to review but i've learned!

PYTHON-5737
Changes in this PR
Add four optimizations to the C extension BSON implementation:
These changes result in the following BSON benchmark performance improvements on Python 3.14.3:
This represents up to a ~25% improvement for some benchmarks and brings them close to the high mark of Python 3.11 performance.
Test Plan
Verified with the existing test suite.
Checklist
Checklist for Author
Checklist for Reviewer