Skip to content

Commit 38f2da5

Browse files
committed
Avoid unnecessary array copy before calling .Remap(); new() -> ValueTuple; Pair<> (#48)
* Avoid unnecessary array copy before calling .Remap(); new() -> ValueTuple; Pair<> * rename x -> other in .Equals() method * rename to 'other'
1 parent 9498343 commit 38f2da5

73 files changed

Lines changed: 304 additions & 558 deletions

File tree

Some content is hidden

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

Orm/Xtensive.Orm.Tests/Rse/HeaderParseTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void MainTest()
5454
ResetState(state);
5555

5656
// Select Id, TypeId, Title
57-
CompilableProvider rsTitle = rsMain.Select(0, 1, 2);
57+
CompilableProvider rsTitle = rsMain.Select(new[] { 0, 1, 2 });
5858
UpdateCache(session, rsTitle.GetRecordSetReader(session, parameterContext));
5959
state = Session.Current.EntityStateCache[key, true];
6060
Assert.IsNotNull(state);
@@ -63,7 +63,7 @@ public void MainTest()
6363
ResetState(state);
6464

6565
// Select Id, TypeId, Text
66-
CompilableProvider rsText = rsMain.Select(0, 1, 3);
66+
CompilableProvider rsText = rsMain.Select(new[] { 0, 1, 3 });
6767
UpdateCache(session, rsText.GetRecordSetReader(session, parameterContext));
6868
state = Session.Current.EntityStateCache[key, true];
6969
Assert.IsNotNull(state);

Orm/Xtensive.Orm.Tests/Storage/Providers/Sql/CharSupportTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void SelectCharTest()
6666
var rs = GetRseQuery<MyEntity>();
6767
var parameterContext = new ParameterContext();
6868
var result = rs
69-
.Select(rs.Header.IndexOf(charColumn))
69+
.Select(new[] { rs.Header.IndexOf(charColumn) })
7070
.GetRecordSetReader(Session.Current, parameterContext)
7171
.ToEnumerable()
7272
.Select(i => i.GetValueOrDefault<char>(0))
@@ -88,7 +88,7 @@ public void CharParameterTest()
8888
var rs = GetRseQuery<MyEntity>();
8989
var parameterContext = new ParameterContext();
9090
var result = rs
91-
.Select(rs.Header.IndexOf(charColumn))
91+
.Select(new[] { rs.Header.IndexOf(charColumn) })
9292
.Filter(t => t.GetValueOrDefault<char>(0) == y)
9393
.GetRecordSetReader(Session.Current, parameterContext)
9494
.ToEnumerable()
@@ -108,7 +108,7 @@ public void CharConstantTest()
108108
var rs = GetRseQuery<MyEntity>();
109109
var parameterContext = new ParameterContext();
110110
var result = rs
111-
.Select(rs.Header.IndexOf(charColumn))
111+
.Select(new[] { rs.Header.IndexOf(charColumn) })
112112
.Filter(t => t.GetValueOrDefault<char>(0)=='Y')
113113
.GetRecordSetReader(Session.Current, parameterContext)
114114
.ToEnumerable()

Orm/Xtensive.Orm/Arithmetic/ArithmeticRules.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,8 @@ public bool Equals(ArithmeticRules other)
4141
}
4242

4343
/// <inheritdoc/>
44-
public override bool Equals(object obj)
45-
{
46-
if (obj==null)
47-
return false;
48-
if (obj is ArithmeticRules)
49-
return Equals((ArithmeticRules)obj);
50-
return false;
51-
}
44+
public override bool Equals(object obj) =>
45+
obj is ArithmeticRules other && Equals(other);
5246

5347
/// <inheritdoc/>
5448
public override int GetHashCode()

Orm/Xtensive.Orm/Caching/WeakestCache.cs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -131,33 +131,24 @@ internal sealed class WeakEntryEqualityComparer : IEqualityComparer<object>
131131

