Skip to content

Commit 9a58868

Browse files
committed
Implementing litedb-org#2698
1 parent 97ec522 commit 9a58868

2 files changed

Lines changed: 219 additions & 106 deletions

File tree

LiteDBX.Tests/Document/ObjectId_Tests.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using FluentAssertions;
1+
using System;
2+
using FluentAssertions;
23
using Xunit;
34

45
namespace LiteDbX.Tests.Document;
@@ -41,4 +42,44 @@ public void ObjectId_Equals_Null_Does_Not_Throw()
4142
oid1.Equals(null).Should().BeFalse();
4243
oid1.Equals(oid0).Should().BeFalse();
4344
}
45+
46+
47+
[Fact]
48+
public void ObjectId_ToString_Minimizes_Allocations()
49+
{
50+
var objectId = ObjectId.NewObjectId();
51+
52+
objectId.ToString();
53+
54+
GC.Collect();
55+
GC.WaitForPendingFinalizers();
56+
GC.Collect();
57+
58+
var before = GC.GetAllocatedBytesForCurrentThread();
59+
var hex = objectId.ToString();
60+
var allocated = GC.GetAllocatedBytesForCurrentThread() - before;
61+
62+
hex.Should().HaveLength(24);
63+
allocated.Should().BeLessThan(220);
64+
}
65+
66+
[Fact]
67+
public void ObjectId_FromHex_Minimizes_Allocations()
68+
{
69+
var original = ObjectId.NewObjectId();
70+
var hex = original.ToString();
71+
72+
_ = new ObjectId(hex);
73+
74+
GC.Collect();
75+
GC.WaitForPendingFinalizers();
76+
GC.Collect();
77+
78+
var before = GC.GetAllocatedBytesForCurrentThread();
79+
var parsed = new ObjectId(hex);
80+
var allocated = GC.GetAllocatedBytesForCurrentThread() - before;
81+
82+
parsed.Should().Be(original);
83+
allocated.Should().BeLessThan(220);
84+
}
4485
}

0 commit comments

Comments
 (0)