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

math/big: fast path for Cmp if same #30856

TuomLarsen opened this Issue Mar 14, 2019 · 2 comments


None yet
3 participants
Copy link

TuomLarsen commented Mar 14, 2019

I noticed that math/big.Int Cmp method does not have a fast path for the case if x and y are the same. Wouldn't it be beneficial to have there something along the lines:

if x == y {
    r = 0

Mul seems to do similar.

Same applies for big.Rat and big.Float.

@ALTree ALTree added this to the Go1.13 milestone Mar 15, 2019


This comment has been minimized.

Copy link

ALTree commented Mar 15, 2019

Note that Mul has that check because if the two numbers are the same you can do squaring, which is faster than a fully general multiplication. This is a fast-path that cannot be easily implemented by the caller since a fast squaring routine in not trivial to write. On the other hand, it's easy to avoid an Int.Cmp call when the pointers are the same, and you can do that in user code.


This comment has been minimized.

Copy link

josharian commented Mar 15, 2019

Seems reasonable, if it fires. You might try putting it in, adding some logging when it does/doesn’t fire, and then running make.bash. If it fires a non-trivial percentage of times it definitely seems worthwhile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.