Skip to content

Improve polymorphic performance of (de)serialization #38637

Closed
@steveharter

Description

@steveharter

Scenarios that use typeof(System.Object) are significantly slower since:

  • During serialization, the object's type is obtained and a dictionary lookup is performed to obtain the correct "converter". The dictionary lookup is expensive. It is possible to improve the caching here, especially when serializing a collection of objects where all elements are the same Type.
  • During deserialization, the converter for System.Object creates a JsonElement to represent the object. For primitive types like boolean, the cost is high. It is possible to improve performance here by avoiding extra array pool allocations that are created and then released via Dispose(), so going from this:
using (JsonDocument document = JsonDocument.ParseValue(ref reader))
{
    return document.RootElement.Clone();	
}

to

return JsonElement.Parse(ref reader);

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions