Skip to content

Commit 611a739

Browse files
authored
DOCSP-40227: memory serialization (#588)
* DOCSP-40227: memory serialization * add note * MM PR fixes 1 * BD tech review comments 1
1 parent 7fe3954 commit 611a739

File tree

4 files changed

+114
-3
lines changed

4 files changed

+114
-3
lines changed

Diff for: source/fundamentals/atlas-vector-search.txt

+10-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ To learn more about {+vector-search+}, see the :atlas:`{+vector-search+}
3838
</atlas-vector-search/vector-search-overview/>` guide in the MongoDB Atlas
3939
documentation.
4040

41+
.. _csharp-supported-vector-types:
42+
4143
Supported Vector Embedding Types
4244
--------------------------------
4345

@@ -50,7 +52,6 @@ search and retrieval.
5052
The {+driver-short+} supports vector embeddings of several types. The following
5153
sections describe the supported vector embedding types.
5254

53-
5455
.. _csharp-vector-array-representation:
5556

5657
Array Representations
@@ -71,6 +72,12 @@ The following example shows a class with properties of the preceding types:
7172
:start-after: start-bson-arrays
7273
:end-before: end-bson-arrays
7374

75+
.. tip::
76+
77+
To learn more about using the ``Memory`` and ``ReadOnlyMemory``
78+
types, see the :ref:`csharp-array-serialization` section of the
79+
Serialization guide.
80+
7481
.. _csharp-binary-vector-representation:
7582

7683
Binary Vector Representations
@@ -190,4 +197,5 @@ guide, see the following API Documentation:
190197
- `BinaryVectorFloat32 <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BinaryVectorPackedBit.html>`__
191198
- `ToQueryVector() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.BinaryVectorDriverExtensions.ToQueryVector.html>`__
192199
- `VectorSearch() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.AggregateFluentBase-1.VectorSearch.html>`__
193-
- `Aggregate() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.IMongoCollectionExtensions.Aggregate.html>`__
200+
- `Aggregate()
201+
<{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.IMongoCollectionExtensions.Aggregate.html>`__

Diff for: source/fundamentals/serialization.txt

+65
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,71 @@ specified conventions, then passing it to the
201201
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
202202
ConventionRegistry.Register("CamelCaseConvention", camelCaseConvention, t => true);
203203

204+
.. _csharp-array-serialization:
205+
206+
Improve Array Serialization Performance
207+
---------------------------------------
208+
209+
You can improve your application's performance by representing
210+
arrays of primitives as `Memory<T> <https://learn.microsoft.com/en-us/dotnet/api/system.memory-1?view=net-8.0>`__
211+
and `ReadOnlyMemory<T> <https://learn.microsoft.com/en-us/dotnet/api/system.readonlymemory-1?view=net-8.0>`__
212+
structs instead of by using types such as standard {+language+} arrays or
213+
``BsonArray`` objects. The driver implements fast serialization and
214+
deserialization paths for ``Memory<T>`` and ``ReadOnlyMemory<T>``, which
215+
enhances speed and reduces memory usage.
216+
217+
.. note::
218+
219+
Truncation and overflow checks are not supported for ``Memory<T>`` or
220+
``ReadOnlyMemory<T>``, but these checks are implemented for standard
221+
arrays.
222+
223+
You can effect these performance improvements by storing the following
224+
primitive types in ``Memory<T>`` or ``ReadOnlyMemory<T>`` structs:
225+
226+
- ``bool``
227+
- ``sbyte``
228+
- ``byte``
229+
- ``char``
230+
- ``short``
231+
- ``ushort``
232+
- ``int``
233+
- ``uint``
234+
- ``long``
235+
- ``ulong``
236+
- ``float``
237+
- ``double``
238+
- ``decimal``
239+
240+
The following example defines a ``Line`` POCO that contains array fields
241+
modeled by ``Memory`` and ``ReadOnlyMemory`` structs:
242+
243+
.. literalinclude:: /includes/fundamentals/code-examples/MemorySerialization.cs
244+
:start-after: start-line-class
245+
:end-before: end-line-class
246+
:language: csharp
247+
:dedent:
248+
249+
The following document represents how a sample ``Line`` object is
250+
represented in MongoDB:
251+
252+
.. code-block:: json
253+
254+
{
255+
"_id": ...,
256+
"X": [ 1, 2, 3, 4, 5 ],
257+
"Y": [ 1, 1.409999966621399, 1.7300000190734863, 2, 2.240000009536743 ]
258+
}
259+
260+
.. tip:: Model Vectors
261+
262+
:ref:`csharp-atlas-vector-search` involves creating and querying
263+
large numerical arrays. If your application uses
264+
{+vector-search+}, you might benefit from the performance
265+
improvements from using ``Memory`` and ``ReadOnlyMemory`` to store
266+
array representations of embeddings and query vectors. To learn more,
267+
see :ref:`csharp-supported-vector-types` in the {+vector-search+} guide.
268+
204269
Additional Information
205270
----------------------
206271

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
@@ -393,7 +393,8 @@ The 2.26 driver release includes the following new features:
393393
- Enabled use of native ``crypto`` in ``libmongocrypt`` bindings.
394394

395395
- Added support for serialization of ``Memory`` and ``ReadOnlyMemory``
396-
structs.
396+
structs. To learn more about implementing these types, see the
397+
:ref:`csharp-array-serialization` section of the Serialization guide.
397398

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

0 commit comments

Comments
 (0)