Any way to further optimize Java reflective method invocation?

前端 未结 5 1365
感动是毒
感动是毒 2020-12-08 08:22

I am wondering if there are any additional optimizations I can implement to improve the speed of reflective invocations in Java. Not that the performance is prohibitive, but

5条回答
  •  独厮守ぢ
    2020-12-08 08:55

    You will definitely want to reflect the method object only once (likely as a private static), and use that for the invocation rather than reflect it out every time. Don't bother with a cache map unless you don't know the name at compile time.

    If it is sensible in your context, you may want to hang on to and reuse the argument array object (don't forget to null the array elements on method exit to prevent temporary GC inhibition).

    If always invoked with the same parms (highly unlikely), you could hang onto the parms and (the argument array with it's values) reuse them.

    I would not attempt anything beyond that for the reasons already given by the other answers.

提交回复
热议问题