[ 
https://issues.apache.org/jira/browse/SCB-919?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16621377#comment-16621377
 ] 

ASF GitHub Bot commented on SCB-919:
------------------------------------

liubao68 closed pull request #912: [SCB-919] generate lambda Getter/Setter from 
reflect method or field to improve performance
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/912
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/LambdaMetafactoryUtils.java
 
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/LambdaMetafactoryUtils.java
new file mode 100644
index 000000000..e6bfa73c9
--- /dev/null
+++ 
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/LambdaMetafactoryUtils.java
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.foundation.common.utils;
+
+import java.lang.invoke.CallSite;
+import java.lang.invoke.LambdaMetafactory;
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodHandles.Lookup;
+import java.lang.invoke.MethodType;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+import org.apache.servicecomb.foundation.common.utils.bean.Getter;
+import org.apache.servicecomb.foundation.common.utils.bean.Setter;
+
+public final class LambdaMetafactoryUtils {
+  private static Lookup lookup = MethodHandles.lookup();
+
+  private LambdaMetafactoryUtils() {
+  }
+
+  protected static Method findAbstractMethod(Class<?> functionalInterface) {
+    for (Method method : functionalInterface.getMethods()) {
+      if ((method.getModifiers() & Modifier.ABSTRACT) != 0) {
+        return method;
+      }
+    }
+
+    return null;
+  }
+
+  @SuppressWarnings("unchecked")
+  public static <T> T createLambda(Object instance, Method instanceMethod, 
Class<?> functionalIntfCls)
+      throws Throwable {
+    Method intfMethod = findAbstractMethod(functionalIntfCls);
+    MethodHandle methodHandle = lookup.unreflect(instanceMethod);
+
+    MethodType intfMethodType = 
MethodType.methodType(intfMethod.getReturnType(), 
intfMethod.getParameterTypes());
+    MethodType instanceMethodType = MethodType
+        .methodType(instanceMethod.getReturnType(), 
instanceMethod.getParameterTypes());
+    CallSite callSite = LambdaMetafactory.metafactory(
+        lookup,
+        intfMethod.getName(),
+        MethodType.methodType(functionalIntfCls, instance.getClass()),
+        intfMethodType,
+        methodHandle,
+        instanceMethodType
+    );
+
+    return (T) callSite.getTarget().bindTo(instance).invoke();
+  }
+
+  @SuppressWarnings("unchecked")
+  public static <T> T createLambda(Method instanceMethod, Class<?> 
functionalIntfCls)
+      throws Throwable {
+    Method intfMethod = findAbstractMethod(functionalIntfCls);
+    MethodHandle methodHandle = lookup.unreflect(instanceMethod);
+
+    MethodType intfMethodType = 
MethodType.methodType(intfMethod.getReturnType(), 
intfMethod.getParameterTypes());
+    MethodType instanceMethodType = methodHandle.type();
+    CallSite callSite = LambdaMetafactory.metafactory(
+        lookup,
+        intfMethod.getName(),
+        MethodType.methodType(functionalIntfCls),
+        intfMethodType,
+        methodHandle,
+        instanceMethodType
+    );
+
+    return (T) callSite.getTarget().invoke();
+  }
+
+  public static Getter createGetter(Method getMethod) throws Throwable {
+    return createLambda(getMethod, Getter.class);
+  }
+
+  // slower than reflect directly
+  public static Getter createGetter(Field field) {
+    field.setAccessible(true);
+    return instance -> field.get(instance);
+  }
+
+  public static Setter createSetter(Method setMethod) throws Throwable {
+    return createLambda(setMethod, Setter.class);
+  }
+
+  // slower than reflect directly
+  public static Setter createSetter(Field field) {
+    field.setAccessible(true);
+    return (instance, value) -> field.set(instance, value);
+  }
+}
diff --git 
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/bean/Getter.java
 
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/bean/Getter.java
new file mode 100644
index 000000000..1404f4738
--- /dev/null
+++ 
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/bean/Getter.java
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.foundation.common.utils.bean;
+
+public interface Getter {
+  Object get(Object instance) throws Throwable;
+}
diff --git 
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/bean/Setter.java
 
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/bean/Setter.java
new file mode 100644
index 000000000..eb1c1c9e2
--- /dev/null
+++ 
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/bean/Setter.java
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.foundation.common.utils.bean;
+
+public interface Setter {
+  void set(Object instance, Object value) throws Throwable;
+}
diff --git 
a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestLambdaMetafactoryUtils.java
 
