Skip to content

Commit 4f784dd

Browse files
authored
Optimize TranslatorContext (#261)
* Optimize `TranslatorContext` * ArgumentNullException.ThrowIfNull() * Fix AliasGenerators * in arg * in arg * Optimize `CompositePostCompiler` * Optimize ColumnIndexMapping * Optimize NodeCollection * Optimize BuildKeyInfo()
1 parent b6ae044 commit 4f784dd

File tree

23 files changed

+87
-175
lines changed

23 files changed

+87
-175
lines changed

Orm/Xtensive.Orm.Firebird/Orm.Providers.Firebird/SqlCompiler.cs

+3-10
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@
88

99
namespace Xtensive.Orm.Providers.Firebird
1010
{
11-
internal class SqlCompiler : Providers.SqlCompiler
12-
{
13-
// Constructors
14-
15-
public SqlCompiler(HandlerAccessor handlers, CompilerConfiguration configuration)
16-
: base(handlers, configuration)
17-
{
18-
}
19-
}
20-
}
11+
internal class SqlCompiler(HandlerAccessor handlers, in CompilerConfiguration configuration)
12+
: Providers.SqlCompiler(handlers, configuration);
13+
}

Orm/Xtensive.Orm.MySql/Orm.Providers.MySql/SqlCompiler.cs

+3-10
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@
99

1010
namespace Xtensive.Orm.Providers.MySql
1111
{
12-
internal class SqlCompiler : Providers.SqlCompiler
13-
{
14-
// Constructors
15-
16-
public SqlCompiler(HandlerAccessor handlers, CompilerConfiguration configuration)
17-
: base(handlers, configuration)
18-
{
19-
}
20-
}
21-
}
12+
internal class SqlCompiler(HandlerAccessor handlers, in CompilerConfiguration configuration)
13+
: Providers.SqlCompiler(handlers, configuration);
14+
}

Orm/Xtensive.Orm.Oracle/Orm.Providers.Oracle/SqlCompiler.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ protected override SqlExpression ProcessAggregate(SqlProvider source, IReadOnlyL
3737

3838
// Constructors
3939

40-
public SqlCompiler(HandlerAccessor handlers, CompilerConfiguration configuration)
40+
public SqlCompiler(HandlerAccessor handlers, in CompilerConfiguration configuration)
4141
: base(handlers, configuration)
4242
{
4343
}
4444
}
45-
}
45+
}

Orm/Xtensive.Orm.PostgreSql/Orm.Providers.PostgreSql/DomainHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ protected override IEnumerable<Type> GetProviderCompilerContainers()
3535
});
3636
}
3737
}
38-
}
38+
}

Orm/Xtensive.Orm.PostgreSql/Orm.Providers.PostgreSql/SqlCompiler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected override SqlExpression ProcessAggregate(SqlProvider source, IReadOnlyL
6262
return result;
6363
}
6464

65-
public SqlCompiler(HandlerAccessor handlers, CompilerConfiguration configuration)
65+
public SqlCompiler(HandlerAccessor handlers, in CompilerConfiguration configuration)
6666
: base(handlers, configuration)
6767
{
6868
}

Orm/Xtensive.Orm.SqlServer/Orm.Providers.SqlServer/DomainHandler.cs

+2-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ namespace Xtensive.Orm.Providers.SqlServer
1717
public class DomainHandler : Providers.DomainHandler
1818
{
1919
/// <inheritdoc/>
20-
protected override ICompiler CreateCompiler(CompilerConfiguration configuration)
21-
{
22-
return new SqlCompiler(Handlers, configuration);
23-
}
20+
protected override ICompiler CreateCompiler(CompilerConfiguration configuration) => new SqlCompiler(Handlers, configuration);
2421

2522
/// <inheritdoc/>
2623
protected override SearchConditionCompiler CreateSearchConditionVisitor()
@@ -30,4 +27,4 @@ protected override SearchConditionCompiler CreateSearchConditionVisitor()
3027
return new SearchConditionCompilerV11();
3128
}
3229
}
33-
}
30+
}

Orm/Xtensive.Orm.SqlServer/Orm.Providers.SqlServer/SqlCompiler.cs

