Skip to content

Commit

Permalink
Replacing current MD5.ToString() implementation with HexMate. HexMate…
Browse files Browse the repository at this point in the history
… is overall faster by a significant amount, around 7x speed improvement. Additionally this implementation is much easier to read and understand.
  • Loading branch information
tpill90 committed May 22, 2024
1 parent 15d1072 commit 4b9556d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 93 deletions.
1 change: 1 addition & 0 deletions BattleNetPrefill.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=downloader/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Dropbox/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Endian/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Hexmate/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Lancache/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=mkdocs/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Multiplayer/@EntryIndexedValue">True</s:Boolean>
Expand Down
3 changes: 2 additions & 1 deletion BattleNetPrefill/BattleNetPrefill.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
</ItemGroup>

<ItemGroup>
<!-- External Packages -->
<!-- External Packages -->
<PackageReference Include="HexMate" Version="0.0.3" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.2.0" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="System.Text.Json" Version="6.0.5" />
Expand Down
2 changes: 1 addition & 1 deletion BattleNetPrefill/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"Prefill WOW - Compare Requests": {
"commandName": "Project",
"commandLineArgs": "prefill -p s1 --compare-requests --no-download --force"
"commandLineArgs": "prefill -p wow_classic --compare-requests --no-download --force"
},
"Select Apps": {
"commandName": "Project",
Expand Down
65 changes: 12 additions & 53 deletions BattleNetPrefill/Structs/MD5Hash.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace BattleNetPrefill.Structs
using HexMate;

namespace BattleNetPrefill.Structs
{
public readonly struct MD5Hash : IEquatable<MD5Hash>
{
Expand All @@ -12,7 +14,6 @@
public readonly ulong lowPart;

[JsonInclude]

public readonly ulong highPart;
#pragma warning restore SYSLIB1038

Expand All @@ -28,6 +29,7 @@ public override int GetHashCode()
return Md5HashEqualityComparer.Instance.GetHashCode(this);
}

//TODO benchmark equality again
public bool Equals(MD5Hash other)
{
return other.lowPart == lowPart && other.highPart == highPart;
Expand All @@ -39,63 +41,20 @@ public override bool Equals(object obj)
return other.lowPart == lowPart && other.highPart == highPart;
}

// TODO benchmark this and see if it is actually faster. Remove it otherwise
public override string ToString()
{
return string.Create(32, (highPart, lowPart), static (dst, state) =>
{
ulong highPartTemp = state.highPart;
ulong lowPartTemp = state.lowPart;

ulong lowMask = (ulong)15;
ulong highMask = 15 << 4;
int i = 0;

while (i != 16)
{
dst[i] = HexConverter.ToCharUpper((uint)((lowPartTemp & highMask) >> 4));
dst[i + 1] = HexConverter.ToCharUpper((uint)(lowPartTemp & lowMask));
i += 2;
lowPartTemp >>= 8;
}

while (i != 32)
{
dst[i] = HexConverter.ToCharUpper((uint)((highPartTemp & highMask) >> 4));
dst[i + 1] = HexConverter.ToCharUpper((uint)(highPartTemp & lowMask));
i += 2;
highPartTemp >>= 8;
}
});
var bytes = new byte[16];
BitConverter.TryWriteBytes(bytes.AsSpan(0, 8), lowPart);
BitConverter.TryWriteBytes(bytes.AsSpan(8, 8), highPart);
return HexMate.Convert.ToHexString(bytes);
}

public string ToStringLower()
{
return string.Create(32, (highPart, lowPart), static (dst, state) =>
{
ulong highPartTemp = state.highPart;
ulong lowPartTemp = state.lowPart;

ulong lowMask = (ulong)15;
ulong highMask = 15 << 4;
int i = 0;

while (i != 16)
{
dst[i] = HexConverter.ToCharLower((uint)((lowPartTemp & highMask) >> 4));
dst[i + 1] = HexConverter.ToCharLower((uint)(lowPartTemp & lowMask));
i += 2;
lowPartTemp >>= 8;
}

while (i != 32)
{
dst[i] = HexConverter.ToCharLower((uint)((highPartTemp & highMask) >> 4));
dst[i + 1] = HexConverter.ToCharLower((uint)(highPartTemp & lowMask));
i += 2;
highPartTemp >>= 8;
}
});
var bytes = new byte[16];
BitConverter.TryWriteBytes(bytes.AsSpan(0, 8), lowPart);
BitConverter.TryWriteBytes(bytes.AsSpan(8, 8), highPart);
return HexMate.Convert.ToHexString(bytes, HexFormattingOptions.Lowercase);
}

public static bool operator ==(MD5Hash obj1, MD5Hash obj2)
Expand Down
5 changes: 0 additions & 5 deletions BattleNetPrefill/Structs/VersionsFile.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
namespace BattleNetPrefill.Structs
{
public struct VersionsFile
{
public VersionsEntry[] entries;
}

public struct VersionsEntry
{
public MD5Hash buildConfig;
Expand Down
33 changes: 0 additions & 33 deletions BattleNetPrefill/Utils/HexConverter.cs

This file was deleted.

0 comments on commit 4b9556d

Please sign in to comment.