Skip to content

Commit

Permalink
performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
n9Mtq4 committed Oct 25, 2015
1 parent f6d31fe commit 5f38aae
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/main/java/com/n9mtq4/reflection/ReflectionProtector.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@
@SuppressWarnings("unused")
public class ReflectionProtector {

private static final int DEFAULT_INDEX = 3;

/**
* In general, it seems that
* new Throwable().getStackTrace()
* is faster than
* Thread.currentThread().getStackTrace()
*
* 420 ms, 316 ms, 298 ms, 297 ms, 349 ms, 307 ms, 297 ms, 298 ms, 307 ms - throwable
* 482 ms, 474 ms, 340 ms, 325 ms, 360 ms, 339 ms, 333 ms, 347 ms, 340 ms - thread
*
* the first one requires the default index to be 3
* the second one requires the default index to be 3
* */
private StackTraceElement[] getStackTrace() {
return new Throwable().getStackTrace();
// return Thread.currentThread().getStackTrace();
}

/**
* Returns if the calling method was invoked with reflection.
* <p>
Expand All @@ -25,7 +44,7 @@ public static boolean usedReflection() {
}

private static boolean usedReflection(StackTraceElement[] stackTraceElements) {
return usedReflection(3, stackTraceElements);
return usedReflection(DEFAULT_INDEX, stackTraceElements);
}

/**
Expand Down

0 comments on commit 5f38aae

Please sign in to comment.