132132
public new bool Equals(object x, object y)
133133
{
134-
TKey key;
135-
var we = x as WeakEntry;
136-
if (we!=null) {
134+
if (x is WeakEntry we) {
137135
// x is WeakEntry
138-
key = y as TKey;
139-
if (key!=null)
136+
if (y is TKey key)
140137
// x is WeakEntry, y is TKey
141138
return keyComparer.Equals(we.Key, key);
142139
else {
143-
var we2 = y as WeakEntry;
144-
if (we2==null)
145-
return false;
146-
// x is WeakEntry, y is WeakEntry
147-
return keyComparer.Equals(we.Key, we2.Key);
140+
return y is WeakEntry we2 && keyComparer.Equals(we.Key, we2.Key); // x is WeakEntry, y is WeakEntry
148141
}
149142
}
150-
key = x as TKey;
151-
if (key==null)
152-
return false;
153-
// x is TKey
154-
we = y as WeakEntry;
155-
if (we!=null)
156-
// x is TKey, y is WeakEntry
157-
return keyComparer.Equals(key, we.Key);
158-
else
159-
// x is TKey, y must be TKey
160-
return keyComparer.Equals(key, y as TKey);
143+
if (x is TKey keyX) {
144+
if (y is WeakEntry weY)
145+
// x is TKey, y is WeakEntry
146+
return keyComparer.Equals(keyX, weY.Key);
147+
else
148+
// x is TKey, y must be TKey
149+
return keyComparer.Equals(keyX, y as TKey);
150+
}
151+
return false;
161152
}
162153

163154
public int GetHashCode(object obj)

Orm/Xtensive.Orm/Comparison/ComparisonRule.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,8 @@ public bool Equals(ComparisonRule other)
8383
}
8484

8585
/// <inheritdoc/>
86-
public override bool Equals(object obj)
87-
{
88-
if (obj is ComparisonRule)
89-
return Equals((ComparisonRule)obj);
90-
return false;
91-
}
86+
public override bool Equals(object obj) =>
87+
obj is ComparisonRule other && Equals(other);
9288

9389
/// <inheritdoc/>
9490
public override int GetHashCode()

Orm/Xtensive.Orm/Comparison/ComparisonRules.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,8 @@ public bool Equals(ComparisonRules other)
193193
#region Equals, GetHashCode
194194

195195
/// <inheritdoc/>
196-
public override bool Equals(object obj)
197-
{
198-
if (obj is ComparisonRules)
199-
return Equals((ComparisonRules)obj);
200-
return false;
201-
}
196+
public override bool Equals(object obj) =>
197+
obj is ComparisonRules other && Equals(other);
202198

203199
/// <inheritdoc/>
204200
public override int GetHashCode()

Orm/Xtensive.Orm/Conversion/Biconverter.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,8 @@ public bool Equals(Biconverter<TFrom, TTo> obj)
5353
}
5454

5555
/// <inheritdoc/>
56-
public override bool Equals(object obj)
57-
{
58-
if (obj.GetType()!=typeof (Biconverter<TFrom, TTo>))
59-
return false;
60-
return Equals((Biconverter<TFrom, TTo>) obj);
61-
}
56+
public override bool Equals(object obj) =>
57+
obj is Biconverter<TFrom, TTo> other && Equals(other);
6258

6359
/// <inheritdoc/>
6460
public override int GetHashCode()

Orm/Xtensive.Orm/Core/HasVersion{TValue,TVersion}.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,8 @@ public int CompareTo(HasVersion<TValue, TVersion> other)
5757
#region Equals, GetHashCode, ==, !=
5858

5959
/// <inheritdoc/>
60-
public override bool Equals(object obj)
61-
{
62-
if (obj.GetType()!=typeof (HasVersion<TValue, TVersion>))
63-
return false;
64-
return Equals((HasVersion<TValue, TVersion>) obj);
65-
}
60+
public override bool Equals(object obj) =>
61+
obj is HasVersion<TValue, TVersion> other && Equals(other);
6662

6763
/// <inheritdoc/>
6864
public override int GetHashCode()

