Skip to content

Commit 82c98fb

Browse files
committed
Shape: added 'readonly' to methods to prevent local copying on calls
1 parent 9959f78 commit 82c98fb

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

src/NumSharp.Core/View/Shape.Reshaping.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public partial struct Shape
2020
/// <exception cref="ArgumentException">If <paramref name="newShape"/>'s size == 0</exception>
2121
/// <param name="unsafe">When true, then guards are skipped.</param>
2222
[MethodImpl((MethodImplOptions)768)]
23-
public Shape Reshape(Shape newShape, bool @unsafe = true)
23+
public readonly Shape Reshape(Shape newShape, bool @unsafe = true)
2424
{
2525
if (IsBroadcasted)
2626
{
@@ -53,7 +53,7 @@ public Shape Reshape(Shape newShape, bool @unsafe = true)
5353
/// <exception cref="IncorrectShapeException">If shape's size mismatches current shape size.</exception>
5454
/// <exception cref="ArgumentException">If <paramref name="newShape"/>'s size == 0</exception>
5555
[MethodImpl((MethodImplOptions)768)]
56-
private void _reshapeBroadcast(ref Shape newShape, bool @unsafe = true)
56+
private readonly void _reshapeBroadcast(ref Shape newShape, bool @unsafe = true)
5757
{
5858
//handle -1 in reshape
5959
_inferMissingDimension(ref newShape);
@@ -75,7 +75,7 @@ private void _reshapeBroadcast(ref Shape newShape, bool @unsafe = true)
7575
}
7676

7777
[SuppressMessage("ReSharper", "ParameterHidesMember")]
78-
private void _inferMissingDimension(ref Shape shape)
78+
private readonly void _inferMissingDimension(ref Shape shape)
7979
{
8080
var indexOfNegOne = -1;
8181
int product = 1;
@@ -148,7 +148,7 @@ private void _inferMissingDimension(ref Shape shape)
148148
/// <param name="axis"></param>
149149
/// <returns></returns>
150150
[SuppressMessage("ReSharper", "LocalVariableHidesMember")]
151-
public Shape ExpandDimension(int axis)
151+
public readonly Shape ExpandDimension(int axis)
152152
{
153153
Shape ret;
154154
if (IsScalar)

src/NumSharp.Core/View/Shape.Unmanaged.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial struct Shape
1616
/// <returns>The index in the memory block that refers to a specific value.</returns>
1717
/// <remarks>Handles sliced indices and broadcasting</remarks>
1818
[MethodImpl((MethodImplOptions)768)]
19-
public unsafe int GetOffset(int* indices, int ndims)
19+
public readonly unsafe int GetOffset(int* indices, int ndims)
2020
{
2121
int offset;
2222
if (!IsSliced)
@@ -116,7 +116,7 @@ public unsafe int GetOffset(int* indices, int ndims)
116116
/// <returns></returns>
117117
/// <remarks>Used for slicing, returned shape is the new shape of the slice and offset is the offset from current address.</remarks>
118118
[MethodImpl((MethodImplOptions)768)]
119-
public unsafe (Shape Shape, int Offset) GetSubshape(int* dims, int ndims)
119+
public readonly unsafe (Shape Shape, int Offset) GetSubshape(int* dims, int ndims)
120120
{
121121
if (ndims == 0)
122122
return (this, 0);
@@ -215,7 +215,7 @@ public static unsafe void InferNegativeCoordinates(int[] dimensions, int* coords
215215
/// <returns>The index in the memory block that refers to a specific value.</returns>
216216
/// <remarks>Handles sliced indices and broadcasting</remarks>
217217
[MethodImpl((MethodImplOptions)768)]
218-
private unsafe int GetOffset_broadcasted(int* indices, int ndims)
218+
private readonly unsafe int GetOffset_broadcasted(int* indices, int ndims)
219219
{
220220
int offset;
221221
var vi = ViewInfo;
@@ -313,7 +313,7 @@ private unsafe int GetOffset_broadcasted(int* indices, int ndims)
313313
/// Note: to be used only inside of GetOffset()
314314
/// </summary>
315315
[MethodImpl((MethodImplOptions)768)]
316-
private unsafe int GetOffset_IgnoreViewInfo(int* indices, int ndims)
316+
private readonly unsafe int GetOffset_IgnoreViewInfo(int* indices, int ndims)
317317
{
318318
if (dimensions.Length == 0 && ndims == 1)
319319
return indices[0];

src/NumSharp.Core/View/Shape.cs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public partial struct Shape : ICloneable, IEquatable<Shape>
2424
/// <summary>
2525
/// True if the shape of this array was obtained by a slicing operation that caused the underlying data to be non-contiguous
2626
/// </summary>
27-
public bool IsSliced
27+
public readonly bool IsSliced
2828
{
2929
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3030
get => ViewInfo != null;
@@ -33,7 +33,7 @@ public bool IsSliced
3333
/// <summary>
3434
/// Does this Shape represents a non-sliced and non-broadcasted hence contagious unmanaged memory?
3535
/// </summary>
36-
public bool IsContiguous
36+
public readonly bool IsContiguous
3737
{
3838
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3939
get => !IsSliced && !IsBroadcasted;
@@ -42,7 +42,7 @@ public bool IsContiguous
4242
/// <summary>
4343
/// Is this Shape a recusive view? (deeper than 1 view)
4444
/// </summary>
45-
public bool IsRecursive
45+
public readonly bool IsRecursive
4646
{
4747
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4848
get => ViewInfo != null && ViewInfo.ParentShape.IsEmpty == false;
@@ -64,7 +64,7 @@ public bool IsRecursive
6464
/// <summary>
6565
/// Is this shape a broadcast and/or has modified strides?
6666
/// </summary>
67-
public bool IsBroadcasted => BroadcastInfo != null;
67+
public readonly bool IsBroadcasted => BroadcastInfo != null;
6868

6969
/// <summary>
7070
/// Is this shape a scalar? (<see cref="NDim"/>==0 && <see cref="size"/> == 1)
@@ -75,9 +75,9 @@ public bool IsRecursive
7575
/// True if the shape is not initialized.
7676
/// Note: A scalar shape is not empty.
7777
/// </summary>
78-
public bool IsEmpty => _hashCode == 0;
78+
public readonly bool IsEmpty => _hashCode == 0;
7979

80-
public char Order => layout;
80+
public readonly char Order => layout;
8181

8282
/// <summary>
8383
/// Singleton instance of a <see cref="Shape"/> that represents a scalar.
@@ -160,19 +160,19 @@ public static Shape Matrix(int rows, int cols)
160160
return shape;
161161
}
162162

163-
public int NDim
163+
public readonly int NDim
164164
{
165165
[MethodImpl(MethodImplOptions.AggressiveInlining)]
166166
get => dimensions.Length;
167167
}
168168

169-
public int[] Dimensions
169+
public readonly int[] Dimensions
170170
{
171171
[MethodImpl(MethodImplOptions.AggressiveInlining)]
172172
get => dimensions;
173173
}
174174

175-
public int[] Strides
175+
public readonly int[] Strides
176176
{
177177
[MethodImpl(MethodImplOptions.AggressiveInlining)]
178178
get => strides;
@@ -181,7 +181,7 @@ public int[] Strides
181181
/// <summary>
182182
/// The linear size of this shape.
183183
/// </summary>
184-
public int Size
184+
public readonly int Size
185185
{
186186
[MethodImpl(MethodImplOptions.AggressiveInlining)]
187187
get => size;
@@ -346,7 +346,7 @@ public static Shape Empty(int ndim)
346346

347347

348348
[MethodImpl((MethodImplOptions)768)]
349-
private void _computeStrides()
349+
private readonly void _computeStrides()
350350
{
351351
if (dimensions.Length == 0)
352352
return;
@@ -361,11 +361,11 @@ private void _computeStrides()
361361

362362

363363
[MethodImpl((MethodImplOptions)768)]
364-
private void _computeStrides(int axis)
364+
private readonly void _computeStrides(int axis)
365365
{
366366
if (dimensions.Length == 0)
367367
return;
368-
368+
369369
if (axis == 0)
370370
strides[0] = strides[1] * dimensions[1];
371371
else
@@ -378,7 +378,7 @@ private void _computeStrides(int axis)
378378
}
379379
}
380380

381-
public int this[int dim]
381+
public readonly int this[int dim]
382382
{
383383
[MethodImpl(MethodImplOptions.AggressiveInlining)]
384384
get => dimensions[dim < 0 ? dimensions.Length + dim : dim];
@@ -393,7 +393,7 @@ public int this[int dim]
393393
/// <returns>The transformed offset.</returns>
394394
/// <remarks>Avoid using unless it is unclear if shape is sliced or not.</remarks>
395395
[MethodImpl(MethodImplOptions.AggressiveInlining)]
396-
public int TransformOffset(int offset)
396+
public readonly int TransformOffset(int offset)
397397
{
398398
// ReSharper disable once ConvertIfStatementToReturnStatement
399399
if (ViewInfo == null && BroadcastInfo == null)
@@ -415,7 +415,7 @@ public int TransformOffset(int offset)
415415
/// <returns>The index in the memory block that refers to a specific value.</returns>
416416
/// <remarks>Handles sliced indices and broadcasting</remarks>
417417
[MethodImpl((MethodImplOptions)768)]
418-
public int GetOffset(params int[] indices)
418+
public readonly int GetOffset(params int[] indices)
419419
{
420420
int offset;
421421
if (!IsSliced)
@@ -513,7 +513,7 @@ public int GetOffset(params int[] indices)
513513
/// <returns>The index in the memory block that refers to a specific value.</returns>
514514
/// <remarks>Handles sliced indices and broadcasting</remarks>
515515
[MethodImpl((MethodImplOptions)768)]
516-
internal int GetOffset_1D(int index)
516+
internal readonly int GetOffset_1D(int index)
517517
{
518518
int offset;
519519
if (!IsSliced)
@@ -609,7 +609,7 @@ internal int GetOffset_1D(int index)
609609
/// Note: to be used only inside of GetOffset()
610610
/// </summary>
611611
[MethodImpl((MethodImplOptions)768)]
612-
private int GetOffset_IgnoreViewInfo(params int[] indices)
612+
private readonly int GetOffset_IgnoreViewInfo(params int[] indices)
613613
{
614614
if (dimensions.Length == 0 && indices.Length == 1)
615615
return indices[0];
@@ -634,7 +634,7 @@ private int GetOffset_IgnoreViewInfo(params int[] indices)
634634
/// <returns>The index in the memory block that refers to a specific value.</returns>
635635
/// <remarks>Handles sliced indices and broadcasting</remarks>
636636
[MethodImpl((MethodImplOptions)768)]
637-
private int GetOffset_broadcasted(params int[] indices)
637+
private readonly int GetOffset_broadcasted(params int[] indices)
638638
{
639639
int offset;
640640
var vi = ViewInfo;
@@ -704,7 +704,7 @@ private int GetOffset_broadcasted(params int[] indices)
704704
/// <returns>The index in the memory block that refers to a specific value.</returns>
705705
/// <remarks>Handles sliced indices and broadcasting</remarks>
706706
[MethodImpl((MethodImplOptions)768)]
707-
private int GetOffset_broadcasted_1D(int index)
707+
private readonly int GetOffset_broadcasted_1D(int index)
708708
{
709709
int offset;
710710
var vi = ViewInfo;
@@ -775,7 +775,7 @@ private int GetOffset_broadcasted_1D(int index)
775775
/// <returns></returns>
776776
/// <remarks>Used for slicing, returned shape is the new shape of the slice and offset is the offset from current address.</remarks>
777777
[MethodImpl((MethodImplOptions)768)]
778-
public (Shape Shape, int Offset) GetSubshape(params int[] indicies)
778+
public readonly (Shape Shape, int Offset) GetSubshape(params int[] indicies)
779779
{
780780
if (indicies.Length == 0)
781781
return (this, 0);
@@ -853,7 +853,7 @@ private int GetOffset_broadcasted_1D(int index)
853853
/// <param name="offset">the index if you would iterate from 0 to shape.size in row major order</param>
854854
/// <returns></returns>
855855
[MethodImpl((MethodImplOptions)768)]
856-
public int[] GetCoordinates(int offset)
856+
public readonly int[] GetCoordinates(int offset)
857857
{
858858
int[] coords = null;
859859

@@ -903,7 +903,7 @@ public int[] GetCoordinates(int offset)
903903
/// <param name="offset">Is the index in the original array before it was sliced and/or reshaped</param>
904904
/// <remarks>Note: due to slicing the absolute indices (offset in memory) are different from what GetCoordinates would return, which are relative indices in the shape.</remarks>
905905
[MethodImpl((MethodImplOptions)768)]
906-
public int[] GetCoordinatesFromAbsoluteIndex(int offset)
906+
public readonly int[] GetCoordinatesFromAbsoluteIndex(int offset)
907907
{
908908
if (!IsSliced)
909909
return GetCoordinates(offset);
@@ -1095,11 +1095,11 @@ internal void ComputeHashcode()
10951095
#region Slicing support
10961096

10971097
[MethodImpl((MethodImplOptions)768)]
1098-
public Shape Slice(string slicing_notation) =>
1098+
public readonly Shape Slice(string slicing_notation) =>
10991099
this.Slice(NumSharp.Slice.ParseSlices(slicing_notation));
11001100

11011101
[MethodImpl((MethodImplOptions)768)]
1102-
public Shape Slice(params Slice[] input_slices)
1102+
public readonly Shape Slice(params Slice[] input_slices)
11031103
{
11041104
if (IsEmpty)
11051105
throw new InvalidOperationException("Unable to slice an empty shape.");
@@ -1200,22 +1200,22 @@ public static implicit operator Shape((int, int, int, int, int, int) dims) =>
12001200

12011201
#region Deconstructor
12021202

1203-
public void Deconstruct(out int dim1, out int dim2)
1203+
public readonly void Deconstruct(out int dim1, out int dim2)
12041204
{
12051205
var dims = this.dimensions;
12061206
dim1 = dims[0];
12071207
dim2 = dims[1];
12081208
}
12091209

1210-
public void Deconstruct(out int dim1, out int dim2, out int dim3)
1210+
public readonly void Deconstruct(out int dim1, out int dim2, out int dim3)
12111211
{
12121212
var dims = this.dimensions;
12131213
dim1 = dims[0];
12141214
dim2 = dims[1];
12151215
dim3 = dims[2];
12161216
}
12171217

1218-
public void Deconstruct(out int dim1, out int dim2, out int dim3, out int dim4)
1218+
public readonly void Deconstruct(out int dim1, out int dim2, out int dim3, out int dim4)
12191219
{
12201220
var dims = this.dimensions;
12211221
dim1 = dims[0];
@@ -1224,7 +1224,7 @@ public void Deconstruct(out int dim1, out int dim2, out int dim3, out int dim4)
12241224
dim4 = dims[3];
12251225
}
12261226

1227-
public void Deconstruct(out int dim1, out int dim2, out int dim3, out int dim4, out int dim5)
1227+
public readonly void Deconstruct(out int dim1, out int dim2, out int dim3, out int dim4, out int dim5)
12281228
{
12291229
var dims = this.dimensions;
12301230
dim1 = dims[0];
@@ -1234,7 +1234,7 @@ public void Deconstruct(out int dim1, out int dim2, out int dim3, out int dim4,
12341234
dim5 = dims[4];
12351235
}
12361236

1237-
public void Deconstruct(out int dim1, out int dim2, out int dim3, out int dim4, out int dim5, out int dim6)
1237+
public readonly void Deconstruct(out int dim1, out int dim2, out int dim3, out int dim4, out int dim5, out int dim6)
12381238
{
12391239
var dims = this.dimensions;
12401240
dim1 = dims[0];
@@ -1275,7 +1275,7 @@ public void Deconstruct(out int dim1, out int dim2, out int dim3, out int dim4,
12751275
return !(a == b);
12761276
}
12771277

1278-
public override bool Equals(object obj)
1278+
public override readonly bool Equals(object obj)
12791279
{
12801280
if (ReferenceEquals(null, obj))
12811281
{
@@ -1293,7 +1293,7 @@ public override bool Equals(object obj)
12931293
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
12941294
/// <param name="other">An object to compare with this object.</param>
12951295
/// <returns>true if the current object is equal to the <paramref name="other">other</paramref> parameter; otherwise, false.</returns>
1296-
public bool Equals(Shape other)
1296+
public readonly bool Equals(Shape other)
12971297
{
12981298
if ((_hashCode == 0 && _hashCode == other._hashCode) || dimensions == null && other.dimensions == null) //they are empty.
12991299
return true;
@@ -1361,14 +1361,14 @@ public override string ToString() =>
13611361

13621362
/// <summary>Creates a new object that is a copy of the current instance.</summary>
13631363
/// <returns>A new object that is a copy of this instance.</returns>
1364-
object ICloneable.Clone() =>
1364+
readonly object ICloneable.Clone() =>
13651365
Clone(true, false, false);
13661366

13671367
/// <summary>
13681368
/// Creates a complete copy of this Shape.
13691369
/// </summary>
13701370
/// <param name="deep">Should make a complete deep clone or a shallow if false.</param>
1371-
public Shape Clone(bool deep = true, bool unview = false, bool unbroadcast = false)
1371+
public readonly Shape Clone(bool deep = true, bool unview = false, bool unbroadcast = false)
13721372
{
13731373
if (IsEmpty)
13741374
return default;
@@ -1406,7 +1406,7 @@ public Shape Clone(bool deep = true, bool unview = false, bool unbroadcast = fal
14061406
/// Cleans ViewInfo and returns a newly constructed.
14071407
/// </summary>
14081408
/// <returns></returns>
1409-
public Shape Clean()
1409+
public readonly Shape Clean()
14101410
{
14111411
if (IsScalar)
14121412
return NewScalar();

0 commit comments

Comments
 (0)