Skip to content

Optimize TranslatorContext #261

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
merged 11 commits into from
Sep 18, 2024
13 changes: 3 additions & 10 deletions Orm/Xtensive.Orm.Firebird/Orm.Providers.Firebird/SqlCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@

namespace Xtensive.Orm.Providers.Firebird
{
internal class SqlCompiler : Providers.SqlCompiler
{
// Constructors

public SqlCompiler(HandlerAccessor handlers, CompilerConfiguration configuration)
: base(handlers, configuration)
{
}
}
}
internal class SqlCompiler(HandlerAccessor handlers, in CompilerConfiguration configuration)
: Providers.SqlCompiler(handlers, configuration);
}
13 changes: 3 additions & 10 deletions Orm/Xtensive.Orm.MySql/Orm.Providers.MySql/SqlCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@

namespace Xtensive.Orm.Providers.MySql
{
internal class SqlCompiler : Providers.SqlCompiler
{
// Constructors

public SqlCompiler(HandlerAccessor handlers, CompilerConfiguration configuration)
: base(handlers, configuration)
{
}
}
}
internal class SqlCompiler(HandlerAccessor handlers, in CompilerConfiguration configuration)
: Providers.SqlCompiler(handlers, configuration);
}
4 changes: 2 additions & 2 deletions Orm/Xtensive.Orm.Oracle/Orm.Providers.Oracle/SqlCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ protected override SqlExpression ProcessAggregate(SqlProvider source, IReadOnlyL

// Constructors

public SqlCompiler(HandlerAccessor handlers, CompilerConfiguration configuration)
public SqlCompiler(HandlerAccessor handlers, in CompilerConfiguration configuration)
: base(handlers, configuration)
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ protected override IEnumerable<Type> GetProviderCompilerContainers()
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected override SqlExpression ProcessAggregate(SqlProvider source, IReadOnlyL
return result;
}

public SqlCompiler(HandlerAccessor handlers, CompilerConfiguration configuration)
public SqlCompiler(HandlerAccessor handlers, in CompilerConfiguration configuration)
: base(handlers, configuration)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ namespace Xtensive.Orm.Providers.SqlServer
public class DomainHandler : Providers.DomainHandler
{
/// <inheritdoc/>
protected override ICompiler CreateCompiler(CompilerConfiguration configuration)
{
return new SqlCompiler(Handlers, configuration);
}
protected override ICompiler CreateCompiler(CompilerConfiguration configuration) => new SqlCompiler(Handlers, configuration);

/// <inheritdoc/>
protected override SearchConditionCompiler CreateSearchConditionVisitor()
Expand All @@ -30,4 +27,4 @@ protected override SearchConditionCompiler CreateSearchConditionVisitor()
return new SearchConditionCompilerV11();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Xtensive.Orm.Providers.SqlServer
{
internal class SqlCompiler : Providers.SqlCompiler
internal class SqlCompiler(HandlerAccessor handlers, in CompilerConfiguration configuration) : Providers.SqlCompiler(handlers, configuration)
{
private static readonly Type ByteType = typeof(byte);
private static readonly Type Int16Type = typeof(short);
Expand Down Expand Up @@ -149,12 +149,5 @@ private bool ShouldCastDueType(Type type)
|| type == DecimalType
|| type == FloatType;
}

// Constructors

public SqlCompiler(HandlerAccessor handlers, CompilerConfiguration configuration)
: base(handlers, configuration)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,35 @@
// Created by: Dmitri Maximov
// Created: 2009.08.12

using System;
using System.Collections.Generic;
using System.Linq;
using Xtensive.Sql.Model;

namespace Xtensive.Sql.Drivers.SqlServer.v09
{
internal sealed class ColumnResolver
internal sealed class ColumnResolver(DataTable table)
{
public DataTable Table;
private List<ColumnIndexMapping> columnMappings;

private class ColumnIndexMapping
{
public readonly int DbIndex;
public readonly int ModelIndex;

public ColumnIndexMapping(int dbIndex, int modelIndex)
{
DbIndex = dbIndex;
ModelIndex = modelIndex;
}
}
private readonly record struct ColumnIndexMapping(int DbIndex, int ModelIndex, bool IsValid);

public void RegisterColumnMapping(int dbIndex, int modelIndex)
{
if (columnMappings == null)
columnMappings = new List<ColumnIndexMapping>(1);
public DataTable Table = table;
private List<ColumnIndexMapping> columnMappings;

columnMappings.Add(new ColumnIndexMapping(dbIndex, modelIndex));
}
public void RegisterColumnMapping(int dbIndex, int modelIndex) =>
(columnMappings ??= new(1)).Add(new ColumnIndexMapping(dbIndex, modelIndex, true));

public DataTableColumn GetColumn(int dbIndex)
{
int modelIndex = dbIndex-1;
var view = Table as View;
if (view != null)
if (Table is View view)
return view.ViewColumns[modelIndex];

var table = (Table)Table;
if (columnMappings == null)
return table.TableColumns[modelIndex];

var mapping = columnMappings.Where(item => item.DbIndex==dbIndex).FirstOrDefault();
if (mapping != null)
var mapping = columnMappings.FirstOrDefault(item => item.DbIndex==dbIndex);
if (mapping != default)
return table.TableColumns[mapping.ModelIndex];

throw new ArgumentOutOfRangeException("dbIndex");
}

public ColumnResolver(DataTable table)
{
Table = table;
}
}
}
}
14 changes: 3 additions & 11 deletions Orm/Xtensive.Orm.Sqlite/Orm.Providers.Sqlite/SqlCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@

namespace Xtensive.Orm.Providers.Sqlite
{
internal class SqlCompiler : Providers.SqlCompiler
{

// Constructors

public SqlCompiler(HandlerAccessor handlers, CompilerConfiguration configuration)
: base(handlers, configuration)
{
}
}
}
internal class SqlCompiler(HandlerAccessor handlers, in CompilerConfiguration configuration)
: Providers.SqlCompiler(handlers, configuration);
}
6 changes: 3 additions & 3 deletions Orm/Xtensive.Orm/Core/AliasGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Xtensive.Core
/// Universal alias generator.
/// </summary>
[Serializable]
public sealed class AliasGenerator
public struct AliasGenerator
{
/// <summary>
/// Default alias template. Value is "{0}{1}". Where {0} - template parameter for prefix and {1} - template parameter for suffix.
Expand Down Expand Up @@ -93,7 +93,7 @@ public static AliasGenerator Create(string[] overriddenPrefixes, string aliasTem

// Constructors

private AliasGenerator()
public AliasGenerator()
: this (DefaultAliasTemplate)
{}

Expand All @@ -111,4 +111,4 @@ private AliasGenerator(string[] prefixes, string aliasTemplate)
this.aliasTemplate = aliasTemplate;
}
}
}
}
10 changes: 5 additions & 5 deletions Orm/Xtensive.Orm/Orm/Building/Builders/TypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,18 +485,18 @@ private KeyInfo BuildKeyInfo(TypeInfo root, HierarchyDef hierarchyDef)
var keyFields = root.Fields
.Where(static field => field.IsPrimaryKey)
.OrderBy(static field => field.MappingInfo.Offset)
.ToList();
.ToArray();

