Skip to content

Huge performance degradation using System.Text.Json to serialize a model with an Object type property #31103

@mrlund

Description

@mrlund

I've been troubleshooting some performance issues that came up during a core 2.2 -> 3.0 upgrade, and was stumped why a particular ASP.NET Core API endpoint showed terrible performance compared to the others.

After countless hours I finally discovered the cause; the model being serialized contained a child class, with a collection property that was simply defined with type Object, i.e. what should have been:

public IEnumerable<Table> Tables { get; set; }

was defined as:

public object Tables { get; set; }

The reasons for this odd coding choice are unfortunately lost in history, but it works fine, was insidiously hard to diagnose, and the perf impact was very severe, with the API going from ~300 requests/sec under local load tests to ~3300 requests/sec after fixing.

I assume that the poor performance is due to reflection being used to serialize when there's an unknown object type, and I guess it only gets worse because it's a list. It's not clear to me if there's a way of fixing this, or if it would be necessary to remove support for serializing Object altogether. Perhaps just log a warning to make it easier to catch?

I'd be happy to try my hand with a PR, if someone could point me in the right direction?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions