summaryrefslogtreecommitdiff
path: root/test/escape2n.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2015-01-21 17:37:59 +0300
committerDmitry Vyukov <dvyukov@google.com>2015-01-28 20:12:38 +0000
commite6fac08146df323eb95f46508bef937cdfb802fd (patch)
tree65760c0480aae6cfdbd00e2013fc39a51acd6354 /test/escape2n.go
parent13aff7831d32c80b98ede611f1ffb0476f16ec51 (diff)
downloadgo-git-e6fac08146df323eb95f46508bef937cdfb802fd.tar.gz
cmd/gc: allocate buffers for non-escaped strings on stack
Currently we always allocate string buffers in heap. For example, in the following code we allocate a temp string just for comparison: if string(byteSlice) == "abc" { ... } This change extends escape analysis to cover []byte->string conversions and string concatenation. If the result of operations does not escape, compiler allocates a small buffer on stack and passes it to slicebytetostring and concatstrings. Then runtime uses the buffer if the result fits into it. Size of the buffer is 32 bytes. There is no fundamental theory behind this number. Just an observation that on std lib tests/benchmarks frequency of string allocation is inversely proportional to string length; and there is significant number of allocations up to length 32. benchmark old allocs new allocs delta BenchmarkFprintfBytes 2 1 -50.00% BenchmarkDecodeComplex128Slice 318 316 -0.63% BenchmarkDecodeFloat64Slice 318 316 -0.63% BenchmarkDecodeInt32Slice 318 316 -0.63% BenchmarkDecodeStringSlice 2318 2316 -0.09% BenchmarkStripTags 11 5 -54.55% BenchmarkDecodeGray 111 102 -8.11% BenchmarkDecodeNRGBAGradient 200 188 -6.00% BenchmarkDecodeNRGBAOpaque 165 152 -7.88% BenchmarkDecodePaletted 319 309 -3.13% BenchmarkDecodeRGB 166 157 -5.42% BenchmarkDecodeInterlacing 279 268 -3.94% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% BenchmarkGoLookupIPWithBrokenNameServer 245 226 -7.76% BenchmarkClientServerParallel4 62 61 -1.61% BenchmarkClientServerParallel64 62 61 -1.61% BenchmarkClientServerParallelTLS4 79 78 -1.27% BenchmarkClientServerParallelTLS64 112 111 -0.89% benchmark old ns/op new ns/op delta BenchmarkFprintfBytes 381 311 -18.37% BenchmarkStripTags 2615 2351 -10.10% BenchmarkDecodeNRGBAGradient 3715887 3635096 -2.17% BenchmarkDecodeNRGBAOpaque 3047645 2928644 -3.90% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% Change-Id: I9ec01da816945c3329d7be3c7794b520418c3f99 Reviewed-on: https://go-review.googlesource.com/3120 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'test/escape2n.go')
-rw-r--r--test/escape2n.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/test/escape2n.go b/test/escape2n.go
index e514bde59e..6769906e30 100644
--- a/test/escape2n.go
+++ b/test/escape2n.go
@@ -1556,3 +1556,77 @@ func quux(sp *string, bp *[]byte) { // ERROR "sp does not escape" "bp does not e
*sp = (*sp)[1:2] // ERROR "quux ignoring self-assignment to \*sp"
*bp = (*bp)[1:2] // ERROR "quux ignoring self-assignment to \*bp"
}
+
+type StructWithString struct {
+ p *int
+ s string
+}
+
+// This is escape analysis false negative.
+// We assign the pointer to x.p but leak x.s. Escape analysis coarsens flows
+// to just x, and thus &i looks escaping.
+func fieldFlowTracking() {
+ var x StructWithString
+ i := 0 // ERROR "moved to heap: i"
+ x.p = &i // ERROR "&i escapes to heap"
+ sink = x.s
+}
+
+// String operations.
+
+func slicebytetostring0() {
+ b := make([]byte, 20) // ERROR "does not escape"
+ s := string(b) // ERROR "string\(b\) does not escape"
+ _ = s
+}
+
+func slicebytetostring1() {
+ b := make([]byte, 20) // ERROR "does not escape"
+ s := string(b) // ERROR "string\(b\) does not escape"
+ s1 := s[0:1]
+ _ = s1
+}
+
+func slicebytetostring2() {
+ b := make([]byte, 20) // ERROR "does not escape"
+ s := string(b) // ERROR "string\(b\) escapes to heap"
+ s1 := s[0:1] // ERROR "moved to heap: s1"
+ sink = &s1 // ERROR "&s1 escapes to heap"
+}
+
+func slicebytetostring3() {
+ b := make([]byte, 20) // ERROR "does not escape"
+ s := string(b) // ERROR "string\(b\) escapes to heap"
+ s1 := s[0:1]
+ sink = s1
+}
+
+func addstr0() {
+ s0 := "a"
+ s1 := "b"
+ s := s0 + s1 // ERROR "s0 \+ s1 does not escape"
+ _ = s
+}
+
+func addstr1() {
+ s0 := "a"
+ s1 := "b"
+ s := "c"
+ s += s0 + s1 // ERROR "s0 \+ s1 does not escape"
+ _ = s
+}
+
+func addstr2() {
+ b := make([]byte, 20) // ERROR "does not escape"
+ s0 := "a"
+ s := string(b) + s0 // ERROR "string\(b\) does not escape" "string\(b\) \+ s0 does not escape"
+ _ = s
+}
+
+func addstr3() {
+ s0 := "a"
+ s1 := "b"
+ s := s0 + s1 // ERROR "s0 \+ s1 escapes to heap"
+ s2 := s[0:1]
+ sink = s2
+}