Skip to content

Commit 74d92fa

Browse files
authored
Merge pull request #208 from servicetitan/upstream/Optimize_ArraySegment
Optimizing refactoring: use ArraySegment instead of subarrays allocation
2 parents 12bc6cf + 63bbb53 commit 74d92fa

10 files changed

Lines changed: 67 additions & 63 deletions

File tree

Orm/Xtensive.Orm/Orm/Internals/GenericKeyFactory.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2003-2021 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: Alexander Nikolaev
55
// Created: 2009.07.27
66

77
using System;
8+
using System.Collections.Generic;
89
using Xtensive.Orm.Model;
910
using Tuple = Xtensive.Tuples.Tuple;
1011

@@ -15,14 +16,14 @@ internal sealed class GenericKeyFactory
1516
{
1617
public readonly Type Type;
1718
public readonly Func<string, TypeInfo, Tuple, TypeReferenceAccuracy, Key> DefaultConstructor;
18-
public readonly Func<string, TypeInfo, Tuple, TypeReferenceAccuracy, int[], Key> KeyIndexBasedConstructor;
19+
public readonly Func<string, TypeInfo, Tuple, TypeReferenceAccuracy, IReadOnlyList<int>, Key> KeyIndexBasedConstructor;
1920

2021

2122
// Constructors
2223

2324
public GenericKeyFactory(Type type,
2425
Func<string, TypeInfo, Tuple, TypeReferenceAccuracy, Key> defaultConstructor,
25-
Func<string, TypeInfo, Tuple, TypeReferenceAccuracy, int[], Key> keyIndexBasedConstructor)
26+
Func<string, TypeInfo, Tuple, TypeReferenceAccuracy, IReadOnlyList<int>, Key> keyIndexBasedConstructor)
2627
{
2728
Type = type;
2829
DefaultConstructor = defaultConstructor;

Orm/Xtensive.Orm/Orm/Internals/KeyFactory.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2009-2020 Xtensive LLC.
1+
// Copyright (C) 2009-2021 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: Dmitri Maximov
@@ -35,7 +35,7 @@ public static Key Generate(Session session, TypeInfo typeInfo)
3535
}
3636

3737
public static Key Materialize(Domain domain, string nodeId,
38-
TypeInfo type, Tuple value, TypeReferenceAccuracy accuracy, bool canCache, int[] keyIndexes)
38+
TypeInfo type, Tuple value, TypeReferenceAccuracy accuracy, bool canCache, IReadOnlyList<int> keyIndexes)
3939
{
4040
var hierarchy = type.Hierarchy;
4141
var keyInfo = type.Key;
@@ -131,18 +131,18 @@ public static bool IsValidKeyTuple(Tuple tuple)
131131
return true;
132132
}
133133

134-
public static bool IsValidKeyTuple(Tuple tuple, int[] keyIndexes)
134+
public static bool IsValidKeyTuple(Tuple tuple, IReadOnlyList<int> keyIndexes)
135135
{
136136
if (keyIndexes==null)
137137
return IsValidKeyTuple(tuple);
138-
var limit = keyIndexes.Length;
138+
var limit = keyIndexes.Count;
139139
for (int i = 0; i < limit; i++)
140140
if (tuple.GetFieldState(keyIndexes[i]).IsNull())
141141
return false;
142142
return true;
143143
}
144144

