Skip to content

Commit aae7c4b

Browse files
committed
Added significant optimization by avoiding np's slice when memory-slice is possible
1 parent 2bce2fd commit aae7c4b

3 files changed

Lines changed: 51 additions & 15 deletions

File tree

src/NumSharp.Core/Backends/Unmanaged/UnmanagedStorage.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1937,9 +1937,34 @@ internal void ExpandDimension(int axis)
19371937

19381938
#region Slicing
19391939

1940+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
19401941
public UnmanagedStorage GetView(string slicing_notation) => GetView(Slice.ParseSlices(slicing_notation));
19411942

1942-
public UnmanagedStorage GetView(params Slice[] slices) => Alias(_shape.Slice(slices));
1943+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1944+
[SuppressMessage("ReSharper", "PossibleInvalidOperationException")]
1945+
public UnmanagedStorage GetView(params Slice[] slices)
1946+
{
1947+
if (slices == null)
1948+
throw new ArgumentNullException(nameof(slices));
1949+
1950+
//handle memory slice if possible
1951+
if (!_shape.IsSliced)
1952+
{
1953+
var indices = new int[slices.Length];
1954+
for (var i = 0; i < slices.Length; i++)
1955+
{
1956+
var inputSlice = slices[i];
1957+
if (!inputSlice.IsIndex)
1958+
goto _perform_slice;
1959+
indices[i] = inputSlice.Start.Value;
1960+
}
1961+
1962+
return GetData(indices);
1963+
}
1964+
1965+
_perform_slice:
1966+
return Alias(_shape.Slice(slices));
1967+
}
19431968

19441969
#endregion
19451970

test/NumSharp.UnitTest/Utilities/FluentExtension.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,7 @@ public AndConstraint<NDArrayAssertions> BeShaped(params int[] dimensions)
165165
return new AndConstraint<NDArrayAssertions>(this);
166166
}
167167

168-
public AndConstraint<NDArrayAssertions> Be(Shape shape, string because = null, params object[] becauseArgs)
169-
{
170-
Execute.Assertion
171-
.BecauseOf(because, becauseArgs)
172-
.ForCondition(Subject.Equals(shape))
173-
.FailWith($"Expected shape to be {shape.ToString()} but got {Subject.ToString()}");
174-
175-
return new AndConstraint<NDArrayAssertions>(this);
176-
}
177-
178-
public AndConstraint<NDArrayAssertions> BeEquivalentTo(int? size = null, int? ndim = null, ITuple shape = null)
168+
public AndConstraint<NDArrayAssertions> BeShaped(int? size = null, int? ndim = null, ITuple shape = null)
179169
{
180170
if (size.HasValue)
181171
{
@@ -194,7 +184,7 @@ public AndConstraint<NDArrayAssertions> BeEquivalentTo(int? size = null, int? nd
194184
return new AndConstraint<NDArrayAssertions>(this);
195185
}
196186

197-
public AndConstraint<NDArrayAssertions> NotBe(Shape shape, string because = null, params object[] becauseArgs)
187+
public AndConstraint<NDArrayAssertions> NotBeShaped(Shape shape, string because = null, params object[] becauseArgs)
198188
{
199189
Execute.Assertion
200190
.BecauseOf(because, becauseArgs)
@@ -266,15 +256,20 @@ public AndConstraint<NDArrayAssertions> BeNDim(int ndim)
266256
return new AndConstraint<NDArrayAssertions>(this);
267257
}
268258

269-
public AndConstraint<NDArrayAssertions> HaveValues(NDArray other)
259+
public AndConstraint<NDArrayAssertions> HaveValues(NDArray expected)
270260
{
271261
Execute.Assertion
272-
.ForCondition(np.array_equal(Subject, other))
262+
.ForCondition(np.array_equal(Subject, expected))
273263
.FailWith($"Expected the subject and other ndarray to be equals.");
274264

275265
return new AndConstraint<NDArrayAssertions>(this);
276266
}
277267

268+
public AndConstraint<NDArrayAssertions> Be(NDArray expected)
269+
{
270+
return HaveValues(expected);
271+
}
272+
278273
public AndConstraint<NDArrayAssertions> HaveValues(params object[] values)
279274
{
280275
if (values == null)

test/NumSharp.UnitTest/View/NDArray.View.Test.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using FluentAssertions;
66
using Microsoft.VisualStudio.TestTools.UnitTesting;
77
using NumSharp.Backends;
8+
using NumSharp.UnitTest.Utilities;
89

910
namespace NumSharp.UnitTest.View
1011
{
@@ -566,6 +567,7 @@ public void SliceSelectsAll()
566567
ret = b * c;
567568
cSharp.asCode2D("ret54", ret)
568569
*/
570+
569571
[TestMethod]
570572
public void Multiply_2DSlice_By_1D()
571573
{
@@ -580,5 +582,19 @@ public void Multiply_2DSlice_By_1D()
580582
var ret = b * c;
581583
Assert.AreEqual(ret54, ret);
582584
}
585+
586+
[TestMethod]
587+
public void AllSlicesAreIndexes()
588+
{
589+
var a = np.arange(27).reshape(3, 3, 3);
590+
var ret = a[Slice.Index(0), Slice.Index(0), Slice.Index(0)];
591+
ret.Should().NotBeSliced().And.BeScalar(value: 0);
592+
593+
ret = a[Slice.Index(0), Slice.Index(1)];
594+
ret.Should().NotBeSliced().And.BeShaped(3).And.HaveValues(3,4,5);
595+
596+
ret = a[Slice.Index(0), Slice.Index(1), Slice.All];
597+
ret.Should().BeSliced();
598+
}
583599
}
584600
}

0 commit comments

Comments
 (0)