Hakky54 / gatekeeper

🔐 A lightweight java library which guards your publicly accessible internal implementations.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Performance: Choose the quickest available way to get the caller

tr7zw opened this issue · comments

commented

Currently the implementation uses the StackTrace of a Throwable to get the Caller class, and then uses Strings to compare the Class names. https://stackoverflow.com/a/2924426 has examples on how to use the Reflection class/Security manager to get the caller class way faster. Best-case gatekeeper should check for the fastest available method(since the Reflection way is sadly deprecated without replacement) that the runtime JVM has to offer.
Doing a quick test with optimized class lookups(only getting the target class when the caller was not allowed), comparing classes instead of class-names and the Reflection way of getting the caller class speed up checks from ~400k checks/second to ~9million checks/second.

Thank you very much for this valuable insight regarding the performance. I tried the improve the performance already by getting the stacktrace from an Exception but your suggestion from the stackoverflow topic is even better. Especially the option with the security manager. I don't think the option with reflection is a good alternative as it is marked deprecated and should be avoided.

Would you like to create a pull request?

commented

Yea I'll do that later at home.