Java program runs slower when code that is never executed is commented out

前端 未结 3 1801
不思量自难忘°
不思量自难忘° 2021-01-30 13:00

I observed some strange behaviour in one of my Java programs. I have tried to strip the code down as much as possible while still being able to replicate the behaviour. Code in

3条回答
  •  误落风尘
    2021-01-30 13:21

    The commented code affects how inlining is handled. If functionB gets longer/bigger (more bytecode instructions) it will not be inlined into functionA.

    So @J3D1 was able to use VMOptions to manually switch off inlining for functionB(): -XX:CompileCommand=dontinline,com.jd.benchmarking.StrangeBeh‌​aviour::functionB This appears to eliminate the delay with the shorter function.

    with the vm options you can display inlining -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining

    The bigger Version, wont inline functionB

    @ 8   StrangeBehaviour::functionB (326 bytes)   callee is too large
    @ 21   StrangeBehaviour::functionA (12 bytes)
      @ 8   StrangeBehaviour::functionB (326 bytes)   callee is too large
    @ 35   StrangeBehaviour::functionA (12 bytes)
      @ 8   StrangeBehaviour::functionB (326 bytes)   callee is too large
    

    The shorter Version will try to inline functionB, causing a few of further attempts.

    @ 8   StrangeBehaviour::functionB (318 bytes)   inline (hot)
     @ 21   StrangeBehaviour::functionA (12 bytes)   inline (hot)
       @ 8   StrangeBehaviour::functionB (318 bytes)   inline (hot)
         @ 35   StrangeBehaviour::functionA (12 bytes)   recursive inlining is too deep
     @ 35   StrangeBehaviour::functionA (12 bytes)   inline (hot)
       @ 8   StrangeBehaviour::functionB (318 bytes)   inline (hot)
         @ 21   StrangeBehaviour::functionA (12 bytes)   recursive inlining is too deep
         @ 35   StrangeBehaviour::functionA (12 bytes)   recursive inlining is too deep
    @ 21   StrangeBehaviour::functionA (12 bytes)   inline (hot)
     @ 8   StrangeBehaviour::functionB (318 bytes)   inline (hot)
       @ 35   StrangeBehaviour::functionA (12 bytes)   inline (hot)
         @ 8   StrangeBehaviour::functionB (318 bytes)   recursive inlining is too deep
    @ 35   StrangeBehaviour::functionA (12 bytes)   inline (hot)
     @ 8   StrangeBehaviour::functionB (318 bytes)   inline (hot)
       @ 21   StrangeBehaviour::functionA (12 bytes)   inline (hot)
        @ 8   StrangeBehaviour::functionB (318 bytes)   recursive inlining is too deep
       @ 35   StrangeBehaviour::functionA (12 bytes)   inline (hot)
         @ 8   StrangeBehaviour::functionB (318 bytes)   recursive inlining is too deep
    

    Mostly guessing, but the bigger/inlined bytecode will cause problems with branch prediction and caching

提交回复
热议问题