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

8266622: Optimize Class.descriptorString() and Class.getCanonicalName0() #3903

Closed
wants to merge 2 commits into from

Conversation

stsypanov
Copy link
Contributor

@stsypanov stsypanov commented May 6, 2021

Hello, from discussion in #3464 and #2212 it appears, that in j.l.Class expressions like

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

are not compiled into invokedynamic-based code, but into one using StringBuilder.

This happens due to some bootstraping issues. Currently the bytecode for the last (most often used) branch of Class.descriptorString() looks like

public sb()Ljava/lang/String;
   L0
    LINENUMBER 21 L0
    NEW java/lang/StringBuilder
    DUP
    INVOKESPECIAL java/lang/StringBuilder.<init> ()V
    ASTORE 1
   L1
    LINENUMBER 23 L1
    ALOAD 1
    LDC "a"
    INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
    POP
   L2
    LINENUMBER 24 L2
    ALOAD 1
    LDC "b"
    INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
    POP
   L3
    LINENUMBER 26 L3
    ALOAD 1
    INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
    ARETURN

Here the StringBuilder is created with default constructor and then expands if necessary while appending.

This can be improved by manually allocating StringBuilder of exact size. The benchmark demonstrates measurable improvement:

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

    private final Class<?> clazzWithShortDescriptor = Object.class;
    private final Class<?> clazzWithLongDescriptor = getClass();

    @Benchmark
    public String descriptorString_short() {
        return clazzWithShortDescriptor.descriptorString();
    }

    @Benchmark
    public String descriptorString_long() {
        return clazzWithLongDescriptor.descriptorString();
    }
}
original
-Xint
                                               Mode     Score     Error   Units
descriptorString_long                          avgt  6326.478 ± 107.251   ns/op
descriptorString_short                         avgt  5220.729 ± 103.545   ns/op
descriptorString_long:·gc.alloc.rate.norm      avgt   528.089 ±   0.021    B/op
descriptorString_short:·gc.alloc.rate.norm     avgt   232.036 ±   0.015    B/op

-XX:TieredStopAtLevel=1
                                               Mode      Score    Error   Units
descriptorString_long                          avgt    230.223 ±  1.254   ns/op
descriptorString_short                         avgt    164.255 ±  0.755   ns/op
descriptorString_long:·gc.alloc.rate.norm      avgt    528.046 ±  0.002    B/op
descriptorString_short:·gc.alloc.rate.norm     avgt    232.022 ±  0.001    B/op

full
                                               Mode      Score     Error   Units
descriptorString_long                          avgt     74.835 ±   0.262   ns/op
descriptorString_short                         avgt     43.822 ±   0.788   ns/op
descriptorString_long:·gc.alloc.rate.norm      avgt    504.010 ±   0.001    B/op
descriptorString_short:·gc.alloc.rate.norm     avgt    208.004 ±   0.001    B/op

------------------------
patched
-Xint
                                               Mode      Score     Error   Units
descriptorString_long                          avgt   4485.994 ±  60.173   ns/op
descriptorString_short                         avgt   3949.965 ± 278.143   ns/op
descriptorString_long:·gc.alloc.rate.norm      avgt    336.051 ±   0.004    B/op
descriptorString_short:·gc.alloc.rate.norm     avgt    184.027 ±   0.010    B/op

-XX:TieredStopAtLevel=1
                                               Mode        Score    Error   Units
descriptorString_long                          avgt      185.774 ±  1.100   ns/op
descriptorString_short                         avgt      135.338 ±  1.066   ns/op
descriptorString_long:·gc.alloc.rate.norm      avgt      336.030 ±  0.001    B/op
descriptorString_short:·gc.alloc.rate.norm     avgt      184.019 ±  0.001    B/op

full
                                               Mode      Score     Error   Units
descriptorString_long                          avgt     42.864 ±   0.160   ns/op
descriptorString_short                         avgt     27.255 ±   0.381   ns/op
descriptorString_long:·gc.alloc.rate.norm      avgt    224.005 ±   0.001    B/op
descriptorString_short:·gc.alloc.rate.norm     avgt    120.002 ±   0.001    B/op

Same can be done also for Class.isHidden() branch in Class.descriptorString() and for Class.getCanonicalName0()


