Skip to content

Commit a4229c0

Browse files
committed
PERF-407: Optimization: get rid of .ToArray(n) usages
1 parent f58d042 commit a4229c0

File tree

49 files changed

+113
-194
lines changed

Some content is hidden

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

49 files changed

+113
-194
lines changed

Orm/Xtensive.Orm.Firebird/Sql.Drivers.Firebird/v2_5/Compiler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public override void Visit(SqlFunctionCall node)
208208

209209
switch (node.FunctionType) {
210210
case SqlFunctionType.Concat:
211-
Visit(SqlDml.Concat(arguments.ToArray(node.Arguments.Count)));
211+
Visit(SqlDml.Concat(arguments.ToArray()));
212212
return;
213213
case SqlFunctionType.DateTimeTruncate:
214214
Visit(SqlDml.Cast(arguments[0], new SqlValueType("Date")));
@@ -458,4 +458,4 @@ protected internal Compiler(SqlDriver driver)
458458
{
459459
}
460460
}
461-
}
461+
}

Orm/Xtensive.Orm.MySql/Sql.Drivers.MySql/v5_0/Compiler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public override void Visit(SqlFunctionCall node)
152152
SqlDml.FunctionCall("TRUNCATE", arguments[0], SqlDml.Literal(0)).AcceptVisitor(this);
153153
return;
154154
case SqlFunctionType.Concat:
155-
Visit(SqlDml.Concat(arguments.ToArray(node.Arguments.Count)));
155+
Visit(SqlDml.Concat(arguments.ToArray()));
156156
return;
157157
case SqlFunctionType.CharLength:
158158
SqlDml.FunctionCall(translator.TranslateToString(SqlFunctionType.CharLength), node.Arguments[0]).AcceptVisitor(this);
@@ -448,4 +448,4 @@ protected internal Compiler(SqlDriver driver)
448448
{
449449
}
450450
}
451-
}
451+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected override SqlProvider VisitFreeText(FreeTextProvider provider)
3737
var query = queryAndBindings.Query;
3838
var table = Mapping[realPrimaryIndex.ReflectedType];
3939
var fromTable = SqlDml.FreeTextTable(table, searchCriteriaBinding.ParameterReference,
40-
table.Columns.Select(column => column.Name).Append(rankColumnName).ToArray(table.Columns.Count + 1));
40+
table.Columns.Select(column => column.Name).Append(rankColumnName).ToArray());
4141
var fromTableRef = SqlDml.QueryRef(fromTable);
4242
foreach (var column in query.Columns) {
4343
select.Columns.Add(fromTableRef.Columns[column.Name] ?? column);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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).ToArray(count));
193+
return new PreparedTemplate(regexBuilder.ToString(), Enumerable.Range(1, count).ToArray());
194194
}
195195

196196
private static PreparedTemplate PrepareNonEnglishTemplate(string template)
@@ -246,4 +246,4 @@ public ErrorMessageParser(IEnumerable<KeyValuePair<int,string>> messageTemplates
246246
: messageTemplates.ToDictionary(item => item.Key, item => PrepareNonEnglishTemplate(item.Value));
247247
}
248248
}
249-
}
249+
}

Orm/Xtensive.Orm/Core/AssociateProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ protected AssociateProvider(SerializationInfo info, StreamingContext context)
305305
typeSuffixes = (string[]) info.GetValue(nameof(typeSuffixes), typeof(string[]));
306306

307307
var highPriorityLocationsSerializable = (List<(string, string)>) info.GetValue(nameof(highPriorityLocations), typeof(List<(string, string)>));
308-
highPriorityLocations = highPriorityLocationsSerializable.SelectToList(ls => (Assembly.Load(ls.Item1), ls.Item2));
308+
highPriorityLocations = highPriorityLocationsSerializable.Select(ls => (Assembly.Load(ls.Item1), ls.Item2)).ToList();
309309
}
310310

