]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: fix Malloc test
authorRob Pike <r@golang.org>
Tue, 17 Jan 2012 23:42:02 +0000 (15:42 -0800)
committerRob Pike <r@golang.org>
Tue, 17 Jan 2012 23:42:02 +0000 (15:42 -0800)
We need to avoid allocating an extra word for the interface value
passing the floating-point value as an interface{}. It's easy.

Fixes #2722.

R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/5553044

src/pkg/fmt/fmt_test.go

index d733721aff7e63dddea0032a926ed4f92ac18e54..2f92f947cd8a6d998440f072e37b33c1765dad36 100644 (file)
@@ -509,16 +509,18 @@ func BenchmarkSprintfFloat(b *testing.B) {
 var mallocBuf bytes.Buffer
 
 var mallocTest = []struct {
-       max  int
-       desc string
-       fn   func()
+       count int
+       desc  string
+       fn    func()
 }{
        {0, `Sprintf("")`, func() { Sprintf("") }},
        {1, `Sprintf("xxx")`, func() { Sprintf("xxx") }},
        {1, `Sprintf("%x")`, func() { Sprintf("%x", 7) }},
        {2, `Sprintf("%s")`, func() { Sprintf("%s", "hello") }},
        {1, `Sprintf("%x %x")`, func() { Sprintf("%x %x", 7, 112) }},
-       {2, `Sprintf("%g")`, func() { Sprintf("%g", 3.14159) }}, // TODO: should be 1. See Issue 2722.
+       // For %g we use a float32, not float64, to guarantee passing the argument
+       // does not need to allocate memory to store the result in a pointer-sized word.
+       {2, `Sprintf("%g")`, func() { Sprintf("%g", float32(3.14159)) }},
        {0, `Fprintf(buf, "%x %x %x")`, func() { mallocBuf.Reset(); Fprintf(&mallocBuf, "%x %x %x", 7, 8, 9) }},
        {1, `Fprintf(buf, "%s")`, func() { mallocBuf.Reset(); Fprintf(&mallocBuf, "%s", "hello") }},
 }
@@ -535,8 +537,8 @@ func TestCountMallocs(t *testing.T) {
                }
                runtime.UpdateMemStats()
                mallocs += runtime.MemStats.Mallocs
-               if mallocs/N > uint64(mt.max) {
-                       t.Errorf("%s: expected at most %d mallocs, got %d", mt.desc, mt.max, mallocs/N)
+               if mallocs/N > uint64(mt.count) {
+                       t.Errorf("%s: expected %d mallocs, got %d", mt.desc, mt.count, mallocs/N)
                }
        }
 }