Progress

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

Issue

  • JDK-8266622: Optimize Class.descriptorString() and Class.getCanonicalName0()

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 3903

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

Using diff file

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

@bridgekeeper
Copy link

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

openjdk bot commented May 6, 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 May 6, 2021
@mlbridge
Copy link

mlbridge bot commented May 6, 2021

Webrevs

@plevart
Copy link
Contributor

plevart commented May 6, 2021

Hi Sergei,
You are right that what javac generates is sub-optimal as it doesn't take into account the possible known final lenght of the string. So manually doing so is better. Since your 1st attempt a patch for String.join() method improved it quite a bit and is now not using StringBuilder under the hood any more. Could you try to measure for example the following:

String str = String.join("/", baseName.replace('.', '/'), name);

as an alternative to:

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

or for example:

            return String.join(
                        "", // delimiter
                        "L", name.substring(0, index).replace('.', '/'),
                        ".", name.substring(index+1), ";");

as an alternative for:

            return "L" + name.substring(0, index).replace('.', '/')
                       + "." + name.substring(index+1) + ";";

and see how it compares?

Regards, Peter

@stsypanov
Copy link
Contributor Author

@plevart hi, to my surprise String.join() makes it worse:

                                                         Mode  Cnt     Score     Error   Units
descriptorString_long                                    avgt   50    77.644 ±   0.846   ns/op
descriptorString_short                                   avgt   50    61.591 ±   2.044   ns/op

descriptorString_long:·gc.alloc.rate.norm                avgt   50   288.006 ±   0.001    B/op
descriptorString_short:·gc.alloc.rate.norm               avgt   50   184.004 ±   0.001    B/op

@plevart
Copy link
Contributor

plevart commented May 7, 2021

@plevart hi, to my surprise String.join() makes it worse:

Yeah, it seems JIT does a very good job with StringBuilder in this form while the overheads of redundant byte[] copying doesn't show yet at string lengths typical for class names.

@stsypanov
Copy link
Contributor Author

Together with #3627 this allows to reduce minimalistic Spring Boot application start-up time from 653 to 645 milliseconds and memory consumprion from 43804 to 43668 kB.

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.

I'm a bit apprehensive about desugaring like this, but if as you claim it's linked to a decent Spring Boot startup gain then I think we should accept it.

.append('L')
.append(name.substring(0, index).replace('.', '/'))
.append('.')
.append(name.substring(index + 1))
Copy link
Member

Choose a reason for hiding this comment

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

.append(name, index + 1, name.length()) might be a small win here, but it might be hard to benchmark this branch since it's only for hidden classes.

Copy link
Contributor Author

@stsypanov stsypanov May 13, 2021

Choose a reason for hiding this comment

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

Indeed, I've measured the case for hidden classes with benchmark

@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class HiddenClassBenchmark {
  private Class<?> hiddenClass;

  @Setup
  public void setUp() throws Exception {
    byte[] bytes = getClassWriter().toByteArray();

    hiddenClass = MethodHandles
            .lookup()
            .defineHiddenClass(bytes, true, NESTMATE)
            .lookupClass();

    if (hiddenClass.isHidden()) {
      return;
    }
    throw new RuntimeException();
  }

  @Benchmark
  public String descriptorString() {
    return hiddenClass.descriptorString();
  }

  private static ClassWriter getClassWriter() {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);

    var name = HiddenClassDemo.class.getName().replace('.', '/');
    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, name, null, "java/lang/Object", null);
    cw.visitEnd();
    return cw;
  }

  private static class HiddenClassDemo {
  }
}

and got those results:

jdk 16
Benchmark                                                               Mode  Cnt     Score    Error   Units
HiddenClassBenchmark.descriptorString                                   avgt  100   112.591 ±  1.320   ns/op
HiddenClassBenchmark.descriptorString:·gc.alloc.rate.norm               avgt  100   600.045 ±  0.001    B/op

patched

Benchmark                                                               Mode  Cnt     Score    Error   Units
HiddenClassBenchmark.descriptorString                                   avgt  100    85.958 ±  0.561   ns/op
HiddenClassBenchmark.descriptorString:·gc.alloc.rate.norm               avgt  100   448.034 ±  0.001    B/op