var keyColumns = keyFields
.Where(static field => field.Column != null)
.Select(static field => field.Column)
.ToList();
.ToArray();

var keyTupleDescriptor = TupleDescriptor.Create(
keyColumns.Select(static c => c.ValueType).ToArray(keyColumns.Count));
keyColumns.Select(static c => c.ValueType).ToArray(keyColumns.Length));
var typeIdColumnIndex = -1;
if (hierarchyDef.IncludeTypeId) {
for (var i = 0; i < keyColumns.Count; i++) {
for (var i = 0; i < keyColumns.Length; i++) {
if (keyColumns[i].Field.IsTypeId) {
typeIdColumnIndex = i;
}
Expand Down Expand Up @@ -640,4 +640,4 @@ public TypeBuilder(BuildingContext context)
.ToDictionary(configuration => context.NameBuilder.BuildKeyGeneratorName(configuration));
}
}
}
}
4 changes: 2 additions & 2 deletions Orm/Xtensive.Orm/Orm/Linq/QueryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ internal TranslatedQuery Translate(Expression expression) =>
Translate(expression, Session.CompilationService.CreateConfiguration(Session));

internal TranslatedQuery Translate(Expression expression,
CompilerConfiguration compilerConfiguration)
in CompilerConfiguration compilerConfiguration)
{
try {
var compiledQueryScope = CompiledQueryProcessingScope.Current;
Expand All @@ -189,4 +189,4 @@ internal QueryProvider(Session session)
Session = session;
}
}
}
}
6 changes: 4 additions & 2 deletions Orm/Xtensive.Orm/Orm/Linq/Rewriters/AggregateOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace Xtensive.Orm.Linq.Rewriters
{
internal sealed class AggregateOptimizer : ExtendedExpressionVisitor
{
private static readonly AggregateOptimizer Instance = new();

private sealed class GroupByItemWrapper<TKey, TElement>
{
public TKey Key { get; set; }
Expand Down Expand Up @@ -214,7 +216,7 @@ public static Expression Rewrite(Expression expression)
// This would help Translator to translate GroupBy+Aggregate
// into single AggregateProvider.

return new AggregateOptimizer().Visit(expression);
return Instance.Visit(expression);
}

private static void AddGroupByItemWrapper(GroupByQuery groupBy, List<LambdaExpression> elementProjections)
Expand Down Expand Up @@ -284,4 +286,4 @@ private AggregateOptimizer()
{
}
}
}
}
17 changes: 5 additions & 12 deletions Orm/Xtensive.Orm/Orm/Linq/TranslatorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ namespace Xtensive.Orm.Linq
{
internal sealed class TranslatorContext
{
private readonly AliasGenerator resultAliasGenerator;
private readonly AliasGenerator columnAliasGenerator;
private AliasGenerator resultAliasGenerator = AliasGenerator.Create("#{0}{1}");
private AliasGenerator columnAliasGenerator = AliasGenerator.Create(new[] {"c01umn"});
private readonly Dictionary<ParameterExpression, Parameter<Tuple>> tupleParameters;
private readonly Dictionary<CompilableProvider, ApplyParameter> applyParameters;
private readonly Dictionary<ParameterExpression, ItemProjectorExpression> boundItemProjectors;
private readonly Dictionary<MemberInfo, int> queryReuses;

public CompilerConfiguration RseCompilerConfiguration { get; }
public readonly CompilerConfiguration RseCompilerConfiguration;

public ProviderInfo ProviderInfo { get; }

Expand Down Expand Up @@ -124,11 +124,7 @@ public ItemProjectorExpression GetBoundItemProjector(ParameterExpression paramet
return result;
}

public void RegisterPossibleQueryReuse(MemberInfo memberInfo)
{
if (!queryReuses.ContainsKey(memberInfo))
queryReuses.Add(memberInfo, 0);
}
public void RegisterPossibleQueryReuse(MemberInfo memberInfo) => queryReuses.TryAdd(memberInfo, 0);

public bool CheckIfQueryReusePossible(MemberInfo memberInfo)
{
Expand All @@ -148,11 +144,10 @@ private Expression ApplyPreprocessor(IQueryPreprocessor preprocessor, Session se

// Constructors

public TranslatorContext(Session session, CompilerConfiguration rseCompilerConfiguration, Expression query,
public TranslatorContext(Session session, in CompilerConfiguration rseCompilerConfiguration, Expression query,
CompiledQueryProcessingScope compiledQueryScope)
{
ArgumentNullException.ThrowIfNull(session);
ArgumentNullException.ThrowIfNull(rseCompilerConfiguration);
ArgumentNullException.ThrowIfNull(query);

Domain = session.Domain;
Expand All @@ -173,8 +168,6 @@ public TranslatorContext(Session session, CompilerConfiguration rseCompilerConfi
query = PersistentIndexerRewriter.Rewrite(query, this);
Query = query;

resultAliasGenerator = AliasGenerator.Create("#{0}{1}");
columnAliasGenerator = AliasGenerator.Create(new[] {"c01umn"});
CustomCompilerProvider = Domain.Handler.GetMemberCompilerProvider<Expression>();
Model = Domain.Model;
TypeIdRegistry = session.StorageNode.TypeIdRegistry;
Expand Down
19 changes: 10 additions & 9 deletions Orm/Xtensive.Orm/Orm/Providers/CompilationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ namespace Xtensive.Orm.Providers
/// <summary>
/// <see cref="CompilableProvider"/> compilation service.
/// </summary>
public sealed class CompilationService
public readonly struct CompilationService
{
private readonly Func<CompilerConfiguration, ICompiler> compilerProvider;
private readonly Func<CompilerConfiguration, IPreCompiler> preCompilerProvider;
private readonly Func<CompilerConfiguration, ICompiler, IPostCompiler> postCompilerProvider;

public CompilerConfiguration CreateConfiguration(Session session) =>
new() {
StorageNode = session.StorageNode,
Tags = session.Tags,
new(
PrepareRequest: true,
StorageNode: session.StorageNode,
Tags: session.Tags,
// prefer constants during upgrade process
PreferTypeIdAsParameter = !session.Name.Equals(WellKnown.Sessions.System) && session.Domain.Configuration.PreferTypeIdsAsQueryParameters
};
PreferTypeIdAsParameter: !session.Name.Equals(WellKnown.Sessions.System) && session.Domain.Configuration.PreferTypeIdsAsQueryParameters
);

public ExecutableProvider Compile(CompilableProvider provider, CompilerConfiguration configuration)
{
Expand Down Expand Up @@ -58,12 +59,12 @@ public CompilationService(
Func<CompilerConfiguration, ICompiler, IPostCompiler> postCompilerProvider)
{
ArgumentNullException.ThrowIfNull(compilerProvider);
ArgumentNullException.ThrowIfNull(compilerProvider, "preCompilerProvider");
ArgumentNullException.ThrowIfNull(compilerProvider, "postCompilerProvider");
ArgumentNullException.ThrowIfNull(preCompilerProvider);
ArgumentNullException.ThrowIfNull(postCompilerProvider);

this.compilerProvider = compilerProvider;
this.preCompilerProvider = preCompilerProvider;
this.postCompilerProvider = postCompilerProvider;
}
}
}
}
Loading