Query: Inline shapers for the cases when we don't need variables

Open
#21,334 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
database

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

area-perf area-query customer-reported
(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

Open the contributing guide

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.