patched without substring

Benchmark                                                               Mode  Cnt     Score    Error   Units
HiddenClassBenchmark.descriptorString                                   avgt  100    76.580 ±  0.587   ns/op
HiddenClassBenchmark.descriptorString:·gc.alloc.rate.norm               avgt  100   432.031 ±  0.001    B/op

@openjdk
Copy link

openjdk bot commented May 12, 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:

8266622: Optimize Class.descriptorString() and Class.getCanonicalName0()

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

  • f3c6cda: 8266162: Remove JPackage duplicate tests
  • a259ab4: 8258795: Update IANA Language Subtag Registry to Version 2021-05-11
  • b4371e9: 8266552: Technical corrections to java/util/random/package-info.java
  • e14b026: 8243287: Removal of Unsafe::defineAnonymousClass
  • a564f2c: 8266821: G1: Prefetch cards during merge heap roots phase
  • 127bfe4: 8266074: Vtable-based CHA implementation
  • 347d41d: 8164804: sun/security/ssl/SSLSocketImpl/CloseSocket.java makes not reliable time assumption
  • 17ceef9: 8266819: Separate the stop policies from the compile policies completely
  • a270cbe: 8267043: IntelliJ project doesn't handle generated sources correctly
  • 08a5a5c: 8263382: java/util/logging/ParentLoggersTest.java failed with "checkLoggers: getLoggerNames() returned unexpected loggers"
  • ... and 105 more: https://git.openjdk.java.net/jdk/compare/22ca62c2cb61940dd7b1028925cd651ffdf80690...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 (@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 May 12, 2021
@cl4es
Copy link
Member

cl4es commented May 12, 2021

Together with #3627 this allows to reduce minimalistic Spring Boot application start-up time from 653 to 645 milliseconds and memory consumprion from 43804 to 43668 kB.

How do you run this benchmark? Something like -bm ss -f 20? Otherwise repeatedly invoking the spring boot initialization in a JMH benchmark method doesn't seem to model startup very realistically - unless that capture some iterative development scenario. Since JMH itself loads quite a bit of things on startup it likely skews your results somewhat - our startup tests are typically more barebone scripts that repeatedly run the app and capture the time to "start" and time to run the JVM to completion.

@stsypanov
Copy link
Contributor Author

Something like -bm ss -f 20

Yes, I use SingleShotTime as the mode and 400 forks

@stsypanov
Copy link
Contributor Author

/integrate

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

openjdk bot commented May 12, 2021

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

@openjdk openjdk bot removed the sponsor Pull request is ready to be sponsored label May 13, 2021
@stsypanov
Copy link
Contributor Author

/integrate

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

openjdk bot commented May 13, 2021

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

@cl4es
Copy link
Member

cl4es commented May 14, 2021

/sponsor

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

openjdk bot commented May 14, 2021

@cl4es @stsypanov Since your change was applied there have been 124 commits pushed to the master branch:

  • 644f28c: 8266810: Move trivial Matcher code to cpu-specific header files
  • 88907bb: 8266904: Use function pointer typedefs in OopOopIterateDispatch
  • 301095c: 8266795: Remove dead code LowMemoryDetectorDisabler
  • 1e0ecd6: 8265605: Cannot call BootLoader::loadClassOrNull before initPhase2
  • 4086081: 8264846: Regression ~5% in J2dBench.bimg_misc on Linux after JDK-8263142
  • 2a2f105: 8267117: sun/hotspot/whitebox/CPUInfoTest.java fails on Ice Lake
  • 2667024: 8266881: Enable debug log for SSLEngineExplorerMatchedSNI.java
  • 6c107fd: 8264299: Create implementation of native accessibility peer for ScrollPane and ScrollBar Java Accessibility roles
  • 853ffdb: 8265934: Cleanup _suspend_flags and _special_runtime_exit_condition
  • f3c6cda: 8266162: Remove JPackage duplicate tests
  • ... and 114 more: https://git.openjdk.java.net/jdk/compare/22ca62c2cb61940dd7b1028925cd651ffdf80690...master

Your commit was automatically rebased without conflicts.

Pushed as commit ebcf399.

💡 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
Development

Successfully merging this pull request may close these issues.

3 participants