RFR: 8291641: Optimize StackTraceElement.toString() [v6]

David Schlosnagle duke at openjdk.org
Tue Aug 2 15:38:05 UTC 2022


> 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

David Schlosnagle has updated the pull request incrementally with two additional commits since the last revision:

 - Mark StackTraceElement SerialTest for bug 8291641
 - Revert "Mark StackTraceElement SerialTest for bug 8291641"
   
   This reverts commit e7b04faafb026e61829c81c75121e2d3be6644d9.

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

Changes:
  - all: https://git.openjdk.org/jdk/pull/9665/files
  - new: https://git.openjdk.org/jdk/pull/9665/files/e7b04faa..c93ac5af

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=9665&range=05
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=9665&range=04-05

  Stats: 7 lines in 2 files changed: 0 ins; 4 del; 3 mod
  Patch: https://git.openjdk.org/jdk/pull/9665.diff
  Fetch: git fetch https://git.openjdk.org/jdk pull/9665/head:pull/9665

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


More information about the core-libs-dev mailing list