Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8333086: Using Console.println is unnecessarily slow due to JLine initalization #19479

Closed
wants to merge 6 commits into from

Conversation

lahodaj
Copy link
Contributor

@lahodaj lahodaj commented May 30, 2024

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.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8333086: Using Console.println is unnecessarily slow due to JLine initalization (Bug - P3)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/19479/head:pull/19479
$ git checkout pull/19479

Update a local copy of the PR:
$ git checkout pull/19479
$ git pull https://git.openjdk.org/jdk.git pull/19479/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 19479

View PR using the GUI difftool:
$ git pr show -t 19479

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/19479.diff

Webrev

Link to Webrev Comment

Sorry, something went wrong.

@bridgekeeper
Copy link

bridgekeeper bot commented May 30, 2024

👋 Welcome back jlahoda! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented May 30, 2024

@lahodaj This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8333086: Using Console.println is unnecessarily slow due to JLine initalization

Reviewed-by: asotona, naoto

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 173 new commits pushed to the master branch:

  • 9b3694c: 8319822: Use a linear-time algorithm for assert_different_registers()
  • f73922b: 8333235: vmTestbase/nsk/jdb/kill/kill001/kill001.java fails with C1
  • 5dcb7a6: 8160755: bug6492108.java test fails with exception Image comparison failed at (0, 0) for image 4 in GTK L&F
  • 438121b: 8332785: Replace naked uses of UseSharedSpaces with CDSConfig::is_using_archive
  • d7d1afb: 8206447: InflaterInputStream.skip receives long but it's limited to Integer.MAX_VALUE
  • 7acfba2: 8327650: Test java/nio/channels/DatagramChannel/StressNativeSignal.java timed out
  • c5c0867: 8333252: C2: assert(assertion_predicate_has_loop_opaque_node(iff)) failed: must find OpaqueLoop* nodes
  • d85b0ca: 8332457: Examine startup overheads from JDK-8294961
  • 326dbb1: 8312436: CompletableFuture never completes when 'Throwable.toString()' method throws Exception
  • 9a8096f: 8330047: ASAN build error with gcc 13
  • ... and 163 more: https://git.openjdk.org/jdk/compare/08d51003d142e89b9d2f66187a4ea50e12b94fbb...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the rfr Pull request is ready for review label May 30, 2024
@openjdk
Copy link

openjdk bot commented May 30, 2024

@lahodaj The following labels will be automatically applied to this pull request:

  • core-libs
  • kulla

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added core-libs core-libs-dev@openjdk.org kulla kulla-dev@openjdk.org labels May 30, 2024
@mlbridge
Copy link

mlbridge bot commented May 30, 2024

Webrevs

Copy link
Member

@asotona asotona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like a legit performance trick.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 4, 2024
Copy link
Member

@naotoj naotoj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good with a minor nit.

for (TestCase tc : testCases) {
ProcessBuilder builder =
ProcessTools.createTestJavaProcessBuilder("--enable-preview",
"-verbose:class",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to explicitly specify -Djdk.console=jdk.internal.le

Copy link
Member

@naotoj naotoj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the changes.

@lahodaj
Copy link
Contributor Author

lahodaj commented Jun 5, 2024

/integrate

@openjdk
Copy link

openjdk bot commented Jun 5, 2024

Going to push as commit f7dbb98.
Since your change was applied there have been 173 commits pushed to the master branch:

  • 9b3694c: 8319822: Use a linear-time algorithm for assert_different_registers()
  • f73922b: 8333235: vmTestbase/nsk/jdb/kill/kill001/kill001.java fails with C1
  • 5dcb7a6: 8160755: bug6492108.java test fails with exception Image comparison failed at (0, 0) for image 4 in GTK L&F
  • 438121b: 8332785: Replace naked uses of UseSharedSpaces with CDSConfig::is_using_archive
  • d7d1afb: 8206447: InflaterInputStream.skip receives long but it's limited to Integer.MAX_VALUE
  • 7acfba2: 8327650: Test java/nio/channels/DatagramChannel/StressNativeSignal.java timed out
  • c5c0867: 8333252: C2: assert(assertion_predicate_has_loop_opaque_node(iff)) failed: must find OpaqueLoop* nodes
  • d85b0ca: 8332457: Examine startup overheads from JDK-8294961
  • 326dbb1: 8312436: CompletableFuture never completes when 'Throwable.toString()' method throws Exception
  • 9a8096f: 8330047: ASAN build error with gcc 13
  • ... and 163 more: https://git.openjdk.org/jdk/compare/08d51003d142e89b9d2f66187a4ea50e12b94fbb...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jun 5, 2024
@openjdk openjdk bot closed this Jun 5, 2024
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jun 5, 2024
@openjdk
Copy link

openjdk bot commented Jun 5, 2024

@lahodaj Pushed as commit f7dbb98.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated kulla kulla-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.

None yet

3 participants