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

Hendrik Schick duke at openjdk.org
Sat Apr 26 04:04:30 UTC 2025


On Fri, 25 Apr 2025 01:36:50 GMT, Shaojin Wen <swen 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 712:

> 710: 
> 711:                 // Print cause, if any
> 712: 

Suggestion:

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

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


More information about the core-libs-dev mailing list