Skip to content

Commit ff12ca0

Browse files
authored
Merge pull request #392 from DataObjects-NET/7.0-perf-imps
A bunch of small improvements related to query translation
2 parents 0d73858 + 0973d9e commit ff12ca0

File tree

174 files changed

+1815
-2157
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

174 files changed

+1815
-2157
lines changed

ChangeLog/7.0.5_dev.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
[main] Updated BitFaster.Caching package
1+
[main] Some constructors of SqlPersistTask marked Obsolete, new constructors provided
2+
[main] Some constructors of CalculateProvider marked Obsolete, new constructors provided
3+
[main] Some constructors of AggregateProvider marked Obsolete, new constructors provided
4+
[main] Updated BitFaster.Caching package
5+
[main] Certain optimizations connected to query translation made

Orm/Xtensive.Orm.PostgreSql/Sql.Drivers.PostgreSql/v8_0/Extractor.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2003-2023 Xtensive LLC.
1+
// Copyright (C) 2003-2024 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44

@@ -718,14 +718,14 @@ protected virtual void ReadColumnData(DbDataReader dataReader, ExtractionContext
718718
var columnOwnerId = Convert.ToInt64(dataReader["attrelid"]);
719719
var columnId = Convert.ToInt64(dataReader["attnum"]);
720720
var columnName = dataReader["attname"].ToString();
721-
if (tableMap.ContainsKey(columnOwnerId)) {
722-
var table = tableMap[columnOwnerId];
721+
if (tableMap.TryGetValue(columnOwnerId, out var table)) {
723722
var col = table.CreateColumn(columnName);
724-
if (!tableColumns.ContainsKey(columnOwnerId)) {
725-
tableColumns.Add(columnOwnerId, new Dictionary<long, TableColumn>());
723+
if (tableColumns.TryGetValue(columnOwnerId, out var columns)) {
724+
columns.Add(columnId, col);
725+
}
726+
else {
727+
tableColumns.Add(columnOwnerId, new Dictionary<long, TableColumn>() { { columnId, col } });
726728
}
727-
728-
tableColumns[columnOwnerId].Add(columnId, col);
729729

730730
var columnTypeName = dataReader["typname"].ToString();
731731
var columnTypeSpecificData = Convert.ToInt32(dataReader["atttypmod"]);

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/ErrorMessageParser.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ public sealed class ErrorMessageParser
2323
private sealed class PreparedTemplate
2424
{
2525
public readonly Regex ParserExpression;
26-
public readonly ReadOnlyCollection<int> Indexes;
26+
public readonly IReadOnlyList<int> Indexes;
2727

28-
public PreparedTemplate(string regex, IEnumerable<int> indexes)
28+
public PreparedTemplate(string regex, IReadOnlyList<int> indexes)
2929
{
3030
ParserExpression = new Regex(regex, RegexOptions.Compiled | RegexOptions.CultureInvariant);
31-
Indexes = indexes.ToList().AsReadOnly();
31+
Indexes = indexes;
3232
}
3333
}
3434

@@ -190,7 +190,7 @@ private static PreparedTemplate PrepareEnglishTemplate(string template)
190190
}
191191

192192
CollectLastChunk(regexBuilder, template, offset);
193-
return new PreparedTemplate(regexBuilder.ToString(), Enumerable.Range(1, count));
193+
return new PreparedTemplate(regexBuilder.ToString(), Enumerable.Range(1, count).ToArray(count));
194194
}
195195

196196
private static PreparedTemplate PrepareNonEnglishTemplate(string template)

Orm/Xtensive.Orm/Core/Extensions/EnumerableExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2008-2021 Xtensive LLC.
1+
// Copyright (C) 2008-2024 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Alex Yakunin
@@ -601,7 +601,7 @@ public static List<TValue> SortTopologically<TValue>(this IEnumerable<TValue> va
601601
if (edgeTester.Invoke(left.Value, right.Value))
602602
new Edge(left, right);
603603
var result = TopologicalSorter.Sort(graph);
604-
return result.HasLoops ? null : result.SortedNodes.Select(node => node.Value).ToList();
604+
return result.HasLoops ? null : result.SortedNodes.SelectToList(node => node.Value);
605605
}
606606

607607
/// <summary>

Orm/Xtensive.Orm/Linq/Internals/ExpressionComparer.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2009-2020 Xtensive LLC.
1+
// Copyright (C) 2009-2024 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Denis Krjuchkov
@@ -111,7 +111,7 @@ private bool VisitListInit(ListInitExpression x, ListInitExpression y)
111111
return VisitNew(x.NewExpression, y.NewExpression)
112112
&& x.Initializers.Count==y.Initializers.Count
113113
&& x.Initializers
114-
.Zip(y.Initializers, (first, second) => new Pair<ElementInit>(first, second))
114+
.Zip(y.Initializers)
115115
.All(p => VisitElementInit(p.First, p.Second));
116116
}
117117

@@ -126,7 +126,7 @@ private bool VisitMemberInit(MemberInitExpression x, MemberInitExpression y)
126126
return VisitNew(x.NewExpression, y.NewExpression)
127127
&& x.Bindings.Count==y.Bindings.Count
128128
&& x.Bindings
129-
.Zip(y.Bindings, (first, second) => new Pair<MemberBinding>(first, second))
129+
.Zip(y.Bindings)
130130
.All(p => VisitMemberBinding(p.First, p.Second));
131131
}
132132

