Skip to content
New issue

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

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

Already on GitHub? # to your account

Use ref byte for compression input pointers #56

Merged
merged 1 commit into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 7 additions & 7 deletions Snappier.Benchmarks/FindMatchLength.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BenchmarkDotNet.Attributes;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using Snappier.Internal;

namespace Snappier.Benchmarks
Expand All @@ -15,13 +16,12 @@ public class FindMatchLength
public unsafe (long, bool) Regular()
{
ulong data = 0;
fixed (byte* s1 = _array)
{
var s2 = s1 + 12;
var s2Limit = s1 + _array.Length;

return SnappyCompressor.FindMatchLength(s1, s2, s2Limit, ref data);
}
ref byte s1 = ref _array[0];
ref byte s2 = ref Unsafe.Add(ref s1, 12);
ref byte s2Limit = ref Unsafe.Add(ref s1, _array.Length - 1);

return SnappyCompressor.FindMatchLength(ref s1, ref s2, ref s2Limit, ref data);
}
}
}
15 changes: 7 additions & 8 deletions Snappier.Tests/Internal/SnappyCompressorTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using System.Text;
using Snappier.Internal;
using Xunit;
Expand Down Expand Up @@ -87,16 +88,14 @@ public unsafe void FindMatchLength(int expectedResult, string s1String, string s
+ new string('\0', Math.Max(0, length - s2String.Length)));

ulong data = 0;
fixed (byte* s1 = array)
{
byte* s2 = s1 + s1String.Length;
ref byte s1 = ref array[0];
ref byte s2 = ref Unsafe.Add(ref s1, s1String.Length);

var result =
SnappyCompressor.FindMatchLength(s1, s2, s2 + length, ref data);
var result =
SnappyCompressor.FindMatchLength(ref s1, ref s2, ref Unsafe.Add(ref s2, length - 1), ref data);

Assert.Equal(result.matchLength < 8, result.matchLengthLessThan8);
Assert.Equal(expectedResult, result.matchLength);
}
Assert.Equal(result.matchLength < 8, result.matchLengthLessThan8);
Assert.Equal(expectedResult, result.matchLength);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion Snappier/Internal/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum ChunkType : byte

public const int BlockLog = 16;
public const long BlockSize = 1 << BlockLog;
public const long InputMarginBytes = 15;
public const nint InputMarginBytes = 15;

/// <summary>
/// Data stored per entry in lookup table:
Expand Down
4 changes: 2 additions & 2 deletions Snappier/Internal/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ public static uint UnsafeReadUInt32(ref byte ptr)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe ulong UnsafeReadUInt64(void* ptr)
public static ulong UnsafeReadUInt64(ref byte ptr)
{
var result = Unsafe.ReadUnaligned<ulong>(ptr);
var result = Unsafe.ReadUnaligned<ulong>(ref ptr);
if (!BitConverter.IsLittleEndian)
{
result = BinaryPrimitives.ReverseEndianness(result);
Expand Down
Loading