-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Conversation
docs/standard/serialization/system-text-json-source-generation-modes.md
Outdated
Show resolved
Hide resolved
docs/standard/serialization/system-text-json-source-generation-modes.md
Outdated
Show resolved
Hide resolved
docs/standard/serialization/system-text-json-source-generation-modes.md
Outdated
Show resolved
Hide resolved
docs/standard/serialization/system-text-json-source-generation-modes.md
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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.
docs/standard/serialization/system-text-json-source-generation-modes.md
Outdated
Show resolved
Hide resolved
…-modes.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
@gewarren Thanks for the review, great suggestions! |
There was a problem hiding this 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. | ✔️ | ❌ | ❌ | |
There was a problem hiding this comment.
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. | ❌ | ✔️ | ❌ | |
There was a problem hiding this comment.
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. | ✔️ | ❌ | ❌ | |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
Fixes #26040