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

Revert "Reintroduce FNV hashing" #9768

Merged
merged 1 commit into from
Feb 22, 2024
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
42 changes: 4 additions & 38 deletions src/Build.UnitTests/Evaluation/Expander_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3890,14 +3890,8 @@ public void PropertyStringConstructorConsumingItemMetadata(string metadatumName,
result.ShouldBe(metadatumValue);
}

public static IEnumerable<object[]> GetHashAlgoTypes()
=> Enum.GetNames(typeof(IntrinsicFunctions.StringHashingAlgorithm))
.Append(null)
.Select(t => new object[] { t });

[Theory]
[MemberData(nameof(GetHashAlgoTypes))]
public void PropertyFunctionHashCodeSameOnlyIfStringSame(string hashType)
[Fact]
public void PropertyFunctionHashCodeSameOnlyIfStringSame()
{
PropertyDictionary<ProjectPropertyInstance> pg = new PropertyDictionary<ProjectPropertyInstance>();
Expander<ProjectPropertyInstance, ProjectItemInstance> expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
Expand All @@ -3912,9 +3906,8 @@ public void PropertyFunctionHashCodeSameOnlyIfStringSame(string hashType)
"cat12s",
"cat1s"
};
string hashTypeString = hashType == null ? "" : $", '{hashType}'";
object[] hashes = stringsToHash.Select(toHash =>
expander.ExpandPropertiesLeaveTypedAndEscaped($"$([MSBuild]::StableStringHash('{toHash}'{hashTypeString}))", ExpanderOptions.ExpandProperties, MockElementLocation.Instance))
int[] hashes = stringsToHash.Select(toHash =>
(int)expander.ExpandPropertiesLeaveTypedAndEscaped($"$([MSBuild]::StableStringHash('{toHash}'))", ExpanderOptions.ExpandProperties, MockElementLocation.Instance))
.ToArray();
for (int a = 0; a < hashes.Length; a++)
{
Expand All @@ -3932,33 +3925,6 @@ public void PropertyFunctionHashCodeSameOnlyIfStringSame(string hashType)
}
}

[Theory]
[MemberData(nameof(GetHashAlgoTypes))]
public void PropertyFunctionHashCodeReturnsExpectedType(string hashType)
{
PropertyDictionary<ProjectPropertyInstance> pg = new PropertyDictionary<ProjectPropertyInstance>();
Expander<ProjectPropertyInstance, ProjectItemInstance> expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
Type expectedType;

expectedType = hashType switch
{
null => typeof(int),
"Legacy" => typeof(int),
"Fnv1a32bit" => typeof(int),
"Fnv1a32bitFast" => typeof(int),
"Fnv1a64bit" => typeof(long),
"Fnv1a64bitFast" => typeof(long),
"Sha256" => typeof(string),
_ => throw new ArgumentOutOfRangeException(nameof(hashType))
};


string hashTypeString = hashType == null ? "" : $", '{hashType}'";
object hashValue = expander.ExpandPropertiesLeaveTypedAndEscaped($"$([MSBuild]::StableStringHash('FooBar'{hashTypeString}))", ExpanderOptions.ExpandProperties, MockElementLocation.Instance);

hashValue.ShouldBeOfType(expectedType);
}

[Theory]
[InlineData("easycase")]
[InlineData("")]
Expand Down
5 changes: 0 additions & 5 deletions src/Build/Evaluation/Expander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4162,11 +4162,6 @@ private bool TryExecuteWellKnownFunction(out object returnVal, object objectInst
returnVal = IntrinsicFunctions.StableStringHash(arg0);
return true;
}
else if (TryGetArgs(args, out string arg1, out string arg2) && Enum.TryParse<IntrinsicFunctions.StringHashingAlgorithm>(arg2, true, out var hashAlgorithm))
{
returnVal = IntrinsicFunctions.StableStringHash(arg1, hashAlgorithm);
return true;
}
}
else if (string.Equals(_methodMethodName, nameof(IntrinsicFunctions.AreFeaturesEnabled), StringComparison.OrdinalIgnoreCase))
{
Expand Down
46 changes: 4 additions & 42 deletions src/Build/Evaluation/IntrinsicFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using Microsoft.Build.Shared;
using Microsoft.Build.Shared.FileSystem;
using Microsoft.Build.Utilities;
using Microsoft.NET.StringTools;
using Microsoft.Win32;

// Needed for DoesTaskHostExistForParameters
Expand Down Expand Up @@ -398,49 +397,12 @@ internal static string ConvertFromBase64(string toDecode)
return Encoding.UTF8.GetString(Convert.FromBase64String(toDecode));
}