145-
private static Key CreateGenericKey(Domain domain, string nodeId, TypeInfo type, TypeReferenceAccuracy accuracy, Tuple tuple, int[] keyIndexes)
145+
private static Key CreateGenericKey(Domain domain, string nodeId, TypeInfo type, TypeReferenceAccuracy accuracy, Tuple tuple, IReadOnlyList<int> keyIndexes)
146146
{
147147
var keyTypeInfo = domain.GenericKeyFactories.GetOrAdd(type, BuildGenericKeyFactory);
148148
if (keyIndexes==null)
@@ -158,7 +158,7 @@ private static GenericKeyFactory BuildGenericKeyFactory(TypeInfo typeInfo)
158158
keyType = keyType.MakeGenericType(descriptor.ToArray(descriptor.Count));
159159
var defaultConstructor = DelegateHelper.CreateDelegate<Func<string, TypeInfo, Tuple, TypeReferenceAccuracy, Key>>(
160160
null, keyType, "Create", Array.Empty<Type>());
161-
var keyIndexBasedConstructor = DelegateHelper.CreateDelegate<Func<string, TypeInfo, Tuple, TypeReferenceAccuracy, int[], Key>>(
161+
var keyIndexBasedConstructor = DelegateHelper.CreateDelegate<Func<string, TypeInfo, Tuple, TypeReferenceAccuracy, IReadOnlyList<int>, Key>>(
162162
null, keyType, "Create", Array.Empty<Type>());
163163
return new GenericKeyFactory(keyType, defaultConstructor, keyIndexBasedConstructor);
164164
}

Orm/Xtensive.Orm/Orm/Internals/Key{T1,T2,T3,T4}.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2003-2021 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: Alexander Nikolaev
55
// Created: 2009.07.13
66

77
using System;
8+
using System.Collections.Generic;
89
using JetBrains.Annotations;
910
using Xtensive.Core;
1011
using Xtensive.Orm.Model;
@@ -16,13 +17,13 @@ namespace Xtensive.Orm.Internals
1617
[Serializable]
1718
internal sealed class Key<T1, T2, T3, T4> : Key
1819
{
19-
private static readonly Predicate<T1, T1> equalityComparer1 =
20+
private static readonly Predicate<T1, T1> EqualityComparer1 =
2021
ComparerProvider.Default.GetComparer<T1>().Equals;
21-
private static readonly Predicate<T2, T2> equalityComparer2 =
22+
private static readonly Predicate<T2, T2> EqualityComparer2 =
2223
ComparerProvider.Default.GetComparer<T2>().Equals;
23-
private static readonly Predicate<T3, T3> equalityComparer3 =
24+
private static readonly Predicate<T3, T3> EqualityComparer3 =
2425
ComparerProvider.Default.GetComparer<T3>().Equals;
25-
private static readonly Predicate<T4, T4> equalityComparer4 =
26+
private static readonly Predicate<T4, T4> EqualityComparer4 =
2627
ComparerProvider.Default.GetComparer<T4>().Equals;
2728

2829
private readonly T1 value1;
@@ -46,10 +47,10 @@ protected override bool ValueEquals(Key other)
4647
if (otherKey == null)
4748
return false;
4849
return
49-
equalityComparer4.Invoke(value4, otherKey.value4) &&
50-
equalityComparer3.Invoke(value3, otherKey.value3) &&
51-
equalityComparer2.Invoke(value2, otherKey.value2) &&
52-
equalityComparer1.Invoke(value1, otherKey.value1);
50+
EqualityComparer4.Invoke(value4, otherKey.value4) &&
51+
EqualityComparer3.Invoke(value3, otherKey.value3) &&
52+
EqualityComparer2.Invoke(value2, otherKey.value2) &&
53+
EqualityComparer1.Invoke(value1, otherKey.value1);
5354
}
5455

5556
protected override int CalculateHashCode()
@@ -62,7 +63,7 @@ protected override int CalculateHashCode()
6263
}
6364

