Skip to content

Commit 0e4c9c2

Browse files
committed
Minor optimizations (#59)
* Make QueryTranslationResult readonly struct * Make QueryRequest readonly struct * Don't load string resource until necessary * improve is/as usages * Optimize PrefetchHelper && make Fetcher readonluy struct * Optimize BatchingCommandProcessor.PutTasksForExecution * Refactor PrefetchManager.GetGraphContainer() to avoid unnecessary allocation * Avoid double HashSet-search in TypeRegistry.Register() * Improving refactoring of TypeDefCollection * Use Queue.TryDequeue() * Avoid ReadOnly-wrapping of Empty arrays. They are always immutable * Avoid double dictionary search when removing * Make BuildBatch() to accept IReadOnlyList<string> arg to avoid conversions * Make ColumnInfoRef readonly struct * Refactor ColumnCollection * Make TypeReference readonly struct * Make ColumnGroup readonly struct * Make TypeInfoRef readonly struct
1 parent b852fb7 commit 0e4c9c2

56 files changed

Lines changed: 314 additions & 478 deletions

Some content is hidden

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

Extensions/Xtensive.Orm.BulkOperations/Internals/BulkDeleteOperation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ protected async override Task<int> ExecuteInternalAsync(CancellationToken token
5151
}
5252
}
5353

54-
private QueryCommand CreateCommand(QueryTranslationResult request)
54+
private QueryCommand CreateCommand(in QueryTranslationResult request)
5555
{
5656
var delete = SqlDml.Delete(SqlDml.TableRef(PrimaryIndexes[0].Table));
57-
Join(delete, (SqlSelect) request.Query);
57+
Join(delete, request.Query);
5858
return ToCommand(delete);
5959
}
6060

Extensions/Xtensive.Orm.BulkOperations/Internals/BulkUpdateOperation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ protected async override Task<int> ExecuteInternalAsync(CancellationToken token
5454
}
5555
}
5656

57-
private QueryCommand CreateCommand(QueryTranslationResult request)
57+
private QueryCommand CreateCommand(in QueryTranslationResult request)
5858
{
5959
var update = SqlDml.Update(SqlDml.TableRef(PrimaryIndexes[0].Table));
6060
setOperation.Statement = SetStatement.Create(update);
61-
Join(update, (SqlSelect) request.Query);
61+
Join(update, request.Query);
6262
setOperation.AddValues();
6363
return ToCommand(update);
6464
}

Extensions/Xtensive.Orm.BulkOperations/Internals/SetOperation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private void AddComputedStaticExpression(AddValueContext addContext)
103103
all,
104104
addContext.Lambda);
105105
QueryTranslationResult request = parent.GetRequest(parent.QueryProvider.CreateQuery<T>(selectExpression));
106-
var sqlSelect = ((SqlSelect)request.Query);
106+
var sqlSelect = request.Query;
107107
SqlExpression ex = sqlSelect.OrderBy[0].Expression;
108108
var placeholder = ex as SqlPlaceholder;
109109
if (placeholder == null)
@@ -133,7 +133,7 @@ private void AddComputedExpression(AddValueContext addContext)
133133
all,
134134
addContext.Lambda);
135135
QueryTranslationResult request = parent.GetRequest(parent.QueryProvider.CreateQuery<T>(selectExpression));
136-
var sqlSelect = ((SqlSelect) request.Query);
136+
var sqlSelect = request.Query;
137137
SqlExpression ex = sqlSelect.OrderBy[0].Expression;
138138
parent.Bindings.AddRange(request.ParameterBindings);
139139

@@ -217,7 +217,7 @@ private void AddEntityValue(AddValueContext addContext)
217217
QueryTranslationResult request = parent.GetRequest(field.ValueType, q);
218218
parent.Bindings.AddRange(request.ParameterBindings);
219219
SqlTableColumn c = SqlDml.TableColumn(addContext.Statement.Table, addContext.Field.Columns[i].Name);
220-
addContext.Statement.AddValue(c, SqlDml.SubQuery((ISqlQueryExpression) request.Query));
220+
addContext.Statement.AddValue(c, SqlDml.SubQuery(request.Query));
221221
}
222222
return;
223223
}

Orm/Xtensive.Orm.Tests/Storage/QueryBuilderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void ModifyQueryTest()
5656
Assert.That(translated.Query, Is.Not.Null);
5757
Assert.That(translated.ParameterBindings, Is.Not.Null);
5858

59-
var select = (SqlSelect) translated.Query;
59+
var select = translated.Query;
6060
select.Columns.Clear();
6161
select.Columns.Add(SqlDml.Count());
6262
var compiled = builder.CompileQuery(translated.Query);

Orm/Xtensive.Orm/Caching/WeakestCache.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,7 @@ public override TItem Add(TItem item, bool replaceIfExists)
236236
public override void RemoveKey(TKey key)
237237
{
238238
ArgumentValidator.EnsureArgumentNotNull(key, "key");
239-
WeakEntry entry;
240-
if (items.TryGetValue(key, out entry)) {
241-
items.Remove(key);
239+
if (items.Remove(key, out var entry)) {
242240
entry.Dispose();
243241
}
244242
}

Orm/Xtensive.Orm/Collections/ExtensionCollection.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,13 @@ public ExtensionCollection(IExtensionCollection source)
153153
ArgumentValidator.EnsureArgumentNotNull(source, "source");
154154
if (source.Count==0)
155155
return;
156-
var sourceLikeMe = source as ExtensionCollection;
157-
if (sourceLikeMe!=null)
156+
if (source is ExtensionCollection sourceLikeMe) {
158157
extensions = new Dictionary<Type, object>(sourceLikeMe.extensions);
159-
else
158+
}
159+
else {
160160
foreach (Type extensionType in source)
161161
Set(extensionType, source.Get(extensionType));
162+
}
162163
}
163164
}
164165
}

