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

Optimize VisitorState#getConstantExpression #4586

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

Stephan202
Copy link
Contributor

By avoiding a second pass over the string.

Copy link
Contributor Author

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

I filed this PR as a result of work in PicnicSupermarket/error-prone-support#1138, where I found out that I overlooked VisitorState#getConstantExpression and instead reinvented the wheel.

} else {
sb.append(c);
}
if (!(value instanceof CharSequence str)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a second commit that enables a further optimization by accepting any CharSequence; this can be backed out if desired.

if (c == '\'') {
sb.append('\'');
} else {
sb.append(Convert.quote(c));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Incidentally I recently learned that Convert.quote does the thing we'd want now, but only for JDK 23+: openjdk/jdk@144a08e

Copy link
Collaborator

Choose a reason for hiding this comment

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

Er, and as a result of that change Convert.quote(char) was replaced with Convert.quote(char, boolean), so this change doesn't work with the latest JDK EA builds.

I'm not sure what the best option is. We could use reflection or MR-JARs to call the method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, nice catch! I haven't looked at the MR-JAR setup yet, but indeed reflection should work. TBD whether I'll find some time today for a new pass.

} else {
sb.append(c);
}
if (!(value instanceof CharSequence str)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unfortunately we're not quite ready to start using Java > 11 features in non-test code internally. I can remove the instanceof pattern when importing this, I'm just leaving a note here in case you wonder why it disappears.

Copy link
Collaborator

@cushon cushon left a comment

Choose a reason for hiding this comment

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

The Convert.quote part will need more work to be compatible with the latest JDK EA builds

copybara-service bot pushed a commit that referenced this pull request Sep 21, 2024
By avoiding a second pass over the string.

Fixes #4586

FUTURE_COPYBARA_INTEGRATE_REVIEW=#4586 from PicnicSupermarket:sschroevers/optimize-getConstantExpression ff02145
PiperOrigin-RevId: 677291745
copybara-service bot pushed a commit that referenced this pull request Sep 22, 2024
By avoiding a second pass over the string.

Fixes #4586

FUTURE_COPYBARA_INTEGRATE_REVIEW=#4586 from PicnicSupermarket:sschroevers/optimize-getConstantExpression ff02145
PiperOrigin-RevId: 677291745
copybara-service bot pushed a commit that referenced this pull request Sep 22, 2024
By avoiding a second pass over the string.

Fixes #4586

FUTURE_COPYBARA_INTEGRATE_REVIEW=#4586 from PicnicSupermarket:sschroevers/optimize-getConstantExpression ff02145
PiperOrigin-RevId: 677291745
@Stephan202 Stephan202 force-pushed the sschroevers/optimize-getConstantExpression branch from ff02145 to 45f3401 Compare December 28, 2024 22:28
Copy link
Contributor Author

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

Rebased and added a commit with a multi-release JAR approach. Locally this makes the build pass with JDK 25.ea.3-open.

I suspect that changes to .github/workflows/ci.yml are required, but as the GitHub Actions workflow isn't auto-triggered, I'm not sure 👀.

I'll cross-reference this PR in #3756.

Comment on lines +184 to +199
<execution>
<id>java23</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<jdkToolchain>
<version>23</version>
</jdkToolchain>
<compileSourceRoots>
<compileSourceRoot>${basedir}/src/main/java23</compileSourceRoot>
</compileSourceRoots>
<!-- multiReleaseOutput requires setting release -->
<outputDirectory>${project.build.outputDirectory}/META-INF/versions/23</outputDirectory>
</configuration>
</execution>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A few things are of note here:

  • Without <goal>compile</goal> this execution is skipped. This also means that the java24 execution below is currently a no-op.
  • This configuration doesn't override the inherited <source>17</source> and <target>17</target> settings, though it could. (If so, we should do the same below.)
  • I'm not sure each of these executions should use an exactly-matching toolchain JDK version. Perhaps it's better to use the latest JDK whenever possible, so as to minimize the number of JDKs required.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This also means that the java24 execution below is currently a no-op.

Huh, thanks for pointing that out. This is also used with an Bazel-based build that does the multi-release jar stuff differently, so maybe there are no uses (or test coverage) for the maven build and ErrorProneSignatureGenerator that require JDK 24 support.

I'm not sure each of these executions should use an exactly-matching toolchain JDK version. Perhaps it's better to use the latest JDK whenever possible, so as to minimize the number of JDKs required.

The answer might be 'it depends'. For javac API changes, it's probably best to try to use an exact match, but that does require installing extra JDKs for the github actions setup. If the API changes once and is stable on newer versions we'd get away with a newer version, though.

Comment on lines +216 to +242
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</archive>
</configuration>
</plugin>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also had to add this to make sure that the class file bundled under META-INF/versions/23 is picked up.

Comment on lines +21 to +25
/**
* @see VisitorState#getConstantExpression(Object)
*/
final class ConstantStringExpressions {
private ConstantStringExpressions() {}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Certainly open to approaches other than a special-purpose static utility class named com.google.errorprone.ConstantStringExpressions.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems good for now, thanks.

}

// Don't escape single-quotes in string literals.
CharSequence str = (CharSequence) value;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dropped the instanceof pattern matching as suggested here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We're finally able to use Java 21 features here internally, the earlier comment is obsolete.

return state
.getElements()
.getConstantExpression(
value instanceof CharSequence charSequence ? charSequence.toString() : value);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here I did keep the instanceof pattern matching, because this code anyway only applies to JDK 23+, but if that anyway impacts the Google-internal setup, that can be changed, of course.

}

// Don't escape single-quotes in string literals.
CharSequence str = (CharSequence) value;
Copy link
Collaborator

Choose a reason for hiding this comment

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

We're finally able to use Java 21 features here internally, the earlier comment is obsolete.

Comment on lines +21 to +25
/**
* @see VisitorState#getConstantExpression(Object)
*/
final class ConstantStringExpressions {
private ConstantStringExpressions() {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems good for now, thanks.

Comment on lines +184 to +199
<execution>
<id>java23</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<jdkToolchain>
<version>23</version>
</jdkToolchain>
<compileSourceRoots>
<compileSourceRoot>${basedir}/src/main/java23</compileSourceRoot>
</compileSourceRoots>
<!-- multiReleaseOutput requires setting release -->
<outputDirectory>${project.build.outputDirectory}/META-INF/versions/23</outputDirectory>
</configuration>
</execution>
Copy link
Collaborator

Choose a reason for hiding this comment

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

This also means that the java24 execution below is currently a no-op.

Huh, thanks for pointing that out. This is also used with an Bazel-based build that does the multi-release jar stuff differently, so maybe there are no uses (or test coverage) for the maven build and ErrorProneSignatureGenerator that require JDK 24 support.

I'm not sure each of these executions should use an exactly-matching toolchain JDK version. Perhaps it's better to use the latest JDK whenever possible, so as to minimize the number of JDKs required.

The answer might be 'it depends'. For javac API changes, it's probably best to try to use an exact match, but that does require installing extra JDKs for the github actions setup. If the API changes once and is stable on newer versions we'd get away with a newer version, though.

copybara-service bot pushed a commit that referenced this pull request Jan 3, 2025
By avoiding a second pass over the string.

Fixes #4586

FUTURE_COPYBARA_INTEGRATE_REVIEW=#4586 from PicnicSupermarket:sschroevers/optimize-getConstantExpression 45f3401
PiperOrigin-RevId: 711440462
copybara-service bot pushed a commit that referenced this pull request Jan 3, 2025
By avoiding a second pass over the string.

Fixes #4586

FUTURE_COPYBARA_INTEGRATE_REVIEW=#4586 from PicnicSupermarket:sschroevers/optimize-getConstantExpression 45f3401
PiperOrigin-RevId: 711440462
copybara-service bot pushed a commit that referenced this pull request Jan 3, 2025
By avoiding a second pass over the string.

Fixes #4586

FUTURE_COPYBARA_INTEGRATE_REVIEW=#4586 from PicnicSupermarket:sschroevers/optimize-getConstantExpression 45f3401
PiperOrigin-RevId: 711440462
final class ConstantStringExpressions {
private ConstantStringExpressions() {}

static String toConstantStringExpression(Object value, VisitorState state) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd prefer to pass in the javax.lang.model.util.Elements here instead of VisitorState, it's helpful if the multi-release part is more self contained and does depend on other parts of check_api

@cushon
Copy link
Collaborator

cushon commented Jan 3, 2025

I tried merging this an am seeing failures like the ones in https://github.com/google/error-prone/actions/runs/12602521683/job/35125772504?pr=4744

I'm able to reproduce those locally with:

mvn clean install -DskipTests=true
mvn test -e
...
Caused by: org.apache.maven.surefire.booter.SurefireBooterForkException: There was an error in the forked process
com/google/errorprone/InvalidCommandLineOptionException
java.lang.NoClassDefFoundError: com/google/errorprone/InvalidCommandLineOptionException
	at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
	at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3578)
	at java.base/java.lang.Class.getMethodsRecursive(Class.java:3719)
	at java.base/java.lang.Class.getMethod0(Class.java:3705)
	at java.base/java.lang.Class.getMethod(Class.java:2393)
	at org.apache.maven.surefire.api.util.ReflectionUtils.tryGetMethod(ReflectionUtils.java:50)
	at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isSuiteOnly(JUnit3TestChecker.java:60)
	at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isValidJUnit3Test(JUnit3TestChecker.java:56)
	at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.accept(JUnit3TestChecker.java:52)
	at org.apache.maven.surefire.common.junit4.JUnit4TestChecker.accept(JUnit4TestChecker.java:48)
	at org.apache.maven.surefire.api.util.DefaultScanResult.applyFilter(DefaultScanResult.java:87)
	at org.apache.maven.surefire.junit4.JUnit4Provider.scanClassPath(JUnit4Provider.java:272)
	at org.apache.maven.surefire.junit4.JUnit4Provider.setTestsToRun(JUnit4Provider.java:174)
	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:132)
	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:385)
	at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:162)
	at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:507)
	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:495)
