Small optimizations

This commit is contained in:
mlemanczyk committed 2025-10-17 00:29:53 +02:00
1 parent e63fba4df1
commit 0a2e65e190
1 file changed
+19 -26
+19 -26
View File
@@ -940,45 +940,38 @@ internal static partial class PrimeOrderCalculator
index++; index++;
} }
Span<FactorEntry> span = factors.AsSpan(0, index); Array.Sort(factors, static (a, b) => a.Value.CompareTo(b.Value));
span.Sort(static (a, b) => a.Value.CompareTo(b.Value));
bool fullyFactored = cofactor == 1UL; bool fullyFactored = cofactor == 1UL;
result = PartialFactorResult.Rent(factors, cofactor, fullyFactored, index); result = PartialFactorResult.Rent(factors, cofactor, fullyFactored, index);
goto ReturnResult; goto ReturnResult;
} }
int actualCount = 0; // This will never happen in production code. We'll always get at least 1 factor
for (int i = 0; i < factorCount; i++) // if (factorCount == 0)
{ // {
if (primeSlots[i] != 0UL && exponentSlots[i] != 0) // if (cofactor == value)
{ // {
actualCount++; // result = PartialFactorResult.Rent(null, value, false, 0);
} // goto ReturnResult;
} // }
if (actualCount == 0) // result = PartialFactorResult.Rent(null, cofactor, cofactor == 1UL, 0);
{ // goto ReturnResult;
if (cofactor == value) // }
{
result = PartialFactorResult.Rent(null, value, false, 0);
goto ReturnResult;
}
result = PartialFactorResult.Rent(null, cofactor, cofactor == 1UL, 0); FactorEntry[] array = pool.Rent(factorCount);
goto ReturnResult;
}
FactorEntry[] array = pool.Rent(actualCount);
int arrayIndex = 0; int arrayIndex = 0;
for (int i = 0; i < factorCount; i++) for (int i = 0; i < factorCount; i++)
{ {
ulong primeValue = primeSlots[i]; ulong primeValue = primeSlots[i];
int exponentValue = exponentSlots[i]; int exponentValue = exponentSlots[i];
if (primeValue == 0UL || exponentValue == 0) // This will never happen on the execution path from production code
{ // if (primeValue == 0UL || exponentValue == 0)
continue; // {
} // throw new Exception("Prime value or exponent equals zero");
// continue;
// }
array[arrayIndex] = new FactorEntry(primeValue, exponentValue); array[arrayIndex] = new FactorEntry(primeValue, exponentValue);
arrayIndex++; arrayIndex++;