-
Notifications
You must be signed in to change notification settings - Fork 746
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
base: master
Are you sure you want to change the base?
Optimize VisitorState#getConstantExpression #4586
Conversation
There was a problem hiding this 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)) { |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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.
There was a problem hiding this 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
By avoiding a second pass over the string.
ff02145
to
45f3401
Compare
There was a problem hiding this 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.
<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> |
There was a problem hiding this comment.
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 thejava24
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.
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<configuration> | ||
<archive> | ||
<manifestEntries> | ||
<Multi-Release>true</Multi-Release> | ||
</manifestEntries> | ||
</archive> | ||
</configuration> | ||
</plugin> |
There was a problem hiding this comment.
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.
/** | ||
* @see VisitorState#getConstantExpression(Object) | ||
*/ | ||
final class ConstantStringExpressions { | ||
private ConstantStringExpressions() {} |
There was a problem hiding this comment.
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
.
} | ||
|
||
// Don't escape single-quotes in string literals. | ||
CharSequence str = (CharSequence) value; |
There was a problem hiding this comment.
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.
return state | ||
.getElements() | ||
.getConstantExpression( | ||
value instanceof CharSequence charSequence ? charSequence.toString() : value); |
There was a problem hiding this comment.
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.
By avoiding a second pass over the string.