Skip to content

Commit fe42406

Browse files
authored
Revert "Reintroduce FNV hashing (#9721)"
This reverts commit 23f7752.
1 parent aa5b552 commit fe42406

File tree

5 files changed

+42
-236
lines changed

5 files changed

+42
-236
lines changed

src/Build.UnitTests/Evaluation/Expander_Tests.cs

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3890,14 +3890,8 @@ public void PropertyStringConstructorConsumingItemMetadata(string metadatumName,
38903890
result.ShouldBe(metadatumValue);
38913891
}
38923892

3893-
public static IEnumerable<object[]> GetHashAlgoTypes()
3894-
=> Enum.GetNames(typeof(IntrinsicFunctions.StringHashingAlgorithm))
3895-
.Append(null)
3896-
.Select(t => new object[] { t });
3897-
3898-
[Theory]
3899-
[MemberData(nameof(GetHashAlgoTypes))]
3900-
public void PropertyFunctionHashCodeSameOnlyIfStringSame(string hashType)
3893+
[Fact]
3894+
public void PropertyFunctionHashCodeSameOnlyIfStringSame()
39013895
{
39023896
PropertyDictionary<ProjectPropertyInstance> pg = new PropertyDictionary<ProjectPropertyInstance>();
39033897
Expander<ProjectPropertyInstance, ProjectItemInstance> expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
@@ -3912,9 +3906,8 @@ public void PropertyFunctionHashCodeSameOnlyIfStringSame(string hashType)
39123906
"cat12s",
39133907
"cat1s"
39143908
};
3915-
string hashTypeString = hashType == null ? "" : $", '{hashType}'";
3916-
object[] hashes = stringsToHash.Select(toHash =>
3917-
expander.ExpandPropertiesLeaveTypedAndEscaped($"$([MSBuild]::StableStringHash('{toHash}'{hashTypeString}))", ExpanderOptions.ExpandProperties, MockElementLocation.Instance))
3909+
int[] hashes = stringsToHash.Select(toHash =>
3910+
(int)expander.ExpandPropertiesLeaveTypedAndEscaped($"$([MSBuild]::StableStringHash('{toHash}'))", ExpanderOptions.ExpandProperties, MockElementLocation.Instance))
39183911
.ToArray();
39193912
for (int a = 0; a < hashes.Length; a++)
39203913
{
@@ -3932,33 +3925,6 @@ public void PropertyFunctionHashCodeSameOnlyIfStringSame(string hashType)
39323925
}
39333926
}
39343927

3935-
[Theory]
3936-
[MemberData(nameof(GetHashAlgoTypes))]
3937-
public void PropertyFunctionHashCodeReturnsExpectedType(string hashType)
3938-
{
3939-
PropertyDictionary<ProjectPropertyInstance> pg = new PropertyDictionary<ProjectPropertyInstance>();
3940-
Expander<ProjectPropertyInstance, ProjectItemInstance> expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
3941-
Type expectedType;
3942-
3943-
expectedType = hashType switch
3944-
{
3945-
null => typeof(int),
3946-
"Legacy" => typeof(int),
3947-
"Fnv1a32bit" => typeof(int),
3948-
"Fnv1a32bitFast" => typeof(int),
3949-
"Fnv1a64bit" => typeof(long),
3950-
"Fnv1a64bitFast" => typeof(long),
3951-
"Sha256" => typeof(string),
3952-
_ => throw new ArgumentOutOfRangeException(nameof(hashType))
3953-
};
3954-
3955-
3956-
string hashTypeString = hashType == null ? "" : $", '{hashType}'";
3957-
object hashValue = expander.ExpandPropertiesLeaveTypedAndEscaped($"$([MSBuild]::StableStringHash('FooBar'{hashTypeString}))", ExpanderOptions.ExpandProperties, MockElementLocation.Instance);
3958-
3959-
hashValue.ShouldBeOfType(expectedType);
3960-
}
3961-
39623928
[Theory]
39633929
[InlineData("easycase")]
39643930
[InlineData("")]

