Reuse CosmosTypeMapping.Default for primitive JSON reader/writers#38481
Open
Copilot wants to merge 5 commits into
Open
Reuse CosmosTypeMapping.Default for primitive JSON reader/writers#38481Copilot wants to merge 5 commits into
Copilot wants to merge 5 commits into
Conversation
…d update baselines Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
AndriySvyryd
June 22, 2026 17:17
View session
There was a problem hiding this comment.
Pull request overview
This PR reduces Cosmos compiled-model size and allocations by reusing CosmosTypeMapping<T>.Default for primitive JSON reader/writers when the reader/writer is the well-known singleton, instead of emitting/creating Default.Clone(jsonValueReaderWriter: ...) instances.
Changes:
- Seed
CosmosTypeMapping<T>.Defaultwith a built-in primitiveJsonValueReaderWritervia the new reflection-freeCosmosTypeMapping<T>.FindReaderWriter(). - Update
CosmosTypeMappingSource.Create<T>to returnCosmosTypeMapping<T>.Defaultwhen the supplied reader/writer matchesDefault.JsonValueReaderWriter(or isnull). - Regenerate Cosmos compiled-model baselines to emit
CosmosTypeMapping<T>.Defaultfor primitives.
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/EFCore.Cosmos/Storage/Internal/CosmosTypeMapping`1.cs | Adds FindReaderWriter() and seeds CosmosTypeMapping<T>.Default with the appropriate primitive JSON reader/writer singleton. |
| src/EFCore.Cosmos/Storage/Internal/CosmosTypeMappingSource.cs | Reuses CosmosTypeMapping<T>.Default when the provided JSON reader/writer matches the default’s reader/writer. |
| test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/Basic_cosmos_model/DataEntityType.cs | Updates baseline to use CosmosTypeMapping<T>.Default directly for primitive mappings where applicable. |
| test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/ComplexTypes/PrincipalBaseEntityType.cs | Updates baseline primitive type mappings to reuse CosmosTypeMapping<T>.Default. |
| test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/ComplexTypes/PrincipalDerivedEntityType.cs | Updates baseline primitive/element mappings to reuse CosmosTypeMapping<T>.Default where applicable. |
| test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/Cosmos_model_with_index_types/IndexedDataEntityType.cs | Updates baseline primitive mappings to reuse CosmosTypeMapping<T>.Default. |
| test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/SimpleModel/DependentDerivedEntityType.cs | Updates baseline primitive mappings to reuse CosmosTypeMapping<T>.Default (and removes now-unneeded JSON using). |
| test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/BigModel/DependentBaseEntityType.cs | Updates baseline primitive mappings to reuse CosmosTypeMapping<T>.Default. |
| test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/BigModel/DependentDerivedEntityType.cs | Updates baseline primitive mappings to reuse CosmosTypeMapping<T>.Default (and removes now-unneeded JSON using). |
| test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/BigModel/ManyTypesEntityType.cs | Updates baseline primitive/element mappings to reuse CosmosTypeMapping<T>.Default where applicable. |
| test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/BigModel/OwnedType0EntityType.cs | Updates baseline primitive/element mappings to reuse CosmosTypeMapping<T>.Default where applicable. |
| test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/BigModel/OwnedTypeEntityType.cs | Updates baseline primitive/element mappings to reuse CosmosTypeMapping<T>.Default where applicable. |
| test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/BigModel/PrincipalBaseEntityType.cs | Updates baseline primitive/element mappings to reuse CosmosTypeMapping<T>.Default where applicable. |
| test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/BigModel/PrincipalBasePrincipalDerivedDependentBasebyteEntityType.cs | Updates baseline primitive mappings to reuse CosmosTypeMapping<T>.Default where applicable. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…d with switch expression and enum support Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot
AI
changed the title
Reuse CosmosTypeMapping<T>.Default for primitive JSON reader/writers
Reuse CosmosTypeMapping.Default for primitive JSON reader/writers
Jun 23, 2026
59d33b4 to
d048c66
Compare
…nternal], replace MakeGenericType with constrained helper Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
d048c66 to
55a3b20
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (2)
src/EFCore/Storage/Json/JsonSignedEnumReaderWriter.cs:18
- The generic constraints were removed, so
JsonSignedEnumReaderWriter<TEnum>can now be constructed for non-enumTEnum, which will throw later whenGetEnumUnderlyingType()is accessed. Add a debug-time guard to make incorrect usage fail fast and with a clearer signal during development.
public sealed class JsonSignedEnumReaderWriter<TEnum> : JsonValueReaderWriter<TEnum>
{
private static readonly PropertyInfo InstanceProperty = typeof(JsonSignedEnumReaderWriter<TEnum>).GetProperty(nameof(Instance))!;
/// <summary>
/// The singleton instance of this stateless reader/writer.
/// </summary>
public static JsonSignedEnumReaderWriter<TEnum> Instance { get; } = new();
src/EFCore/Storage/Json/JsonUnsignedEnumReaderWriter.cs:18
- The generic constraints were removed, so
JsonUnsignedEnumReaderWriter<TEnum>can now be constructed for non-enumTEnum, which will throw later whenGetEnumUnderlyingType()is accessed. Add a debug-time guard to make incorrect usage fail fast and with a clearer signal during development.
public sealed class JsonUnsignedEnumReaderWriter<TEnum> : JsonValueReaderWriter<TEnum>
{
private static readonly PropertyInfo InstanceProperty = typeof(JsonUnsignedEnumReaderWriter<TEnum>).GetProperty(nameof(Instance))!;
/// <summary>
/// The singleton instance of this stateless reader/writer.
/// </summary>
public static JsonUnsignedEnumReaderWriter<TEnum> Instance { get; } = new();
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Primitive Cosmos type mappings always allocated a fresh instance via
Default.Clone(jsonValueReaderWriter: ...)even though the reader/writer was the well-known singleton, bloating compiled models and preventing reference reuse.Changes
JsonValueReaderWriterSource.FindReaderWriter<T>()— new public static generic method using a switch expression to dispatch ontypeof(T)to the singletonJson*ReaderWriter.Instancefor all primitive types. Enums are handled viaJsonSignedEnumReaderWriter<>/JsonUnsignedEnumReaderWriter<>using the same reflection approach as the existingFindReaderWriter(Type)instance method.CosmosTypeMapping<T>.Default— now seeded withJsonValueReaderWriterSource.FindReaderWriter<T>()so the default mapping carries its reader/writer.CosmosTypeMappingSource.Create<T>— returnsDefaultwhen the supplied reader/writer isnullor reference-equal toDefault.JsonValueReaderWriter, instead of only whennull.CosmosTypeMapping<T>.Defaultdirectly. Converted/enum/collection mappings still emit.Clone(...).Result