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

Open
TuomLarsen opened this issue Mar 14, 2019 · 5 comments

Comments

Projects
None yet
6 participants
@TuomLarsen
Copy link

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
    return
}

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

@ALTree

This comment has been minimized.

Copy link
Member

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.

@josharian

This comment has been minimized.

Copy link
Contributor

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.

@rsc

This comment has been minimized.

Copy link
Contributor

commented May 1, 2019

Spoke to @griesemer; go ahead, for Go 1.14.

@rsc rsc added the NeedsFix label May 1, 2019

@rsc rsc modified the milestones: Go1.13, Go1.14 May 1, 2019

@gopherbot gopherbot removed the NeedsDecision label May 1, 2019

@IllyaYalovyy

This comment has been minimized.

Copy link

commented May 25, 2019

Is anyone working on it? I'm looking to start contributing, and need an easy task for a warmup.

@gopherbot

This comment has been minimized.

Copy link

commented May 26, 2019

Change https://golang.org/cl/178957 mentions this issue: math/big: fast path for Cmp if same

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.