@@ -140,6 +140,62 @@ Implementing the `IBsonArraySerializer
140140interface enables the driver to access serialization information for individual
141141items 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+
143199Additional Information
144200----------------------
145201
0 commit comments