Integrated: 8291641: Optimize StackTraceElement.toString()

David Schlosnagle duke at openjdk.org
Wed Aug 10 16:26:07 UTC 2022


On Wed, 27 Jul 2022 22:56:39 GMT, David Schlosnagle <duke at openjdk.org> wrote:

> I would like to contribute an optimized version of `StackTraceElement#toString()` that uses a single StringBuilder throughout creation to avoid intermediate `String` allocations. `StackTraceElement#toString()` is used in a number of JDK code paths including `Throwable#printStackTrace()`, as well as many JDK consumers may transform `StackTraceElement` `toString()` in logging frameworks capturing throwables and exceptions, and diagnostics performing dumps.
> 
> Given this usage and some observed JFR profiles from production services, I'd like to reduce the intermediate allocations to reduce CPU pressure in these circumstances. I have added a couple benchmarks for a sample `Throwable#printStackTrace()` converted to String via `StringWriter` and individual `StackTraceElement` `toString`. The former shows ~15% improvement, while the latter shows ~40% improvement.
> 
> Before
> 
> Benchmark                               Mode  Cnt       Score      Error  Units
> StackTraceElementBench.printStackTrace  avgt   15  167147.066 ± 4260.521  ns/op
> StackTraceElementBench.toString         avgt   15     132.781 ±    2.095  ns/op
> 
> 
> After
> 
> Benchmark                               Mode  Cnt       Score      Error  Units
> StackTraceElementBench.printStackTrace  avgt   15  142909.133 ± 2290.720  ns/op
> StackTraceElementBench.toString         avgt   15      78.939 ±    0.469  ns/op

This pull request has now been integrated.

Changeset: 9825c335
Author:    David Schlosnagle <davids at palantir.com>
Committer: Mandy Chung <mchung at openjdk.org>
URL:       https://git.openjdk.org/jdk/commit/9825c3356068feba8dde8217e8f8a56c9c573da4
Stats:     137 lines in 3 files changed: 121 ins; 4 del; 12 mod

8291641: Optimize StackTraceElement.toString()

Reviewed-by: rriggs, mchung

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

PR: https://git.openjdk.org/jdk/pull/9665


More information about the core-libs-dev mailing list