Skip to content

Commit 289c8c9

Browse files
committed
Only use key length when the density of keys are above 75%
1 parent 68eb15a commit 289c8c9

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

Src/FastData/FastDataGenerator.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,10 @@ private static string GenerateInternalString<TValue>(string[] keys, TValue[]? va
160160
return GenerateWrapper(generator, genCfg, new SingleValueStructure<string, TValue>(), keys, values);
161161

162162
// For small amounts of data, logic is the fastest. However, it increases the assembly size, so we want to try some special cases first.
163+
double density = (double)keys.Length / (strProps.LengthData.Max - strProps.LengthData.Min + 1);
163164

164-
// If strings have unique lengths, we prefer to use a KeyLengthStructure.
165-
if (strProps.LengthData.Unique)
165+
// Use KeyLengthStructure only when string lengths are unique and density >= 75%
166+
if (strProps.LengthData.Unique && density >= 0.75)
166167
return GenerateWrapper(generator, genCfg, new KeyLengthStructure<string, TValue>(strProps), keys, values);
167168

168169
// Note: Experiments show it is at the ~500-element boundary that Conditional starts to become slower. Use 400 to be safe.

0 commit comments

Comments
 (0)