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

factor: Refactor the factoring logic #1558

Merged
merged 9 commits into from
Jul 15, 2020
Merged

factor: Refactor the factoring logic #1558

merged 9 commits into from
Jul 15, 2020

Conversation

nbraud
Copy link
Contributor

@nbraud nbraud commented Jun 24, 2020

  • Separate the data structure and factoring logic (uu_factor::factor) from the CLI code and I/O.
    Improves maintainability and readability.
  • Avoid moving data around (~1% runtime improvements, reduction in memory usage)
  • Refactor things so factoring algorithms return a factor (or None) to factor.
    This refactoring will enable implementing multiple factoring methods (and dynamically selecting the best one based on smoothness etc.)

On its own, the performance improvement compared to master is pretty negligible, but when combined with #1554 we get another ~15% improvement.

@nbraud
Copy link
Contributor Author

nbraud commented Jun 24, 2020

@Arcterus (or any other maintainer) : this fails to build because of the uucore_procs::main! invocation, and I have no idea where that macro is documented; could you suggest a fix so I can make further progress on this?

@Arcterus
Copy link
Collaborator

That’s in the uucore repo at the moment. I think the path is something like src/uucore_procs/src/lib.rs or something like that. If you can wait until tomorrow (or I guess later today at this point...), I can suggest a fix.

@nbraud
Copy link
Contributor Author

nbraud commented Jun 24, 2020

If you can wait until tomorrow (or I guess later today at this point...), I can suggest a fix.

Yes, don't stress out about it :)
It's not as if I didn't have another draft PR to work on, or a bunch of other local branches 😅

@nbraud nbraud marked this pull request as ready for review June 25, 2020 02:37
@nbraud
Copy link
Contributor Author

nbraud commented Jun 25, 2020

I think this is ready for review as-is.

I did not (yet) move the factoring functionality to its own crate, as I realised it's not obvious where to put it, how to integrate with the release process for coreutils, etc...

@nbraud
Copy link
Contributor Author

nbraud commented Jun 30, 2020

Found a couple minor improvements, should now be clean on Clippy.

I'm not doing the last change I mentioned (making find_factor return an Option<u64> instead of looping until success) quite yet, as it doesn't make sense without also implementing another factor-finding algorithm (which is entirely out-of-scope for this PR)

@Arcterus
Copy link
Collaborator

Arcterus commented Jul 1, 2020

I’ll try to review this later today.

No functional change, but prepares a coming optimisation.
Instead, the same `Factors` object is passed around through the execution.
~10% faster.
Useful for printing out in-progress factorisations when debugging.
Non-prime numbers, such as 0 or 1, shouldn't be inserted in the factorisation.
Montgomery only works for odd n, so attempting to construct an instance
for an even number results in a panic!

The most obvious solution is to special-case even numbers.
@nbraud
Copy link
Contributor Author

nbraud commented Jul 4, 2020

I just rebased on master and added further testsuite improvements, shaking a couple more bugs lose (one of which was also caught in #1554).

Regarding performance, the improvement compared to master is pretty negligible (~1% on its own), but when combined with #1554 we get another ~15% improvement (compared to #1554 alone, which is already 2× faster than master)

Copy link
Sponsor Contributor

@sylvestre sylvestre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.
Approving to avoid having a PR staying for too long and because it has tests & great coverage.

@sylvestre sylvestre merged commit 1fb2e89 into uutils:master Jul 15, 2020
@nbraud nbraud deleted the factor/faster/centralise_logic branch July 15, 2020 22:08
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.

3 participants