diff options
author | Matthew Dempsky <mdempsky@google.com> | 2023-08-16 22:57:12 -0700 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2023-08-17 19:37:36 +0000 |
commit | c8adb3004ff05654d98041c76c68b64e1d479782 (patch) | |
tree | a6e6a92c4c180a6b0ccf4bf975b681649c98370b /src/cmd/compile/internal/escape | |
parent | b805e18fbf6dee945236159d89cf3d29fcd541c4 (diff) | |
download | go-c8adb3004ff05654d98041c76c68b64e1d479782.tar.gz |
cmd/compile: enable zero-copy string->[]byte conversions
This CL enables the latent support for string->[]byte conversions
added go.dev/cl/520259.
One catch is that we need to make sure []byte("") evaluates to a
non-nil slice, even if "" is (nil, 0). This CL addresses that by
adding a "ptr != nil" check for OSTR2BYTESTMP, unless the NonNil flag
is set.
The existing uses of OSTR2BYTESTMP (which aren't concerned about
[]byte("") evaluating to nil) are updated to set this flag.
Fixes #2205.
Change-Id: I35a9cb16c164cd86156b7560915aba5108d8b523
Reviewed-on: https://go-review.googlesource.com/c/go/+/520395
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Diffstat (limited to 'src/cmd/compile/internal/escape')
-rw-r--r-- | src/cmd/compile/internal/escape/escape.go | 13 |
1 files changed, 4 insertions, 9 deletions
diff --git a/src/cmd/compile/internal/escape/escape.go b/src/cmd/compile/internal/escape/escape.go index 2882f9fda3..5f5dab31f7 100644 --- a/src/cmd/compile/internal/escape/escape.go +++ b/src/cmd/compile/internal/escape/escape.go @@ -345,16 +345,11 @@ func (b *batch) finish(fns []*ir.Func) { // If the result of a string->[]byte conversion is never mutated, // then it can simply reuse the string's memory directly. - // - // TODO(mdempsky): Enable in a subsequent CL. We need to ensure - // []byte("") evaluates to []byte{}, not []byte(nil). - if false { - if n, ok := n.(*ir.ConvExpr); ok && n.Op() == ir.OSTR2BYTES && !loc.hasAttr(attrMutates) { - if base.Flag.LowerM >= 1 { - base.WarnfAt(n.Pos(), "zero-copy string->[]byte conversion") - } - n.SetOp(ir.OSTR2BYTESTMP) + if n, ok := n.(*ir.ConvExpr); ok && n.Op() == ir.OSTR2BYTES && !loc.hasAttr(attrMutates) { + if base.Flag.LowerM >= 1 { + base.WarnfAt(n.Pos(), "zero-copy string->[]byte conversion") } + n.SetOp(ir.OSTR2BYTESTMP) } } } |