@@ -138,6 +138,62 @@ Implementing the `IBsonArraySerializer
138138interface enables the driver to access serialization information for individual
139139items 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+ 
141197Additional Information
142198----------------------
143199
0 commit comments