internal enum StringHashingAlgorithm
{
// Legacy way of calculating StableStringHash - which was derived from string GetHashCode
Legacy,
// FNV-1a 32bit hash
Fnv1a32bit,
// Custom FNV-1a 32bit hash - optimized for speed by hashing by the whole chars (not individual bytes)
Fnv1a32bitFast,
// FNV-1a 64bit hash
Fnv1a64bit,
// Custom FNV-1a 64bit hash - optimized for speed by hashing by the whole chars (not individual bytes)
Fnv1a64bitFast,
// SHA256 hash - gets the hex string of the hash (with no prefix)
Sha256
}

/// <summary>
/// Hash the string independent of bitness, target framework and default codepage of the environment.
/// Hash the string independent of bitness and target framework.
/// </summary>
internal static object StableStringHash(string toHash)
=> StableStringHash(toHash, StringHashingAlgorithm.Legacy);

internal static object StableStringHash(string toHash, StringHashingAlgorithm algo) =>
algo switch
{
StringHashingAlgorithm.Legacy => CommunicationsUtilities.GetHashCode(toHash),
StringHashingAlgorithm.Fnv1a32bit => FowlerNollVo1aHash.ComputeHash32(toHash),
StringHashingAlgorithm.Fnv1a32bitFast => FowlerNollVo1aHash.ComputeHash32Fast(toHash),
StringHashingAlgorithm.Fnv1a64bit => FowlerNollVo1aHash.ComputeHash64(toHash),
StringHashingAlgorithm.Fnv1a64bitFast => FowlerNollVo1aHash.ComputeHash64Fast(toHash),
StringHashingAlgorithm.Sha256 => CalculateSha256(toHash),
_ => throw new ArgumentOutOfRangeException(nameof(algo), algo, null)
};

private static string CalculateSha256(string toHash)
{
var sha = System.Security.Cryptography.SHA256.Create();
var hashResult = new StringBuilder();
foreach (byte theByte in sha.ComputeHash(Encoding.UTF8.GetBytes(toHash)))
{
hashResult.Append(theByte.ToString("x2"));
}
return hashResult.ToString();
internal static int StableStringHash(string toHash)
{
return CommunicationsUtilities.GetHashCode(toHash);
}

/// <summary>
Expand Down
40 changes: 34 additions & 6 deletions src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Framework.Profiler;
using Microsoft.Build.Shared;
using Microsoft.Build.Utilities;
using Microsoft.NET.StringTools;

#nullable disable

Expand Down Expand Up @@ -1262,9 +1260,9 @@ private void Write(IExtendedBuildEventArgs extendedData)

internal readonly struct HashKey : IEquatable<HashKey>
{
private readonly long value;
private readonly ulong value;

private HashKey(long i)
private HashKey(ulong i)
{
value = i;
}
Expand All @@ -1277,13 +1275,13 @@ public HashKey(string text)
}
else
{
value = FowlerNollVo1aHash.ComputeHash64Fast(text);
value = FnvHash64.GetHashCode(text);
}
}

public static HashKey Combine(HashKey left, HashKey right)
{
return new HashKey(FowlerNollVo1aHash.Combine64(left.value, right.value));
return new HashKey(FnvHash64.Combine(left.value, right.value));
}

public HashKey Add(HashKey other) => Combine(this, other);
Expand Down Expand Up @@ -1313,5 +1311,35 @@ public override string ToString()
return value.ToString();
}
}

internal static class FnvHash64
{
public const ulong Offset = 14695981039346656037;
public const ulong Prime = 1099511628211;

public static ulong GetHashCode(string text)
{
ulong hash = Offset;

unchecked
{
for (int i = 0; i < text.Length; i++)
{
char ch = text[i];
hash = (hash ^ ch) * Prime;
}
}

return hash;
}

public static ulong Combine(ulong left, ulong right)
{
unchecked
{
return (left ^ right) * Prime;
}
}
}
}
}
145 changes: 0 additions & 145 deletions src/StringTools/FowlerNollVo1aHash.cs

This file was deleted.