6465
[UsedImplicitly]
65-
public static Key Create(string nodeId, TypeInfo type, Tuple tuple, TypeReferenceAccuracy accuracy, int[] keyIndexes)
66+
public static Key Create(string nodeId, TypeInfo type, Tuple tuple, TypeReferenceAccuracy accuracy, IReadOnlyList<int> keyIndexes)
6667
{
6768
return new Key<T1, T2, T3, T4>(nodeId, type, accuracy,
6869
tuple.GetValueOrDefault<T1>(keyIndexes[0]),
@@ -81,7 +82,7 @@ public static Key Create(string nodeId, TypeInfo type, Tuple tuple, TypeReferenc
8182
tuple.GetValueOrDefault<T4>(3));
8283
}
8384

84-
85+
8586
// Constructors
8687

8788
private Key(string nodeId, TypeInfo type, TypeReferenceAccuracy accuracy, T1 value1, T2 value2, T3 value3, T4 value4)

Orm/Xtensive.Orm/Orm/Internals/Key{T1,T2,T3}.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2003-2021 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: Alexander Nikolaev
55
// Created: 2009.07.13
66

77
using System;
8+
using System.Collections.Generic;
89
using JetBrains.Annotations;
910
using Xtensive.Core;
1011
using Xtensive.Orm.Model;
@@ -16,11 +17,11 @@ namespace Xtensive.Orm.Internals
1617
[Serializable]
1718
internal sealed class Key<T1, T2, T3> : Key
1819
{
19-
private static readonly Predicate<T1, T1> equalityComparer1 =
20+
private static readonly Predicate<T1, T1> EqualityComparer1 =
2021
ComparerProvider.Default.GetComparer<T1>().Equals;
21-
private static readonly Predicate<T2, T2> equalityComparer2 =
22+
private static readonly Predicate<T2, T2> EqualityComparer2 =
2223
ComparerProvider.Default.GetComparer<T2>().Equals;
23-
private static readonly Predicate<T3, T3> equalityComparer3 =
24+
private static readonly Predicate<T3, T3> EqualityComparer3 =
2425
ComparerProvider.Default.GetComparer<T3>().Equals;
2526

2627
private readonly T1 value1;
@@ -42,9 +43,9 @@ protected override bool ValueEquals(Key other)
4243
if (otherKey == null)
4344
return false;
4445
return
45-
equalityComparer3.Invoke(value3, otherKey.value3) &&
46-
equalityComparer2.Invoke(value2, otherKey.value2) &&
47-
equalityComparer1.Invoke(value1, otherKey.value1);
46+
EqualityComparer3.Invoke(value3, otherKey.value3) &&
47+
EqualityComparer2.Invoke(value2, otherKey.value2) &&
48+
EqualityComparer1.Invoke(value1, otherKey.value1);
4849
}
4950

5051
protected override int CalculateHashCode()
@@ -56,7 +57,7 @@ protected override int CalculateHashCode()
5657
}
5758

5859
[UsedImplicitly]
59-
public static Key Create(string nodeId, TypeInfo type, Tuple tuple, TypeReferenceAccuracy accuracy, int[] keyIndexes)
60+
public static Key Create(string nodeId, TypeInfo type, Tuple tuple, TypeReferenceAccuracy accuracy, IReadOnlyList<int> keyIndexes)
6061
{
6162
return new Key<T1, T2, T3>(nodeId, type, accuracy,
6263
tuple.GetValueOrDefault<T1>(keyIndexes[0]),
@@ -73,7 +74,7 @@ public static Key Create(string nodeId, TypeInfo type, Tuple tuple, TypeReferenc
7374
tuple.GetValueOrDefault<T3>(2));
7475
}
7576

76-
77+
7778
// Constructors
7879

7980
private Key(string nodeId, TypeInfo type, TypeReferenceAccuracy accuracy, T1 value1, T2 value2, T3 value3)

Orm/Xtensive.Orm/Orm/Internals/Key{T1,T2}.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2003-2021 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: Alexander Nikolaev
55
// Created: 2009.07.13
66

77
using System;
8+
using System.Collections.Generic;
89
using JetBrains.Annotations;
910
using Xtensive.Core;
1011
using Xtensive.Orm.Model;
@@ -16,9 +17,9 @@ namespace Xtensive.Orm.Internals
1617
[Serializable]
1718
internal sealed class Key<T1, T2> : Key
1819
{
19-
private static readonly Predicate<T1, T1> equalityComparer1 =
20+
private static readonly Predicate<T1, T1> EqualityComparer1 =
2021
ComparerProvider.Default.GetComparer<T1>().Equals;
21-
private static readonly Predicate<T2, T2> equalityComparer2 =
22+
private static readonly Predicate<T2, T2> EqualityComparer2 =
2223
ComparerProvider.Default.GetComparer<T2>().Equals;
2324

2425
private readonly T1 value1;
@@ -38,8 +39,8 @@ protected override bool ValueEquals(Key other)
3839
if (otherKey == null)
3940
return false;
4041
return
41-
equalityComparer2.Invoke(value2, otherKey.value2) &&
42-
equalityComparer1.Invoke(value1, otherKey.value1);
42+
EqualityComparer2.Invoke(value2, otherKey.value2) &&
43+
EqualityComparer1.Invoke(value1, otherKey.value1);
4344
}
4445

4546
protected override int CalculateHashCode()
@@ -48,7 +49,7 @@ protected override int CalculateHashCode()
4849
}
4950

