Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Commit

Permalink
Remove the digit table optimization
Browse files Browse the repository at this point in the history
The benefit was fairly marginal. We can get some of it by doing only the
"avoid 64-bit divisions" optimization and not take the size hit.

name                                     old time/op  new time/op  delta
AppendFloat64/0e+00-12                   3.30ns ± 2%  3.30ns ± 1%    ~     (p=0.881 n=5+5)
AppendFloat64/1e+00-12                   15.3ns ± 1%  15.3ns ± 1%    ~     (p=0.635 n=5+5)
AppendFloat64/3e-01-12                   50.0ns ± 1%  50.8ns ± 1%  +1.44%  (p=0.016 n=5+5)
AppendFloat64/1e+06-12                   20.8ns ± 1%  21.2ns ± 1%  +2.02%  (p=0.024 n=5+5)
AppendFloat64/-1.2345e+02-12             49.5ns ± 1%  50.0ns ± 1%  +1.01%  (p=0.032 n=5+5)
AppendFloat64/6.226662346353213e-309-12  39.1ns ± 1%  41.4ns ± 2%  +5.89%  (p=0.008 n=5+5)
  • Loading branch information
cespare committed Jan 13, 2019
1 parent 463ddad commit 06e9fdf
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 71 deletions.
9 changes: 0 additions & 9 deletions maketables.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,6 @@ const (
func main() {
b := bytes.NewBuffer(header)

fmt.Fprintln(b, "var twoDigits = [200]byte{")
for i := 0; i < 10; i++ {
for j := 0; j < 10; j++ {
fmt.Fprintf(b, "'%c','%c',", '0'+i, '0'+j)
}
fmt.Fprintln(b)
}
fmt.Fprintln(b, "\n}")

fmt.Fprintf(b, "const pow5NumBits32 = %d\n", pow5NumBits32)
fmt.Fprintln(b, "var pow5Split32 = [...]uint64{")
for i := int64(0); i < posTableSize32; i++ {
Expand Down
5 changes: 0 additions & 5 deletions ryu.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,6 @@ func pow5Bits(e int32) int32 {
return int32((uint32(e)*1217359)>>19 + 1)
}

func copyTwoDigits(b []byte, d uint) {
b[0] = twoDigits[d]
b[1] = twoDigits[d+1]
}

// FIXME(caleb): Document how these are optimized.

func boolToUint32(b bool) uint32 {
Expand Down
1 change: 0 additions & 1 deletion ryu32.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func (d dec32) append(b []byte, neg bool) []byte {
}

// Print the decimal digits.
// FIXME: optimize
n := len(b)
b = append(b, make([]byte, bufLen)...)
for i := 0; i < outLen-1; i++ {
Expand Down
54 changes: 11 additions & 43 deletions ryu64.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,59 +53,27 @@ func (d dec64) append(b []byte, neg bool) []byte {
} else {
b = append(b, make([]byte, bufLen)...)
}
// The code below is equivalent to the following:
// for i := 0; i < outLen-1; i++ {
// b[n+outLen-i] = '0' + byte(out%10)
// out /= 10
// }
// b[n] = '0' + byte(out%10)

// We prefer 32-bit divisions even on 64-bit platforms.
// Avoid expensive 64-bit divisions.
// We have at most 17 digits, and uint32 can store 9 digits.
// If the output doesn't fit into a uint32, cut off 8 digits
// so the rest will fit into a uint32.
var i int
if out>>32 > 0 {
// FIXME: bits.Div?
q, r := out/1e8, out%1e8
out = q

c := r % 1e4
r /= 1e4
d := r % 1e4
c0 := uint(c%100) << 1
c1 := uint(c/100) << 1
d0 := uint(d%100) << 1
d1 := uint(d/100) << 1
copyTwoDigits(b[n+outLen-i-7:], d1)
copyTwoDigits(b[n+outLen-i-5:], d0)
copyTwoDigits(b[n+outLen-i-3:], c1)
copyTwoDigits(b[n+outLen-i-1:], c0)
i += 8
var out32 uint32
out, out32 = out/1e8, uint32(out%1e8)
for ; i < 8; i++ {
b[n+outLen-i] = '0' + byte(out32%10)
out32 /= 10
}
}
out32 := uint32(out)
for ; out32 >= 1e4; i += 4 {
c := out32 % 1e4
out32 /= 1e4
c0 := uint(c%100) << 1
c1 := uint(c/100) << 1
copyTwoDigits(b[n+outLen-i-1:], c0)
copyTwoDigits(b[n+outLen-i-3:], c1)
}
if out32 >= 100 {
c := uint(out32%100) << 1
out32 /= 100
copyTwoDigits(b[n+outLen-i-1:], c)
i += 2
}
if out32 >= 10 {
c := out32 << 1
// The dot goes between the digits.
b[n+outLen-i] = twoDigits[c+1]
b[n] = twoDigits[c]
} else {
b[n] = '0' + byte(out32)
for ; i < outLen-1; i++ {
b[n+outLen-i] = '0' + byte(out32%10)
out32 /= 10
}
b[n] = '0' + byte(out32%10)

// Print the '.' if needed.
if outLen > 1 {
Expand Down
13 changes: 0 additions & 13 deletions tables.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 06e9fdf

Please sign in to comment.