diff options
author | Dmitry Vyukov <dvyukov@google.com> | 2015-01-28 08:42:20 +0300 |
---|---|---|
committer | Dmitry Vyukov <dvyukov@google.com> | 2015-01-28 20:37:20 +0000 |
commit | 4ce4d8b2c4ffb0378a246b26815e9e27d077670a (patch) | |
tree | 47a9ab04b5123b069d3f868be8590b14314cb18c /src/runtime/string_test.go | |
parent | e6fac08146df323eb95f46508bef937cdfb802fd (diff) | |
download | go-4ce4d8b2c4ffb0378a246b26815e9e27d077670a.tar.gz go-4ce4d8b2c4ffb0378a246b26815e9e27d077670a.zip |
cmd/gc: allocate stack buffer for ORUNESTR
If result of string(i) does not escape,
allocate a [4]byte temp on stack for it.
Change-Id: If31ce9447982929d5b3b963fd0830efae4247c37
Reviewed-on: https://go-review.googlesource.com/3411
Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/runtime/string_test.go')
-rw-r--r-- | src/runtime/string_test.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/runtime/string_test.go b/src/runtime/string_test.go index 249f431e18..27a44ad645 100644 --- a/src/runtime/string_test.go +++ b/src/runtime/string_test.go @@ -186,3 +186,38 @@ func TestStringOnStack(t *testing.T) { t.Fatalf("want: '%v', got '%v'", want, s) } } + +func TestIntString(t *testing.T) { + // Non-escaping result of intstring. + s := "" + for i := 0; i < 4; i++ { + s += string(i+'0') + string(i+'0'+1) + } + if want := "01122334"; s != want { + t.Fatalf("want '%v', got '%v'", want, s) + } + + // Escaping result of intstring. + var a [4]string + for i := 0; i < 4; i++ { + a[i] = string(i + '0') + } + s = a[0] + a[1] + a[2] + a[3] + if want := "0123"; s != want { + t.Fatalf("want '%v', got '%v'", want, s) + } +} + +func TestIntStringAllocs(t *testing.T) { + unknown := '0' + n := testing.AllocsPerRun(1000, func() { + s1 := string(unknown) + s2 := string(unknown + 1) + if s1 == s2 { + t.Fatalf("bad") + } + }) + if n != 0 { + t.Fatalf("want 0 allocs, got %v", n) + } +} |