@@ -145,14 +145,14 @@ private bool VisitMemberBinding(MemberBinding x, MemberBinding y)
145145
var mby = (MemberMemberBinding)y;
146146
return mbx.Bindings.Count==mby.Bindings.Count
147147
&& mbx.Bindings
148-
.Zip(mby.Bindings, (first, second) => new Pair<MemberBinding>(first, second))
148+
.Zip(mby.Bindings)
149149
.All(p => VisitMemberBinding(p.First, p.Second));
150150
case MemberBindingType.ListBinding:
151151
var mlx = (MemberListBinding)x;
152152
var mly = (MemberListBinding)y;
153153
return mlx.Initializers.Count==mly.Initializers.Count
154154
&& mlx.Initializers
155-
.Zip(mly.Initializers, (first, second) => new Pair<ElementInit>(first, second))
155+
.Zip(mly.Initializers)
156156
.All(p => VisitElementInit(p.First, p.Second));
157157
default:
158158
throw new ArgumentOutOfRangeException();
@@ -189,7 +189,7 @@ private bool VisitNew(NewExpression x, NewExpression y)
189189
return false;
190190
if (x.Members.Count != y.Members.Count)
191191
return false;
192-
for (int i = 0; i < x.Members.Count; i++)
192+
for (int i = 0, count = x.Members.Count; i < count; i++)
193193
if (x.Members[i] != y.Members[i])
194194
return false;
195195
return true;
@@ -253,7 +253,7 @@ private bool CompareExpressionSequences<T>(
253253
{
254254
if (x.Count != y.Count)
255255
return false;
256-
for (int i = 0; i < x.Count; i++)
256+
for (int i = 0, count = x.Count; i < count; i++)
257257
if (!Visit(x[i], y[i]))
258258
return false;
259259
return true;

Orm/Xtensive.Orm/Modelling/Actions/ActionSequence.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2009-2024 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Alex Yakunin
55
// Created: 2009.03.23
66

@@ -51,8 +51,7 @@ public void Add(NodeAction action)
5151
var last = actions[lastIndex] as PropertyChangeAction;
5252
if (last!=null && ca.Path==last.Path) {
5353
foreach (var pair in last.Properties) {
54-
if (!ca.Properties.ContainsKey(pair.Key))
55-
ca.Properties.Add(pair.Key, pair.Value);
54+
_ = ca.Properties.TryAdd(pair.Key, pair.Value);
5655
}
5756
actions.RemoveAt(lastIndex);
5857
}

Orm/Xtensive.Orm/Modelling/Actions/CreateNodeAction.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2009-2024 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Alex Yakunin
55
// Created: 2009.03.23
66

@@ -102,8 +102,8 @@ protected override void PerformExecute(IModel model, IPathNode item)
102102
protected Node TryConstructor(IModel model, params object[] arguments)
103103
{
104104
if (parameters!=null)
105-
arguments = arguments.Concat(parameters.Select(p => PathNodeReference.Resolve(model, p))).ToArray();
106-
var argTypes = arguments.Select(a => a.GetType()).ToArray();
105+
arguments = arguments.Concat(parameters.Select(p => PathNodeReference.Resolve(model, p))).ToArray(arguments.Length + parameters.Length);
106+
var argTypes = arguments.SelectToArray(a => a.GetType());
107107
var ci = type.GetConstructor(argTypes);
108108
if (ci==null)
109109
return null;

Orm/Xtensive.Orm/Modelling/Actions/GroupingNodeAction.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2009-2024 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Alex Yakunin
55
// Created: 2009.04.22
66

@@ -56,8 +56,7 @@ public void Add(NodeAction action)
5656
var last = actions[lastIndex] as PropertyChangeAction;
5757
if (last!=null && ca.Path==last.Path) {
5858
foreach (var pair in last.Properties) {
59-
if (!ca.Properties.ContainsKey(pair.Key))
60-
ca.Properties.Add(pair.Key, pair.Value);
59+
_ = ca.Properties.TryAdd(pair.Key, pair.Value);
6160
}
6261
actions.RemoveAt(lastIndex);
6362
}

Orm/Xtensive.Orm/Modelling/Comparison/Hints/HintSet.cs

+41-82
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2009-2021 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2009-2024 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Alex Yakunin
55
// Created: 2009.03.26
66

@@ -77,31 +77,25 @@ public void Add(Hint hint)
7777
var nodes = new List<Node>();
7878
foreach (var target in targets) {
7979
Node node;
80-
if (target.Model==ModelType.Source)
80+
if (target.Model == ModelType.Source)
8181
node = (Node) SourceModel.Resolve(target.Path, true);
8282
else
8383
node = (Node) TargetModel.Resolve(target.Path, true);
8484
nodes.Add(node);
85-
86-
if (!hintMap.ContainsKey(node))
87-
hintMap.Add(node, new Dictionary<Type, object>());
88-
var nodeHintMap = hintMap[node];
85+
86+
var nodeHintMap = GetNodeHints(node);
8987
var hintType = hint.GetType();
90-
91-
if (!nodeHintMap.ContainsKey(hintType))
92-
nodeHintMap.Add(hintType, null);
93-
94-
var hintOrList = nodeHintMap[hintType];
95-
if (hintOrList==null)
96-
nodeHintMap[hintType] = hint;
97-
else {
98-
var list = hintOrList as List<Hint>;
99-
if (list==null) {
100-
var oldHint = (Hint) hintOrList;
101-
nodeHintMap[hintType] = new List<Hint>(new[] {oldHint, hint});
102-
}
103-
else
88+
89+
if (nodeHintMap.TryGetValue(hintType, out var hintOrList)) {
90+
if (hintOrList is List<Hint> list) {
10491
list.Add(hint);
92+
}
93+
else {
94+
nodeHintMap[hintType] = new List<Hint>(new[] { (Hint) hintOrList, hint });
95+
}
96+
}
97+
else {
98+
nodeHintMap.Add(hintType, hint);
10599
}
106100
}
107101
}
@@ -126,46 +120,20 @@ public void Clear()
126120

127121
/// <inheritdoc/>
128122
/// <exception cref="InvalidOperationException">Multiple hints found.</exception>
129-
public THint GetHint<THint>(Node node)
130-
where THint : Hint
131-
{
132-
ArgumentValidator.EnsureArgumentNotNull(node, "node");
133-
134-
if (!hintMap.ContainsKey(node))
135-
hintMap.Add(node, new Dictionary<Type, object>());
136-
var nodeHintMap = hintMap.GetValueOrDefault(node);
137-
if (nodeHintMap==null)
138-
return null;
139-
var hintType = typeof(THint);
140-
var hintOrList = nodeHintMap.GetValueOrDefault(hintType);
141-
if (hintOrList==null)
142-
return null;
143-
var hint = hintOrList as THint;
144-
if (hint!=null)
145-
return hint;
146-
throw new InvalidOperationException(Strings.ExMultipleHintsFound);
147-
}
123+
public THint GetHint<THint>(Node node) where THint : Hint =>
124+
GetNodeHints(node).GetValueOrDefault(typeof(THint)) switch {
125+
null => null,
126+
THint hint => hint,
127+
_ => throw new InvalidOperationException(Strings.ExMultipleHintsFound)
128+
};
148129

149130
/// <inheritdoc/>
150-
public THint[] GetHints<THint>(Node node)
151-
where THint : Hint
152-
{
153-
ArgumentValidator.EnsureArgumentNotNull(node, "node");
154-
155-
if (!hintMap.ContainsKey(node))
156-
hintMap.Add(node, new Dictionary<Type, object>());
157-
var nodeHintMap = hintMap.GetValueOrDefault(node);
158-
if (nodeHintMap==null)
159-
return ArrayUtils<THint>.EmptyArray;
160-
var hintType = typeof (THint);
161-
var hintOrList = nodeHintMap.GetValueOrDefault(hintType);
162-
if (hintOrList==null)
163-
return ArrayUtils<THint>.EmptyArray;
164-
var hint = hintOrList as THint;
165-
if (hint!=null)
166-
return new[] {hint};
167-
return ((List<Hint>) hintOrList).Cast<THint>().ToArray();
168-
}
131+
public THint[] GetHints<THint>(Node node) where THint : Hint =>
132+
GetNodeHints(node).GetValueOrDefault(typeof(THint)) switch {
133+
null => Array.Empty<THint>(),
134+
THint hint => new[] { hint },
135+
var list => ((List<Hint>) list).Cast<THint>().ToArray()
136+
};
169137

170138
/// <summary>
171139
/// Determines whether there are any hints associated with the specified.
@@ -175,29 +143,10 @@ public THint[] GetHints<THint>(Node node)
175143
/// <see langword="true"/> if the specified node has associated hints;
176144
/// otherwise, <see langword="false"/>.
177145
/// </returns>
178-
public bool HasHints(Node node)
179-
{
180-
ArgumentValidator.EnsureArgumentNotNull(node, "node");
146+
public bool HasHints(Node node) => GetNodeHints(node).Count > 0;
181147

182-
if (!hintMap.ContainsKey(node))
183-
hintMap.Add(node, new Dictionary<Type, object>());
184-
var nodeHintMap = hintMap.GetValueOrDefault(node);
185-
if (nodeHintMap==null)
186-
return false;
187-
188-
return nodeHintMap.Values.Count > 0;
189-
}
190-
191-
public bool HasHints<THint>(Node node)
192-
where THint : Hint
193-
{
194-
ArgumentValidator.EnsureArgumentNotNull(node, "node");
195-
196-
if (!hintMap.TryGetValue(node, out var nodeHintMap)) {
197-
hintMap.Add(node, nodeHintMap = new Dictionary<Type, object>());
198-
}
199-
return nodeHintMap.ContainsKey(typeof(THint));
200-
}
148+
public bool HasHints<THint>(Node node) where THint : Hint =>
149+
GetNodeHints(node).ContainsKey(typeof(THint));
201150

202151
#region IEnumerable<...> methods
203152

@@ -215,6 +164,16 @@ IEnumerator IEnumerable.GetEnumerator()
215164

216165
#endregion
217166

167+
private Dictionary<Type, object> GetNodeHints(Node node)
168+
{
169+
ArgumentValidator.EnsureArgumentNotNull(node, "node");
170+
171+
if (!hintMap.TryGetValue(node, out var nodeHintMap)) {
172+
hintMap.Add(node, nodeHintMap = new Dictionary<Type, object>());
173+
}
174+
return nodeHintMap;
175+
}
176+
218177
#region ILockable methods
219178

220179
/// <inheritdoc/>

0 commit comments

Comments
 (0)