5051
[UsedImplicitly]
51-
public static Key Create(string nodeId, TypeInfo type, Tuple tuple, TypeReferenceAccuracy accuracy, int[] keyIndexes)
52+
public static Key Create(string nodeId, TypeInfo type, Tuple tuple, TypeReferenceAccuracy accuracy, IReadOnlyList<int> keyIndexes)
5253
{
5354
return new Key<T1, T2>(nodeId, type, accuracy,
5455
tuple.GetValueOrDefault<T1>(keyIndexes[0]),
@@ -63,7 +64,7 @@ public static Key Create(string nodeId, TypeInfo type, Tuple tuple, TypeReferenc
6364
tuple.GetValueOrDefault<T2>(1));
6465
}
6566

66-
67+
6768
// Constructors
6869

6970
private Key(string nodeId, TypeInfo type, TypeReferenceAccuracy accuracy, T1 value1, T2 value2)

Orm/Xtensive.Orm/Orm/Internals/Key{T}.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2003-2021 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: Alexander Nikolaev
55
// Created: 2009.07.13
66

77
using System;
8+
using System.Collections.Generic;
89
using JetBrains.Annotations;
910
using Xtensive.Core;
1011
using Xtensive.Orm.Model;
@@ -16,7 +17,7 @@ namespace Xtensive.Orm.Internals
1617
[Serializable]
1718
internal sealed class Key<T> : Key
1819
{
19-
private static readonly Predicate<T, T> equalityComparer1 =
20+
private static readonly Predicate<T, T> EqualityComparer1 =
2021
ComparerProvider.Default.GetComparer<T>().Equals;
2122

2223
private readonly T value1;
@@ -34,7 +35,7 @@ protected override bool ValueEquals(Key other)
3435
if (otherKey == null)
3536
return false;
3637
return
37-
equalityComparer1.Invoke(value1, otherKey.value1);
38+
EqualityComparer1.Invoke(value1, otherKey.value1);
3839
}
3940

4041
protected override int CalculateHashCode()
@@ -43,7 +44,7 @@ protected override int CalculateHashCode()
4344
}
4445

4546
[UsedImplicitly]
46-
public static Key Create(string nodeId, TypeInfo type, Tuple tuple, TypeReferenceAccuracy accuracy, int[] keyIndexes)
47+
public static Key Create(string nodeId, TypeInfo type, Tuple tuple, TypeReferenceAccuracy accuracy, IReadOnlyList<int> keyIndexes)
4748
{
4849
return new Key<T>(nodeId, type, accuracy, tuple.GetValueOrDefault<T>(keyIndexes[0]));
4950
}
@@ -54,7 +55,7 @@ public static Key Create(string nodeId, TypeInfo type, Tuple tuple, TypeReferenc
5455
return new Key<T>(nodeId, type, accuracy, tuple.GetValueOrDefault<T>(0));
5556
}
5657

