1
0
mirror of https://github.com/golang/go synced 2024-11-17 02:14:42 -07:00
go/src
Alberto Donizetti 010579c237 math/big: allocate less in Float.Sqrt
The Newton sqrtInverse procedure we use to compute Float.Sqrt should
not allocate a number of times proportional to the number of Newton
iterations we need to reach the desired precision.

At the beginning the function the target precision is known, so even
if we do want to perform the early steps at low precisions (to save
time), it's still possible to pre-allocate larger backing arrays, both
for the temp variables in the loop and the variable that'll hold the
final result.

There's one complication. At the following line:

  u.Sub(three, u)

the Sub method will allocate, because the receiver aliases one of the
arguments, and the large backing array we initially allocated for u
will be replaced by a smaller one allocated by Sub. We can work around
this by introducing a second temp variable u2 that we use to hold the
Sub call result.

Overall, the sqrtInverse procedure still allocates a number of times
proportional to the number of Newton steps, because unfortunately a
few of the Mul calls in the Newton function allocate; but at least we
allocate less in the function itself.

FloatSqrt/256-4        1.97µs ± 1%    1.84µs ± 1%   -6.61%  (p=0.000 n=8+8)
FloatSqrt/1000-4       4.80µs ± 3%    4.28µs ± 1%  -10.78%  (p=0.000 n=8+8)
FloatSqrt/10000-4      40.0µs ± 1%    38.3µs ± 1%   -4.15%  (p=0.000 n=8+8)
FloatSqrt/100000-4      955µs ± 1%     932µs ± 0%   -2.49%  (p=0.000 n=8+7)
FloatSqrt/1000000-4    79.8ms ± 1%    79.4ms ± 1%     ~     (p=0.105 n=8+8)

name                 old alloc/op   new alloc/op   delta
FloatSqrt/256-4          816B ± 0%      512B ± 0%  -37.25%  (p=0.000 n=8+8)
FloatSqrt/1000-4       2.50kB ± 0%    1.47kB ± 0%  -41.03%  (p=0.000 n=8+8)
FloatSqrt/10000-4      23.5kB ± 0%    18.2kB ± 0%  -22.62%  (p=0.000 n=8+8)
FloatSqrt/100000-4      251kB ± 0%     173kB ± 0%  -31.26%  (p=0.000 n=8+8)
FloatSqrt/1000000-4    4.61MB ± 0%    2.86MB ± 0%  -37.90%  (p=0.000 n=8+8)

name                 old allocs/op  new allocs/op  delta
FloatSqrt/256-4          12.0 ± 0%       8.0 ± 0%  -33.33%  (p=0.000 n=8+8)
FloatSqrt/1000-4         19.0 ± 0%       9.0 ± 0%  -52.63%  (p=0.000 n=8+8)
FloatSqrt/10000-4        35.0 ± 0%      14.0 ± 0%  -60.00%  (p=0.000 n=8+8)
FloatSqrt/100000-4       55.0 ± 0%      23.0 ± 0%  -58.18%  (p=0.000 n=8+8)
FloatSqrt/1000000-4       122 ± 0%        75 ± 0%  -38.52%  (p=0.000 n=8+8)

Change-Id: I950dbf61a40267a6cca82ae72524c3024bcb149c
Reviewed-on: https://go-review.googlesource.com/87659
Reviewed-by: Robert Griesemer <gri@golang.org>
2018-03-08 19:12:35 +00:00
..
archive
bufio
builtin
bytes internal/bytealg: move short string Index implementations into bytealg 2018-03-04 19:49:44 +00:00
cmd math/big: speedup nat.setBytes for bigger slices 2018-03-08 18:50:10 +00:00
compress
container
context
crypto crypto/aes: optimize arm64 AES implementation 2018-03-06 00:44:29 +00:00
database/sql database/sql: fix typo in comment 2018-03-07 15:42:31 +00:00
debug debug/pe: use bytes.IndexByte instead of a loop 2018-03-07 16:12:08 +00:00
encoding encoding/csv: avoid mangling invalid UTF-8 in Writer 2018-03-08 03:26:22 +00:00
errors
expvar
flag
fmt fmt: change some unexported method names to camel case 2018-02-27 20:12:04 +00:00
go go/build: correct value of .Doc field 2018-03-07 14:35:52 +00:00
hash crypto: remove hand encoded amd64 instructions 2018-03-01 19:20:53 +00:00
html
image
index/suffixarray
internal internal/poll: if poller init fails, assume blocking mode 2018-03-06 23:21:25 +00:00
io
log
math math/big: allocate less in Float.Sqrt 2018-03-08 19:12:35 +00:00
mime
net net/http: panic when a nil handler is passed to (*ServeMux)HandleFunc 2018-03-08 18:11:41 +00:00
os os: use WIN32_FIND_DATA.Reserved0 to identify symlinks 2018-03-07 08:51:04 +00:00
path path/filepath: use a temp dir in path_test.go 2018-03-05 23:38:39 +00:00
plugin
reflect
regexp
runtime runtime: use systemstack around throw in sysSigaction 2018-03-08 16:35:53 +00:00
sort
strconv
strings internal/bytealg: move short string Index implementations into bytealg 2018-03-04 19:49:44 +00:00
sync
syscall runtime: rename vdso symbols to use camel case 2018-03-05 19:12:32 +00:00
testing testing: gracefully handle subtest failing parent’s T 2018-03-01 10:17:22 +00:00
text
time time: add support for parsing timezones denoted by sign and offset 2018-03-08 17:15:35 +00:00
unicode
unsafe
vendor/golang_org/x
all.bash
all.bat
all.rc
androidtest.bash androidtest.bash: don't require GOARCH set 2018-03-07 14:01:43 +00:00
bootstrap.bash
buildall.bash
clean.bash
clean.bat
clean.rc
cmp.bash
iostest.bash iostest.bash: don't build std library twice 2018-03-06 16:08:20 +00:00
make.bash
make.bat
Make.dist
make.rc
naclmake.bash
nacltest.bash
race.bash
race.bat
run.bash
run.bat
run.rc