Caused by: java.lang.ClassNotFoundException: com.google.errorprone.InvalidCommandLineOptionException
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
	... 18 more

Any ideas what's going wrong?

@Stephan202 Stephan202 force-pushed the sschroevers/optimize-getConstantExpression branch from 45f3401 to 07dfafa Compare January 4, 2025 12:43
@Stephan202
Copy link
Contributor Author

@cushon this one was tricky! First to debug, then to find a (hacky...) workaround. It's a shame that we can't use multiReleaseOutput, as that would likely have avoided these shenanigans. Maybe one of the other watchers of this project knows of a more elegant solution 👀 🤞. I rebased the branch and added a commit; PTAL :)

NB: instead of introducing a new reset-output-directory step I also tried to move down the default-compile step, but that did not cause the order of the compilation steps to change.

@Stephan202
Copy link
Contributor Author

Added another commit to apply the Elements change.

One issue I just identified: with my change mvn clean install -DskipTests=true && mvn test -e still doesn't fully work when building with JDK 23+, with test failures due to the wrong Convert.quote being invoked. I think that this is because without reference to the JAR, the META-INF/MANIFEST.MF file is ignored. As-is ./check_api/target/classes/META-INF/MANIFEST.MF also doesn't contain Multi-Release: true, thought the following patch would "fix" that:

diff --git a/check_api/pom.xml b/check_api/pom.xml
index 9e8bc1e8df..a5de90556e 100644
--- a/check_api/pom.xml
+++ b/check_api/pom.xml
@@ -229,17 +229,6 @@
         </executions>
 
       </plugin>
-      <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-jar-plugin</artifactId>
-        <configuration>
-          <archive>
-            <manifestEntries>
-              <Multi-Release>true</Multi-Release>
-            </manifestEntries>
-          </archive>
-        </configuration>
-      </plugin>
     </plugins>
   </build>
 
diff --git a/pom.xml b/pom.xml
index ac716fb30f..11238fe292 100644
--- a/pom.xml
+++ b/pom.xml
@@ -147,6 +147,7 @@
             </goals>
             <configuration>
               <bnd><![CDATA[
+                Multi-Release: true
                 Bundle-SymbolicName: com.google.$<replacestring;$<replacestring;${project.artifactId};^error_prone;errorprone>;_;.>
                 Automatic-Module-Name: $<Bundle-SymbolicName>
                 -exportcontents: com.google.errorprone*

... but as said, that doesn't fix mvn clean install -DskipTests=true && mvn test -e on JDK 23+.

A simple fix would be to update the CI configuration to just combine installation and testing:

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 25684fd42f..afcc2c6970 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -84,12 +84,9 @@ jobs:
           java-version: ${{ matrix.java }}
           distribution: 'zulu'
           cache: 'maven'
-      - name: 'Install'
+      - name: 'Install and test'
         shell: bash
-        run: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
-      - name: 'Test'
-        shell: bash
-        run: mvn test -B
+        run: mvn install -Dmaven.javadoc.skip=true -B -V
       - name: 'Javadoc'
         shell: bash
         run: mvn -P '!examples' javadoc:javadoc

Right now I don't have other ideas. 🤔

@cushon
Copy link
Collaborator

cushon commented Jan 7, 2025

I'm fine with changing the invoke in CI invocation, but if separate mvn install and mvn test don't work that seems like it might also affect non-CI development builds? Or would you not expect this to affect development builds?

(I guess another option here would be to give up on multi-release jars for now and just use reflection for this method. But there isn't a good reflection-based alternative alternative for the problem 08e0d10 solves, so it would be nice to make mr-jars work.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants