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

8267840: Improve URLStreamHandler.parseURL() #4526

Closed
wants to merge 4 commits into from

Conversation

stsypanov
Copy link
Contributor

@stsypanov stsypanov commented Jun 18, 2021

There is an optimization opportunity for the widespread use-case when a resource is read from classpath using getClass().getClassLoader().getResource() or getClass().getClassLoader().getResourceAsStream().

Pay attention to lines starting from 261. In case I run something like

var props = getClass().getClassLoader().getResource("/application.properties");

I get into the if-else block starting from 251 and here 'separator' variable is an empty String. In this case we can skip 'separator' from concatenation chain and use String.concat() as there are only two items concatenated.

In the opposite case separator variable is "/" and at the same time ind variable is -1. This means that expression path.substring(0, ind + 1) always returns an empty String and again can be excluded from concatenation chain allowing usage of String.concat() which allows to dodge utilization of StringBuilder (here StringConcatFactory is not available, see #3627)

In the next else-block, starting from 274, again, String.concat() is applicable.

In another if-else block, starting from 277, when id is 0 again path.substring(0, ind) returns empty String making concatenation pointless and avoidable.

There are also some other minor clean-ups possible regarding constant conditions (lines 252 and 161).

The change allows to reduce significantly resource look-up costs for a very wide-spread case:

@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(jvmArgsAppend = {"-Xms2g", "-Xmx2g"})
public class ClassGetResourceBenchmark {
    private final Class<?> clazz = getClass();

    @Benchmark
    public URL getResource() {
        return clazz.getResource("/application.properties");
    }
}

The change allows to reduce memory consumption significantly:

before

Benchmark                                                               Mode  Cnt     Score     Error   Units
ClassGetResourceBenchmark.getResource                                   avgt  100  1649,367 ±   5,904   ns/op
ClassGetResourceBenchmark.getResource:·gc.alloc.rate                    avgt  100   619,204 ±   2,413  MB/sec
ClassGetResourceBenchmark.getResource:·gc.alloc.rate.norm               avgt  100  1339,232 ±   4,909    B/op
ClassGetResourceBenchmark.getResource:·gc.churn.G1_Eden_Space           avgt  100   627,192 ±  74,972  MB/sec
ClassGetResourceBenchmark.getResource:·gc.churn.G1_Eden_Space.norm      avgt  100  1356,681 ± 162,354    B/op
ClassGetResourceBenchmark.getResource:·gc.churn.G1_Survivor_Space       avgt  100     0,119 ±   0,100  MB/sec
ClassGetResourceBenchmark.getResource:·gc.churn.G1_Survivor_Space.norm  avgt  100     0,257 ±   0,217    B/op
ClassGetResourceBenchmark.getResource:·gc.count                         avgt  100   128,000            counts
ClassGetResourceBenchmark.getResource:·gc.time                          avgt  100   227,000                ms

after

Benchmark                                                               Mode  Cnt     Score     Error   Units
ClassGetResourceBenchmark.getResource                                   avgt  100  1599,948 ±   4,115   ns/op
ClassGetResourceBenchmark.getResource:·gc.alloc.rate                    avgt  100   358,434 ±   0,922  MB/sec
ClassGetResourceBenchmark.getResource:·gc.alloc.rate.norm               avgt  100   752,016 ±   0,004    B/op
ClassGetResourceBenchmark.getResource:·gc.churn.G1_Eden_Space           avgt  100   342,778 ±  76,490  MB/sec
ClassGetResourceBenchmark.getResource:·gc.churn.G1_Eden_Space.norm      avgt  100   719,264 ± 160,513    B/op
ClassGetResourceBenchmark.getResource:·gc.churn.G1_Survivor_Space       avgt  100     0,008 ±   0,005  MB/sec
ClassGetResourceBenchmark.getResource:·gc.churn.G1_Survivor_Space.norm  avgt  100     0,017 ±   0,010    B/op
ClassGetResourceBenchmark.getResource:·gc.count                         avgt  100    70,000            counts
ClassGetResourceBenchmark.getResource:·gc.time                          avgt  100   151,000                ms

Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/4526/head:pull/4526
$ git checkout pull/4526

Update a local copy of the PR:
$ git checkout pull/4526
$ git pull https://git.openjdk.java.net/jdk pull/4526/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 4526

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/4526.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 18, 2021

👋 Welcome back stsypanov! 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 openjdk bot added the rfr Pull request is ready for review label Jun 18, 2021
@openjdk
Copy link

openjdk bot commented Jun 18, 2021

@stsypanov The following label will be automatically applied to this pull request:

  • net

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

@openjdk openjdk bot added the net net-dev@openjdk.org label Jun 18, 2021
@mlbridge
Copy link

mlbridge bot commented Jun 18, 2021

Webrevs

@dfuch
Copy link
Member

dfuch commented Jun 21, 2021

Hi Sergey,

The logics seems correct - but what test did you run? have you run tier1 and tier2?

best regards,
-- daniel

@stsypanov
Copy link
Contributor Author

Hi Daniel, tier1 and tier2 is ok in pipeline, but locally I'm facing

Test selection 'jdk:tier1', will run:
* jtreg:test/jdk:tier1

Running test 'jtreg:test/jdk:tier1'
Error: Unexpected exception occurred! java.lang.IllegalArgumentException: 6+1
java.lang.IllegalArgumentException: 6+1
	at com.sun.javatest.regtest.tool.Version.<init>(Version.java:80)
	at com.sun.javatest.regtest.config.TestProperties$Cache$Entry.initAllowSmartActionArgs(TestProperties.java:452)
	at com.sun.javatest.regtest.config.TestProperties$Cache$Entry.<init>(TestProperties.java:262)
	at com.sun.javatest.regtest.config.TestProperties$Cache.getEntryInternal(TestProperties.java:509)
	at com.sun.javatest.regtest.config.TestProperties$Cache.getEntry(TestProperties.java:500)
	at com.sun.javatest.regtest.config.TestProperties.<init>(TestProperties.java:57)
	at com.sun.javatest.regtest.config.RegressionTestSuite.<init>(RegressionTestSuite.java:80)
	at com.sun.javatest.regtest.config.RegressionTestSuite.open(RegressionTestSuite.java:65)
	at com.sun.javatest.regtest.config.TestManager.getTestSuites(TestManager.java:165)
	at com.sun.javatest.regtest.tool.Tool.run(Tool.java:1124)
	at com.sun.javatest.regtest.tool.Tool.run(Tool.java:1075)
	at com.sun.javatest.regtest.tool.Tool.main(Tool.java:147)
	at com.sun.javatest.regtest.Main.main(Main.java:58)
Finished running test 'jtreg:test/jdk:tier1'
Test report is stored in build/linux-x86_64-server-release/test-results/jtreg_test_jdk_tier1

I configure and run tests with

> bash configure --with-boot-jdk=/home/s.tsypanov/jdks/jdk-16.0.1 --with-jtreg=/home/s.tsypanov/jtreg5
> make test TEST=jdk:tier1 && make test TEST=jdk:tier2

@dfuch
Copy link
Member

dfuch commented Jun 22, 2021

Hi Sergey,

That exception means you're using an obsolete version of jtreg: you need jtreg-6+1 now.

best regards,
-- daniel

@stsypanov
Copy link
Contributor Author

@dfuch I've fixed the issue and retested the changes, tier1 is ok, in tier2 some IPv6 tests on my machine are failing, but they fail both on master and 8267840 branches, so seem to be unrelated to the change.

if (ind < 0) {
path = "/";
} else {
path = path.substring(0, ind).concat("/");
Copy link
Member

Choose a reason for hiding this comment

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

would that be equivalent to

path = path.substring(0, ind + 1);

given that ind = path.lastIndexOf('/') ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, fixed!

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 2, 2021

@stsypanov This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@stsypanov
Copy link
Contributor Author

@stsypanov This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

Let's wait

@dfuch
Copy link
Member

dfuch commented Aug 4, 2021

Thanks for waiting - and sorry for the delay. I want to send this PR through our test system before approving, but things LGTM so far.

@openjdk
Copy link

openjdk bot commented Aug 4, 2021

⚠️ @stsypanov the full name on your profile does not match the author name in this pull requests' HEAD commit. If this pull request gets integrated then the author name from this pull requests' HEAD commit will be used for the resulting commit. If you wish to push a new commit with a different author name, then please run the following commands in a local repository of your personal fork:

$ git checkout 8267840
$ git commit -c user.name='Preferred Full Name' --allow-empty -m 'Update full name'
$ git push

@openjdk
Copy link

openjdk bot commented Aug 4, 2021

@stsypanov 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:

8267840: Improve URLStreamHandler.parseURL()

Reviewed-by: dfuchs, redestad

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 471 new commits pushed to the master branch:

  • 55bd52a: 8271840: Add simple Integer.toString microbenchmarks
  • 18dd4d4: 8271121: ZGC: stack overflow (segv) when -Xlog:gc+start=debug
  • 685fc3c: 8270903: sun.net.httpserver.HttpConnection: Improve toString
  • 4abe531: 8271722: [TESTBUG] gc/g1/TestMixedGCLiveThreshold.java can fail if G1 Full GC uses >1 workers
  • ea9a595: 8270058: Use Objects.check{Index,FromIndexSize} for java.desktop
  • 64d18d4: 4819544: SwingSet2 JTable Demo throws NullPointerException
  • 6c8441f: 8271878: UnProblemList jdk/jfr/event/gc/detailed/TestEvacuationFailedEvent.java in JDK18
  • 7728423: 8271895: UnProblemList javax/swing/JComponent/7154030/bug7154030.java in JDK18
  • cd6b54e: Merge
  • cebcc07: 8271894: ProblemList javax/swing/JComponent/7154030/bug7154030.java in JDK17
  • ... and 461 more: https://git.openjdk.java.net/jdk/compare/a0f32cb1406e4957e84befd9b68444adb662bd13...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.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@dfuch, @cl4es) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 4, 2021
@stsypanov
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Aug 4, 2021
@openjdk
Copy link

openjdk bot commented Aug 4, 2021

@stsypanov
Your change (at version 2d2e1b5) is now ready to be sponsored by a Committer.

Copy link
Member

@cl4es cl4es left a comment

Choose a reason for hiding this comment

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

LGTM

/sponsor

@cl4es
Copy link
Member

cl4es commented Aug 5, 2021

/sponsor

@openjdk
Copy link

openjdk bot commented Aug 5, 2021

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

  • 55bd52a: 8271840: Add simple Integer.toString microbenchmarks
  • 18dd4d4: 8271121: ZGC: stack overflow (segv) when -Xlog:gc+start=debug
  • 685fc3c: 8270903: sun.net.httpserver.HttpConnection: Improve toString
  • 4abe531: 8271722: [TESTBUG] gc/g1/TestMixedGCLiveThreshold.java can fail if G1 Full GC uses >1 workers
  • ea9a595: 8270058: Use Objects.check{Index,FromIndexSize} for java.desktop
  • 64d18d4: 4819544: SwingSet2 JTable Demo throws NullPointerException
  • 6c8441f: 8271878: UnProblemList jdk/jfr/event/gc/detailed/TestEvacuationFailedEvent.java in JDK18
  • 7728423: 8271895: UnProblemList javax/swing/JComponent/7154030/bug7154030.java in JDK18
  • cd6b54e: Merge
  • cebcc07: 8271894: ProblemList javax/swing/JComponent/7154030/bug7154030.java in JDK17
  • ... and 461 more: https://git.openjdk.java.net/jdk/compare/a0f32cb1406e4957e84befd9b68444adb662bd13...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot closed this Aug 5, 2021
@openjdk openjdk bot added integrated Pull request has been integrated and removed ready Pull request is ready to be integrated labels Aug 5, 2021
@openjdk openjdk bot removed rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Aug 5, 2021
@openjdk
Copy link

openjdk bot commented Aug 5, 2021

@cl4es @stsypanov Pushed as commit d7fc9e4.

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

@stsypanov stsypanov deleted the 8267840 branch August 5, 2021 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated net net-dev@openjdk.org
3 participants