The generated serializers currently emit code like this for nested model deserialization (example from AvailabilitySetData.Serialization.cs#L184 in azure-sdk-for-net):
systemData = ModelReaderWriter.Read<SystemData>(
new BinaryData(Encoding.UTF8.GetBytes(property.Value.GetRawText())),
ModelSerializationExtensions.WireOptions,
AzureResourceManagerComputeContext.Default);
This pattern does two extra allocations/copies per property:
GetRawText() allocates a transcoded UTF-16 string from the underlying UTF-8 bytes.
Encoding.UTF8.GetBytes(...) then re-encodes that string back into a new UTF-8 byte array.
On net9.0+ we can avoid both by using JsonMarshal.GetRawUtf8Value(element), which returns the raw UTF-8 span directly from the JsonDocument backing buffer. An extension method already exists in the repo demonstrating the pattern:
https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/System.ClientModel/tests/client/ModelReaderWriter/JsonElementExtensions.cs
public static BinaryData GetUtf8Bytes(this JsonElement element)
{
#if NET9_0_OR_GREATER
return new BinaryData(JsonMarshal.GetRawUtf8Value(element).ToArray());
#else
return BinaryData.FromString(element.GetRawText());
#endif
}
Ask
Update the emitter so the generated deserialization code uses this extension (e.g. property.Value.GetUtf8Bytes()) instead of the Encoding.UTF8.GetBytes(GetRawText()) pattern. This will reduce allocations and improve deserialization throughput on the modern TFMs we now target.
The generated serializers currently emit code like this for nested model deserialization (example from
AvailabilitySetData.Serialization.cs#L184in azure-sdk-for-net):This pattern does two extra allocations/copies per property:
GetRawText()allocates a transcoded UTF-16stringfrom the underlying UTF-8 bytes.Encoding.UTF8.GetBytes(...)then re-encodes that string back into a new UTF-8 byte array.On
net9.0+ we can avoid both by usingJsonMarshal.GetRawUtf8Value(element), which returns the raw UTF-8 span directly from theJsonDocumentbacking buffer. An extension method already exists in the repo demonstrating the pattern:https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/System.ClientModel/tests/client/ModelReaderWriter/JsonElementExtensions.cs
Ask
Update the emitter so the generated deserialization code uses this extension (e.g.
property.Value.GetUtf8Bytes()) instead of theEncoding.UTF8.GetBytes(GetRawText())pattern. This will reduce allocations and improve deserialization throughput on the modern TFMs we now target.