Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Materializer optimizations #29909

Open
7 tasks
roji opened this issue Dec 21, 2022 · 0 comments
Open
7 tasks

Materializer optimizations #29909

roji opened this issue Dec 21, 2022 · 0 comments

Comments

@roji
Copy link
Member

roji commented Dec 21, 2022

As part of the AOT work (#25009), we now have a (hopefully) 100% faithful C# representation of the materializer expression tree (see code below).

Here are some quick perf fixes we can do:

  • Remove the materializationContext creation when not needed:
var materializationContext1 = new MaterializationContext(ValueBuffer.Empty, queryContext0.Context);
  • Eliminate the object array allocation and the boxing for non-composite key:
var entry1 = queryContext0.TryGetEntry(key, new object[]{(object)dataReader.GetInt32(0)}, true, out hasNullKey1);
  • Avoid unneeded always-true entity type check:
entityType1 = entityType;
if (entityType1 == entityType)
  • Avoid check for null entity type (when is this possible?):
entry1 = entityType1 == default(IEntityType) ? default(InternalEntityEntry) : queryContext0.StartTracking(entityType1, instance1, shadowValueBuffer1);
  • Avoid explicitly setting instance to null, as it's already been initialized to null above:
else
{
    instance1 = null;
}

Code

IEntityType entityType1;
bool hasNullKey1;
var materializationContext1 = new MaterializationContext(ValueBuffer.Empty, queryContext0.Context);
Blog instance1 = null;
var entry1 = queryContext0.TryGetEntry(key, new object[]{(object)dataReader.GetInt32(0)}, true, out hasNullKey1);
if (!hasNullKey1)
{
    if (entry1 != default(InternalEntityEntry))
    {
        entityType1 = entry1.EntityType;
        instance1 = (Blog)entry1.Entity;
    }
    else
    {
        var shadowValueBuffer1 = ValueBuffer.Empty;
        entityType1 = entityType;
        if (entityType1 == entityType)
        {
            var instance0 = Activator.CreateInstance<Blog>();
            typeof(Blog).GetField("<Id>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(instance0, dataReader.GetInt32(0));
            typeof(Blog).GetField("<Name>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(instance0, dataReader.GetString(1));
            instance1 = instance0;
        }
        else
        {
            instance1 = null;
        }

        entry1 = entityType1 == default(IEntityType) ? default(InternalEntityEntry) : queryContext0.StartTracking(entityType1, instance1, shadowValueBuffer1);
    }
}

var entity = instance1;
return entity;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants