Cosmos: Handle late discriminators in materialization

Open
#38,576 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Quiet
Tech stack
csharp
Domain
database

Research direction

Start by reviewing the streaming Cosmos materialization introduced by #38550, focusing on the Utf8JsonReader discriminator scan and the serializer's discriminator ordering. Compare late-discriminator behavior with the mentioned STJ DOM and opt-out exception approaches; done requires a decided strategy and corresponding performance and correctness validation.

Written by the indexing model from the issue text.

Description

area-cosmos area-perf area-query customer-reported
What problem are you trying to solve?

Since #38550 cosmos materialization is done in a streaming fashion, using Utf8JsonReader.
This is faster and allocation light. Especially when comparing the to previous materializer that used Newtonsoft's DOM as an intermediate step.
However, when a structural type has an inheritance hierarchy, the materializer needs to know the discriminator value before it can start reading other values. Similar to STJ, the materializer will do a scan over the document to find the discriminator property and it's value. A document could be as large as 2MB and the discriminator can appear anywhere in the document. Limited benchmarking has shown that scanning the document can be slow when the discriminator appears late, to the point where STJ's DOM would be faster with very little increase in GC pressure.
The serializer has been changed to put discriminators early in the document, but using different write method or migrating from an older version of EF, we don't know the location of the discriminator

Describe the solution you'd like

To discuss.
STJ actually throws an opt-out exception when the discriminator is not the first property in a json document that has an inheritance hierarchy. We could consider the same, or even switch to DOM when we can't find the discriminator in the first (few) properties. This would mean maintaining 2 methods of materialization tho, which probably isn't worth it. Completely switching to STJ DOM could be an option, but we would be slightly slowing down the happy path materialization.

Dominant language
C#
Stars
14.8k
Forks
3.5k
Avg merge
3d 7h
Merged PRs (30d)
182

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.