1
0
mirror of https://github.com/golang/go synced 2024-10-01 18:38:34 -06:00

cmd/internal/gc: add internConcat for alloc-free string concatenation

This is a follow-up to review comments on CL 7696.

I believe that this includes the first regular Go test in the compiler.

No functional changes. Passes toolstash -cmp.

Change-Id: Id45f51aa664c5d52ece2a61cd7d8417159ce3cf0
Reviewed-on: https://go-review.googlesource.com/7820
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Josh Bleecher Snyder 2015-03-19 09:28:02 -07:00
parent 7274b1f6c9
commit 42fcc6fea0
4 changed files with 44 additions and 12 deletions

View File

@ -1511,6 +1511,15 @@ func internString(b []byte) string {
return s return s
} }
func internConcat(ss ...string) string {
const bufsiz = 128 // big enough for most compiler uses; must be constant to avoid heap alloc
b := make([]byte, 0, bufsiz)
for _, s := range ss {
b = append(b, s...)
}
return internString(b)
}
func more(pp *string) bool { func more(pp *string) bool {
p := *pp p := *pp
for p != "" && yy_isspace(int(p[0])) { for p != "" && yy_isspace(int(p[0])) {

View File

@ -0,0 +1,22 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gc
import "testing"
func TestInternConcat(t *testing.T) {
fromKind := "T"
toKind := "E"
var s string
n := testing.AllocsPerRun(100, func() {
s = internConcat("conv", fromKind, "2", toKind)
})
if s != "convT2E" {
t.Fatalf("internConcat(\"conv\", \"T\", \"2\", \"E\")=%q want %q", s, "convT2E")
}
if n > 0 {
t.Errorf("internConcat allocs per run=%f", n)
}
}

View File

@ -3574,10 +3574,10 @@ func isdirectiface(t *Type) bool {
return false return false
} }
// type2IET returns "T" if t is a concrete type, // IET returns "T" if t is a concrete type, "I" if t is an interface type, and
// "I" if t is an interface type, and "E" if t is an empty interface type. // "E" if t is an empty interface type.
// It is used to build calls to the conv* and assert* runtime routines. // It is used to build calls to the conv* and assert* runtime routines.
func type2IET(t *Type) string { func (t *Type) IET() string {
if isnilinter(t) { if isnilinter(t) {
return "E" return "E"
} }

View File

@ -678,7 +678,7 @@ func walkexpr(np **Node, init **NodeList) {
n1 := Nod(OADDR, n.Left, nil) n1 := Nod(OADDR, n.Left, nil)
r := n.Right // i.(T) r := n.Right // i.(T)
buf := "assert" + type2IET(r.Left.Type) + "2" + type2IET(r.Type) buf := internConcat("assert", r.Left.Type.IET(), "2", r.Type.IET())
fn := syslook(buf, 1) fn := syslook(buf, 1)
substArgTypes(fn, r.Left.Type, r.Type) substArgTypes(fn, r.Left.Type, r.Type)
@ -869,8 +869,8 @@ func walkexpr(np **Node, init **NodeList) {
oktype = ok.Type oktype = ok.Type
} }
fromKind := type2IET(from.Type) fromKind := from.Type.IET()
toKind := type2IET(t) toKind := t.IET()
// Avoid runtime calls in a few cases of the form _, ok := i.(T). // Avoid runtime calls in a few cases of the form _, ok := i.(T).
// This is faster and shorter and allows the corresponding assertX2X2 // This is faster and shorter and allows the corresponding assertX2X2
@ -903,7 +903,7 @@ func walkexpr(np **Node, init **NodeList) {
} }
resptr.Etype = 1 // addr does not escape resptr.Etype = 1 // addr does not escape
buf := "assert" + fromKind + "2" + toKind + "2" buf := internConcat("assert", fromKind, "2", toKind, "2")
fn := syslook(buf, 1) fn := syslook(buf, 1)
substArgTypes(fn, from.Type, t) substArgTypes(fn, from.Type, t)
call := mkcall1(fn, oktype, init, typename(t), from, resptr) call := mkcall1(fn, oktype, init, typename(t), from, resptr)
@ -927,11 +927,7 @@ func walkexpr(np **Node, init **NodeList) {
goto ret goto ret
} }
// Build name of function: convI2E etc. // Handle fast paths and special cases.
// Not all names are possible
// (e.g., we'll never generate convE2E or convE2I).
buf := "conv" + type2IET(n.Left.Type) + "2" + type2IET(n.Type)
fn := syslook(buf, 1)
var ll *NodeList var ll *NodeList
if !Isinter(n.Left.Type) { if !Isinter(n.Left.Type) {
ll = list(ll, typename(n.Left.Type)) ll = list(ll, typename(n.Left.Type))
@ -1010,6 +1006,11 @@ func walkexpr(np **Node, init **NodeList) {
} }
} }
// Build name of function: convI2E etc.
// Not all names are possible
// (e.g., we'll never generate convE2E or convE2I).
buf := internConcat("conv", n.Left.Type.IET(), "2", n.Type.IET())
fn := syslook(buf, 1)
substArgTypes(fn, n.Left.Type, n.Type) substArgTypes(fn, n.Left.Type, n.Type)
dowidth(fn.Type) dowidth(fn.Type)
n = Nod(OCALL, fn, nil) n = Nod(OCALL, fn, nil)