Skip to content

Commit d066477

Browse files
committed
TupleDescriptor: Optimize members
1 parent b951fa1 commit d066477

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

Orm/Xtensive.Orm/Tuples/TupleDescriptor.cs

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Xtensive.Tuples
2525
[Serializable]
2626
public readonly struct TupleDescriptor : IEquatable<TupleDescriptor>, IReadOnlyList<Type>, ISerializable
2727
{
28-
private static readonly TupleDescriptor EmptyDescriptor = new TupleDescriptor(Array.Empty<Type>());
28+
public static readonly TupleDescriptor Empty = new TupleDescriptor(Array.Empty<Type>());
2929

3030
internal readonly int ValuesLength;
3131
internal readonly int ObjectsLength;
@@ -36,18 +36,6 @@ namespace Xtensive.Tuples
3636
[field: NonSerialized]
3737
private Type[] FieldTypes { get; }
3838

39-
private int FieldCount => FieldTypes.Length;
40-
41-
/// <summary>
42-
/// Gets the empty tuple descriptor.
43-
/// </summary>
44-
/// <value>The empty tuple descriptor.</value>
45-
public static TupleDescriptor Empty
46-
{
47-
[DebuggerStepThrough]
48-
get => EmptyDescriptor;
49-
}
50-
5139
#region IList members
5240

5341
/// <inheritdoc/>
@@ -61,13 +49,13 @@ public Type this[int fieldIndex]
6149
public int Count
6250
{
6351
[DebuggerStepThrough]
64-
get => FieldCount;
52+
get => FieldTypes.Length;
6553
}
6654

6755
/// <inheritdoc/>
6856
public IEnumerator<Type> GetEnumerator()
6957
{
70-
for (var index = 0; index < FieldCount; index++) {
58+
for (var index = 0; index < Count; index++) {
7159
yield return FieldTypes[index];
7260
}
7361
}
@@ -89,11 +77,11 @@ public bool Equals(TupleDescriptor other)
8977
if (FieldTypes == null) {
9078
return other.FieldTypes == null;
9179
}
92-
if (other.FieldTypes == null || FieldCount != other.FieldCount) {
80+
if (other.FieldTypes == null || Count != other.Count) {
9381
return false;
9482
}
9583

96-
for (int i = 0; i < FieldCount; i++) {
84+
for (int i = 0; i < Count; i++) {
9785
if (FieldTypes[i] != other.FieldTypes[i]) {
9886
return false;
9987
}
@@ -108,35 +96,42 @@ public override bool Equals(object obj) =>
10896
/// <inheritdoc/>
10997
public override int GetHashCode()
11098
{
111-
int result = FieldCount;
112-
for (int i = 0; i < FieldCount; i++)
99+
int result = Count;
100+
for (int i = 0; i < Count; i++)
113101
result = unchecked (FieldTypes[i].GetHashCode() + 29 * result);
114102
return result;
115103
}
116104

117-
public static bool operator ==(in TupleDescriptor left, in TupleDescriptor right) => left.Equals(right);
118-
public static bool operator !=(in TupleDescriptor left, in TupleDescriptor right) => !(left == right);
105+
public static bool operator ==(in TupleDescriptor left, in TupleDescriptor right)
106+
{
107+
return left.Equals(right);
108+
}
109+
110+
public static bool operator !=(in TupleDescriptor left, in TupleDescriptor right)
111+
{
112+
return !(left == right);
113+
}
119114

120115
#endregion
121116

122117
public void GetObjectData(SerializationInfo info, StreamingContext context)
123118
{
124-
info.AddValue("ValuesLength", ValuesLength);
125-
info.AddValue("ObjectsLength", ObjectsLength);
119+
info.AddValue(nameof(ValuesLength), ValuesLength);
120+
info.AddValue(nameof(ObjectsLength), ObjectsLength);
126121

127122
var typeNames = new string[FieldTypes.Length];
128123
for (var i = 0; i < typeNames.Length; i++)
129124
typeNames[i] = FieldTypes[i].ToSerializableForm();
130125

131-
info.AddValue("FieldTypes", typeNames);
132-
info.AddValue("FieldDescriptors", FieldDescriptors);
126+
info.AddValue(nameof(FieldTypes), typeNames);
127+
info.AddValue(nameof(FieldDescriptors), FieldDescriptors);
133128
}
134129

135130
/// <inheritdoc/>
136131
public override string ToString()
137132
{
138133
var sb = new ValueStringBuilder(stackalloc char[4096]);
139-
for (int i = 0; i < FieldCount; i++) {
134+
for (int i = 0; i < Count; i++) {
140135
if (i > 0)
141136
sb.Append(", ");
142137
sb.Append(FieldTypes[i].GetShortName());
@@ -177,7 +172,7 @@ public static TupleDescriptor Create(Type[] fieldTypes)
177172
{
178173
ArgumentValidator.EnsureArgumentNotNull(fieldTypes, nameof(fieldTypes));
179174
if (fieldTypes.Length == 0) {
180-
return EmptyDescriptor;
175+
return Empty;
181176
}
182177
return new TupleDescriptor(fieldTypes);
183178
}
@@ -285,12 +280,12 @@ private TupleDescriptor(Type[] fieldTypes)
285280

286281
public TupleDescriptor(SerializationInfo info, StreamingContext context)
287282
{
288-
ValuesLength = info.GetInt32("ValuesLength");
289-
ObjectsLength = info.GetInt32("ObjectsLength");
283+
ValuesLength = info.GetInt32(nameof(ValuesLength));
284+
ObjectsLength = info.GetInt32(nameof(ObjectsLength));
290285

291-
var typeNames = (string[]) info.GetValue("FieldTypes", typeof(string[]));
286+
var typeNames = (string[]) info.GetValue(nameof(FieldTypes), typeof(string[]));
292287
FieldDescriptors = (PackedFieldDescriptor[])info.GetValue(
293-
"FieldDescriptors", typeof(PackedFieldDescriptor[]));
288+
nameof(FieldDescriptors), typeof(PackedFieldDescriptor[]));
294289

295290
FieldTypes = new Type[typeNames.Length];
296291
for (var i = 0; i < typeNames.Length; i++) {

0 commit comments

Comments
 (0)