RFR: 8355636: Speed ​​up Throwable::printStackTrace by reducing object allocation

Shaojin Wen swen at openjdk.org
Sat Apr 26 04:04:31 UTC 2025


On Fri, 25 Apr 2025 03:16:09 GMT, Chen Liang <liach at openjdk.org> wrote:

>> In the Throwable::printStackTrace method, StringBuilder is created multiple times to build String. By sharing StringBuilder to build String, object allocation and copying are reduced.
>> 
>> In the scenario without suppressed and ourCause, unused IdentityHashMap is not created.
>> 
>> Through these optimizations, the performance of `new Exception().printStackTrace()` can be improved by about 10%.
>
> src/java.base/share/classes/java/lang/Throwable.java line 749:
> 
>> 747:             // Print suppressed exceptions, if any
>> 748:             for (Throwable se : getSuppressed())
>> 749:                 se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION,
> 
> Shouldn't SUPPRESSED_CAPTION be added a prefix?
> 
> I think we should revert the `prefixCaption` argument change; you should add a line `String prefixCaption = prefix.concat(caption);` instead.

The reason for using prefixCaption is that javac can do constant folding when it is placed at the call site of the printStackTrace method.

> src/java.base/share/classes/java/lang/Throwable.java line 785:
> 
>> 783:             StackTraceElement[] trace,
>> 784:             int traceEnd,
>> 785:             int framesInCommon) {
> 
> You just need to pass `framesInCommon`, can calculate `int limit = trace.length - framesInCommon;` and then for the loop below, `for (int i = 0; i < limit; i++)` instead of using `traceEnd`

According to your suggestion, I removed the passing of framesInCommon, which can be calculated by `trace.length - 1 - traceEnd`

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/24864#discussion_r2059513887
PR Review Comment: https://git.openjdk.org/jdk/pull/24864#discussion_r2059515401


More information about the core-libs-dev mailing list