Skip to content

Commit 8360dd2

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)
1 parent bca7b7b commit 8360dd2

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

source/fundamentals/serialization.txt

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

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

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

source/whats-new.txt

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

298298
- Added support for serialization of ``Memory`` and ``ReadOnlyMemory``
299-
structs.
299+
structs. To learn more about implementing these types, see the
300+
:ref:`csharp-array-serialization` section of the Serialization guide.
300301

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

0 commit comments

Comments
 (0)