RFR: 8355636: Speed up Throwable::printStackTrace by reducing object allocation [v3]
Shaojin Wen
swen at openjdk.org
Sat Apr 26 07:30:45 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
This PR has added a testcase.
This patch is safe and does not use Unsafe and SharedSecrets. This optimization is to share StringBuilder as Line Buffer when printStackTrace prints each line.
The changes in this PR are actually not many, and the logic has not changed. The goal of the optimization is also very clear, which is to make the program perform better when a large number of exceptions are thrown when an error occurs. This is a very typical scenario.
This optimization has taken compatibility into consideration to ensure that the behavior is the same as before. For example, StackTraceElement.toString remains unchanged and calls PrintStreamOrWriter::println(Object) instead of PrintStreamOrWriter::println(String).
I hope the discussion can return to this PR and discuss specific technical points instead of philosophy. For example, discuss which line of code is bad, unsafe, not maintainable, and has a better way to write it.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/24864#issuecomment-2831929122
More information about the core-libs-dev
mailing list