Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements for factor #1525

Merged
merged 15 commits into from
May 24, 2020
Merged

Performance improvements for factor #1525

merged 15 commits into from
May 24, 2020

Commits on May 24, 2020

  1. factor: Refactor (eheh) around a Factors datatype

    It is clearer to see what is going on, as opposed to passing around an
    unmarked `Vec<u64>`, and there is a single place to add invariants checks.
    
    This is also a more compact memory representation: each prime factor is
    represented only once, with an additional byte for multiplicity.  The
    performance impact is however not significant.
    nbraud committed May 24, 2020
    Configuration menu
    Copy the full SHA
    d9095a2 View commit details
    Browse the repository at this point in the history
  2. factor: Move each factorisation method to its own module

    Also decoupled the factorisation methods; now factor::factor contains
    the logic that chains the different algorithms and aggregates results.
    
    As a side-effect, rho::factor now performs extraneous allocations (as each
    recursive step creates a new `Factors` value, which is then aggregated into
    the previous one) but there is no significant performance impact.
    nbraud committed May 24, 2020
    Configuration menu
    Copy the full SHA
    a1b2522 View commit details
    Browse the repository at this point in the history
  3. factor::factor: Use u64::trailing_zero instead of iterated division

    No significant performance impact (most of the time is spent elsewhere),
    but an easy and satisfying fix nevertheless.
    nbraud committed May 24, 2020
    Configuration menu
    Copy the full SHA
    bc11e57 View commit details
    Browse the repository at this point in the history
  4. factor::factor: Short-circuit the fallback to Pollard's rho

    When the remainder is smaller than the max. entry in the table,
    it is guaranteed to be prime.
    nbraud committed May 24, 2020
    Configuration menu
    Copy the full SHA
    418fd61 View commit details
    Browse the repository at this point in the history
  5. factor::table: Remove extraneous calls to the primality test

    50% performance improvement on factoring all numbers between 2 and 10⁶.
    nbraud committed May 24, 2020
    Configuration menu
    Copy the full SHA
    1697406 View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    e1a6dbe View commit details
    Browse the repository at this point in the history
  7. factor::factor: Remove extraneous call to the primality test

    Another 6.97% runtime improvement
    nbraud committed May 24, 2020
    Configuration menu
    Copy the full SHA
    74054fe View commit details
    Browse the repository at this point in the history
  8. Configuration menu
    Copy the full SHA
    e3ecc81 View commit details
    Browse the repository at this point in the history
  9. factor::miller_rabbin: Refactor before extracting dividers

    Replace iterated division with u64::trailing_zeros, hoist the selection of `mul`
    out of the loop, another cool 49.5% runtime improvement.
    nbraud committed May 24, 2020
    Configuration menu
    Copy the full SHA
    6b9585b View commit details
    Browse the repository at this point in the history
  10. factor::miller_rabin: Extract dividers from the primality test

    Another 36% improvement.
    nbraud committed May 24, 2020
    Configuration menu
    Copy the full SHA
    8241037 View commit details
    Browse the repository at this point in the history
  11. format: Make clippy happy

    nbraud committed May 24, 2020
    Configuration menu
    Copy the full SHA
    29eb8fd View commit details
    Browse the repository at this point in the history
  12. Configuration menu
    Copy the full SHA
    30fd6a0 View commit details
    Browse the repository at this point in the history
  13. factor::rho: Small refactor

    nbraud committed May 24, 2020
    Configuration menu
    Copy the full SHA
    543c7b9 View commit details
    Browse the repository at this point in the history
  14. factor::miller_rabin: Avoid unecessary exponentiation

    Instead of computing a^r and a^(n-1) = a^(r 2ⁱ) separately,
    compute the latter by repeatedly squaring the former.
    
    33.6% performance improvement
    nbraud committed May 24, 2020
    Configuration menu
    Copy the full SHA
    36a2948 View commit details
    Browse the repository at this point in the history
  15. factor::Factors::add: Split up to work without NLL

    Co-authored-by: Roy Ivy III <rivy.dev@gmail.com>
    nbraud and rivy committed May 24, 2020
    Configuration menu
    Copy the full SHA
    4c3682a View commit details
    Browse the repository at this point in the history