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

Eirik Bjørsnøs eirbjo at openjdk.org
Sat Apr 26 08:44:47 UTC 2025


On Sat, 26 Apr 2025 07:01:46 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%.
>
> Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:
> 
>   fix code style

> But that's not what's happening here. While _throwing_ exceptions **can** be performance-sensitive, _printing_ exceptions is far less important. I'm not convinced that printing exceptions is a very typical scenario.

Even if that was the case, for any printing to be useful to any human beeing it would require IO to disk, database or have some form of external effect rather than just printing it to a NullOutputStream which is what the benchmark here does. Factor IO into the equation and my guess is those 10% are a lot closer to zero. 

Doing the wrong thing faster is not the way to go.

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

PR Comment: https://git.openjdk.org/jdk/pull/24864#issuecomment-2831966169


More information about the core-libs-dev mailing list