311311
/// <summary>
@@ -333,7 +333,7 @@ public virtual void GetObjectData(SerializationInfo info, StreamingContext conte
333333
info.AddValue(nameof(constructorParams), constructorParamsExceptThis, constructorParams.GetType());
334334
info.AddValue(nameof(typeSuffixes), typeSuffixes, typeSuffixes.GetType());
335335

336-
var highPriorityLocationsSerializable = HighPriorityLocations.SelectToList(l => (l.Item1.FullName, l.Item2));
336+
var highPriorityLocationsSerializable = HighPriorityLocations.Select(l => (l.Item1.FullName, l.Item2)).ToList();
337337
info.AddValue(nameof(highPriorityLocations), highPriorityLocationsSerializable, highPriorityLocationsSerializable.GetType());
338338
}
339339
}

Orm/Xtensive.Orm/Core/Extensions/CollectionExtensions.cs

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,6 @@ namespace Xtensive.Core
1717
/// </summary>
1818
public static class CollectionExtensionsEx
1919
{
20-
/// <summary>
21-
/// Converts <paramref name="source"/> collection to an array.
22-
/// </summary>
23-
/// <typeparam name="TItem">The type of collection items.</typeparam>
24-
/// <param name="source">Collection to convert.</param>
25-
/// <returns>An array containing all the items from the <paramref name="source"/>.</returns>
26-
public static TItem[] ToArray<TItem>(this ICollection<TItem> source)
27-
{
28-
var items = new TItem[source.Count];
29-
source.Copy(items, 0);
30-
return items;
31-
}
3220

3321
/// <summary>
3422
/// Copies the items from <paramref name="source"/> collection
@@ -139,62 +127,6 @@ public static bool ContainsAny<TItem>(this ICollection<TItem> collection, IEnume
139127
return false;
140128
}
141129

142-
/// <summary>Projects each element of a sequence into a new form.</summary>
143-
/// <param name="source">A collection of values to invoke a transform function on.</param>
144-
/// <param name="selector">A transform function to apply to each element.</param>
145-
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
146-
/// <typeparam name="TResult">The type of the value returned by <paramref name="selector" />.</typeparam>
147-
/// <returns>An <see cref="T:System.Array`1" /> whose elements are the result of invoking the transform function on each element of <paramref name="source" />.</returns>
148-
/// <exception cref="T:System.ArgumentNullException">
149-
/// <paramref name="source" /> or <paramref name="selector" /> is <see langword="null" />.</exception>
150-
public static TResult[] SelectToArray<TSource, TResult>(
151-
this ICollection<TSource> source, Func<TSource, TResult> selector)
152-
{
153-
return source.Select(selector).ToArray(source.Count);
154-
}
155-
156-
/// <summary>Projects each element of a sequence into a new form by incorporating the element's index.</summary>
157-
/// <param name="source">A collection of values to invoke a transform function on.</param>
158-
/// <param name="selector">A transform function to apply to each source element; the second parameter of the function represents the index of the source element.</param>
159-
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
160-
/// <typeparam name="TResult">The type of the value returned by <paramref name="selector" />.</typeparam>
161-
/// <returns>An <see cref="T:System.Array`1" /> whose elements are the result of invoking the transform function on each element of <paramref name="source" />.</returns>
162-
/// <exception cref="T:System.ArgumentNullException">
163-
/// <paramref name="source" /> or <paramref name="selector" /> is <see langword="null" />.</exception>
164-
public static TResult[] SelectToArray<TSource, TResult>(
165-
this ICollection<TSource> source, Func<TSource, int, TResult> selector)
166-
{
167-
return source.Select(selector).ToArray(source.Count);
168-
}
169-
170-
/// <summary>Projects each element of a sequence into a new form.</summary>
171-
/// <param name="source">A collection of values to invoke a transform function on.</param>
172-
/// <param name="selector">A transform function to apply to each element.</param>
173-
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
174-
/// <typeparam name="TResult">The type of the value returned by <paramref name="selector" />.</typeparam>
175-
/// <returns>An <see cref="T:System.Collections.Generic.List`1" /> whose elements are the result of invoking the transform function on each element of <paramref name="source" />.</returns>
176-
/// <exception cref="T:System.ArgumentNullException">
177-
/// <paramref name="source" /> or <paramref name="selector" /> is <see langword="null" />.</exception>
178-
public static List<TResult> SelectToList<TSource, TResult>(
179-
this ICollection<TSource> source, Func<TSource, TResult> selector)
180-
{
181-
return source.Select(selector).ToList(source.Count);
182-
}
183-
184-
/// <summary>Projects each element of a sequence into a new form by incorporating the element's index.</summary>
185-
/// <param name="source">A collection of values to invoke a transform function on.</param>
186-
/// <param name="selector">A transform function to apply to each source element; the second parameter of the function represents the index of the source element.</param>
187-
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
188-
/// <typeparam name="TResult">The type of the value returned by <paramref name="selector" />.</typeparam>
189-
/// <returns>An <see cref="T:System.Collections.Generic.List`1" /> whose elements are the result of invoking the transform function on each element of <paramref name="source" />.</returns>
190-
/// <exception cref="T:System.ArgumentNullException">
191-
/// <paramref name="source" /> or <paramref name="selector" /> is <see langword="null" />.</exception>
192-
public static List<TResult> SelectToList<TSource, TResult>(
193-
this ICollection<TSource> source, Func<TSource, int, TResult> selector)
194-
{
195-
return source.Select(selector).ToList(source.Count);
196-
}
197-
198130
/// <summary>
199131
/// Projects each element of a sequence into two elements of new form and adds it to the first or the second array respectively.
200132
/// </summary>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ public static List<TValue> SortTopologically<TValue>(this IEnumerable<TValue> va
580580
if (edgeTester.Invoke(left.Value, right.Value))
581581
new Edge(left, right);
582582
var result = TopologicalSorter.Sort(graph);
583-
return result.HasLoops ? null : result.SortedNodes.SelectToList(node => node.Value);
583+
return result.HasLoops ? null : result.SortedNodes.Select(node => node.Value).ToList();
584584
}
585585

