caoccao / GraalJS-vs-Javet-vs-Nashorn

A simple performance comparison of GraalJS, Javet and Nashorn.
https://www.caoccao.com/Javet/
Apache License 2.0
4 stars 0 forks source link

include benchmark for js <-> java interop #2

Open UserUNP opened 1 year ago

UserUNP commented 1 year ago

I think you should include some benchmark results for the interop between js and java of javet because there's some confusing trickery going on such as

let fn = Test.something;
for (let i = 0; i < 1000000; i++) {
  fn();
}

being about x6 faster than

for (let i = 0; i < 1000000; i++) {
  Test.something();
}

with Test being a java object and something() being a method that does nothing
(@BlazeMCworld came up with this test)

Sometimes javet is really slow when it comes to interoperability and sometimes its slightly faster, so I thought I'd ask if you know why

caoccao commented 1 year ago

May I know which converter you are using in this case? The slowness comes from the total number of JNI calls.

Could you share the complete code for the performance tuning?

By the way, the official perf test is at JavetPerf. You are welcome taking a look at it.

UserUNP commented 1 year ago

It's using the JavetProxyConverter

I can't provide you the code right now because I'm using my phone at the moment

caoccao commented 1 year ago

JavetProxyConverter is very expensive in a chaining pattern inside a loop. Saving the whole chain to a variable outside the loop is a common performance improvement point.

Why don't JS developers usually realize the slowness in this pattern? That's because V8 JIT optimizes this pattern underlying. However, that chaining pattern involves JNI calls which prevents both JVM and V8 from JIT optimization.