Skip to content
Open
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
3 changes: 2 additions & 1 deletion CardinalityEstimation/CardinalityEstimation.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Configurations>Debug;Release;Release-Signed</Configurations>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId Condition=" '$(Configuration)' == 'Release-Signed' ">CardinalityEstimation.Signed</PackageId>
Expand Down
11 changes: 6 additions & 5 deletions CardinalityEstimation/CardinalityEstimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace CardinalityEstimation
{
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
using Hash;

Expand Down Expand Up @@ -214,8 +215,8 @@ internal CardinalityEstimator(GetHashCodeDelegate hashFunction, CardinalityEstim
this.hashFunction = hashFunction;
if (this.hashFunction == null)
{
this.hashFunction = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x));
this.hashFunctionSpan = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x));
this.hashFunction = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x), 0);
this.hashFunctionSpan = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x), 0);
}
else
{
Expand All @@ -237,8 +238,8 @@ internal CardinalityEstimator(GetHashCodeSpanDelegate hashFunctionSpan, Cardinal
this.hashFunctionSpan = hashFunctionSpan;
if (this.hashFunctionSpan == null)
{
this.hashFunction = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x));
this.hashFunctionSpan = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x));
this.hashFunction = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x), 0);
this.hashFunctionSpan = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x), 0);
}
else
{
Expand Down Expand Up @@ -846,7 +847,7 @@ public static byte GetSigma(ulong hash, byte bitsToCount)
int knownZeros = 64 - bitsToCount;

var masked = hash & mask;
var leadingZeros = (byte)ulong.LeadingZeroCount(masked);
var leadingZeros = (byte)BitOperations.LeadingZeroCount(masked);
return (byte)(leadingZeros - knownZeros + 1);
}

Expand Down
10 changes: 5 additions & 5 deletions CardinalityEstimation/ConcurrentCardinalityEstimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public ConcurrentCardinalityEstimator(CardinalityEstimator other)
lockSlim = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);

// Init the hash function - use default since we can't get it from the other estimator
hashFunction = ((x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x)));
hashFunction = ((x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x), 0));

sparseMaxElements = Math.Max(0, (m / 15) - 10);

Expand Down Expand Up @@ -217,8 +217,8 @@ internal ConcurrentCardinalityEstimator(GetHashCodeDelegate hashFunction, Cardin
this.hashFunction = hashFunction;
if (this.hashFunction == null)
{
this.hashFunction = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x));
this.hashFunctionSpan = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x));
this.hashFunction = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x), 0);
this.hashFunctionSpan = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x), 0);
}
else
{
Expand All @@ -237,8 +237,8 @@ internal ConcurrentCardinalityEstimator(GetHashCodeSpanDelegate hashFunctionSpan
this.hashFunctionSpan = hashFunctionSpan;
if (this.hashFunction == null)
{
this.hashFunction = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x));
this.hashFunctionSpan = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x));
this.hashFunction = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x), 0);
this.hashFunctionSpan = (x) => BitConverter.ToUInt64(System.IO.Hashing.XxHash128.Hash(x), 0);
}
}

Expand Down
26 changes: 26 additions & 0 deletions CardinalityEstimation/Polyfills/BitOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#if !NETCOREAPP3_0_OR_GREATER
namespace System.Numerics;

internal static class BitOperations
{
public static uint LeadingZeroCount(ulong x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;

x -= x >> 1 & 0x5555555555555555;
x = (x >> 2 & 0x3333333333333333) + (x & 0x3333333333333333);
x = (x >> 4) + x & 0x0f0f0f0f0f0f0f0f;
x += x >> 8;
x += x >> 16;
x += x >> 32;

const int numLongBits = sizeof(long) * 8;
return numLongBits - (uint)(x & 0x0000007f);
}
}
#endif
13 changes: 13 additions & 0 deletions CardinalityEstimation/Polyfills/Linq.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#if !NETCOREAPP2_1_OR_GREATER
using System.Collections.Generic;

namespace System.Linq;

internal static class LinqExtensions
{
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
{
return new(source);
}
}
#endif