From 390d1ce686729dea40cee796ce391f9fd8466942 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Mon, 28 Mar 2016 09:32:10 -0700 Subject: [PATCH] cmd/compile: simplify substAny's TSTRUCT case Now that structs use a slice to store their fields, this code can be simplified somewhat. Passes toolstash -cmp. Change-Id: If17b1c89871fa06f34938fa67df0f8c6bcf1a86b Reviewed-on: https://go-review.googlesource.com/21219 Reviewed-by: Brad Fitzpatrick Run-TryBot: Matthew Dempsky TryBot-Result: Gobot Gobot --- src/cmd/compile/internal/gc/subr.go | 36 ++++++++--------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/src/cmd/compile/internal/gc/subr.go b/src/cmd/compile/internal/gc/subr.go index 99f4a776a8..f72ea61ebb 100644 --- a/src/cmd/compile/internal/gc/subr.go +++ b/src/cmd/compile/internal/gc/subr.go @@ -1147,38 +1147,22 @@ func substAny(t *Type, types *[]*Type) *Type { } case TSTRUCT: - // nfs only has to be big enough for the builtin functions. - var nfs [8]*Field fields := t.FieldSlice() - changed := false + var nfs []*Field for i, f := range fields { nft := substAny(f.Type, types) - if nft != f.Type { - nfs[i] = f.Copy() - nfs[i].Type = nft - changed = true + if nft == f.Type { + continue } - } - - if changed { - // Above we've initialized nfs with copied fields - // whenever the field type changed. However, because - // we keep fields in a linked list, we can only safely - // share the unmodified tail of the list. We need to - // copy the rest. - tail := true - for i := len(fields) - 1; i >= 0; i-- { - if nfs[i] != nil { - tail = false - } else if tail { - nfs[i] = fields[i] - } else { - nfs[i] = fields[i].Copy() - } + if nfs == nil { + nfs = append([]*Field(nil), fields...) } - + nfs[i] = f.Copy() + nfs[i].Type = nft + } + if nfs != nil { t = t.Copy() - t.SetFields(nfs[:len(fields)]) + t.SetFields(nfs) } } -- 2.48.1