SpringStudent commented on code in PR #13155:
URL: https://github.com/apache/dubbo/pull/13155#discussion_r1351663748


##########
dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ReflectionServiceDescriptor.java:
##########
@@ -89,6 +98,21 @@ private void initMethods() {
                     +" stream method signatures. method(" + methodName + ")");
         });
     }
+    
+    public static boolean isProtobufClass(Class<?> clazz) {
+        while (clazz != Object.class && clazz != null) {
+            Class<?>[] interfaces = clazz.getInterfaces();
+            if (interfaces.length > 0) {
+                for (Class<?> clazzInterface : interfaces) {
+                    if 
(PROTOBUF_MESSAGE_CLASS_NAME.equalsIgnoreCase(clazzInterface.getName())) {
+                        return true;
+                    }
+                }
+            }
+            clazz = clazz.getSuperclass();

Review Comment:
   I've written some other implementations of the isProtobufClass method. And 
attach some test results
   `public class Test {
   
       public static void main(String[] args) throws NoSuchMethodException {
           System.out.println("####class is pb class start");
           long start = System.currentTimeMillis();
           for (int i = 0; i < 100000; i++) {
               isProtobufClass(HelloRequest.class);
           }
           System.out.println("isProtobufClass cost=" + 
(System.currentTimeMillis() - start));
           start = System.currentTimeMillis();
           for (int i = 0; i < 100000; i++) {
               isProtobufClass2(HelloRequest.class);
           }
           System.out.println("isProtobufClass2 cost=" + 
(System.currentTimeMillis() - start));
           start = System.currentTimeMillis();
           for (int i = 0; i < 100000; i++) {
               isProtobufClass3(HelloRequest.class);
           }
           System.out.println("isProtobufClass3 cost=" + 
(System.currentTimeMillis() - start));
           start = System.currentTimeMillis();
           for (int i = 0; i < 100000; i++) {
               isProtobufClass4(HelloRequest.class);
           }
           System.out.println("isProtobufClass4 cost=" + 
(System.currentTimeMillis() - start));
           System.out.println("####class is pb class end");
   
           System.out.println("####class is not pb class start");
           start = System.currentTimeMillis();
           for (int i = 0; i < 100000; i++) {
               isProtobufClass(TripleClientCall.class);
           }
           System.out.println("isProtobufClass cost=" + 
(System.currentTimeMillis() - start));
           start = System.currentTimeMillis();
           for (int i = 0; i < 100000; i++) {
               isProtobufClass2(TripleClientCall.class);
           }
           System.out.println("isProtobufClass2 cost=" + 
(System.currentTimeMillis() - start));
           start = System.currentTimeMillis();
           for (int i = 0; i < 100000; i++) {
               isProtobufClass3(TripleClientCall.class);
           }
           System.out.println("isProtobufClass3 cost=" + 
(System.currentTimeMillis() - start));
           start = System.currentTimeMillis();
           for (int i = 0; i < 100000; i++) {
               isProtobufClass4(TripleClientCall.class);
           }
           System.out.println("isProtobufClass4 cost=" + 
(System.currentTimeMillis() - start));
           System.out.println("####class is not pb class end");
       }
   
   
       public static boolean isProtobufClass(Class<?> clazz) {
           while (clazz != Object.class && clazz != null) {
               Class<?>[] interfaces = clazz.getInterfaces();
               if (interfaces.length > 0) {
                   for (Class<?> clazzInterface : interfaces) {
                       if 
(PROTOBUF_MESSAGE_CLASS_NAME.equalsIgnoreCase(clazzInterface.getName())) {
                           return true;
                       }
                   }
               }
               clazz = clazz.getSuperclass();
           }
           return false;
       }
   
       public static boolean isProtobufClass2(Class<?> clazz) {
           try {
               //pb class must deserilizable by parseFrom(byte[]) method
               HelloRequest.class.getDeclaredMethod("parseFrom", byte[].class);
           } catch (Exception e) {
               return false;
           }
           while (clazz != Object.class && clazz != null) {
               Class<?>[] interfaces = clazz.getInterfaces();
               if (interfaces.length > 0) {
                   for (Class<?> clazzInterface : interfaces) {
                       if 
(PROTOBUF_MESSAGE_CLASS_NAME.equalsIgnoreCase(clazzInterface.getName())) {
                           return true;
                       }
                   }
               }
               clazz = clazz.getSuperclass();
           }
           return false;
       }
   
       public static boolean isProtobufClass3(Class<?> pojoClazz) {
           try {
               Class<?> protobufBaseClass = 
Class.forName(PROTOBUF_MESSAGE_CLASS_NAME);
               return protobufBaseClass.isAssignableFrom(pojoClazz);
           } catch (ClassNotFoundException | NoClassDefFoundError e) {
               // If sink does not have protobuf in classpath then it cannot be 
protobuf
               return false;
           }
       }
   
       public static boolean isProtobufClass4(Class<?> pojoClazz) {
           return Message.class.isAssignableFrom(pojoClazz);
       }
   }
   `
   test result
   `####class is pb class start
   isProtobufClass cost=35
   isProtobufClass2 cost=74
   isProtobufClass3 cost=55
   isProtobufClass4 cost=4
   ####class is pb class end
   ####class is not pb class start
   isProtobufClass cost=11
   isProtobufClass2 cost=46
   isProtobufClass3 cost=55
   isProtobufClass4 cost=4
   ####class is not pb class end`
   
   You can see that even if the isProtobuf method is called 10W times, the 
implementation time of all methods is not more than 100ms, and the performance 
is isProtobufClass4>isProtobufClass>isProtobufClass2>isProtobufClass3; I 
personally think that the current implementation isProtobufClass has little 
effect on the startup speed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org
For additional commands, e-mail: notifications-h...@dubbo.apache.org

Reply via email to