JDK-8150778 : Reduce Throwable.getStackTrace() calls to the JVM
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2016-02-27
  • Updated: 2017-05-17
  • Resolved: 2016-03-10
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b115Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
In Throwable.getOurStackTrace(), the JDK makes an downcall to the JVM to get the stack trace depth, and once for each element of the stacktrace element.  On the vm side, we have to traverse the backtrace chunks for each element.

                int depth = getStackTraceDepth();
                stackTrace = new StackTraceElement[depth];
                for (int i = 0; i < depth; i++)
                    stackTrace[i] = getStackTraceElement(i);

If we add a depth field in the Throwable object when creating the stack trace, getStackTraceDepth can use that to create an array of StackTraceElement and call the JVM to fill in all the elements:

                int depth = getStackTraceDepth();
                stackTrace = new StackTraceElement[depth];
                for (int i = 0; i < depth; i++) {
                    stackTrace[i] = new StackTraceElement();
                }
                getStackTraceElements(stackTrace);

This would clean out the native code and allow some sharing with code that's being prototyped in the StackWalk API code using a simplified StackTraceElement::fill_in().  Also walking the internal format for Throwable.backtrace in the jvm can be simplified.

It also improves performance of Throwable.getStackTrace() and printStackTrace.

Using 100 stack trace elements in the microbenchmark for throwable:

JDK8Benchmarks.testThrowableGetStackTrace      100  avgt   20   66830.815 ��  474.345  ns/op
vs.
JDK8Benchmarks.testThrowableGetStackTrace      100  avgt   20   59471.911 ��  540.100  ns/op