Orm/Xtensive.Orm/Collections/Graphs/TopologicalSorter.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2003-2012 Xtensive LLC.
1+
// Copyright (C) 2003-2012 Xtensive LLC.
22
// All rights reserved.
33
// For conditions of distribution and use, see license.
44
// Created by: Alex Yakunin
@@ -41,10 +41,8 @@ public T Pop()
4141

4242
public void Remove(T item)
4343
{
44-
LinkedListNode<T> node;
45-
if (map.TryGetValue(item, out node)) {
44+
if (map.Remove(item, out var node)) {
4645
list.Remove(node);
47-
map.Remove(item);
4846
}
4947
}
5048

@@ -104,8 +102,7 @@ public static TopologicalSortResult<TNode, TEdge> Sort<TNode, TEdge>(Graph<TNode
104102

105103
restart:
106104
// Sorting
107-
while (nodesWithoutIncomingEdges.Count!=0) {
108-
var node = nodesWithoutIncomingEdges.Dequeue();
105+
while (nodesWithoutIncomingEdges.TryDequeue(out var node)) {
109106
if (unsortedNodes!=null) // Break edges
110107
unsortedNodes.Remove(node);
111108
else
@@ -126,8 +123,7 @@ public static TopologicalSortResult<TNode, TEdge> Sort<TNode, TEdge>(Graph<TNode
126123
if (unsortedNodes!=null) { // Break edges
127124
if (unsortedNodes.Count != 0) {
128125
// Trying to break edges (collection is always empty when breakEdges==false)
129-
while (breakableEdges.Count != 0) {
130-
var edge = breakableEdges.Dequeue();
126+
while (breakableEdges.TryDequeue(out var edge)) {
131127
if (!edge.IsAttached || !edgeBreaker(edge))
132128
continue;
133129
result.BrokenEdges.Add(edge);

Orm/Xtensive.Orm/Collections/TopDeque.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,8 @@ public void AddToBottom(K key, V value)
253253
/// <inheritdoc/>
254254
public void Remove(K key)
255255
{
256-
LinkedListNode<Pair<K, V>> valueContainer;
257-
if (map.TryGetValue(key, out valueContainer)) {
256+
if (map.Remove(key, out var valueContainer)) {
258257
list.Remove(valueContainer);
259-
map.Remove(key);
260258
}
261259
}
262260

Orm/Xtensive.Orm/Collections/TypeRegistry.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,9 @@ public void Register(Type type)
6161
ArgumentValidator.EnsureArgumentNotNull(type, "type");
6262
if (!isProcessingPendingActions)
6363
Register(new TypeRegistration(type));
64-
else {
65-
if (typeSet.Contains(type))
66-
return;
64+
else if (typeSet.Add(type)) {
6765
serviceRegistrations = null;
6866
types.Add(type);
69-
typeSet.Add(type);
7067
assemblies.Add(type.Assembly);
7168
}
7269
}

Orm/Xtensive.Orm/Modelling/Comparison/Comparer.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,13 @@ protected virtual void CompareProperties(Node source, Node target, NodeDifferenc
296296
continue;
297297

298298
if (any.Nesting.PropertyInfo != null && accessor.DependencyRootType==any.Nesting.PropertyInfo.PropertyType) {
299-
if (propertyDifference is NodeDifference)
300-
((NodeDifference) propertyDifference).IsDependentOnParent = true;
301-
else if (propertyDifference is NodeCollectionDifference)
302-
((NodeCollectionDifference) propertyDifference).ItemChanges
299+
if (propertyDifference is NodeDifference nodeDifference) {
300+
nodeDifference.IsDependentOnParent = true;
301+
}
302+
else if (propertyDifference is NodeCollectionDifference nodeCollectionDifference) {
303+
nodeCollectionDifference.ItemChanges
303304
.ForEach(item=>item.IsDependentOnParent = true);
305+
}
304306
}
305307
difference.PropertyChanges.Add(property.Name, propertyDifference);
306308
}
@@ -411,12 +413,12 @@ protected virtual Difference VisitNodeCollection(NodeCollection source, NodeColl
411413
return difference.ItemChanges.Count != 0 ? difference : null;
412414
}
413415
}
414-
416+
415417
// Sort by items only with source, then by (target ?? source).Index then with source and target and then only with target
416418
private static int CompareNodeDifference(NodeDifference curr, NodeDifference other)
417419
{
418-
var currType = curr.Source != null && curr.Target != null ? 1 : curr.Source == null ? 3 : 0;
419-
var otherType = other.Source != null && other.Target != null ? 1 : other.Source == null ? 3 : 0;
420+
var currType = curr.Source != null && curr.Target != null ? 1 : curr.Source == null ? 3 : 0;
421+
var otherType = other.Source != null && other.Target != null ? 1 : other.Source == null ? 3 : 0;
420422
var typeIsNot0Comparison = (currType != 0).CompareTo(otherType != 0);
421423
if (typeIsNot0Comparison != 0) {
422424
return typeIsNot0Comparison;
@@ -590,10 +592,10 @@ protected virtual bool IsImmutable(Node node)
590592
/// <returns>Comparison key for the specified node.</returns>
591593
protected virtual string GetNodeComparisonKey(Node node)
592594
{
593-
if (!(node is INodeReference))
595+
if (!(node is INodeReference nodeReference))
594596
return GetTargetName(node);
595597

596-
var targetNode = ((INodeReference) node).Value;
598+
var targetNode = nodeReference.Value;
597599
return targetNode==null ? null : GetTargetPath(targetNode);
598600
}
599601

0 commit comments

Comments
 (0)