Skip to content

Commit

Permalink
Use ByteArrayComparer
Browse files Browse the repository at this point in the history
  • Loading branch information
bezysoftware committed Oct 29, 2024
1 parent 96f9071 commit d2f959c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
23 changes: 21 additions & 2 deletions src/Negentropy/ByteArrayComparer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Negentropy
using System.Diagnostics.CodeAnalysis;

namespace Negentropy
{
internal class ByteArrayComparer : IComparer<byte[]>
internal class ByteArrayComparer : IComparer<byte[]>, IEqualityComparer<byte[]>
{
public int Compare(byte[]? x, byte[]? y)
{
Expand All @@ -18,6 +20,23 @@ public int Compare(byte[]? x, byte[]? y)
return 0;
}

public bool Equals(byte[]? x, byte[]? y)
{
return Compare(x, y) == 0;
}

public int GetHashCode([DisallowNull] byte[] a)
{
uint b = 0;

for (int i = 0; i < a.Length; i++)
{
b = ((b << 23) | (b >> 9)) ^ a[i];
}

return unchecked((int)b);
}

public static ByteArrayComparer Instance { get; } = new ByteArrayComparer();
}
}
16 changes: 9 additions & 7 deletions src/Negentropy/Negentropy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Linq;

namespace Negentropy
{
Expand All @@ -10,6 +11,7 @@ public class Negentropy
const byte PROTOCOL_VERSION = 0x61; // Version 1
const int BUCKETS = 16;
const int ID_SIZE = 32;
const int RESERVE = 200;

private readonly Bound[] items;
private readonly NegentropyOptions options;
Expand Down Expand Up @@ -114,7 +116,7 @@ public NegentropyReconciliation Reconcile(string query)
var theirFingerprint = reader.ReadBytes(Fingerprint.SIZE);
var ouringerprint = Fingerprint.Calculate(this.items, lower, upper);

if (StructuralComparisons.StructuralEqualityComparer.Equals(theirFingerprint, ouringerprint))
if (ByteArrayComparer.Instance.Equals(theirFingerprint, ouringerprint))
{
skip = true;
}
Expand All @@ -126,7 +128,7 @@ public NegentropyReconciliation Reconcile(string query)
break;
case Mode.IdList:
var count = reader.ReadVarInt();
var theirIds = new HashSet<byte[]>();
var theirIds = new HashSet<byte[]>(ByteArrayComparer.Instance);

for (var i = 0L; i < count; i++)
{
Expand All @@ -145,10 +147,10 @@ public NegentropyReconciliation Reconcile(string query)
.Skip(lower)
.Take(upper - lower)
.Select(x => x.Id)
.ToArray();
.ToHashSet(ByteArrayComparer.Instance);

haveIds.AddRange(ourIds.Where(our => !theirIds.Any(their => StructuralComparisons.StructuralEqualityComparer.Equals(our, their))));
needIds.AddRange(theirIds.Where(their => !ourIds.Any(our => StructuralComparisons.StructuralEqualityComparer.Equals(our, their))));
haveIds.AddRange(ourIds.Where(our => !theirIds.Contains(our)));
needIds.AddRange(theirIds.Where(their => !ourIds.Contains(their)));
}
else
{
Expand All @@ -157,7 +159,7 @@ public NegentropyReconciliation Reconcile(string query)
var endBound = bound;
var spaceLeft = this.options.FrameSizeLimit == 0
? int.MaxValue
: (int)((this.options.FrameSizeLimit - 200 - writer.BaseStream.Position) / ID_SIZE);
: (int)((this.options.FrameSizeLimit - RESERVE - writer.BaseStream.Position) / ID_SIZE);

var responseIds = this.items
.Skip(lower)
Expand All @@ -177,7 +179,7 @@ public NegentropyReconciliation Reconcile(string query)
throw new InvalidOperationException("Unexpected mode");
}

if (this.options.FrameSizeLimit > 0 && writer.BaseStream.Position > this.options.FrameSizeLimit - 200)
if (this.options.FrameSizeLimit > 0 && writer.BaseStream.Position > this.options.FrameSizeLimit - RESERVE)
{
var remainingFingerprint = Fingerprint.Calculate(this.items, upper, this.items.Length);

Expand Down

0 comments on commit d2f959c

Please sign in to comment.