57-
58+
5859
// Constructors
5960

6061
private Key(string nodeId, TypeInfo type, TypeReferenceAccuracy accuracy, T value)

Orm/Xtensive.Orm/Orm/Internals/TypeMapping.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2003-2021 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: Dmitri Maximov
55
// Created: 2008.08.08
66

7+
using System.Collections.Generic;
78
using Xtensive.Tuples.Transform;
89
using Xtensive.Orm.Model;
910

@@ -13,13 +14,13 @@ internal readonly struct TypeMapping
1314
{
1415
public readonly TypeInfo Type;
1516
public readonly MapTransform KeyTransform;
16-
public readonly int[] KeyIndexes;
17+
public readonly IReadOnlyList<int> KeyIndexes;
1718
public readonly MapTransform Transform;
1819

1920

2021
// Constructors
2122

22-
public TypeMapping(TypeInfo type, MapTransform keyTransform, MapTransform transform, int[] keyIndexes)
23+
public TypeMapping(TypeInfo type, MapTransform keyTransform, MapTransform transform, IReadOnlyList<int> keyIndexes)
2324
{
2425
Type = type;
2526
KeyTransform = keyTransform;

Orm/Xtensive.Orm/Orm/Linq/Materialization/ItemMaterializationContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public Entity Materialize(int entityIndex, int typeIdIndex, TypeInfo type, Pair<
4747
var keyIndexes = materializationInfo.KeyIndexes;
4848
if (!KeyFactory.IsValidKeyTuple(tuple, keyIndexes))
4949
return null;
50-
if (keyIndexes.Length <= WellKnown.MaxGenericKeyLength)
50+
if (keyIndexes.Count <= WellKnown.MaxGenericKeyLength)
5151
key = KeyFactory.Materialize(Session.Domain, Session.StorageNodeId, materializationInfo.Type, tuple, accuracy, canCache, keyIndexes);
5252
else {
5353
var keyTuple = materializationInfo.KeyTransform.Apply(TupleTransformType.TransformedTuple, tuple);

Orm/Xtensive.Orm/Orm/Linq/Materialization/MaterializationContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2009-2020 Xtensive LLC.
1+
// Copyright (C) 2009-2021 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: Alexis Kochetov
@@ -90,8 +90,8 @@ public TypeMapping GetTypeMapping(int entityIndex, TypeInfo approximateType, int
9090
typeColumnMap = newColumns;
9191
}
9292

93-
int[] allIndexes = MaterializationHelper.CreateSingleSourceMap(descriptor.Count, typeColumnMap);
94-
int[] keyIndexes = allIndexes.Take(keyInfo.TupleDescriptor.Count).ToArray();
93+
ArraySegment<int> allIndexes = MaterializationHelper.CreateSingleSourceMap(descriptor.Count, typeColumnMap);
94+
ArraySegment<int> keyIndexes = allIndexes.Slice(0, keyInfo.TupleDescriptor.Count);
9595

9696
var transform = new MapTransform(true, descriptor, allIndexes);
9797
var keyTransform = new MapTransform(true, keyInfo.TupleDescriptor, keyIndexes);

Orm/Xtensive.Orm/Orm/Linq/Materialization/MaterializationHelper.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ internal static class MaterializationHelper
4646
public static int[] CreateSingleSourceMap(int targetLength, IReadOnlyList<Pair<int>> remappedColumns)
4747
{
4848
var map = new int[targetLength];
49-
for (var i = 0; i < map.Length; i++) {
50-
map[i] = MapTransform.NoMapping;
51-
}
49+
Array.Fill(map, MapTransform.NoMapping);
5250

5351
for (var i = 0; i < remappedColumns.Count; i++) {
5452
var remappedColumn = remappedColumns[i];

0 commit comments

Comments
 (0)