aboutsummaryrefslogtreecommitdiff
path: root/src/math
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2020-04-14 12:30:47 -0700
committerBrad Fitzpatrick <bradfitz@golang.org>2020-04-14 20:50:19 +0000
commita55645fa3481413b335561924d8fa626ce440ad4 (patch)
treee3526773d6569bdb38f6c5552ec87940ec99e9d6 /src/math
parentab31e2749f89a65a6381348baa008a7f5010b8b5 (diff)
downloadgo-a55645fa3481413b335561924d8fa626ce440ad4.tar.gz
go-a55645fa3481413b335561924d8fa626ce440ad4.zip
math/big: don't use Float in init to help linker discard 162 KiB
Removes 162 KiB from binaries that don't use math/big.Float: -rwxr-xr-x 1 bradfitz bradfitz 1916590 Apr 14 12:21 x.after -rwxr-xr-x 1 bradfitz bradfitz 2082575 Apr 14 12:21 x.before No change in deps (this package already used sync). No change in benchmarks: name old time/op new time/op delta FloatSqrt/64-8 1.06µs ±10% 1.03µs ± 6% ~ (p=0.133 n=10+9) FloatSqrt/128-8 2.26µs ± 9% 2.28µs ± 9% ~ (p=0.460 n=10+8) FloatSqrt/256-8 2.29µs ± 5% 2.31µs ± 3% ~ (p=0.214 n=9+9) FloatSqrt/1000-8 5.82µs ± 3% 5.87µs ± 7% ~ (p=0.666 n=9+9) FloatSqrt/10000-8 56.4µs ± 5% 57.0µs ± 6% ~ (p=0.436 n=10+10) FloatSqrt/100000-8 1.34ms ± 8% 1.31ms ± 3% ~ (p=0.447 n=10+9) FloatSqrt/1000000-8 106ms ± 5% 107ms ± 7% ~ (p=0.315 n=10+10) name old alloc/op new alloc/op delta FloatSqrt/64-8 280B ± 0% 280B ± 0% ~ (all equal) FloatSqrt/128-8 504B ± 0% 504B ± 0% ~ (all equal) FloatSqrt/256-8 344B ± 0% 344B ± 0% ~ (all equal) FloatSqrt/1000-8 1.30kB ± 0% 1.30kB ± 0% ~ (all equal) FloatSqrt/10000-8 13.5kB ± 0% 13.5kB ± 0% ~ (p=0.403 n=10+10) FloatSqrt/100000-8 123kB ± 0% 123kB ± 0% ~ (p=0.393 n=10+10) FloatSqrt/1000000-8 1.84MB ± 7% 1.84MB ± 5% ~ (p=0.739 n=10+10) name old allocs/op new allocs/op delta FloatSqrt/64-8 8.00 ± 0% 8.00 ± 0% ~ (all equal) FloatSqrt/128-8 11.0 ± 0% 11.0 ± 0% ~ (all equal) FloatSqrt/256-8 5.00 ± 0% 5.00 ± 0% ~ (all equal) FloatSqrt/1000-8 6.00 ± 0% 6.00 ± 0% ~ (all equal) FloatSqrt/10000-8 6.00 ± 0% 6.00 ± 0% ~ (all equal) FloatSqrt/100000-8 6.00 ± 0% 6.00 ± 0% ~ (all equal) FloatSqrt/1000000-8 10.9 ±10% 10.8 ±17% ~ (p=0.974 n=10+10) Change-Id: I3337f1f531bf7b4fae192b9d90cd24ff2be14fea Reviewed-on: https://go-review.googlesource.com/c/go/+/228108 Reviewed-by: Robert Griesemer <gri@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/math')
-rw-r--r--src/math/big/floatconv.go2
-rw-r--r--src/math/big/sqrt.go20
2 files changed, 17 insertions, 5 deletions
diff --git a/src/math/big/floatconv.go b/src/math/big/floatconv.go
index 95e32d3319..57b7df3936 100644
--- a/src/math/big/floatconv.go
+++ b/src/math/big/floatconv.go
@@ -290,7 +290,7 @@ func ParseFloat(s string, base int, prec uint, mode RoundingMode) (f *Float, b i
return new(Float).SetPrec(prec).SetMode(mode).Parse(s, base)
}
-var _ fmt.Scanner = &floatZero // *Float must implement fmt.Scanner
+var _ fmt.Scanner = (*Float)(nil) // *Float must implement fmt.Scanner
// Scan is a support routine for fmt.Scanner; it sets z to the value of
// the scanned number. It accepts formats whose verbs are supported by
diff --git a/src/math/big/sqrt.go b/src/math/big/sqrt.go
index ac2094f28e..e11504ad07 100644
--- a/src/math/big/sqrt.go
+++ b/src/math/big/sqrt.go
@@ -4,12 +4,23 @@
package big
-import "math"
-
-var (
- three = NewFloat(3.0)
+import (
+ "math"
+ "sync"
)
+var threeOnce struct {
+ sync.Once
+ v *Float
+}
+
+func three() *Float {
+ threeOnce.Do(func() {
+ threeOnce.v = NewFloat(3.0)
+ })
+ return threeOnce.v
+}
+
// Sqrt sets z to the rounded square root of x, and returns it.
//
// If z's precision is 0, it is changed to x's precision before the
@@ -129,6 +140,7 @@ func (z *Float) sqrtInverse(x *Float) {
// t2 = t - g(t) = ½t(3 - xt²)
u := newFloat(z.prec)
v := newFloat(z.prec)
+ three := three()
ng := func(t *Float) *Float {
u.prec = t.prec
v.prec = t.prec