+1-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace Xtensive.Orm.Providers.SqlServer
1717
{
18-
internal class SqlCompiler : Providers.SqlCompiler
18+
internal class SqlCompiler(HandlerAccessor handlers, in CompilerConfiguration configuration) : Providers.SqlCompiler(handlers, configuration)
1919
{
2020
private static readonly Type ByteType = typeof(byte);
2121
private static readonly Type Int16Type = typeof(short);
@@ -149,12 +149,5 @@ private bool ShouldCastDueType(Type type)
149149
|| type == DecimalType
150150
|| type == FloatType;
151151
}
152-
153-
// Constructors
154-
155-
public SqlCompiler(HandlerAccessor handlers, CompilerConfiguration configuration)
156-
: base(handlers, configuration)
157-
{
158-
}
159152
}
160153
}

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/v09/ColumnResolver.cs

+10-34
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,35 @@
44
// Created by: Dmitri Maximov
55
// Created: 2009.08.12
66

7-
using System;
8-
using System.Collections.Generic;
9-
using System.Linq;
107
using Xtensive.Sql.Model;
118

129
namespace Xtensive.Sql.Drivers.SqlServer.v09
1310
{
14-
internal sealed class ColumnResolver
11+
internal sealed class ColumnResolver(DataTable table)
1512
{
16-
public DataTable Table;
17-
private List<ColumnIndexMapping> columnMappings;
18-
19-
private class ColumnIndexMapping
20-
{
21-
public readonly int DbIndex;
22-
public readonly int ModelIndex;
23-
24-
public ColumnIndexMapping(int dbIndex, int modelIndex)
25-
{
26-
DbIndex = dbIndex;
27-
ModelIndex = modelIndex;
28-
}
29-
}
13+
private readonly record struct ColumnIndexMapping(int DbIndex, int ModelIndex, bool IsValid);
3014

31-
public void RegisterColumnMapping(int dbIndex, int modelIndex)
32-
{
33-
if (columnMappings == null)
34-
columnMappings = new List<ColumnIndexMapping>(1);
15+
public DataTable Table = table;
16+
private List<ColumnIndexMapping> columnMappings;
3517

36-
columnMappings.Add(new ColumnIndexMapping(dbIndex, modelIndex));
37-
}
18+
public void RegisterColumnMapping(int dbIndex, int modelIndex) =>
19+
(columnMappings ??= new(1)).Add(new ColumnIndexMapping(dbIndex, modelIndex, true));
3820

3921
public DataTableColumn GetColumn(int dbIndex)
4022
{
4123
int modelIndex = dbIndex-1;
42-
var view = Table as View;
43-
if (view != null)
24+
if (Table is View view)
4425
return view.ViewColumns[modelIndex];
4526

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

50-
var mapping = columnMappings.Where(item => item.DbIndex==dbIndex).FirstOrDefault();
51-
if (mapping != null)
31+
var mapping = columnMappings.FirstOrDefault(item => item.DbIndex==dbIndex);
32+
if (mapping != default)
5233
return table.TableColumns[mapping.ModelIndex];
5334

5435
throw new ArgumentOutOfRangeException("dbIndex");
5536
}
56-
57-
public ColumnResolver(DataTable table)
58-
{
59-
Table = table;
60-
}
6137
}
62-
}
38+
}

Orm/Xtensive.Orm.Sqlite/Orm.Providers.Sqlite/SqlCompiler.cs

+3-11
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@
88

99
namespace Xtensive.Orm.Providers.Sqlite
1010
{
11-
internal class SqlCompiler : Providers.SqlCompiler
12-
{
13-
14-
// Constructors
15-
16-
public SqlCompiler(HandlerAccessor handlers, CompilerConfiguration configuration)
17-
: base(handlers, configuration)
18-
{
19-
}
20-
}
21-
}
11+
internal class SqlCompiler(HandlerAccessor handlers, in CompilerConfiguration configuration)
12+
: Providers.SqlCompiler(handlers, configuration);
13+
}

