aboutsummaryrefslogtreecommitdiff
path: root/src/strings/builder.go
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2018-01-09 18:08:43 +0000
committerBrad Fitzpatrick <bradfitz@golang.org>2018-01-09 22:01:28 +0000
commit484586c81a0196e42ac52f651bc56017ca454280 (patch)
tree69983a55b34bb5354421f3ace5a16848dc769fe7 /src/strings/builder.go
parentb26c88db2f338d6529e1021db59f0faa75af5934 (diff)
downloadgo-484586c81a0196e42ac52f651bc56017ca454280.tar.xz
strings: prevent copyCheck from forcing Builder to escape and allocate
All credit and blame goes to Ian for this suggestion, copied from the runtime. Fixes #23382 Updates #7921 Change-Id: I3d5a9ee4ab730c87e0f3feff3e7fceff9bcf9e18 Reviewed-on: https://go-review.googlesource.com/86976 Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/strings/builder.go')
-rw-r--r--src/strings/builder.go19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/strings/builder.go b/src/strings/builder.go
index 11bcee1dfc..ac58f34e1d 100644
--- a/src/strings/builder.go
+++ b/src/strings/builder.go
@@ -17,9 +17,26 @@ type Builder struct {
buf []byte
}
+// noescape hides a pointer from escape analysis. noescape is
+// the identity function but escape analysis doesn't think the
+// output depends on the input. noescape is inlined and currently
+// compiles down to zero instructions.
+// USE CAREFULLY!
+// This was copied from the runtime; see issues 23382 and 7921.
+//go:nosplit
+func noescape(p unsafe.Pointer) unsafe.Pointer {
+ x := uintptr(p)
+ return unsafe.Pointer(x ^ 0)
+}
+
func (b *Builder) copyCheck() {
if b.addr == nil {
- b.addr = b
+ // This hack works around a failing of Go's escape analysis
+ // that was causing b to escape and be heap allocated.
+ // See issue 23382.
+ // TODO: once issue 7921 is fixed, this should be reverted to
+ // just "b.addr = b".
+ b.addr = (*Builder)(noescape(unsafe.Pointer(b)))
} else if b.addr != b {
panic("strings: illegal use of non-zero Builder copied by value")
}