Skip to content

Commit 8c97561

Browse files
committed
NDArray, string api: Perf-ops
- Fixed small type
1 parent 5d12968 commit 8c97561

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

src/NumSharp.Core/Backends/NDArray.String.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,24 @@ public string GetString(params int[] indices)
4747

4848
Debug.Assert(typecode == NPTypeCode.Char);
4949

50-
UnmanagedStorage arr = Storage.GetData(indices);
51-
Debug.Assert(arr.Shape.NDim == 1);
50+
UnmanagedStorage src = Storage.GetData(indices);
51+
Debug.Assert(src.Shape.NDim == 1);
5252

53-
if (!Shape.IsContagious)
53+
if (!Shape.IsContiguous)
5454
{
5555
//this works faster than cloning.
56-
var ret = new string('\0', arr.Count);
56+
var ret = new string('\0', src.Count);
5757
fixed (char* retChars = ret)
5858
{
59-
var dst = new UnmanagedStorage(new ArraySlice<char>(new UnmanagedMemoryBlock<char>(retChars, ret.Length)), arr.Shape.Clean());
60-
MultiIterator.Assign(dst, arr);
59+
var dst = new UnmanagedStorage(new ArraySlice<char>(new UnmanagedMemoryBlock<char>(retChars, ret.Length)), src.Shape.Clean());
60+
MultiIterator.Assign(dst, src);
6161
}
6262

6363
return ret;
6464
}
6565

6666
//new string always performs a copy, there is no need to keep reference to arr's unmanaged storage.
67-
return new string((char*)arr.Address, 0, arr.Count);
67+
return new string((char*)src.Address, 0, src.Count);
6868
}
6969
}
7070

@@ -81,13 +81,13 @@ public void SetString(string value, params int[] indices)
8181

8282
unsafe
8383
{
84-
if (Shape.IsContagious)
84+
if (Shape.IsContiguous)
8585
{
8686
var dst = (char*)Address + Shape.GetOffset(indices);
87-
fixed (char* strChars = value)
87+
fixed (char* src = value)
8888
{
89-
var src = strChars;
90-
Parallel.For(0, value.Length, i => *(dst + i) = *(src + i));
89+
var len = sizeof(char) * value.Length;
90+
Buffer.MemoryCopy(src, dst, len, len);
9191
}
9292
}
9393
else

src/NumSharp.Core/Creation/np.array.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public static NDArray array(string chars)
6161
throw new ArgumentNullException(nameof(chars));
6262
if (chars.Length==0)
6363
return new NDArray(NPTypeCode.Char);
64-
return new NDArray(ArraySlice.FromArray(chars.ToArray()), Shape.Vector(chars.Length));
6564

6665
unsafe
6766
{

src/NumSharp.Core/View/Shape.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public bool IsSliced
2828
/// <summary>
2929
/// Does this Shape represents a non-sliced and non-broadcasted hence contagious unmanaged memory?
3030
/// </summary>
31-
public bool IsContagious
31+
public bool IsContiguous
3232
{
3333
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3434
get => !IsSliced && !IsBroadcasted;

test/NumSharp.UnitTest/Backends/Unmanaged/StringApiTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,27 @@ public void GetString()
4040
str.Should().BeOfType<char>().And.BeShaped(5 * 5);
4141
str = str.reshape(5, 5);
4242
str.GetString(3).Should().Be("lllll");
43-
new Action(() => { var _ = str.GetString(3, 1); }).Should().Throw<ArgumentOutOfRangeException>();
43+
new Action(() =>
44+
{
45+
var _ = str.GetString(3, 1);
46+
}).Should().Throw<ArgumentOutOfRangeException>();
47+
}
48+
49+
[TestMethod]
50+
public void GetString_Sliced()
51+
{
52+
var str = np.repeat(np.array('h', 'e', 'l', 'l', 'o'), 5).reshape(5, 5)[":, 0"];
53+
str.Should().BeOfType<char>().And.BeShaped(5).And.BeOfValues('h', 'e', 'l', 'l', 'o').And.BeSliced();
54+
str.GetString().Should().Be("hello");
55+
}
56+
[TestMethod]
57+
public void SetString_Sliced()
58+
{
59+
var str = np.repeat(np.array('h', 'e', 'l', 'l', 'o'), 5).reshape(5, 5)[":, 0"];
60+
str.Should().BeOfType<char>().And.BeShaped(5).And.BeOfValues('h', 'e', 'l', 'l', 'o').And.BeSliced();
61+
str.GetString().Should().Be("hello");
62+
str.SetString("kekek");
63+
str.Should().BeOfValues("kekek".ToCharArray().Cast<object>().ToArray());
4464
}
4565

4666
[TestMethod]

0 commit comments

Comments
 (0)