On Thu, 30 May 2024 13:50:33 GMT, Jan Lahoda <[email protected]> wrote:
> Consider these two programs:
>
>
> public class SystemPrint {
> public static void main(String... args) {
> System.err.println("Hello!");
> }
> }
>
> and:
>
> public class IOPrint {
> public static void main(String... args) {
> java.io.IO.println("Hello!");
> }
> }
>
>
> They do the same conceptual thing - write a text to the output. But,
> `IO.println` delegates to `Console.println`, which then delegates to a
> `Console` backend, and the default backend is currently based on JLine.
>
> The issues is that JLine takes a quite a long time to initialize, and in a
> program like this, JLine is not really needed - it is used to provide better
> editing experience when reading input, but there's no reading in these
> programs.
>
> For example, on my computer:
>
> $ time java -classpath /tmp SystemPrint
> Hello!
>
> real 0m0,035s
> user 0m0,019s
> sys 0m0,019s
>
> $ time java -classpath /tmp --enable-preview IOPrint
> Hello!
>
> real 0m0,165s
> user 0m0,324s
> sys 0m0,042s
>
>
> The proposal herein is to delegate to the simpler `Console` backend from
> `java.base` as long as the user only uses methods that print to output, and
> switch to the JLine delegate when other methods (typically input) is used.
> Note that while technically `writer()` is a method doing output, it will
> force JLine initialization to avoid possible problems if the client caches
> the writer and uses it after switching the delegates.
>
> With this patch, I can get timing like this:
>
> $ time java --enable-preview -classpath /tmp/ IOPrint
> Hello!
>
> real 0m0,051s
> user 0m0,038s
> sys 0m0,020s
>
>
> which seems much more acceptable.
>
> There is also #19467, which may reduce the time further.
>
> A future work might focus on making JLine initialize faster, but avoiding
> JLine initialization in case where we don't need it seems like a good step to
> me in any case.
It looks like a legit performance trick.
-------------
Marked as reviewed by asotona (Reviewer).
PR Review: https://git.openjdk.org/jdk/pull/19479#pullrequestreview-2096720909