Skip to content

Commit ada3129

Browse files
committed
DOCSP-40227: memory serialization (#588)
* DOCSP-40227: memory serialization * add note * MM PR fixes 1 * BD tech review comments 1 (cherry picked from commit 611a739) (cherry picked from commit 8360dd2)
1 parent 9ba95cf commit ada3129

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

Diff for: source/fundamentals/serialization.txt

+56
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,62 @@ Implementing the `IBsonArraySerializer
138138
interface enables the driver to access serialization information for individual
139139
items in an array.
140140

141+
.. _csharp-array-serialization:
142+
143+
Improve Array Serialization Performance
144+
---------------------------------------
145+
146+
You can improve your application's performance by representing
147+
arrays of primitives as `Memory<T> <https://learn.microsoft.com/en-us/dotnet/api/system.memory-1?view=net-8.0>`__
148+
and `ReadOnlyMemory<T> <https://learn.microsoft.com/en-us/dotnet/api/system.readonlymemory-1?view=net-8.0>`__
149+
structs instead of by using types such as standard {+language+} arrays or
150+
``BsonArray`` objects. The driver implements fast serialization and
151+
deserialization paths for ``Memory<T>`` and ``ReadOnlyMemory<T>``, which
152+
enhances speed and reduces memory usage.
153+
154+
.. note::
155+
156+
Truncation and overflow checks are not supported for ``Memory<T>`` or
157+
``ReadOnlyMemory<T>``, but these checks are implemented for standard
158+
arrays.
159+
160+
You can effect these performance improvements by storing the following
161+
primitive types in ``Memory<T>`` or ``ReadOnlyMemory<T>`` structs:
162+
163+
- ``bool``
164+
- ``sbyte``
165+
- ``byte``
166+
- ``char``
167+
- ``short``
168+
- ``ushort``
169+
- ``int``
170+
- ``uint``
171+
- ``long``
172+
- ``ulong``
173+
- ``float``
174+
- ``double``
175+
- ``decimal``
176+
177+
The following example defines a ``Line`` POCO that contains array fields
178+
modeled by ``Memory`` and ``ReadOnlyMemory`` structs:
179+
180+
.. literalinclude:: /includes/fundamentals/code-examples/MemorySerialization.cs
181+
:start-after: start-line-class
182+
:end-before: end-line-class
183+
:language: csharp
184+
:dedent:
185+
186+
The following document represents how a sample ``Line`` object is
187+
represented in MongoDB:
188+
189+
.. code-block:: json
190+
191+
{
192+
"_id": ...,
193+
"X": [ 1, 2, 3, 4, 5 ],
194+
"Y": [ 1, 1.409999966621399, 1.7300000190734863, 2, 2.240000009536743 ]
195+
}
196+
141197
Additional Information
142198
----------------------
143199

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Bson.Serialization.Conventions;
3+
using MongoDB.Driver;
4+
5+
public class Program
6+
{
7+
public static void Main(string[] args)
8+
{
9+
// Replace with your connection string
10+
const string uri = "<connection string>";
11+
12+
var mongoClient = new MongoClient(uri);
13+
var database = mongoClient.GetDatabase("db");
14+
var _collection = database.GetCollection<Line>("lines");
15+
16+
var line = new Line
17+
{
18+
X = new Memory<int>(new[] { 1, 2, 3, 4, 5 }),
19+
Y = new ReadOnlyMemory<float>(new[] { 1f, 1.41f, 1.73f, 2f, 2.24f })
20+
};
21+
22+
var filter = Builders<Line>.Filter.Empty;
23+
24+
var result = _collection.Find(filter).FirstOrDefault().ToJson();
25+
Console.WriteLine(result);
26+
}
27+
28+
}
29+
30+
// start-line-class
31+
public class Line
32+
{
33+
public ObjectId Id { get; set; }
34+
public Memory<int> X { get; set; }
35+
public ReadOnlyMemory<float> Y { get; set; }
36+
}
37+
// end-line-class

Diff for: source/whats-new.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ The 2.26 driver release includes the following new features:
112112
- Enabled use of native ``crypto`` in ``libmongocrypt`` bindings.
113113

114114
- Added support for serialization of ``Memory`` and ``ReadOnlyMemory``
115-
structs.
115+
structs. To learn more about implementing these types, see the
116+
:ref:`csharp-array-serialization` section of the Serialization guide.
116117

117118
- Added support for the GCP Identity Provider when using the
118119
``MONGODB-OIDC`` authentication mechanism. To learn more, see

0 commit comments

Comments
 (0)