mardi 2 février 2021

Alternatives to Class.getConstructor [for performance reasons]

I am trying to dynamically load ~10,000 classes and have code which looks like:

Class<?> c = getClass().getClassLoader().loadClass(className);
Constructor<?> constr = c.getConstructor(String.class);
constr.newInstance(myString);

On profiling, I noticed that majority of the time is spent in getConstructor call which seems to be taking in the order of 15ms on an average, which obviously multiplied by 10k adds up.

Any suggestions on writing this alternatively to speed up java reflection ?

p.s. Unfortunately, these classes do have to be loaded dynamically (otherwise I would not have used reflection at all)





Aucun commentaire:

Enregistrer un commentaire