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

8265075: Improve and simplify Class.resolveName() #3464

Closed
wants to merge 1 commit into from

Conversation

stsypanov
Copy link
Contributor

@stsypanov stsypanov commented Apr 13, 2021

In mentioned method this code snippet

int len = baseName.length() + 1 + name.length();
StringBuilder sb = new StringBuilder(len);
name = sb.append(baseName.replace('.', '/'))
        .append('/')
        .append(name)
        .toString();

can be simplified with performance improvement as

name = baseName.replace('.', '/') + '/' + name;

Also null check of baseName can be removed as Class.getPackageName() never returns null.

This benchmark

@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(jvmArgsAppend = {"-Xms2g", "-Xmx2g"})
public class ResolveNameBenchmark {

  private final Class<? extends ResolveNameBenchmark> klazz = getClass();

  @Benchmark
  public Object original() {
    return original("com/tsypanov/ovn/ResolveNameBenchmark.class");
  }

  @Benchmark
  public Object patched() {
    return patched("com/tsypanov/ovn/ResolveNameBenchmark.class");
  }

  private String original(String name) {
    if (!name.startsWith("/")) {
      String baseName = getPackageName();
      if (baseName != null && !baseName.isEmpty()) {
        int len = baseName.length() + 1 + name.length();
        StringBuilder sb = new StringBuilder(len);
        name = sb.append(baseName.replace('.', '/'))
                .append('/')
                .append(name)
                .toString();
      }
    } else {
      name = name.substring(1);
    }
    return name;
  }

  private String patched(String name) {
      if (!name.startsWith("/")) {
          String baseName = getPackageName();
          if (!baseName.isEmpty()) {
              return baseName.replace('.', '/') + '/' + name;
          }
          return name;
      }
      return name.substring(1);
  }

  private String getPackageName() {
    return klazz.getPackageName();
  }
}

demonstrates good improvement, especially as of memory consumption:

                                            Mode  Cnt     Score     Error   Units

original                                    avgt   50    57.974 ±   0.365   ns/op
original:·gc.alloc.rate                     avgt   50  3419.447 ±  21.154  MB/sec
original:·gc.alloc.rate.norm                avgt   50   312.006 ±   0.001    B/op
original:·gc.churn.G1_Eden_Space            avgt   50  3399.396 ± 149.836  MB/sec
original:·gc.churn.G1_Eden_Space.norm       avgt   50   310.198 ±  13.628    B/op
original:·gc.churn.G1_Survivor_Space        avgt   50     0.004 ±   0.001  MB/sec
original:·gc.churn.G1_Survivor_Space.norm   avgt   50    ≈ 10⁻³              B/op
original:·gc.count                          avgt   50   208.000            counts
original:·gc.time                           avgt   50   224.000                ms

patched                                     avgt   50    44.367 ±   0.162   ns/op
patched:·gc.alloc.rate                      avgt   50  2749.265 ±  10.014  MB/sec
patched:·gc.alloc.rate.norm                 avgt   50   192.004 ±   0.001    B/op
patched:·gc.churn.G1_Eden_Space             avgt   50  2729.221 ± 193.552  MB/sec
patched:·gc.churn.G1_Eden_Space.norm        avgt   50   190.615 ±  13.539    B/op
patched:·gc.churn.G1_Survivor_Space         avgt   50     0.003 ±   0.001  MB/sec
patched:·gc.churn.G1_Survivor_Space.norm    avgt   50    ≈ 10⁻⁴              B/op
patched:·gc.count                           avgt   50   167.000            counts
patched:·gc.time                            avgt   50   181.000                ms

Progress

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

Issue

  • JDK-8265075: Improve and simplify Class.resolveName()

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 3464

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 13, 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 Apr 13, 2021
@openjdk
Copy link

openjdk bot commented Apr 13, 2021

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

  • core-libs

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 core-libs core-libs-dev@openjdk.org label Apr 13, 2021
@mlbridge
Copy link

mlbridge bot commented Apr 13, 2021

Webrevs

@mlbridge
Copy link

mlbridge bot commented Apr 13, 2021

Mailing list message from Peter Levart on core-libs-dev:

Hi, Sergey!

Have you measured the code change in the java.lang.Class itself or just
equivalent code in the JMH test as you show us?

The JMH test may show better results as it is compiled to bytecode that
uses special invokedynamic-based string concatenation with optimal MH
based underlying strategy. The code in java.lang.Class can't be compiled
to use this kind of concatenation because of bootstraping issues and is
therefore compiled to bytecode that uses StringBuilder directly (much
like the existing code of the patched method). So I'm wondering whether
the improvement in speed is actually there...

Regards, Peter

On 4/13/21 2:55 PM, ?????? ??????? wrote:

@stsypanov
Copy link
Contributor Author

@plevart you are right, when the method is called from java.lang.Class there's no improvement observed. I'll close the PR then along with the ticket

@AlanBateman
Copy link
Contributor

We can remove the check for baseName being null, that's a left over from iteration during JDK 9.

@stsypanov
Copy link
Contributor Author

@AlanBateman should I then do it only for Class.resolveName() or everywhere we call Class.getPackageName()?

@AlanBateman
Copy link
Contributor

If you have the time, then looking for usages of getPackageName where it checks for null would be useful. We can close this PR and open a new issue/PR for that cleanup.

@stsypanov
Copy link
Contributor Author

Ok, then I close this one

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 rfr Pull request is ready for review
2 participants