Orm/Xtensive.Orm/Core/AliasGenerator.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Xtensive.Core
1212
/// Universal alias generator.
1313
/// </summary>
1414
[Serializable]
15-
public sealed class AliasGenerator
15+
public struct AliasGenerator
1616
{
1717
/// <summary>
1818
/// Default alias template. Value is "{0}{1}". Where {0} - template parameter for prefix and {1} - template parameter for suffix.
@@ -93,7 +93,7 @@ public static AliasGenerator Create(string[] overriddenPrefixes, string aliasTem
9393

9494
// Constructors
9595

96-
private AliasGenerator()
96+
public AliasGenerator()
9797
: this (DefaultAliasTemplate)
9898
{}
9999

@@ -111,4 +111,4 @@ private AliasGenerator(string[] prefixes, string aliasTemplate)
111111
this.aliasTemplate = aliasTemplate;
112112
}
113113
}
114-
}
114+
}

Orm/Xtensive.Orm/Orm/Building/Builders/TypeBuilder.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -485,18 +485,18 @@ private KeyInfo BuildKeyInfo(TypeInfo root, HierarchyDef hierarchyDef)
485485
var keyFields = root.Fields
486486
.Where(static field => field.IsPrimaryKey)
487487
.OrderBy(static field => field.MappingInfo.Offset)
488-
.ToList();
488+
.ToArray();
489489

490490
var keyColumns = keyFields
491491
.Where(static field => field.Column != null)
492492
.Select(static field => field.Column)
493-
.ToList();
493+
.ToArray();
494494

495495
var keyTupleDescriptor = TupleDescriptor.Create(
496-
keyColumns.Select(static c => c.ValueType).ToArray(keyColumns.Count));
496+
keyColumns.Select(static c => c.ValueType).ToArray(keyColumns.Length));
497497
var typeIdColumnIndex = -1;
498498
if (hierarchyDef.IncludeTypeId) {
499-
for (var i = 0; i < keyColumns.Count; i++) {
499+
for (var i = 0; i < keyColumns.Length; i++) {
500500
if (keyColumns[i].Field.IsTypeId) {
501501
typeIdColumnIndex = i;
502502
}
@@ -640,4 +640,4 @@ public TypeBuilder(BuildingContext context)
640640
.ToDictionary(configuration => context.NameBuilder.BuildKeyGeneratorName(configuration));
641641
}
642642
}
643-
}
643+
}

Orm/Xtensive.Orm/Orm/Linq/QueryProvider.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ internal TranslatedQuery Translate(Expression expression) =>
168168
Translate(expression, Session.CompilationService.CreateConfiguration(Session));
169169

170170
internal TranslatedQuery Translate(Expression expression,
171-
CompilerConfiguration compilerConfiguration)
171+
in CompilerConfiguration compilerConfiguration)
172172
{
173173
try {
174174
var compiledQueryScope = CompiledQueryProcessingScope.Current;
@@ -189,4 +189,4 @@ internal QueryProvider(Session session)
189189
Session = session;
190190
}
191191
}
192-
}
192+
}

Orm/Xtensive.Orm/Orm/Linq/Rewriters/AggregateOptimizer.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace Xtensive.Orm.Linq.Rewriters
2121
{
2222
internal sealed class AggregateOptimizer : ExtendedExpressionVisitor
2323
{
24+
private static readonly AggregateOptimizer Instance = new();
25+
2426
private sealed class GroupByItemWrapper<TKey, TElement>
2527
{
2628
public TKey Key { get; set; }
@@ -214,7 +216,7 @@ public static Expression Rewrite(Expression expression)
214216
// This would help Translator to translate GroupBy+Aggregate
215217
// into single AggregateProvider.
216218

217-
return new AggregateOptimizer().Visit(expression);
219+
return Instance.Visit(expression);
218220
}
219221

220222
private static void AddGroupByItemWrapper(GroupByQuery groupBy, List<LambdaExpression> elementProjections)
@@ -284,4 +286,4 @@ private AggregateOptimizer()
284286
{
285287
}
286288
}
287-
}
289+
}

Orm/Xtensive.Orm/Orm/Linq/TranslatorContext.cs

