Varargs vs. explicit param method call

208 views
Skip to first unread message

Ionut Balosin

unread,
Apr 30, 2018, 12:39:17 PM4/30/18
to mechanical-sympathy
Hello All,

   I am currently investigating the performance and how works in case of calling a method with explicit parameters vs. a varargs method. Please find attached the JMH Bench and ASM files generated after running the JMH test. (I did not inline them since are quite long)

I would like double check with you if my understanding is correct (btw I am not an ASM expert), especially in regards to native code generated for varargs method, which basically explains the difference in performance between these two.
In such case, to me it seems before calling the varargs method ( e.g. private int var_args(int... args) ) an array is allocated, parameters are collected from stack (i.e. mov    r11d,DWORD PTR [rbp+0x18]), copied into that array and then var_args method itself is called (i.e. call   0x00007f8764dcf640)
-- array allocation and stack parameter manipulation might be an explanation for difference in elapsed time

Benchmark                                         (param1)  (param2)  (param3)  (param4)  Mode  Cnt  Score   Error  Units
MethodParametersJmh.explicit_4_params_bench                 3         5         7         9  avgt   15  5.464 ± 0.112  ns/op
MethodParametersJmh.var_args_4_params_bench              3         5         7         9  avgt   15  8.520 ± 0.127  ns/op

Could you please comment / invalidate my understanding?

Please find attached Java source file and JMH test results + ASM.

Thanks
Ionut

MethodParametersJmh.out
MethodParametersJmh.java

Vitaly Davidovich

unread,
May 6, 2018, 9:07:40 PM5/6/18
to mechanical-sympathy
Your understanding of how varargs calls are made is correct - it's nothing more than sugar for an allocated array to store the args.  Your bench, however, explicitly disables inlining of the varargs method, and thus prevents escape analysis from potentially eliminating the array allocation.  Try the bench without disabling inlining and I would hope the JIT is able to remove the allocation and scalarize instead.

--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-sympathy+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages