Skip to content

Commit f03a493

Browse files
committed
Merge branch 'feature/minor-perf'
2 parents 194e653 + 85f86c6 commit f03a493

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

CardinalityEstimation/CardinalityEstimator.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public class CardinalityEstimator : ICardinalityEstimator<string>, ICardinalityE
112112
/// <summary>
113113
/// Lookup dictionary for the sparse representation of HLL buckets
114114
/// </summary>
115-
private IDictionary<ushort, byte> lookupSparse;
115+
private Dictionary<ushort, byte> lookupSparse;
116116

117117
/// <summary>
118118
/// Max number of elements to hold in the sparse representation before switching to dense
@@ -800,19 +800,24 @@ private bool AddElementHash(ulong hashCode)
800800
if (isSparse)
801801
{
802802
lookupSparse.TryGetValue(substream, out byte prevRank);
803-
lookupSparse[substream] = Math.Max(prevRank, sigma);
804-
changed = changed || (prevRank != sigma && lookupSparse[substream] == sigma);
805-
if (lookupSparse.Count > sparseMaxElements)
803+
if (sigma > prevRank)
806804
{
807-
SwitchToDenseRepresentation();
805+
lookupSparse[substream] = sigma;
806+
if (lookupSparse.Count > sparseMaxElements)
807+
{
808+
SwitchToDenseRepresentation();
809+
}
808810
changed = true;
809811
}
810812
}
811813
else
812814
{
813-
var prevMax = lookupDense[substream];
814-
lookupDense[substream] = Math.Max(prevMax, sigma);
815-
changed = changed || (prevMax != sigma && lookupDense[substream] == sigma);
815+
ref var value = ref lookupDense[substream];
816+
if (sigma > value)
817+
{
818+
value = sigma;
819+
changed = true;
820+
}
816821
}
817822
return changed;
818823
}

0 commit comments

Comments
 (0)