Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address perf+correctness of StrLen and StrChr #480

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions Cesium.Runtime.Tests/StringFunctionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Text;

namespace Cesium.Runtime.Tests;

public unsafe class StringFunctionTests
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some Unicode tests, shall we?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do

{
[Theory]
[InlineData("Hello\0", 5)]
[InlineData("Goodbye\0", 7)]
[InlineData("Hello\0Goodbye\0", 5)]
[InlineData(" \0", 18)]
public void StrLen(string input, int expected)
{
// TODO: If you are rich enough to procure a 2-4+ GB RAM runner,
// please update this test to exercise the path where the string
// length exceeds int.MaxLength of bytes.
var bytes = Encoding.UTF8.GetBytes(input);
fixed (byte* str = bytes)
{
var actual = StringFunctions.StrLen(str);

Assert.Equal((nuint)expected, actual);
}
}

[Fact]
public void StrLen_Null()
{
var actual = StringFunctions.StrLen(null);

Assert.Equal((nuint)0, actual);
}

[Theory]
[InlineData("Hello\n", 5)]
[InlineData("Goodbye\n", 7)]
[InlineData("Hello\nGoodbye\n", 5)]
[InlineData(" \n", 18)]
public void StrChr(string input, int expectedOffset)
{
var needle = '\n';
var bytes = Encoding.UTF8.GetBytes(input);
fixed (byte* str = bytes)
{
var ptr = StringFunctions.StrChr(str, '\n');

Assert.Equal((byte)needle, *ptr);
Assert.Equal(expectedOffset, (int)(ptr - str));
}
}

[Fact]
public void StrChr_Null()
{
var actual = StringFunctions.StrChr(null, '\0');

Assert.True(actual is null);
}
}
4 changes: 4 additions & 0 deletions Cesium.Runtime/Cesium.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
<InternalsVisibleTo Include="Cesium.Runtime.Tests" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>

</Project>
42 changes: 14 additions & 28 deletions Cesium.Runtime/StringFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#if NETSTANDARD
using System;
using System.Text;
#else
using System.Collections.Specialized;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
#endif

Expand All @@ -14,26 +16,8 @@ public unsafe static class StringFunctions
{
public static nuint StrLen(byte* str)
{
#if NETSTANDARD
if (str == null)
{
return 0;
}

Encoding encoding = Encoding.UTF8;
int byteLength = 0;
byte* search = str;
while (*search != '\0')
{
byteLength++;
search++;
}

int stringLength = encoding.GetCharCount(str, byteLength);
ForNeVeR marked this conversation as resolved.
Show resolved Hide resolved
return (uint)stringLength;
#else
return (uint)(Marshal.PtrToStringUTF8((nint)str)?.Length ?? 0);
ForNeVeR marked this conversation as resolved.
Show resolved Hide resolved
#endif
var offset = StrChr(str, 0);
return (nuint)(offset - str);
}
public static byte* StrCpy(byte* dest, byte* src)
{
Expand Down Expand Up @@ -186,19 +170,21 @@ public static int StrNCmp(byte* lhs, byte* rhs, nuint count)
}
public static byte* StrChr(byte* str, int ch)
{
if (str == null)
if (str != null)
{
return null;
}
int match;
nuint offset = 0;
byte c = (byte)ch;

while (*str != 0)
{
if (*str == ch)
// TODO: Copy IndexOfNullByte impl. from CoreLib in a distant future
var span = new Span<byte>(str, int.MaxValue);
ForNeVeR marked this conversation as resolved.
Show resolved Hide resolved
while ((match = span.IndexOf(c)) < 0)
{
return str;
offset += int.MaxValue;
span = new Span<byte>(str + offset, int.MaxValue);
}

str++;
return str + ((uint)match + offset);
}

return null;
Expand Down
Loading