Skip to content

Avoid closures in JsonPropertyInfoOfT/JsonTypeInfoOfT #117498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

MichalStrehovsky
Copy link
Member

The lambda capture of the local variable seems avoidable in these places.

The lambda capture of the local variable seems avoidable in these places.
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@MichalStrehovsky
Copy link
Member Author

20 kB saving in the TodosApi app (one of the benchmarks we use in the ASP.NET perf lab):

Size statistics

Pull request #117498

Project Size before Size after Difference
TodosApi-linux 25567328 25546848 -20480
TodosApi-windows 22930944 22915584 -15360

@MichalStrehovsky MichalStrehovsky marked this pull request as ready for review July 10, 2025 11:02
@Copilot Copilot AI review requested due to automatic review settings July 10, 2025 11:02
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors how factory and accessor delegates are set in JsonTypeInfoOfT and JsonPropertyInfoOfT to eliminate lambda closures over local variables and instead reference instance fields directly, reducing allocations.

  • In JsonTypeInfoOfT.SetCreateObject, the untyped and typed object-creation lambdas now call the stored fields rather than capturing the local delegate.
  • In JsonPropertyInfoOfT, the getter, setter, and should-serialize delegates are similarly rewritten to reference _typedGet/_untypedGet, _typedSet/_untypedSet, and _shouldSerializeTyped/_shouldSerialize fields.
  • Overall change improves performance by avoiding unnecessary closure allocations.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/libraries/System.Text.Json/.../JsonTypeInfoOfT.cs Updated SetCreateObject to use instance fields in lambdas instead of local captures.
src/libraries/System.Text.Json/.../JsonPropertyInfoOfT.cs Updated SetGetter, SetSetter, and SetShouldSerialize to use fields, avoiding closures.

}
else
{
Func<object, object?> untypedGet = (Func<object, object?>)getter;
_typedGet = (obj => (T)untypedGet(obj)!);
_typedGet = (obj => (T)_untypedGet!(obj)!);
_untypedGet = untypedGet;
Copy link
Member

@stephentoub stephentoub Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: it doesn't actually matter, because the delegate won't be invoked yet, but could we switch these two lines so that _untypedGet is set before it's referenced? I find it a bit easier to reason about. (You might also be able to remove the !, I'm not sure)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hypothetically, if I called the getter to store a copy of the existing delegate, but then set a new delegate via the setter, my copy would start pointing to the new behaviour. I think that makes a subtle difference that could regress code that is heavy on contract customization.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hypothetically, if I called the getter to store a copy of the existing delegate, but then set a new delegate via the setter, my copy would start pointing to the new behaviour. I think that makes a subtle difference that could regress code that is heavy on contract customization.

This is in reference to the PR, not to my nit, yes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean I should close this?

I don't mind either way. This is a drop in the bucket. I'm looking at a relatively big app (40 MB) where 15% of the app is System.Text.Json because of how many generics S.T.Json will create for every type used with Json. This looked like something we could do something about, the rest I don't know how to fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean I should close this?

Yeah I think so. It doesn't change things much either way so I would err towards preserving the current behaviour.

}
else
{
Action<object, object?> untypedSet = (Action<object, object?>)setter;
_typedSet = ((obj, value) => untypedSet(obj, value));
_typedSet = ((obj, value) => _untypedSet!(obj, value));
_untypedSet = untypedSet;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

}
else
{
Func<object, object?, bool> untypedPredicate = (Func<object, object?, bool>)predicate;
_shouldSerializeTyped = (obj, value) => untypedPredicate(obj, value);
_shouldSerializeTyped = (obj, value) => _shouldSerialize!(obj, value);
_shouldSerialize = untypedPredicate;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants