Skip to content

Commit a913d58

Browse files
committed
- PR comments
1 parent 918ecac commit a913d58

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ public TClass DeserializeClass(BsonDeserializationContext context)
152152
var allMemberMaps = _classMap.AllMemberMaps;
153153
var extraElementsMemberMapIndex = _classMap.ExtraElementsMemberMapIndex;
154154

155-
var (bitArrayLength, useStackAlloc) = FastMemberMapHelper.GetMembersBitArrayLength(_classMap.AllMemberMaps.Count);
156-
using var bitArray = useStackAlloc ? FastMemberMapHelper.GetMembersBitArray(stackalloc uint[bitArrayLength]) : FastMemberMapHelper.GetMembersBitArray(bitArrayLength);
155+
var (lengthInUInts, useStackAlloc) = FastMemberMapHelper.GetLengthInUInts(_classMap.AllMemberMaps.Count);
156+
using var bitArray = useStackAlloc ? FastMemberMapHelper.GetMembersBitArray(stackalloc uint[lengthInUInts]) : FastMemberMapHelper.GetMembersBitArray(lengthInUInts);
157157

158158
bsonReader.ReadStartDocument();
159159
var elementTrie = _classMap.ElementTrie;
@@ -702,11 +702,11 @@ public MembersBitArray(Span<uint> bitArray) : this()
702702
_bitArray.Clear();
703703
}
704704

705-
public MembersBitArray(int spanLength, uint[] rentedBuffer, ArrayPool<uint> arrayPool) : this()
705+
public MembersBitArray(int lengthInUints, ArrayPool<uint> arrayPool) : this()
706706
{
707707
_arrayPool = arrayPool;
708-
_bitArray = rentedBuffer.AsSpan(0, spanLength);
709-
_rentedBuffer = rentedBuffer;
708+
_rentedBuffer = arrayPool.Rent(lengthInUints);
709+
_bitArray = _rentedBuffer.AsSpan(0, lengthInUints);
710710

711711
_bitArray.Clear();
712712
}
@@ -730,7 +730,7 @@ public void Dispose()
730730
}
731731
}
732732

733-
public static (int BitArrayLength, bool UseStackAlloc) GetMembersBitArrayLength(int membersCount)
733+
public static (int LengthInUInts, bool UseStackAlloc) GetLengthInUInts(int membersCount)
734734
{
735735
var length = (membersCount + 31) >> 5;
736736
return (length, length <= 8); // Use stackalloc for up to 256 members
@@ -740,7 +740,7 @@ public static MembersBitArray GetMembersBitArray(Span<uint> span) =>
740740
new(span);
741741

742742
public static MembersBitArray GetMembersBitArray(int length) =>
743-
new(length, ArrayPool<uint>.Shared.Rent(length), ArrayPool<uint>.Shared);
743+
new(length, ArrayPool<uint>.Shared);
744744
}
745745
}
746746
}

tests/MongoDB.Bson.Tests/Serialization/BsonClassMapSerializerTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,11 @@ public void Equals_with_not_equal_field_should_return_false()
197197
[InlineData(33, 2, true)]
198198
[InlineData(256, 8, true)]
199199
[InlineData(257, 9, false)]
200-
public void FastMemberMapHelper_GetMembersBitArrayLength_should_return_correctValue(int memberCount, int expectedBitArrayLength, bool expectedUseStackAlloc)
200+
public void FastMemberMapHelper_GetMembersBitArrayLength_should_return_correctValue(int memberCount, int expectedLengthInUInts, bool expectedUseStackAlloc)
201201
{
202-
var (bitArrayLength, useStackAlloc) = BsonClassMapSerializer<MyModel>.FastMemberMapHelper.GetMembersBitArrayLength(memberCount);
202+
var (lengthInUInts, useStackAlloc) = BsonClassMapSerializer<MyModel>.FastMemberMapHelper.GetLengthInUInts(memberCount);
203203

204-
bitArrayLength.ShouldBeEquivalentTo(expectedBitArrayLength);
204+
lengthInUInts.ShouldBeEquivalentTo(expectedLengthInUInts);
205205
useStackAlloc.ShouldBeEquivalentTo(expectedUseStackAlloc);
206206
}
207207

@@ -237,7 +237,8 @@ public void FastMemberMapHelper_MembersBitArray_with_arraypool_should_dispose_on
237237
var backingArray = new uint[] { 1, 2, 3 };
238238

239239
var mockArrayPool = new Mock<ArrayPool<uint>>();
240-
var bitArray = new BsonClassMapSerializer<MyModel>.FastMemberMapHelper.MembersBitArray(backingArray.Length, backingArray, mockArrayPool.Object);
240+
mockArrayPool.Setup(p => p.Rent(backingArray.Length)).Returns(backingArray);
241+
var bitArray = new BsonClassMapSerializer<MyModel>.FastMemberMapHelper.MembersBitArray(backingArray.Length, mockArrayPool.Object);
241242

242243
for (int i = 0; i < disposeCount; i++)
243244
{
@@ -257,7 +258,7 @@ public void FastMemberMapHelper_MembersBitArray_with_arraypool_should_dispose_on
257258
[InlineData(621, 255)]
258259
public void FastMemberMapHelper_GetMembersBitArray_SetMemberIndex_should_set_correct_bit(int membersCount, int memberIndex)
259260
{
260-
var (length, _) = BsonClassMapSerializer<MyModel>.FastMemberMapHelper.GetMembersBitArrayLength(membersCount);
261+
var (length, _) = BsonClassMapSerializer<MyModel>.FastMemberMapHelper.GetLengthInUInts(membersCount);
261262
using var bitArray = BsonClassMapSerializer<MyModel>.FastMemberMapHelper.GetMembersBitArray(length);
262263

263264
var span = bitArray.Span;

0 commit comments

Comments
 (0)