Skip to content

Commit 1a5d0f8

Browse files
alexd765griesemer
authored andcommitted
math/big: reduce allocations in Karatsuba case of sqr
For #23221. Change-Id: If55dcf2e0706d6658f4a0863e3740437e008706c Reviewed-on: https://go-review.googlesource.com/114335 Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 3f2039e commit 1a5d0f8

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/math/big/nat.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,7 @@ func karatsubaSqr(z, x nat) {
512512
n := len(x)
513513

514514
if n&1 != 0 || n < karatsubaSqrThreshold || n < 2 {
515-
z = z[:2*n]
516-
basicSqr(z, x)
515+
basicSqr(z[:2*n], x)
517516
return
518517
}
519518

@@ -562,13 +561,14 @@ func (z nat) sqr(x nat) nat {
562561
if alias(z, x) {
563562
z = nil // z is an alias for x - cannot reuse
564563
}
565-
z = z.make(2 * n)
566564

567565
if n < basicSqrThreshold {
566+
z = z.make(2 * n)
568567
basicMul(z, x, x)
569568
return z.norm()
570569
}
571570
if n < karatsubaSqrThreshold {
571+
z = z.make(2 * n)
572572
basicSqr(z, x)
573573
return z.norm()
574574
}

0 commit comments

Comments
 (0)