From e6e9e1d2f931b2a36da85b9282ad0966051ae4d5 Mon Sep 17 00:00:00 2001 From: marsyrc <33244120+marsyrc@users.noreply.github.com> Date: Mon, 26 Apr 2021 14:47:35 +0800 Subject: [PATCH] reflect/type.go: add special case judgement before scan T's and V's methods This change can avoid redundant linear scanning when the number of T's methods is less than the number of V's methods, which can enhance the efficiency while judging whether a type implements a interface. --- src/reflect/type.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/reflect/type.go b/src/reflect/type.go index 9727bfe467c..f4cf5aa3014 100644 --- a/src/reflect/type.go +++ b/src/reflect/type.go @@ -1498,6 +1498,9 @@ func implements(T, V *rtype) bool { // See also ../runtime/iface.go. if V.Kind() == Interface { v := (*interfaceType)(unsafe.Pointer(V)) + if len(v.methods) < len(t.methods) { + return false + } i := 0 for j := 0; j < len(v.methods); j++ { tm := &t.methods[i]