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

Reflection vs. source generation in System.Text.Json #26559

Merged
merged 11 commits into from
Oct 29, 2021

Conversation

tdykstra
Copy link
Contributor

@tdykstra tdykstra commented Oct 18, 2021

Fixes #26040

File Type File Name Published Url
Content docs/standard/serialization/system-text-json-overview.md https://review.docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-overview?branch=pr-en-us-26559
Content docs/standard/serialization/system-text-json-source-generation.md https://review.docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-source-generation?branch=pr-en-us-26559
Content docs/standard/serialization/system-text-json-source-generation-modes.md https://review.docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-source-generation-modes?branch=pr-en-us-26559

@tdykstra tdykstra changed the title WIIP: Reflection vs. source generation in System.Text.Json Reflection vs. source generation in System.Text.Json Oct 19, 2021
@tdykstra tdykstra marked this pull request as ready for review October 19, 2021 23:45
@tdykstra tdykstra requested a review from a team as a code owner October 19, 2021 23:45
@tdykstra tdykstra requested review from layomia and adegeo October 19, 2021 23:45
Copy link
Contributor

@gewarren gewarren left a comment

Choose a reason for hiding this comment

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

One small suggestion to consider because it took me a little while to figure out the columns.

…-modes.md

Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
@tdykstra
Copy link
Contributor Author

@gewarren Thanks for the review, great suggestions!

@tdykstra tdykstra merged commit 14e8ef2 into dotnet:main Oct 29, 2021
@tdykstra tdykstra deleted the stjmodes branch October 29, 2021 16:25
@tdykstra tdykstra added the okr-freshness OKR: Freshness of content label Nov 1, 2021
Copy link
Contributor

@layomia layomia left a comment

Choose a reason for hiding this comment

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

Just a few comments but LGTM


| Benefit | Reflection | Source generation:<br/>Metadata collection | Source generation:<br/>Serialization optimization |
|------------------------------------------------------|------------|---------------------|----------------------------|
| Simpler to code and debug. | ✔️ | ❌ | ❌ |
Copy link
Contributor

Choose a reason for hiding this comment

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

In what ways do you find the reflection serializer simpler to code and debug?

| Supports non-public accessors. | ✔️ | ❌ | ❌ |
| Supports init-only properties. | ✔️ | ❌ | ❌ |
| Supports all available serialization customizations. | ✔️ | ❌ | ❌ |
| Reduces start-up time. | ❌ | ✔️ | ❌ |
Copy link
Contributor

Choose a reason for hiding this comment

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

Serialization optimization also reduces start-up time.

| Simpler to code and debug. | ✔️ | ❌ | ❌ |
| Supports non-public accessors. | ✔️ | ❌ | ❌ |
| Supports init-only properties. | ✔️ | ❌ | ❌ |
| Supports all available serialization customizations. | ✔️ | ❌ | ❌ |
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe metadata collection supports all customizations. Which ones did you find missing?


By default, `JsonSerializer` collects metadata at run time by using [reflection](../../csharp/programming-guide/concepts/reflection.md). Whenever `JsonSerializer` has to serialize or deserialize a type for the first time, it collects and caches this metadata. The metadata collection process takes time and uses memory.

## Source generation - metadata collection mode
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this just be "Metadata collection mode"


The performance improvements provided by source generation can be substantial. For example, [test results](https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-source-generator/#how-source-generation-provides-benefits) have shown up to 40% or more startup time reduction, private memory reduction, throughput speed increase (in serialization optimization mode), and app size reduction.

## Source generation - known issues
Copy link
Contributor

Choose a reason for hiding this comment

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

"Serialization optimization mode" should come before this section. Also should it just be "Known issues"?


You can use source generation to move the metadata collection process from run time to compile time. During compilation, the metadata is collected and source code files are generated. The generated source code files are automatically compiled as an integral part of the application. This compile-time metadata collection eliminates run-time metadata collection, which improves performance of both serialization and deserialization.

The performance improvements provided by source generation can be substantial. For example, [test results](https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-source-generator/#how-source-generation-provides-benefits) have shown up to 40% or more startup time reduction, private memory reduction, throughput speed increase (in serialization optimization mode), and app size reduction.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd avoid quantifying the improvements here and just leave the pointer to the perf section of the blog.

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

Successfully merging this pull request may close these issues.

Document known issues and semantic differences between the various JSON-serialization programming models
4 participants