src/Build/Evaluation/Expander.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4162,11 +4162,6 @@ private bool TryExecuteWellKnownFunction(out object returnVal, object objectInst
41624162
returnVal = IntrinsicFunctions.StableStringHash(arg0);
41634163
return true;
41644164
}
4165-
else if (TryGetArgs(args, out string arg1, out string arg2) && Enum.TryParse<IntrinsicFunctions.StringHashingAlgorithm>(arg2, true, out var hashAlgorithm))
4166-
{
4167-
returnVal = IntrinsicFunctions.StableStringHash(arg1, hashAlgorithm);
4168-
return true;
4169-
}
41704165
}
41714166
else if (string.Equals(_methodMethodName, nameof(IntrinsicFunctions.AreFeaturesEnabled), StringComparison.OrdinalIgnoreCase))
41724167
{

src/Build/Evaluation/IntrinsicFunctions.cs

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using Microsoft.Build.Shared;
1515
using Microsoft.Build.Shared.FileSystem;
1616
using Microsoft.Build.Utilities;
17-
using Microsoft.NET.StringTools;
1817
using Microsoft.Win32;
1918

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

401-
internal enum StringHashingAlgorithm
402-
{
403-
// Legacy way of calculating StableStringHash - which was derived from string GetHashCode
404-
Legacy,
405-
// FNV-1a 32bit hash
406-
Fnv1a32bit,
407-
// Custom FNV-1a 32bit hash - optimized for speed by hashing by the whole chars (not individual bytes)
408-
Fnv1a32bitFast,
409-
// FNV-1a 64bit hash
410-
Fnv1a64bit,
411-
// Custom FNV-1a 64bit hash - optimized for speed by hashing by the whole chars (not individual bytes)
412-
Fnv1a64bitFast,
413-
// SHA256 hash - gets the hex string of the hash (with no prefix)
414-
Sha256
415-
}
416-
417400
/// <summary>
418-
/// Hash the string independent of bitness, target framework and default codepage of the environment.
401+
/// Hash the string independent of bitness and target framework.
419402
/// </summary>
420-
internal static object StableStringHash(string toHash)
421-
=> StableStringHash(toHash, StringHashingAlgorithm.Legacy);
422-
423-
internal static object StableStringHash(string toHash, StringHashingAlgorithm algo) =>
424-
algo switch
425-
{
426-
StringHashingAlgorithm.Legacy => CommunicationsUtilities.GetHashCode(toHash),
427-
StringHashingAlgorithm.Fnv1a32bit => FowlerNollVo1aHash.ComputeHash32(toHash),
428-
StringHashingAlgorithm.Fnv1a32bitFast => FowlerNollVo1aHash.ComputeHash32Fast(toHash),
429-
StringHashingAlgorithm.Fnv1a64bit => FowlerNollVo1aHash.ComputeHash64(toHash),
430-
StringHashingAlgorithm.Fnv1a64bitFast => FowlerNollVo1aHash.ComputeHash64Fast(toHash),
431-
StringHashingAlgorithm.Sha256 => CalculateSha256(toHash),
432-
_ => throw new ArgumentOutOfRangeException(nameof(algo), algo, null)
433-
};
434-
435-
private static string CalculateSha256(string toHash)
436-
{
437-
var sha = System.Security.Cryptography.SHA256.Create();
438-
var hashResult = new StringBuilder();
439-
foreach (byte theByte in sha.ComputeHash(Encoding.UTF8.GetBytes(toHash)))
440-
{
441-
hashResult.Append(theByte.ToString("x2"));
442-
}
443-
return hashResult.ToString();
403+
internal static int StableStringHash(string toHash)
404+
{
405+
return CommunicationsUtilities.GetHashCode(toHash);
444406
}
445407

446408
/// <summary>

src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
using Microsoft.Build.Framework;
1616
using Microsoft.Build.Framework.Profiler;
1717
using Microsoft.Build.Shared;
18-
using Microsoft.Build.Utilities;
19-
using Microsoft.NET.StringTools;
2018

2119
#nullable disable
2220

@@ -1262,9 +1260,9 @@ private void Write(IExtendedBuildEventArgs extendedData)
12621260

12631261
internal readonly struct HashKey : IEquatable<HashKey>
12641262
{
1265-
private readonly long value;
1263+
private readonly ulong value;
12661264

1267-
private HashKey(long i)
1265+
private HashKey(ulong i)
12681266
{
12691267
value = i;
12701268
}
@@ -1277,13 +1275,13 @@ public HashKey(string text)
12771275
}
12781276
else
12791277
{
1280-
value = FowlerNollVo1aHash.ComputeHash64Fast(text);
1278+
value = FnvHash64.GetHashCode(text);
12811279
}
12821280
}
12831281

12841282
public static HashKey Combine(HashKey left, HashKey right)
12851283
{
1286-
return new HashKey(FowlerNollVo1aHash.Combine64(left.value, right.value));
1284+
return new HashKey(FnvHash64.Combine(left.value, right.value));
12871285
}
12881286

12891287
public HashKey Add(HashKey other) => Combine(this, other);
@@ -1313,5 +1311,35 @@ public override string ToString()
13131311
return value.ToString();
13141312
}
13151313
}
1314+
1315+
internal static class FnvHash64
1316+
{
1317+
public const ulong Offset = 14695981039346656037;
1318+
public const ulong Prime = 1099511628211;
1319+
1320+
public static ulong GetHashCode(string text)
1321+
{
1322+
ulong hash = Offset;
1323+
1324+
unchecked
1325+
{
1326+
for (int i = 0; i < text.Length; i++)
1327+
{
1328+
char ch = text[i];
1329+
hash = (hash ^ ch) * Prime;
1330+
}
1331+
}
1332+
1333+
return hash;
1334+
}
1335+
1336+
public static ulong Combine(ulong left, ulong right)
1337+
{
1338+
unchecked
1339+
{
1340+
return (left ^ right) * Prime;
1341+
}
1342+
}
1343+
}
13161344
}
13171345
}

src/StringTools/FowlerNollVo1aHash.cs

Lines changed: 0 additions & 145 deletions
This file was deleted.

0 commit comments

Comments
 (0)