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 2 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
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