RFR: 8291641: Optimize StackTraceElement.toString()
David Schlosnagle
duke at openjdk.org
Mon Aug 1 17:31:50 UTC 2022
On Thu, 28 Jul 2022 01:23:53 GMT, David Schlosnagle <duke at openjdk.org> wrote:
>> src/java.base/share/classes/java/lang/StackTraceElement.java line 374:
>>
>>> 372: * @throws IOException If an I/O error occurs
>>> 373: */
>>> 374: private void appendTo(Appendable dest) throws IOException {
>>
>> Perhaps this could be package-private for reuse by `Throwable.printStackTrace`, avoiding the intermediate toString.
>
> Yes, I think the `PrintStreamOrWriter` could implement `Appendable` as both `PrintStream` and `PrintWriter` implement `Appendable`.
>
> https://github.com/openjdk/jdk/blob/348a0521e1cd602c4093955310f838cf4ce4daae/src/java.base/share/classes/java/lang/Throwable.java#L756
>
> This would also potentially allow optimizing away the additional intermediate strings from prefixing the lines with `"\tat "` concatenated with the `StackTraceElement.toString()`
>
> https://github.com/openjdk/jdk/blob/348a0521e1cd602c4093955310f838cf4ce4daae/src/java.base/share/classes/java/lang/Throwable.java#L686-L687
>
> https://github.com/openjdk/jdk/blob/348a0521e1cd602c4093955310f838cf4ce4daae/src/java.base/share/classes/java/lang/Throwable.java#L724-L725
Just making notes for the future: having `PrintStreamOrWriter implement Appendable` works, but slightly messy due to `append` methods throwing checked `IOException` so these would likely need to catch & ignore. Given `Throwable.printStackTrace` is typically on an error handling path, if the `PrintStreamOrWriter` error stream/writer is not functioning properly, I imagine one would want to know but may be prevented from knowing.
-------------
PR: https://git.openjdk.org/jdk/pull/9665
More information about the core-libs-dev
mailing list