~eliasnaur/gio

b6e0376ad2febfbced42ff9e5edbcaf7b4a8ad94 — Dominik Honnef 1 year, 6 months ago 6937a5d
io/semantic: avoid unnecessary pointer indirection

Putting a string in an interface value has to (normally) heap allocate
the string header and string. However, putting the address of a local
string variable in an interface value has the same effect, as this
causes the local variable to escape to the heap.

Signed-off-by: Dominik Honnef <dominik@honnef.co>
2 files changed, 6 insertions(+), 8 deletions(-)

M io/router/router.go
M io/semantic/semantic.go
M io/router/router.go => io/router/router.go +4 -4
@@ 538,11 538,11 @@ func (q *Router) collect() {

		// Semantic ops.
		case ops.TypeSemanticLabel:
			lbl := encOp.Refs[0].(*string)
			pc.semanticLabel(*lbl)
			lbl := encOp.Refs[0].(string)
			pc.semanticLabel(lbl)
		case ops.TypeSemanticDesc:
			desc := encOp.Refs[0].(*string)
			pc.semanticDesc(*desc)
			desc := encOp.Refs[0].(string)
			pc.semanticDesc(desc)
		case ops.TypeSemanticClass:
			class := semantic.ClassOp(encOp.Data[1])
			pc.semanticClass(class)

M io/semantic/semantic.go => io/semantic/semantic.go +2 -4
@@ 40,14 40,12 @@ type SelectedOp bool
type DisabledOp bool

func (l LabelOp) Add(o *op.Ops) {
	s := string(l)
	data := ops.Write1(&o.Internal, ops.TypeSemanticLabelLen, &s)
	data := ops.Write1(&o.Internal, ops.TypeSemanticLabelLen, string(l))
	data[0] = byte(ops.TypeSemanticLabel)
}

func (d DescriptionOp) Add(o *op.Ops) {
	s := string(d)
	data := ops.Write1(&o.Internal, ops.TypeSemanticDescLen, &s)
	data := ops.Write1(&o.Internal, ops.TypeSemanticDescLen, string(d))
	data[0] = byte(ops.TypeSemanticDesc)
}