Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate use of method handles for property access [DATACMNS-809] #1271

Closed
spring-projects-issues opened this issue Jan 23, 2016 · 4 comments
Closed
Assignees
Labels
in: core type: enhancement

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Jan 23, 2016

Oliver Drotbohm opened DATACMNS-809 and commented

The linked answer on StackOverflow seems to indicate that statically held MethodHandle instances are more than twice as fast as a reflective getting and setting of fields. We should investigate whether we could provide an alternative to the BeanWrapper implementation that uses ASM to generate a PropertyAccessor implementation for a type with the method handles for properties held in static final fields and an if-then-else cascade to select invoke the right one for a given PersistentProperty.

final class $PersistentEntityPropertyAccessor implements PropertyAccessor {

  // For each field/getter
  private static final MethodHandle $memberGetHandle; // property.useFieldAccess() ? MethodHandles.lookup().unreflectGetter(field) : MethodHandles.lookup().unreflect(getter);
  private static final MethodHandle $memberSetHandle; // property.useFieldAccess() ? MethodHandles.lookup().unreflectGetter(field) : MethodHandles.lookup().unreflect(setter);

  private final Object bean;

  @Override
  public Object getProperty(PersistentProperty<?> property) {

    String name = property.getName();

    if (name == "foo") {
      return fooGetHandle.invoke(bean);
    } …
  }

  @Override
  public void setProperty(PersistentProperty<?> property, Object value) {

    String name = property.getName();

    if (name == "foo") {
      return fooSetHandle.invoke(bean, value);
    } …
  }
}

Reference URL: http://stackoverflow.com/questions/22244402/how-can-i-improve-performance-of-field-set-perhap-using-methodhandles/22337726#22337726

Referenced from: pull request #159

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Apr 11, 2016

Mark Paluch commented

Requires Java 1.7+

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Apr 12, 2016

Mark Paluch commented

JMH benchmark using generated vs. reflection accessors:

PropertyMappingJmh.readGenerated         avgt    5   7,431 ± 0,192  ns/op
PropertyMappingJmh.readReflection        avgt    5   9,364 ± 0,123  ns/op
PropertyMappingJmh.readWrite_Generated   avgt    5  11,644 ± 0,134  ns/op
PropertyMappingJmh.readWrite_Reflection  avgt    5  13,303 ± 0,601  ns/op

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Apr 13, 2016

Mark Paluch commented

Using method handles here seems suboptimal as the gain is minimal. There's however another possibility of directly calling methods. As we have details about the getter/setter methods, also regarding visibility, it's more efficient generating method calls instead of using method handles, where applicable. A benchmark shows a higher performance gain:

Field-access:

PropertyMappingJmh.readGenerated         avgt    5   7,679 ± 0,238  ns/op
PropertyMappingJmh.readReflection        avgt    5  10,122 ± 0,465  ns/op
PropertyMappingJmh.readWrite_Generated   avgt    5  12,705 ± 0,193  ns/op
PropertyMappingJmh.readWrite_Reflection  avgt    5  15,434 ± 0,226  ns/op

Property(getter/setter)-access:

PropertyMappingJmh.readGenerated         avgt    5   4,823 ± 0,187  ns/op
PropertyMappingJmh.readReflection        avgt    5  13,451 ± 0,999  ns/op
PropertyMappingJmh.readWrite_Generated   avgt    5   6,346 ± 0,092  ns/op
PropertyMappingJmh.readWrite_Reflection  avgt    5  28,438 ± 0,654  ns/op

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented May 2, 2016

Oliver Drotbohm commented

First round of polishing applied. Final open task documented in the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants