]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: reduce allocations in Karatsuba case of sqr
authorAlexander Döring <email@alexd.ch>
Wed, 23 May 2018 22:25:12 +0000 (00:25 +0200)
committerRobert Griesemer <gri@golang.org>
Wed, 23 May 2018 23:01:10 +0000 (23:01 +0000)
For #23221.

Change-Id: If55dcf2e0706d6658f4a0863e3740437e008706c
Reviewed-on: https://go-review.googlesource.com/114335
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/math/big/nat.go

index dc292b4e7c96abb2e823259f170ede328af8030a..a6f79edccc4397b9fbbeb30dc434c22c3010805f 100644 (file)
@@ -512,8 +512,7 @@ func karatsubaSqr(z, x nat) {
        n := len(x)
 
        if n&1 != 0 || n < karatsubaSqrThreshold || n < 2 {
-               z = z[:2*n]
-               basicSqr(z, x)
+               basicSqr(z[:2*n], x)
                return
        }
 
@@ -562,13 +561,14 @@ func (z nat) sqr(x nat) nat {
        if alias(z, x) {
                z = nil // z is an alias for x - cannot reuse
        }
-       z = z.make(2 * n)
 
        if n < basicSqrThreshold {
+               z = z.make(2 * n)
                basicMul(z, x, x)
                return z.norm()
        }
        if n < karatsubaSqrThreshold {
+               z = z.make(2 * n)
                basicSqr(z, x)
                return z.norm()
        }