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

Shaojin Wen swen at openjdk.org
Sat Apr 26 09:47:46 UTC 2025


On Sat, 26 Apr 2025 09:21:32 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 benchmark, use FileOutputStream

Below are the performance numbers on Aliyun ECS c8a (x64 AMD CPU EPYC™ Genoa + Linux), The results show that writing FileOutputStream on the cloud host still has a performance improvement of 5% to 7%.


git remote addd wenshao https://github.com/wenshao/jdk

# baseline
git checkout 53873fb2178add153d1a6edb932a915fc1c63dc6
make test TEST="micro:java.lang.Throwables"

# current
git checkout e40726c1c0dfcbc284010b187e3203617afef519
make test TEST="micro:java.lang.Throwables"




# baseline 53873fb2178add153d1a6edb932a915fc1c63dc6
-Benchmark                           Mode  Cnt      Score     Error  Units
-Throwables.printEnclosedStackTrace  avgt   15  60816.939 ± 556.481  ns/op
-Throwables.printStackTrace          avgt   15  98745.841 ± 680.632  ns/op

# current e40726c1c0dfcbc284010b187e3203617afef519
Benchmark                           Mode  Cnt      Score      Error  Units
+Throwables.printEnclosedStackTrace  avgt   15  56417.649 ± 2310.963  ns/op +7.79%
+Throwables.printStackTrace          avgt   15  93265.294 ± 3014.466  ns/op +5.87%

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

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


More information about the core-libs-dev mailing list