Skip to content

Commit ee8b13f

Browse files
authored
Merge pull request #31 from servicetitan/readonly-segment-struct
Make Segment struct readonly. Pass it by reference. Access it as field.
2 parents 98ce02f + 2e681f9 commit ee8b13f

21 files changed

+54
-78
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public static T[] ToArray<T>(this IEnumerable<T> sequence, int length)
312312
/// Gets the items from the segment.
313313
/// </summary>
314314
/// <param name="segment">The segment.</param>
315-
public static IEnumerable<int> GetItems(this Segment<int> segment)
315+
public static IEnumerable<int> GetItems(this in Segment<int> segment)
316316
{
317317
return Enumerable.Range(segment.Offset, segment.Length);
318318
}

Orm/Xtensive.Orm/Core/Segment.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Xtensive.Core
1919
/// <typeparam name="T">The type of segment boundaries.</typeparam>
2020
[Serializable]
2121
[DebuggerDisplay("Offset = {Offset}, Length = {Length}")]
22-
public struct Segment<T>
22+
public readonly struct Segment<T>
2323
{
2424
private static ArithmeticStruct<T> arithmetic = ArithmeticStruct<T>.Default;
2525

@@ -87,7 +87,7 @@ public override int GetHashCode()
8787
/// <param name="segment">The segment.</param>
8888
/// <param name="offsetShift">The offset shift.</param>
8989
/// <returns>The result of the operator.</returns>
90-
public static Segment<T> operator +(Segment<T> segment, T offsetShift)
90+
public static Segment<T> operator +(in Segment<T> segment, T offsetShift)
9191
{
9292
var newOffset = arithmetic.Add(segment.Offset, offsetShift);
9393
return new Segment<T>(newOffset, segment.Length);
@@ -99,7 +99,7 @@ public override int GetHashCode()
9999
/// <param name="segment">The segment.</param>
100100
/// <param name="offsetShift">The offset shift.</param>
101101
/// <returns>The result of the operator.</returns>
102-
public static Segment<T> operator -(Segment<T> segment, T offsetShift)
102+
public static Segment<T> operator -(in Segment<T> segment, T offsetShift)
103103
{
104104
var newOffset = arithmetic.Subtract(segment.Offset, offsetShift);
105105
return new Segment<T>(newOffset, segment.Length);

Orm/Xtensive.Orm/Orm/Internals/TupleExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static bool ContainsNonEmptyValues(this Tuple target)
3333
return false;
3434
}
3535

36-
public static bool ContainsEmptyValues(this Tuple target, Segment<int> segment)
36+
public static bool ContainsEmptyValues(this Tuple target, in Segment<int> segment)
3737
{
3838
for (int i = segment.Offset; i < segment.EndOffset; i++) {
3939
var state = target.GetFieldState(i);
@@ -43,7 +43,7 @@ public static bool ContainsEmptyValues(this Tuple target, Segment<int> segment)
4343
return false;
4444
}
4545

46-
public static bool ContainsNonEmptyValues(this Tuple target, Segment<int> segment)
46+
public static bool ContainsNonEmptyValues(this Tuple target, in Segment<int> segment)
4747
{
4848
for (int i = segment.Offset; i < segment.EndOffset; i++) {
4949
var state = target.GetFieldState(i);
@@ -53,7 +53,7 @@ public static bool ContainsNonEmptyValues(this Tuple target, Segment<int> segmen
5353
return false;
5454
}
5555

56-
public static bool AreAllColumnsAvalilable(this Tuple target, Segment<int> segment)
56+
public static bool AreAllColumnsAvalilable(this Tuple target, in Segment<int> segment)
5757
{
5858
for (int i = segment.Offset; i < segment.EndOffset; i++) {
5959
var state = target.GetFieldState(i);
@@ -63,7 +63,7 @@ public static bool AreAllColumnsAvalilable(this Tuple target, Segment<int> segme
6363
return true;
6464
}
6565

66-
public static bool IsAtLeastOneColumAvailable(this Tuple target, Segment<int> segment)
66+
public static bool IsAtLeastOneColumAvailable(this Tuple target, in Segment<int> segment)
6767
{
6868
for (int i = segment.Offset; i < segment.EndOffset; i++) {
6969
var state = target.GetFieldState(i);

Orm/Xtensive.Orm/Orm/Linq/Expressions/ColumnExpression.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,29 @@
77
using System;
88
using System.Collections.Generic;
99
using System.Linq.Expressions;
10-
using Xtensive.Collections;
1110
using Xtensive.Core;
1211

1312
namespace Xtensive.Orm.Linq.Expressions
1413
{
1514
internal class ColumnExpression : ParameterizedExpression,
1615
IMappedExpression
1716
{
18-
public Segment<int> Mapping { get; private set; }
17+
internal readonly Segment<int> Mapping;
1918

2019
public Expression Remap(int offset, Dictionary<Expression, Expression> processedExpressions)
2120
{
2221
if (!CanRemap)
2322
return this;
24-
var mapping = new Segment<int>(Mapping.Offset + offset, 1);
25-
return new ColumnExpression(Type, mapping, OuterParameter, DefaultIfEmpty);
23+
var newMapping = new Segment<int>(Mapping.Offset + offset, 1);
24+
return new ColumnExpression(Type, newMapping, OuterParameter, DefaultIfEmpty);
2625
}
2726

2827
public Expression Remap(int[] map, Dictionary<Expression, Expression> processedExpressions)
2928
{
3029
if (!CanRemap)
3130
return this;
32-
var mapping = new Segment<int>(map.IndexOf(Mapping.Offset), 1);
33-
return new ColumnExpression(Type, mapping, OuterParameter, DefaultIfEmpty);
31+
var newMapping = new Segment<int>(map.IndexOf(Mapping.Offset), 1);
32+
return new ColumnExpression(Type, newMapping, OuterParameter, DefaultIfEmpty);
3433
}
3534

3635
public Expression BindParameter(ParameterExpression parameter)
@@ -63,13 +62,13 @@ public override string ToString()
6362
// Constructors
6463

6564
protected ColumnExpression(
66-
Type type,
67-
Segment<int> mapping,
68-
ParameterExpression parameterExpression,
65+
Type type,
66+
in Segment<int> mapping,
67+
ParameterExpression parameterExpression,
6968
bool defaultIfEmpty)
7069
: base(ExtendedExpressionType.Column, type, parameterExpression, defaultIfEmpty)
7170
{
72-
Mapping = mapping;
71+
this.Mapping = mapping;
7372
}
7473
}
7574
}

Orm/Xtensive.Orm/Orm/Linq/Expressions/ConstructorExpression.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ internal class ConstructorExpression : ParameterizedExpression,
2929

3030
public IEnumerable<Expression> ConstructorArguments { get; private set; }
3131

32-
public Segment<int> Mapping
33-
{
34-
get { throw new NotSupportedException(); }
35-
}
36-
3732
public Expression BindParameter(ParameterExpression parameter, Dictionary<Expression, Expression> processedExpressions)
3833
{
3934
Func<Expression, Expression> genericBinder =

Orm/Xtensive.Orm/Orm/Linq/Expressions/EntityExpression.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ internal class EntityExpression : ParameterizedExpression,
2121

2222
public TypeInfo PersistentType { get; private set; }
2323

24-
public Segment<int> Mapping
25-
{
26-
get { throw new NotSupportedException(); }
27-
}
28-
2924
public KeyExpression Key { get; private set; }
3025

3126
public List<PersistentFieldExpression> Fields

Orm/Xtensive.Orm/Orm/Linq/Expressions/EntityFieldExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private EntityFieldExpression(
176176
TypeInfo persistentType,
177177
FieldInfo field,
178178
List<PersistentFieldExpression> fields,
179-
Segment<int> mapping,
179+
in Segment<int> mapping,
180180
KeyExpression key,
181181
EntityExpression entity,
182182
ParameterExpression parameterExpression,

Orm/Xtensive.Orm/Orm/Linq/Expressions/FieldExpression.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public override Expression Remap(int offset, Dictionary<Expression, Expression>
4343
return result;
4444
}
4545

46-
var mapping = new Segment<int>(Mapping.Offset + offset, Mapping.Length);
47-
result = new FieldExpression(ExtendedExpressionType.Field, Field, mapping, OuterParameter, DefaultIfEmpty);
46+
var newMapping = new Segment<int>(Mapping.Offset + offset, Mapping.Length);
47+
result = new FieldExpression(ExtendedExpressionType.Field, Field, newMapping, OuterParameter, DefaultIfEmpty);
4848
if (owner == null) {
4949
return result;
5050
}
@@ -72,8 +72,8 @@ public override Expression Remap(int[] map, Dictionary<Expression, Expression> p
7272
Owner.Remap(map, processedExpressions);
7373
return null;
7474
}
75-
var mapping = new Segment<int>(offset, Mapping.Length);
76-
result = new FieldExpression(ExtendedExpressionType.Field, Field, mapping, OuterParameter, DefaultIfEmpty);
75+
var newMapping = new Segment<int>(offset, Mapping.Length);
76+
result = new FieldExpression(ExtendedExpressionType.Field, Field, newMapping, OuterParameter, DefaultIfEmpty);
7777
if (owner == null)
7878
return result;
7979

@@ -130,7 +130,7 @@ public static FieldExpression CreateField(FieldInfo field, int offset)
130130
protected FieldExpression(
131131
ExtendedExpressionType expressionType,
132132
FieldInfo field,
133-
Segment<int> mapping,
133+
in Segment<int> mapping,
134134
ParameterExpression parameterExpression,
135135
bool defaultIfEmpty)
136136
: base(expressionType, field.Name, field.ValueType, mapping, field.UnderlyingProperty, parameterExpression, defaultIfEmpty)

Orm/Xtensive.Orm/Orm/Linq/Expressions/FullTextExpression.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ internal class FullTextExpression : ParameterizedExpression,
2222

2323
public EntityExpression EntityExpression { get; private set; }
2424

25-
/// <exception cref="NotSupportedException"><c>NotSupportedException</c>.</exception>
26-
public Segment<int> Mapping
27-
{
28-
get { throw new NotSupportedException(); }
29-
}
30-
3125
public Expression BindParameter(ParameterExpression parameter, Dictionary<Expression, Expression> processedExpressions)
3226
{
3327
Expression result;

Orm/Xtensive.Orm/Orm/Linq/Expressions/Interfaces/IMappedExpression.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace Xtensive.Orm.Linq.Expressions
1212
{
1313
internal interface IMappedExpression
1414
{
15-
Segment<int> Mapping { get; }
1615
Expression BindParameter(ParameterExpression parameter, Dictionary<Expression, Expression> processedExpressions);
1716
Expression RemoveOuterParameter(Dictionary<Expression, Expression> processedExpressions);
1817
Expression Remap(int offset, Dictionary<Expression, Expression> processedExpressions);

0 commit comments

Comments
 (0)