From edbaa18a35df30e09ba11d6835e964a20ba9ba2a Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Fri, 10 Jan 2025 12:08:42 -0800 Subject: [PATCH] PYTHON-4991 Fix perf client.bulkWrite perf regression --- pymongo/message.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pymongo/message.py b/pymongo/message.py index b6c00f06cb..5a630f08b2 100644 --- a/pymongo/message.py +++ b/pymongo/message.py @@ -24,7 +24,6 @@ import datetime import random import struct -from collections import ChainMap from io import BytesIO as _BytesIO from typing import ( TYPE_CHECKING, @@ -1117,14 +1116,14 @@ def _check_doc_size_limits( op_doc[op_type] = ns_info[namespace] # type: ignore[index] # Since the data document itself is nested within the insert document - # it won't be automatically re-ordered by the BSON conversion. - # We use ChainMap here to make the _id field the first field instead. + # it won't be automatically re-ordered by the BSON conversion, so we need + # to encode it directly as a top-level document first. doc_to_encode = op_doc if real_op_type == "insert": doc = op_doc["document"] if not isinstance(doc, RawBSONDocument): doc_to_encode = op_doc.copy() # type: ignore[attr-defined] # Shallow copy - doc_to_encode["document"] = ChainMap(doc, {"_id": doc["_id"]}) # type: ignore[index] + doc_to_encode["document"] = RawBSONDocument(_dict_to_bson(doc, False, opts)) # type: ignore[index] # Encode current operation doc and, if newly added, namespace doc. op_doc_encoded = _dict_to_bson(doc_to_encode, False, opts)