Skip to content

Optimize TypeId accessors #158

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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions Orm/Xtensive.Orm/Orm/Providers/SqlCompiler.Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created: 2009.11.13

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
Expand All @@ -17,6 +18,7 @@
using Xtensive.Sql;
using Xtensive.Sql.Dml;
using IndexInfo = Xtensive.Orm.Model.IndexInfo;
using TypeMapping = Xtensive.Sql.TypeMapping;

namespace Xtensive.Orm.Providers
{
Expand All @@ -39,6 +41,13 @@ public QueryAndBindings(SqlSelect initialQuery, List<QueryParameterBinding> bind
}
}

private static readonly ConcurrentDictionary<int, Func<ParameterContext, object>> typeIdParameterAccessorCache =
new ConcurrentDictionary<int, Func<ParameterContext, object>>();

private static readonly Func<int, Func<ParameterContext, object>> AccessorFactory = typeId => _ => typeId;

private TypeMapping int32TypeMapping;

protected override SqlProvider VisitFreeText(FreeTextProvider provider)
{
throw new NotSupportedException();
Expand Down Expand Up @@ -249,20 +258,15 @@ private QueryAndBindings BuildFilteredQuery(IndexInfo index)
SqlDml.Array(filterByTypes.Select(t => TypeIdRegistry[t]).ToArray(filterByTypes.Count)));
}
else {
var typeMapping = Driver.GetTypeMapping(WellKnownTypes.Int32);
if (filterByTypes.Count == 1) {
var binding = new QueryParameterBinding(typeMapping,
CreateTypeIdAccessor(TypeIdRegistry[type]).CachingCompile(),
QueryParameterBindingType.Regular);
var binding = CreateQueryParameterBinding(type);
bindings.Add(binding);
filter = typeIdColumn == binding.ParameterReference;
}
else {
var typeIdParameters = filterByTypes
.Select(t => {
var binding = new QueryParameterBinding(typeMapping,
CreateTypeIdAccessor(TypeIdRegistry[t]).CachingCompile(),
QueryParameterBindingType.Regular);
var binding = CreateQueryParameterBinding(t);
bindings.Add(binding);
return binding.ParameterReference;
})
Expand Down Expand Up @@ -308,8 +312,7 @@ private QueryAndBindings BuildTypedQuery(IndexInfo index)

SqlUserColumn typeIdColumn;
if (useParameterForTypeId) {
var typeMapping = Driver.GetTypeMapping(WellKnownTypes.Int32);
var binding = new QueryParameterBinding(typeMapping, CreateTypeIdAccessor(TypeIdRegistry[type]).CachingCompile(), QueryParameterBindingType.Regular);
var binding = CreateQueryParameterBinding(type);

typeIdColumn = SqlDml.Column(binding.ParameterReference);
bindings.Add(binding);
Expand Down Expand Up @@ -352,11 +355,11 @@ private object GetDiscriminatorValue(TypeDiscriminatorMap discriminatorMap, obje
: fieldValue;
}

private Expression<Func<ParameterContext, object>> CreateTypeIdAccessor(int value)
{
var bodyExpression = Expression.Convert(Expression.Constant(value),WellKnownTypes.Object);
var lambdaParemeter = Expression.Parameter(WellKnownOrmTypes.ParameterContext, "context");
return (Expression<Func<ParameterContext, object>>) FastExpression.Lambda(bodyExpression, lambdaParemeter);
}
private QueryParameterBinding CreateQueryParameterBinding(TypeInfo type) =>
new QueryParameterBinding(
int32TypeMapping ??= Driver.GetTypeMapping(WellKnownTypes.Int32),
typeIdParameterAccessorCache.GetOrAdd(TypeIdRegistry[type], AccessorFactory),
QueryParameterBindingType.Regular
);
}
}