Orm/Xtensive.Orm/Core/Pair{TFirst,TSecond}.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ namespace Xtensive.Core
2121
IComparable<Pair<TFirst, TSecond>>,
2222
IEquatable<Pair<TFirst, TSecond>>
2323
{
24+
private static readonly AdvancedComparerStruct<TFirst> firstComparer = AdvancedComparerStruct<TFirst>.System;
25+
private static readonly AdvancedComparerStruct<TSecond> secondComparer = AdvancedComparerStruct<TSecond>.System;
26+
2427
/// <summary>
2528
/// A first value.
2629
/// </summary>
@@ -33,33 +36,25 @@ namespace Xtensive.Core
3336
#region IComparable<...>, IEquatable<...> methods
3437

3538
/// <inheritdoc/>
36-
public bool Equals(Pair<TFirst, TSecond> other)
37-
{
38-
if (!AdvancedComparerStruct<TFirst>.System.Equals(First, other.First))
39-
return false;
40-
return AdvancedComparerStruct<TSecond>.System.Equals(Second, other.Second);
41-
}
39+
public bool Equals(Pair<TFirst, TSecond> other) =>
40+
firstComparer.Equals(First, other.First) && secondComparer.Equals(Second, other.Second);
4241

4342
/// <inheritdoc/>
4443
public int CompareTo(Pair<TFirst, TSecond> other)
4544
{
46-
int result = AdvancedComparerStruct<TFirst>.System.Compare(First, other.First);
47-
if (result != 0)
48-
return result;
49-
return AdvancedComparerStruct<TSecond>.System.Compare(Second, other.Second);
45+
int result = firstComparer.Compare(First, other.First);
46+
return result != 0
47+
? result
48+
: secondComparer.Compare(Second, other.Second);
5049
}
5150

5251
#endregion
5352

5453
#region Equals, GetHashCode, ==, !=
5554

5655
/// <inheritdoc/>
57-
public override bool Equals(object obj)
58-
{
59-
if (obj.GetType() != typeof(Pair<TFirst, TSecond>))
60-
return false;
61-
return Equals((Pair<TFirst, TSecond>) obj);
62-
}
56+
public override bool Equals(object obj) =>
57+
obj is Pair<TFirst, TSecond> other && Equals(other);
6358

6459
/// <inheritdoc/>
6560
public override int GetHashCode()

Orm/Xtensive.Orm/Core/Pair{T}.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ namespace Xtensive.Core
2222
IEquatable<Pair<T>>,
2323
IComparable<Pair<T>>
2424
{
25+
private static readonly AdvancedComparerStruct<T> comparer = AdvancedComparerStruct<T>.System;
26+
2527
/// <summary>
2628
/// The first value.
2729
/// </summary>
@@ -35,33 +37,25 @@ namespace Xtensive.Core
3537
#region IComparable<...>, IEquatable<...> methods
3638

3739
/// <inheritdoc/>
38-
public bool Equals(Pair<T> other)
39-
{
40-
if (!AdvancedComparerStruct<T>.System.Equals(First, other.First))
41-
return false;
42-
return AdvancedComparerStruct<T>.System.Equals(Second, other.Second);
43-
}
40+
public bool Equals(Pair<T> other) =>
41+
comparer.Equals(First, other.First) && comparer.Equals(Second, other.Second);
4442

4543
/// <inheritdoc/>
4644
public int CompareTo(Pair<T> other)
4745
{
48-
int result = AdvancedComparerStruct<T>.System.Compare(First, other.First);
49-
if (result!=0)
50-
return result;
51-
return AdvancedComparerStruct<T>.System.Compare(Second, other.Second);
46+
int result = comparer.Compare(First, other.First);
47+
return result != 0
48+
? result
49+
: comparer.Compare(Second, other.Second);
5250
}
5351

5452
#endregion
5553

5654
#region Equals, GetHashCode, ==, !=
5755

5856
/// <inheritdoc/>
59-
public override bool Equals(object obj)
60-
{
61-
if (obj.GetType()!=typeof (Pair<T>))
62-
return false;
63-
return Equals((Pair<T>) obj);
64-
}
57+
public override bool Equals(object obj) =>
58+
obj is Pair<T> other && Equals(other);
6559

6660
/// <inheritdoc/>
6761
public override int GetHashCode()
@@ -117,4 +111,4 @@ public Pair(T first, T second)
117111
Second = second;
118112
}
119113
}
120-
}
114+
}

0 commit comments

Comments
 (0)