]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: optimize common case in structuralType
authorRobert Griesemer <gri@golang.org>
Sat, 13 Nov 2021 19:02:51 +0000 (11:02 -0800)
committerRobert Griesemer <gri@golang.org>
Mon, 15 Nov 2021 21:22:11 +0000 (21:22 +0000)
Most of the time we don't have a type parameter. Avoid using a
closure in that case.

While at it, rename argument from typ to t (to match style in
that file), and clarify the doc string.

Change-Id: Ie62821073f60f353526263f8b380bad9f72d842e
Reviewed-on: https://go-review.googlesource.com/c/go/+/363668
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/type.go

index af195c08a41d2606f9e11bc924d2fdc773fcfd41..39737d47a7b5830d6c864b4dbad0eade8b2dd487 100644 (file)
@@ -56,15 +56,20 @@ func match(x, y Type) Type {
        return nil
 }
 
-// If typ is a type parameter, structuralType returns the single underlying
-// type of all types in the corresponding type constraint if it exists, or
-// nil otherwise. If the type set contains only unrestricted and restricted
-// channel types (with identical element types), the single underlying type
-// is the restricted channel type if the restrictions are always the same.
-// If typ is not a type parameter, structuralType returns the underlying type.
-func structuralType(typ Type) Type {
+// If t is not a type parameter, structuralType returns the underlying type.
+// If t is a type parameter, structuralType returns the single underlying
+// type of all types in its type set if it exists, or nil otherwise. If the
+// type set contains only unrestricted and restricted channel types (with
+// identical element types), the single underlying type is the restricted
+// channel type if the restrictions are always the same, or nil otherwise.
+func structuralType(t Type) Type {
+       tpar, _ := t.(*TypeParam)
+       if tpar == nil {
+               return under(t)
+       }
+
        var su Type
-       if underIs(typ, func(u Type) bool {
+       if tpar.underIs(func(u Type) bool {
                if u == nil {
                        return false
                }