RFR: 6983726: Reimplement MethodHandleProxies.asInterfaceInstance [v6]

Chen Liang liach at openjdk.org
Tue Jun 6 16:32:47 UTC 2023


On Thu, 6 Apr 2023 03:44:07 GMT, Chen Liang <liach at openjdk.org> wrote:

>> As John Rose has pointed out in this issue, the current j.l.r.Proxy based implementation of MethodHandleProxies.asInterface has a few issues:
>> 1. Exposes too much information via Proxy supertype (and WrapperInstance interface)
>> 2. Does not allow future expansion to support SAM[^1] abstract classes
>> 3. Slow (in fact, very slow)
>> 
>> This patch addresses all 3 problems:
>> 1. It updates the WrapperInstance methods to take an `Empty` to avoid method clashes
>> 2. This patch obtains already generated classes from a ClassValue by the requested interface type; the ClassValue can later be updated to compute implementation generation for abstract classes as well.
>> 3. This patch's faster than old implementation in general.
>> 
>> 
>> Benchmark                                                          Mode  Cnt      Score      Error  Units
>> MethodHandleProxiesAsIFInstance.baselineAllocCompute               avgt   15      1.483 ±    0.025  ns/op
>> MethodHandleProxiesAsIFInstance.baselineCompute                    avgt   15      0.264 ±    0.006  ns/op
>> MethodHandleProxiesAsIFInstance.testCall                           avgt   15      1.773 ±    0.040  ns/op
>> MethodHandleProxiesAsIFInstance.testCreate                         avgt   15     16.754 ±    0.411  ns/op
>> MethodHandleProxiesAsIFInstance.testCreateCall                     avgt   15     19.609 ±    1.598  ns/op
>> MethodHandleProxiesAsIFInstanceCall.callDoable                     avgt   15      0.424 ±    0.024  ns/op
>> MethodHandleProxiesAsIFInstanceCall.callHandle                     avgt   15      1.936 ±    0.008  ns/op
>> MethodHandleProxiesAsIFInstanceCall.callInterfaceInstance          avgt   15      1.766 ±    0.014  ns/op
>> MethodHandleProxiesAsIFInstanceCall.callLambda                     avgt   15      0.414 ±    0.005  ns/op
>> MethodHandleProxiesAsIFInstanceCall.constantDoable                 avgt   15      0.271 ±    0.006  ns/op
>> MethodHandleProxiesAsIFInstanceCall.constantHandle                 avgt   15      0.263 ±    0.004  ns/op
>> MethodHandleProxiesAsIFInstanceCall.constantInterfaceInstance      avgt   15      0.266 ±    0.005  ns/op
>> MethodHandleProxiesAsIFInstanceCall.constantLambda                 avgt   15      0.262 ±    0.003  ns/op
>> MethodHandleProxiesAsIFInstanceCall.direct                         avgt   15      0.264 ±    0.005  ns/op
>> MethodHandleProxiesAsIFInstanceCreate.createCallInterfaceInstance  avgt   15     18.000 ±    0.181  ns/op
>> MethodHandleProxiesAsIFInstanceCreat...
>
> Chen Liang has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Whitespace, cleanup, rename benchmarks to be informative

@JornVernee Thanks for your review! Updated per your comments and refined the benchmark as well.

> I think the call benchmark could be fleshed out a bit more as well. It would be interesting to see these cases:
> 1. direct call to `doWork` (this would be the baseline)
> 2. call through non-constant method handle
> 3. call through non-constant interface instance created with lambda (existing `lambdaCall`)
> 4. call through non-constant interface instance created with MHP::asInterfaceInstance (existing `testCall`)
> 5. call through constant (`static final`) method handle
> 6. call through constant (`static final`) interface instance created with lambda
> 7. call through constant (`static final`) interface instance created with MHP::asInterfaceInstance

Here's the latest benchmark results: 

Benchmark                                                          Mode  Cnt      Score      Error  Units
MethodHandleProxiesAsIFInstanceCall.callDoable                     avgt   15      0.609 ±    0.290  ns/op
MethodHandleProxiesAsIFInstanceCall.callHandle                     avgt   15      1.952 ±    0.014  ns/op
MethodHandleProxiesAsIFInstanceCall.callInterfaceInstance          avgt   15      0.411 ±    0.005  ns/op
MethodHandleProxiesAsIFInstanceCall.callLambda                     avgt   15      0.408 ±    0.002  ns/op
MethodHandleProxiesAsIFInstanceCall.constantDoable                 avgt   15      0.260 ±    0.005  ns/op
MethodHandleProxiesAsIFInstanceCall.constantHandle                 avgt   15      0.259 ±    0.004  ns/op
MethodHandleProxiesAsIFInstanceCall.constantInterfaceInstance      avgt   15      0.260 ±    0.007  ns/op
MethodHandleProxiesAsIFInstanceCall.constantLambda                 avgt   15      0.257 ±    0.004  ns/op
MethodHandleProxiesAsIFInstanceCall.direct                         avgt   15      0.256 ±    0.003  ns/op
MethodHandleProxiesAsIFInstanceCreate.createCallInterfaceInstance  avgt   15  22033.536 ± 3300.920  ns/op
MethodHandleProxiesAsIFInstanceCreate.createCallLambda             avgt   15  20081.732 ± 4523.516  ns/op
MethodHandleProxiesAsIFInstanceCreate.createInterfaceInstance      avgt   15  18523.812 ± 2585.683  ns/op
MethodHandleProxiesAsIFInstanceCreate.createLambda                 avgt   15  19206.093 ± 4743.297  ns/op


1. call through constant all has similar performance as direct call
2. call through non-constant method handle is significantly slower over than others, as anticipated
3. call through non-constant interface instance created with lambda, MHP::asInterfaceInstance and anonymous class instance (Doable) have similar performance

@DasBrain Thanks for the recommendation to test with SecurityManager, added a test and found two places that needs to do privileged indeed.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/13197#issuecomment-1498450131


More information about the core-libs-dev mailing list