b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestLambdaMetafactoryUtils.java
new file mode 100644
index 000000000..9b6b301a1
--- /dev/null
+++ 
b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestLambdaMetafactoryUtils.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.foundation.common.utils;
+
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+
+import org.apache.servicecomb.foundation.common.utils.bean.Getter;
+import org.apache.servicecomb.foundation.common.utils.bean.Setter;
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestLambdaMetafactoryUtils {
+  public static class Model {
+    private int f1;
+
+    public int getF1() {
+      return f1;
+    }
+
+    public void setF1(int f1) {
+      this.f1 = f1;
+    }
+
+    public List<Integer> echo(List<Integer> value) {
+      return value;
+    }
+  }
+
+  Model model = new Model();
+
+  @SuppressWarnings("unchecked")
+  @Test
+  public void createLambda_withInstance() throws Throwable {
+    Supplier<Object> getter = LambdaMetafactoryUtils
+        .createLambda(model, Model.class.getMethod("getF1"), Supplier.class);
+    Consumer<Object> setter = LambdaMetafactoryUtils
+        .createLambda(model, Model.class.getMethod("setF1", int.class), 
Consumer.class);
+    Function<Object, Object> echo = LambdaMetafactoryUtils
+        .createLambda(model, Model.class.getMethod("echo", List.class), 
Function.class);
+
+    setter.accept(1);
+    int f1 = (int) getter.get();
+    Assert.assertEquals(1, f1);
+    Assert.assertThat((List<Integer>) echo.apply(Arrays.asList(2)), 
Matchers.contains(2));
+  }
+
+  @SuppressWarnings("unchecked")
+  @Test
+  public void createGetterSetterByMethod() throws Throwable {
+    Getter getter = 
LambdaMetafactoryUtils.createGetter(Model.class.getMethod("getF1"));
+    Setter setter = 
LambdaMetafactoryUtils.createSetter(Model.class.getMethod("setF1", int.class));
+    BiFunction<Object, Object, Object> echo = LambdaMetafactoryUtils
+        .createLambda(Model.class.getMethod("echo", List.class), 
BiFunction.class);
+
+    setter.set(model, 1);
+    int f1 = (int) getter.get(model);
+    Assert.assertEquals(1, f1);
+    Assert.assertThat((List<Integer>) echo.apply(model, Arrays.asList(2)), 
Matchers.contains(2));
+  }
+
+  @Test
+  public void createGetterSetterByField() throws Throwable {
+    Field f1 = Model.class.getDeclaredField("f1");
+    Getter getter = LambdaMetafactoryUtils.createGetter(f1);
+    Setter setter = LambdaMetafactoryUtils.createSetter(f1);
+
+    setter.set(model, 1);
+    Assert.assertEquals(1, (int) getter.get(model));
+  }
+}
diff --git 
a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestLambdaPerformance.java
 
b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestLambdaPerformance.java
new file mode 100644
index 000000000..5ee8d7856
--- /dev/null
+++ 
b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestLambdaPerformance.java
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.foundation.common.utils;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import 
org.apache.servicecomb.foundation.common.utils.TestLambdaMetafactoryUtils.Model;
+import org.apache.servicecomb.foundation.common.utils.bean.Getter;
+
+public class TestLambdaPerformance {
+  static Model model = new Model();
+
+  // 20 field/msg, 10_0000tps
+  static int count = 20 * 10_0000;
+
+  static int sum = 0;
+
+  static Method f1_getter;
+
+  static MethodHandle mh_f1_getter;
+
+  static Getter lambda_f1_method_getter;
+
+  static Field f1_field;
+
+  static Getter lambda_f1_field_getter;
+
+  static {
+    try {
+      f1_field = Model.class.getDeclaredField("f1");
+      f1_field.setAccessible(true);
+
+      f1_getter = Model.class.getMethod("getF1");
+      mh_f1_getter = MethodHandles.lookup().unreflect(f1_getter);
+      lambda_f1_method_getter = LambdaMetafactoryUtils.createGetter(f1_getter);
+      lambda_f1_field_getter = LambdaMetafactoryUtils.createGetter(f1_field);
+    } catch (Throwable throwable) {
+      throwable.printStackTrace();
+    }
+  }
+
+  public static long directGetter() {
+    long start = System.nanoTime();
+    sum = 0;
+    for (int idx = 0; idx < count; idx++) {
+      sum += model.getF1();
+    }
+    return System.nanoTime() - start;
+  }
+
+  public static long reflectfieldF1() throws IllegalAccessException {
+    long start = System.nanoTime();
+    sum = 0;
+    for (int idx = 0; idx < count; idx++) {
+      sum += (int) f1_field.get(model);
+    }
+    return System.nanoTime() - start;
+  }
+
+  public static long lambdaMethodGetter() throws Throwable {
+    long start = System.nanoTime();
+    sum = 0;
+    for (int idx = 0; idx < count; idx++) {
+      sum += (int) lambda_f1_method_getter.get(model);
+    }
+    return System.nanoTime() - start;
+  }
+
+  public static long lambdaFieldGetter() throws Throwable {
+    long start = System.nanoTime();
+    sum = 0;
+    for (int idx = 0; idx < count; idx++) {
+      sum += (int) lambda_f1_field_getter.get(model);
+    }
+    return System.nanoTime() - start;
+  }
+
+  public static long mhGetterInvoke() throws Throwable {
+    long start = System.nanoTime();
+    sum = 0;
+    for (int idx = 0; idx < count; idx++) {
+      sum += (int) mh_f1_getter.invoke(model);
+    }
+    return System.nanoTime() - start;
+  }
+
+  public static long mhGetterExact() throws Throwable {
+    long start = System.nanoTime();
+    sum = 0;
+    for (int idx = 0; idx < count; idx++) {
+      sum += (int) mh_f1_getter.invokeExact(model);
+    }
+    return System.nanoTime() - start;
+  }
+
+  public static long reflectGetter() throws InvocationTargetException, 
IllegalAccessException {
+    long start = System.nanoTime();
+    sum = 0;
+    for (int idx = 0; idx < count; idx++) {
+      sum += (int) f1_getter.invoke(model);
+    }
+    return System.nanoTime() - start;
+  }
+
+  public static void main(String[] args) throws Throwable {
+    directGetter();
+    lambdaMethodGetter();
+    reflectGetter();
+    mhGetterInvoke();
+    mhGetterExact();
+    reflectfieldF1();
+
+    System.out.println("directGetter      : " + directGetter());
+    System.out.println("lambdaMethodGetter: " + lambdaMethodGetter());
+    System.out.println("mhGetterInvoke    : " + mhGetterInvoke());
+    System.out.println("mhGetterExact     : " + mhGetterExact());
+    System.out.println("reflectGetter     : " + reflectGetter());
+
+    System.out.println("");
+
+    System.out.println("reflectfieldF1    : " + reflectfieldF1());
+    System.out.println("lambdaFieldGetter : " + lambdaFieldGetter());
+  }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> generate lambda Getter/Setter from reflect method or field to improve 
> performance
> ---------------------------------------------------------------------------------
>
>                 Key: SCB-919
>                 URL: https://issues.apache.org/jira/browse/SCB-919
>             Project: Apache ServiceComb
>          Issue Type: Task
>          Components: Java-Chassis
>            Reporter: wujimin
>            Assignee: wujimin
>            Priority: Major
>             Fix For: java-chassis-1.1.0
>
>
> from method to lambda can improve performance
> but from field is slower
>  
> almost all cases is get/set method, so there is no problem



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to