586586
internal static IReadOnlyList<T> ToReadOnlyList<T>(this IEnumerable<T> seq) =>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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(arguments.Length + parameters.Length);
106-
var argTypes = arguments.SelectToArray(a => a.GetType());
105+
arguments = arguments.Concat(parameters.Select(p => PathNodeReference.Resolve(model, p))).ToArray();
106+
var argTypes = arguments.Select(a => a.GetType()).ToArray();
107107
var ci = type.GetConstructor(argTypes);
108108
if (ci==null)
109109
return null;

Orm/Xtensive.Orm/Orm/Building/Builders/IndexBuilder.ClassTable.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private void BuildClassTableIndexes(TypeInfo type)
9797
if (untypedIndexes.Contains(primaryIndex) && primaryIndex.ReflectedType == root) {
9898
primaryIndex = type.Indexes.Single(i => i.DeclaringIndex == primaryIndex.DeclaringIndex && i.IsTyped);
9999
}
100-
var filterByTypes = type.AllDescendants.Append(type).ToList(type.AllDescendants.Count + 1);
100+
var filterByTypes = type.AllDescendants.Append(type).ToList();
101101

102102
// Build virtual primary index
103103
if (ancestors.Count > 0) {
@@ -156,4 +156,4 @@ private static bool IndexBuiltOverInheritedFields(IndexInfo index)
156156
return false;
157157
}
158158
}
159-
}
159+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,7 @@ private KeyInfo BuildKeyInfo(TypeInfo root, HierarchyDef hierarchyDef)
489489
.Select(static field => field.Column)
490490
.ToArray();
491491

492-
var keyTupleDescriptor = TupleDescriptor.Create(
493-
keyColumns.Select(static c => c.ValueType).ToArray(keyColumns.Length));
492+
var keyTupleDescriptor = TupleDescriptor.Create(keyColumns.Select(static c => c.ValueType).ToArray());
494493
var typeIdColumnIndex = -1;
495494
if (hierarchyDef.IncludeTypeId) {
496495
for (var i = 0; i < keyColumns.Length; i++) {

0 commit comments

Comments
 (0)