Skip to content

Reuse CosmosTypeMapping.Default for primitive JSON reader/writers#38481

Open
Copilot wants to merge 5 commits into
mainfrom
copilot/update-cosmos-compiled-model-tests-baselines
Open

Reuse CosmosTypeMapping.Default for primitive JSON reader/writers#38481
Copilot wants to merge 5 commits into
mainfrom
copilot/update-cosmos-compiled-model-tests-baselines

Conversation

Copilot AI commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

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 on typeof(T) to the singleton Json*ReaderWriter.Instance for all primitive types. Enums are handled via JsonSignedEnumReaderWriter<> / JsonUnsignedEnumReaderWriter<> using the same reflection approach as the existing FindReaderWriter(Type) instance method.
  • CosmosTypeMapping<T>.Default — now seeded with JsonValueReaderWriterSource.FindReaderWriter<T>() so the default mapping carries its reader/writer.
  • CosmosTypeMappingSource.Create<T> — returns Default when the supplied reader/writer is null or reference-equal to Default.JsonValueReaderWriter, instead of only when null.
  • Cosmos compiled-model baselines — regenerated; primitives now emit CosmosTypeMapping<T>.Default directly. Converted/enum/collection mappings still emit .Clone(...).

Result

// before
id.TypeMapping = CosmosTypeMapping<int>.Default.Clone(
    jsonValueReaderWriter: JsonInt32ReaderWriter.Instance);

// after
id.TypeMapping = CosmosTypeMapping<int>.Default;

…d update baselines

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

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 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>.Default with a built-in primitive JsonValueReaderWriter via the new reflection-free CosmosTypeMapping<T>.FindReaderWriter().
  • Update CosmosTypeMappingSource.Create<T> to return CosmosTypeMapping<T>.Default when the supplied reader/writer matches Default.JsonValueReaderWriter (or is null).
  • Regenerate Cosmos compiled-model baselines to emit CosmosTypeMapping<T>.Default for 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.

Comment thread src/EFCore.Cosmos/Storage/Internal/CosmosTypeMappingSource.cs
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment thread src/EFCore.Cosmos/Storage/Internal/CosmosTypeMapping`1.cs Outdated
…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
Copilot AI requested a review from AndriySvyryd June 23, 2026 01:30
Comment thread src/EFCore/Storage/Json/JsonValueReaderWriterSource.cs
Comment thread src/EFCore/Storage/Json/JsonValueReaderWriterSource.cs Outdated
Comment thread src/EFCore.Cosmos/Storage/Internal/CosmosTypeMappingSource.cs
Copilot AI requested a review from AndriySvyryd June 23, 2026 04:33
@AndriySvyryd AndriySvyryd force-pushed the copilot/update-cosmos-compiled-model-tests-baselines branch from 59d33b4 to d048c66 Compare June 23, 2026 23:56
…nternal], replace MakeGenericType with constrained helper

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
@AndriySvyryd AndriySvyryd force-pushed the copilot/update-cosmos-compiled-model-tests-baselines branch from d048c66 to 55a3b20 Compare June 23, 2026 23:59
@AndriySvyryd AndriySvyryd marked this pull request as ready for review June 24, 2026 00:00
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner June 24, 2026 00:00
Copilot AI review requested due to automatic review settings June 24, 2026 00:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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-enum TEnum, which will throw later when GetEnumUnderlyingType() 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-enum TEnum, which will throw later when GetEnumUnderlyingType() 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();

Comment thread src/EFCore/Storage/Json/JsonValueReaderWriterSource.cs Outdated
Comment thread src/EFCore/Storage/Json/JsonValueReaderWriterSource.cs Outdated
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants