Skip to content
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

[release/7.0] Fix delegate allocation in CachingContext.GetOrAddJsonTypeInfo #80513

Merged
merged 3 commits into from
Jan 12, 2023

Conversation

eiriktsarpalis
Copy link
Member

Fixes #80430.

Customer Impact

Fixes a customer reported performance regression in STJ's metadata caching implementation, which will a allocate a new delegate on every metadata lookup operation. This becomes particularly noticeable when serializing object graphs containing many polymorphic values, for example large non-generic collections.

Testing

N/A.

Risk

Low. It is a small change that caches the metadata factory delegate in a field.

…t#80437)

* Fix delegate allocation in CachingContext.GetOrAddJsonTypeInfo

* Address PR feedback
@eiriktsarpalis eiriktsarpalis added this to the 7.0.x milestone Jan 11, 2023
@eiriktsarpalis eiriktsarpalis self-assigned this Jan 11, 2023
@eiriktsarpalis eiriktsarpalis added tenet-performance Performance related issue Servicing-consider Issue for next servicing release review labels Jan 11, 2023
@ghost
Copy link

ghost commented Jan 11, 2023

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

Issue Details

Fixes #80430.

Customer Impact

Fixes a customer reported performance regression in STJ's metadata caching implementation, which will a allocate a new delegate on every metadata lookup operation. This becomes particularly noticeable when serializing object graphs containing many polymorphic values, for example large non-generic collections.

Testing

N/A.

Risk

Low. It is a small change that caches the metadata factory delegate in a field.

Author: eiriktsarpalis
Assignees: eiriktsarpalis
Labels:

area-System.Text.Json

Milestone: 7.0.x

@eiriktsarpalis eiriktsarpalis changed the title Fix delegate allocation in CachingContext.GetOrAddJsonTypeInfo (#80437) [release/7.0] Fix delegate allocation in CachingContext.GetOrAddJsonTypeInfo Jan 11, 2023
@ericstj
Copy link
Member

ericstj commented Jan 11, 2023

Needs the servicing authoring. cc @carlossanlop

@ericstj
Copy link
Member

ericstj commented Jan 11, 2023

Test failing is JsonSerializerOptions_ReuseConverterCaches -- seems significant.

Child exception:
  Xunit.Sdk.NotNullException: Assert.NotNull() Failure
   at System.Text.Json.Serialization.Tests.CacheTests.<JsonSerializerOptions_ReuseConverterCaches>g__CreateCacheOptionsAccessor|9_1() in /_/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CacheTests.cs:line 305
   at System.Text.Json.Serialization.Tests.CacheTests.<>c.<JsonSerializerOptions_ReuseConverterCaches>b__9_0() in /_/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CacheTests.cs:line 271
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr) in /_/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInvoker.cs:line 64

Child process:
  System.Text.Json.Tests, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 System.Text.Json.Serialization.Tests.CacheTests+<>c Void <JsonSerializerOptions_ReuseConverterCaches>b__9_0()

@eiriktsarpalis
Copy link
Member Author

Approved by Tactics over email.

@eiriktsarpalis eiriktsarpalis added Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review labels Jan 12, 2023
@eiriktsarpalis
Copy link
Member Author

Test failing is JsonSerializerOptions_ReuseConverterCaches -- seems significant.

It's a reflection-based test validating that the CachingContext.Options property property exists:

https://github.com/dotnet/runtime/blob/release/7.0/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CacheTests.cs#L304-L305

I think it's because the CachingContext.Options ends up being trimmed since the change makes it dead code. If that's the case, I'm surprised that this isn't failing in main.

Copy link
Member

@ericstj ericstj left a comment

Choose a reason for hiding this comment

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

LGTM

@ericstj ericstj modified the milestones: 7.0.x, 7.0.3 Jan 12, 2023
@Sergio0694
Copy link
Contributor

Just curious, will this also be shipped for the OOB package? 🙂

@carlossanlop
Copy link
Member

Just curious, will this also be shipped for the OOB package? 🙂

Yep. This commit will take care of that: 81a7358

@Sergio0694
Copy link
Contributor

Sweet, that's awesome! Thank you 😄

@carlossanlop
Copy link
Member

carlossanlop commented Jan 12, 2023

Approved by Tactics (7.0.3).
Signed off by area owners.
Required OOB changes look correct (and may be reused by the other Json PR if it also gets merged: #80576).
CI failure was an unrelated timeout cancellation (Linq). No test-specific failures logged.
Ready to merge. :shipit:

@carlossanlop carlossanlop merged commit 84b1ddd into dotnet:release/7.0 Jan 12, 2023
@eiriktsarpalis eiriktsarpalis deleted the backport-jsonalloc branch January 12, 2023 22:02
@ghost ghost locked as resolved and limited conversation to collaborators Feb 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json Servicing-approved Approved for servicing release tenet-performance Performance related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants