Skip to content

Optimize double-word integer factorization - #2783

Merged
fredrik-johansson merged 4 commits into
flintlib:mainfrom
fredrik-johansson:llfactor
Jul 26, 2026
Merged

Optimize double-word integer factorization#2783
fredrik-johansson merged 4 commits into
flintlib:mainfrom
fredrik-johansson:llfactor

Conversation

@fredrik-johansson

Copy link
Copy Markdown
Collaborator

This fixes most of the slowness of fmpz_factor in the 65-160 bit range observed in #2625. The speedup is largest around 80 bits where we gain nearly 6x on average for random integers. Performance is also slightly improved for 3-4 word integers.

Several changes:

  • Lower SQUFOF threshold to 72 bits
  • Optimize Pollard-Brent rho and ECM for double-word arithmetic and use these in appropriate ranges
  • Reduce initialization costs for qsieve
  • Optimize removal for double-word values in qsieve
  • Tweak tuning values for qsieve
  • Optimize modular square root in qsieve

Average time per factorization to factor 100 random integers of the given bit size:

          uniformly random                 semiprimes
bits        old        new  speedup         old        new  speedup
  72  2.740e-04  1.187e-04  2.310x    2.102e-03  2.218e-03  0.948x
  80  8.527e-04  1.431e-04  5.959x    3.445e-03  2.625e-03  1.312x
  88  1.559e-03  3.535e-04  4.411x    2.697e-03  1.923e-03  1.402x
  96  2.387e-03  6.841e-04  3.489x    3.678e-03  2.745e-03  1.340x
 104  2.642e-03  1.053e-03  2.509x    5.304e-03  4.094e-03  1.296x
 112  3.377e-03  1.354e-03  2.495x    8.032e-03  6.359e-03  1.263x
 120  4.202e-03  2.161e-03  1.945x    1.308e-02  1.163e-02  1.125x
 128  6.392e-03  3.862e-03  1.655x    2.366e-02  1.741e-02  1.359x
 136  9.211e-03  6.904e-03  1.334x    3.489e-02  2.818e-02  1.238x
 144  1.471e-02  1.190e-02  1.236x    6.179e-02  5.237e-02  1.180x
 152  1.703e-02  1.479e-02  1.152x    9.785e-02  8.795e-02  1.113x
 160  2.768e-02  2.527e-02  1.095x    2.010e-01  1.511e-01  1.330x
 168  4.596e-02  4.154e-02  1.106x    3.128e-01  2.504e-01  1.249x
 176  5.872e-02  5.031e-02  1.167x    5.764e-01  4.122e-01  1.398x
 184  9.741e-02  7.924e-02  1.229x    9.098e-01  6.880e-01  1.323x
 192  1.359e-01  1.170e-01  1.162x    1.504e+00  1.356e+00  1.109x

Worst-case time for the same 100 inputs:

          uniformly random                 semiprimes
bits        old        new  speedup         old        new  speedup
  72  5.800e-03  4.600e-03  1.261x    1.900e-02  1.900e-02  1.000x
  80  6.700e-03  2.100e-03  3.190x    6.300e-03  5.000e-03  1.260x
  88  1.000e-02  4.100e-03  2.439x    4.000e-03  3.400e-03  1.176x
  96  7.200e-03  3.700e-03  1.946x    4.400e-03  3.500e-03  1.257x
 104  7.200e-03  5.300e-03  1.358x    7.800e-03  6.500e-03  1.200x
 112  1.000e-02  8.100e-03  1.235x    1.100e-02  8.900e-03  1.236x
 120  1.500e-02  1.400e-02  1.071x    2.400e-02  2.100e-02  1.143x
 128  2.400e-02  2.100e-02  1.143x    4.000e-02  3.100e-02  1.290x
 136  4.100e-02  3.700e-02  1.108x    6.400e-02  5.500e-02  1.164x
 144  6.000e-02  5.400e-02  1.111x    2.160e-01  1.980e-01  1.091x
 152  1.020e-01  9.700e-02  1.052x    1.670e-01  1.560e-01  1.071x
 160  1.900e-01  1.820e-01  1.044x    3.770e-01  2.590e-01  1.456x
 168  3.140e-01  2.550e-01  1.231x    5.810e-01  4.990e-01  1.164x
 176  6.070e-01  4.480e-01  1.355x    1.020e+00  7.500e-01  1.360x
 184  1.248e+00  8.900e-01  1.402x    1.398e+00  2.649e+00  0.528x
 192  1.730e+00  1.576e+00  1.098x    3.318e+00  3.079e+00  1.078x

Tried before to get Opus 4.8 to do this, but it persistently failed to identify the real bottlenecks/solutions; Fable refused to touch this problem (work on integer factorization apparently triggers its safety guardrails); finally Opus 5 got the job done in a few hours.

@fredrik-johansson

Copy link
Copy Markdown
Collaborator Author

Opus also identified a trivial improvement to fmpz_is_prime in the Pocklington and Morrison tests. Time to run fmpz_is_prime on 10 random primes of each bit size:

bits        old       new   speedup
  80  2.960e-05  2.990e-05  0.990x
  88  5.230e-03  4.880e-03  1.072x
  96  5.690e-03  5.350e-03  1.064x
 104  1.130e-02  1.060e-02  1.066x
 112  1.210e-02  1.140e-02  1.061x
 120  1.570e-02  1.470e-02  1.068x
 128  1.900e-02  1.790e-02  1.061x
 136  2.700e-02  2.510e-02  1.076x
 144  2.490e-02  2.340e-02  1.064x
 152  5.120e-02  3.580e-02  1.430x
 160  4.310e-02  3.850e-02  1.119x
 168  8.140e-02  4.580e-02  1.777x
 176  1.030e-01  4.220e-02  2.441x
 184  1.010e-01  4.770e-02  2.117x
 192  9.100e-02  5.740e-02  1.585x
 200  1.200e-01  6.980e-02  1.719x
 208  1.230e-01  6.740e-02  1.825x
 216  1.340e-01  7.700e-02  1.740x
 224  1.320e-01  8.480e-02  1.557x
 232  1.400e-01  8.800e-02  1.591x
 240  1.960e-01  1.100e-01  1.782x
 248  1.560e-01  1.140e-01  1.368x
 256  2.120e-01  1.240e-01  1.710x
 264  1.820e-01  1.450e-01  1.255x
 272  2.040e-01  1.360e-01  1.500x
 280  2.770e-01  1.610e-01  1.720x
 288  2.680e-01  1.740e-01  1.540x
 296  2.690e-01  1.840e-01  1.462x
 304  2.850e-01  1.950e-01  1.462x
 312  3.030e-01  2.070e-01  1.464x
 320  3.210e-01  2.240e-01  1.433x
 328  2.950e-01  2.540e-01  1.161x
 336  3.470e-01  2.690e-01  1.290x
 344  3.700e-01  2.850e-01  1.298x
 352  4.230e-01  3.350e-01  1.263x
 360  4.920e-01  3.750e-01  1.312x
 368  4.770e-01  3.590e-01  1.329x
 376  5.250e-01  4.170e-01  1.259x
 384  5.240e-01  4.320e-01  1.213x
 392  5.690e-01  4.730e-01  1.203x
 400  6.240e-01  5.220e-01  1.195x
 408  6.190e-01  5.270e-01  1.175x
 416  6.350e-01  5.480e-01  1.159x
 424  7.110e-01  5.770e-01  1.232x
 432  7.480e-01  6.280e-01  1.191x
 440  7.760e-01  6.590e-01  1.178x
 448  8.270e-01  7.060e-01  1.171x
 456  8.730e-01  7.590e-01  1.150x
 464  9.230e-01  7.980e-01  1.157x
 472  9.260e-01  7.960e-01  1.163x
 480  9.600e-01  8.520e-01  1.127x
 488  9.960e-01  9.020e-01  1.104x
 496  1.058e+00  9.180e-01  1.153x
 504  1.068e+00  9.540e-01  1.119x
 512  1.134e+00  1.014e+00  1.118x

@fredrik-johansson
fredrik-johansson merged commit dc9f739 into flintlib:main Jul 26, 2026
13 checks passed
@fredrik-johansson
fredrik-johansson deleted the llfactor branch July 27, 2026 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant