Query: Inline shapers for the cases when we don't need variables
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 30/100
Research direction
Start by tracing EF Core's shaper generation and the handling of ProjectionBindingExpression for queries without collections. Confirm how repeated projections and conditional entity materialization are represented, then add coverage showing that single-use projections inline operations without unnecessary variables while preserving materialization behavior.
Written by the indexing model from the issue text.
Description
(queryContext, dataReader, resultContext, resultCoordinator) =>
{
Customer namelessParameter{0};
namelessParameter{0} =
{
MaterializationContext materializationContext1;
IEntityType entityType1;
Customer instance1;
InternalEntityEntry entry1;
bool hasNullKey1;
materializationContext1 = new MaterializationContext(
ValueBuffer,
queryContext.Context
);
instance1 = null;
entry1 = queryContext.TryGetEntry(
key: Key: Customer.CustomerID PK,
keyValues: new object[]{ try { (object)dataReader.GetString(0) } catch (Exception) { ... } },
throwOnNullKey: True,
hasNullKey: hasNullKey1);
!(hasNullKey1) ? entry1 != default(InternalEntityEntry) ?
{
entityType1 = entry1.EntityType;
return instance1 = (Customer)entry1.Entity;
} :
{
ValueBuffer shadowValueBuffer1;
shadowValueBuffer1 = ValueBuffer;
entityType1 = EntityType: Customer;
instance1 = switch (entityType1)
{
case EntityType: Customer:
{
return
{
Customer instance;
instance = new Customer(
materializationContext1.Context,
InfrastructureExtensions.GetService<ILazyLoader>((IInfrastructure<IServiceProvider>)materializationContext1.Context),
try { dataReader.GetString(0) } catch (Exception) { ... }
);
instance.Context = (materializationContext1.Context as NorthwindContext);
instance.<Address>k__BackingField = dataReader.IsDBNull(1) ? default(string) : try { dataReader.GetString(1) } catch (Exception) { ... } ;
instance.<City>k__BackingField = dataReader.IsDBNull(2) ? default(string) : try { dataReader.GetString(2) } catch (Exception) { ... } ;
instance.<CompanyName>k__BackingField = dataReader.IsDBNull(3) ? default(string) : try { dataReader.GetString(3) } catch (Exception) { ... } ;
instance.<ContactName>k__BackingField = dataReader.IsDBNull(4) ? default(string) : try { dataReader.GetString(4) } catch (Exception) { ... } ;
instance.<ContactTitle>k__BackingField = dataReader.IsDBNull(5) ? default(string) : try { dataReader.GetString(5) } catch (Exception) { ... } ;
instance.<Country>k__BackingField = dataReader.IsDBNull(6) ? default(string) : try { dataReader.GetString(6) } catch (Exception) { ... } ;
instance.<Fax>k__BackingField = dataReader.IsDBNull(7) ? default(string) : try { dataReader.GetString(7) } catch (Exception) { ... } ;
instance.<Phone>k__BackingField = dataReader.IsDBNull(8) ? default(string) : try { dataReader.GetString(8) } catch (Exception) { ... } ;
instance.<PostalCode>k__BackingField = dataReader.IsDBNull(9) ? default(string) : try { dataReader.GetString(9) } catch (Exception) { ... } ;
instance.<Region>k__BackingField = dataReader.IsDBNull(10) ? default(string) : try { dataReader.GetString(10) } catch (Exception) { ... } ;
return instance;
};
}
default:
null
}
;
entry1 = entityType1 == default(IEntityType) ? default(InternalEntityEntry) : queryContext.StartTracking(
entityType: entityType1,
entity: instance1,
valueBuffer: shadowValueBuffer1);
return instance1;
} : default(void);
return instance1;
};
return namelessParameter{0};
}
Currently, we generate shaper like above for a simple query like context.Customers.ToList().
Point to notice is the introduction of variables. We do this since we don't want to materialize same entity instance multiple times if it is being referenced multiple times in the projection. (this is all true only when there are no collection to be materialized, which has different structure for shaper). It also helps for a column being read from server if the column is something like binary which could have large size.
For scenarios where each ProjectionBindingExpression is being used only once and there are no collection, we can inline the operations without introducing variables.
An additional perf benefit would be that if the statement contains condition ? EntityA : EntityB then we could skip materializing one entity altogether based on how condition evaluates.
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 134
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
customer-reported
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
area-cosmos area-vector-search
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-cosmos
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-tools needs-design
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100