+5-12
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ namespace Xtensive.Orm.Linq
2525
{
2626
internal sealed class TranslatorContext
2727
{
28-
private readonly AliasGenerator resultAliasGenerator;
29-
private readonly AliasGenerator columnAliasGenerator;
28+
private AliasGenerator resultAliasGenerator = AliasGenerator.Create("#{0}{1}");
29+
private AliasGenerator columnAliasGenerator = AliasGenerator.Create(new[] {"c01umn"});
3030
private readonly Dictionary<ParameterExpression, Parameter<Tuple>> tupleParameters;
3131
private readonly Dictionary<CompilableProvider, ApplyParameter> applyParameters;
3232
private readonly Dictionary<ParameterExpression, ItemProjectorExpression> boundItemProjectors;
3333
private readonly Dictionary<MemberInfo, int> queryReuses;
3434

35-
public CompilerConfiguration RseCompilerConfiguration { get; }
35+
public readonly CompilerConfiguration RseCompilerConfiguration;
3636

3737
public ProviderInfo ProviderInfo { get; }
3838

@@ -124,11 +124,7 @@ public ItemProjectorExpression GetBoundItemProjector(ParameterExpression paramet
124124
return result;
125125
}
126126

127-
public void RegisterPossibleQueryReuse(MemberInfo memberInfo)
128-
{
129-
if (!queryReuses.ContainsKey(memberInfo))
130-
queryReuses.Add(memberInfo, 0);
131-
}
127+
public void RegisterPossibleQueryReuse(MemberInfo memberInfo) => queryReuses.TryAdd(memberInfo, 0);
132128

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

149145
// Constructors
150146

151-
public TranslatorContext(Session session, CompilerConfiguration rseCompilerConfiguration, Expression query,
147+
public TranslatorContext(Session session, in CompilerConfiguration rseCompilerConfiguration, Expression query,
152148
CompiledQueryProcessingScope compiledQueryScope)
153149
{
154150
ArgumentNullException.ThrowIfNull(session);
155-
ArgumentNullException.ThrowIfNull(rseCompilerConfiguration);
156151
ArgumentNullException.ThrowIfNull(query);
157152

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

176-
resultAliasGenerator = AliasGenerator.Create("#{0}{1}");
177-
columnAliasGenerator = AliasGenerator.Create(new[] {"c01umn"});
178171
CustomCompilerProvider = Domain.Handler.GetMemberCompilerProvider<Expression>();
179172
Model = Domain.Model;
180173
TypeIdRegistry = session.StorageNode.TypeIdRegistry;

Orm/Xtensive.Orm/Orm/Providers/CompilationService.cs

+10-9
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@ namespace Xtensive.Orm.Providers
1414
/// <summary>
1515
/// <see cref="CompilableProvider"/> compilation service.
1616
/// </summary>
17-
public sealed class CompilationService
17+
public readonly struct CompilationService
1818
{
1919
private readonly Func<CompilerConfiguration, ICompiler> compilerProvider;
2020
private readonly Func<CompilerConfiguration, IPreCompiler> preCompilerProvider;
2121
private readonly Func<CompilerConfiguration, ICompiler, IPostCompiler> postCompilerProvider;
2222

2323
public CompilerConfiguration CreateConfiguration(Session session) =>
24-
new() {
25-
StorageNode = session.StorageNode,
26-
Tags = session.Tags,
24+
new(
25+
PrepareRequest: true,
26+
StorageNode: session.StorageNode,
27+
Tags: session.Tags,
2728
// prefer constants during upgrade process
28-
PreferTypeIdAsParameter = !session.Name.Equals(WellKnown.Sessions.System) && session.Domain.Configuration.PreferTypeIdsAsQueryParameters
29-
};
29+
PreferTypeIdAsParameter: !session.Name.Equals(WellKnown.Sessions.System) && session.Domain.Configuration.PreferTypeIdsAsQueryParameters
30+
);
3031

3132
public ExecutableProvider Compile(CompilableProvider provider, CompilerConfiguration configuration)
3233
{
@@ -58,12 +59,12 @@ public CompilationService(
5859
Func<CompilerConfiguration, ICompiler, IPostCompiler> postCompilerProvider)
5960
{
6061
ArgumentNullException.ThrowIfNull(compilerProvider);
61-
ArgumentNullException.ThrowIfNull(compilerProvider, "preCompilerProvider");
62-
ArgumentNullException.ThrowIfNull(compilerProvider, "postCompilerProvider");
62+
ArgumentNullException.ThrowIfNull(preCompilerProvider);
63+
ArgumentNullException.ThrowIfNull(postCompilerProvider);
6364

6465
this.compilerProvider = compilerProvider;
6566
this.preCompilerProvider = preCompilerProvider;
6667
this.postCompilerProvider = postCompilerProvider;
6768
}
6869
}
69-
}
70+
}

0 commit comments

Comments
 (0)