-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
[java] Use static Patterns for regex-matching #15499
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
base: trunk
Are you sure you want to change the base?
Conversation
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
@@ -109,7 +112,7 @@ public Executable configure(PrintStream out, PrintStream err, String... args) { | |||
break; | |||
} | |||
|
|||
String path = getClass().getPackage().getName().replaceAll("\\.", "/") + "/" + toDisplay; | |||
String path = getClass().getPackage().getName().replace('.', '/') + "/" + toDisplay; |
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.
Why is this changed? Just curious
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.
Similar to the other changes to make the Pattern
instances static. The #replaceAll(String, String)
method considered the first argument to be a regex, meaning it needs to be compiled for each call.
But since this call is just matching a literal dot, we can replace it with #replace(char, char)
which will iterate through the characters in the String and replaces them.
@@ -143,11 +146,11 @@ private String readContent(String path) throws IOException { | |||
} else if ("```".equals(line)) { | |||
inCode = !inCode; | |||
} else { | |||
if (line.startsWith("=")) { | |||
if (line.charAt(0) == '=') { |
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.
Any reason for modifying this?
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.
Minor performance improvement, while I'm in the same class. The #startsWith(String)
method can take a String of multiple characters, so it needs to search through the String. Whereas if you're only searching for a single character, charAt(int)
directly accesses the char[] backing the String and returns the value in a single call.
Not a Pattern
, so I can move it into another PR if you think that's appropriate.
@@ -58,7 +56,7 @@ public PersistentCapabilities setCapability(String name, Object value) { | |||
@Override | |||
public Map<String, Object> asMap() { | |||
return getCapabilityNames().stream() | |||
.collect(toUnmodifiableMap(Function.identity(), this::getCapability)); | |||
.collect(Collectors.toUnmodifiableMap(Function.identity(), this::getCapability)); |
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.
This change makes sense. But these are unrelated to changes made as part of the rest of this PR. Can we please separate this out into a separate PR?
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.
That's fine, I'll create a separate PR for these changes.
Motivation and Context
There were several locations where
Pattern
objects were being used to perform regex matches. This is inefficient as thePattern
would be recompiled on each call. I've extracted these to constants in the appropriate class to ensure they are only compiled once. In one case there was a regex being used to check for digits, which I've replaced with a simple non-regex based function which should be less complex.Types of changes
Checklist
bazel test //java/... --test_size_filters=small
)