Skip to content

Commit

Permalink
Optimize GetSigma in net8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
saguiitay committed Sep 18, 2023
1 parent e2334be commit ef2bc36
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CardinalityEstimation.Test/CardinalityEstimatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void TestCountAdditions()
[Fact]
public void TestChanged()
{
var estimator = new CardinalityEstimator();
var estimator = new CardinalityEstimator(Murmur3.GetHashCode);

Assert.Equal(0UL, estimator.CountAdditions);

Expand Down
2 changes: 1 addition & 1 deletion CardinalityEstimation/CardinalityEstimation.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net8.0;net7.0;net6.0</TargetFrameworks>
<Configurations>Debug;Release;Release-Signed</Configurations>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId Condition=" '$(Configuration)' == 'Release-Signed' ">CardinalityEstimation.Signed</PackageId>
Expand Down
26 changes: 20 additions & 6 deletions CardinalityEstimation/CardinalityEstimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class CardinalityEstimator : ICardinalityEstimator<string>, ICardinalityE
/// <summary>
/// Number of bits to compute the HLL estimate on
/// </summary>
private readonly int bitsForHll;
private readonly byte bitsForHll;

/// <summary>
/// HLL lookup table size
Expand Down Expand Up @@ -173,7 +173,7 @@ public CardinalityEstimator(CardinalityEstimator other)
internal CardinalityEstimator(GetHashCodeDelegate hashFunction, CardinalityEstimatorState state)
{
bitsPerIndex = state.BitsPerIndex;
bitsForHll = 64 - bitsPerIndex;
bitsForHll = (byte)(64 - bitsPerIndex);
m = (int) Math.Pow(2, bitsPerIndex);
alphaM = GetAlphaM(m);
subAlgorithmSelectionThreshold = GetSubAlgorithmSelectionThreshold(bitsPerIndex);
Expand All @@ -183,9 +183,9 @@ internal CardinalityEstimator(GetHashCodeDelegate hashFunction, CardinalityEstim
if (this.hashFunction == null)
{
#if NET8_0_OR_GREATER
hashFunction = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x));
this.hashFunction = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x));
#else
hashFunction = Murmur3.GetHashCode;
this.hashFunction = Murmur3.GetHashCode;
#endif
}

Expand Down Expand Up @@ -656,8 +656,21 @@ private double GetAlphaM(int m)
/// <param name="hash">Hash value to calculate the statistic on</param>
/// <param name="bitsToCount">Lowest bit to count from <paramref name="hash" /></param>
/// <returns>The number of leading zeroes in the binary representation of <paramref name="hash" />, plus one</returns>
internal static byte GetSigma(ulong hash, int bitsToCount)
public static byte GetSigma(ulong hash, byte bitsToCount)
{
if (hash == 0)
{
return (byte)(bitsToCount + 1);
}

#if NET8_0_OR_GREATER
ulong mask = ((1UL << bitsToCount) - 1);
int knownZeros = 64 - bitsToCount;

var masked = hash & mask;
var leadingZeros = (byte)ulong.LeadingZeroCount(masked);
return (byte)(leadingZeros - knownZeros + 1);
#else
byte sigma = 1;
for (int i = bitsToCount - 1; i >= 0; --i)
{
Expand All @@ -671,6 +684,7 @@ internal static byte GetSigma(ulong hash, int bitsToCount)
}
}
return sigma;
#endif
}

/// <summary>
Expand All @@ -692,7 +706,7 @@ private void SwitchToDenseRepresentation()
lookupSparse = null;
isSparse = false;
}
#endregion
#endregion

#region IEquatable implementation
public bool Equals(CardinalityEstimator other)
Expand Down

0 comments on commit ef2bc36

Please sign in to comment.