Skip to content

Prohibit String.replace() and String.replaceAll(), fix and prohibit some toString()-related redundancies#6607

Merged
b-slim merged 3 commits intoapache:masterfrom
metamx:implicit-pattern
Nov 15, 2018
Merged

Prohibit String.replace() and String.replaceAll(), fix and prohibit some toString()-related redundancies#6607
b-slim merged 3 commits intoapache:masterfrom
metamx:implicit-pattern

Conversation

@leventov
Copy link
Copy Markdown
Member

@leventov leventov commented Nov 12, 2018

This PR primarily prohibits methods in String class that compile a Pattern implicitly:

  • String.matches()
  • String.replace() (It should have not in the first place; it's fixed in OpenJDK 9+)
  • String.replaceAll()
  • String.replaceFirst()

I've seen 10% of allocations on a Coordinator instance to happen in Pattern.compile() in one of those String.replace() calls.

Also prohibited and fixed some other String-related redundancies and inefficient patterns.

Copy link
Copy Markdown
Member

@clintropolis clintropolis left a comment

Choose a reason for hiding this comment

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

LGTM

private static String removeChar(String s, char c, int firstOccurranceIndex)
/**
* Removes all occurrences of the given char from the given string. This method is an optimal version of
* {@link String#replace(CharSequence, CharSequence) s.replace("c", "")}.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i think the description is not accurate, what is "" in terms of char ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's an empty string. s.replace(":", "") is literally the pattern that should be replaced with removeChar(s, ':') in the Druid code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i see.

* Replaces all occurrences of the given target substring in the given string with the given replacement string. This
* method is an optimal version of {@link String#replace(CharSequence, CharSequence) s.replace(target, replacement)}.
*/
public static String replace(String s, String target, String replacement)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

although the original method String#replace(CharSequence, CharSequence) has CharSequence as API, the one you are proposing is using String (String s, String target, String replacement) wondering if that is totally equivalent as a replacement since it is only covering a subset of it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There are uses for it yet. BTW even the new OpenJDK 9+ version converts the target to String internally because the implementation relies on indexOf() that is optimized only for two Strings, i. e. making the target a CharSequence will only give false sense of "optimality". The replacement could be made CharSequence but there is no need for that yet.

@b-slim b-slim merged commit 8f3fe9c into apache:master Nov 15, 2018
@leventov leventov deleted the implicit-pattern branch November 16, 2018 16:10
Guadrado added a commit to Guadrado/incubator-druid that referenced this pull request Nov 20, 2018
jihoonson pushed a commit that referenced this pull request Nov 20, 2018
…rage (#6510)

* 1. added support for unused DateTime start parameter in getRecentlyFinishedTaskInfoSince method:
 HeapMemoryTaskStorage.getRecentlyFinishedTaskInfoSince return the finished tasks by comparing TaskStuff.createdDate with the start time
2. added filtering by status complete to TaskStuff list stream in HeapMemoryTaskStorage.getNRecentlyFinishedTaskInfo method.
3. changed names of methods and parameters to present that public API method OverlordResource.getTasks return the list of completed tasks, which createdDate, not date of completion, belongs to the interval parameter.

* 1. added support for unused DateTime start parameter in getRecentlyFinishedTaskInfoSince method:
 HeapMemoryTaskStorage.getRecentlyFinishedTaskInfoSince return the finished tasks by comparing TaskStuff.createdDate with the start time
2. added filtering by status complete to TaskStuff list stream in HeapMemoryTaskStorage.getNRecentlyFinishedTaskInfo method.
3. changed names of methods and parameters to present that public API method OverlordResource.getTasks return the list of completed tasks, which createdDate, not date of completion, belongs to the interval parameter.

* Fixed OverlordResourceTest to Support changed methods names

* Changed methods and parameters names to make them more obvious to understand.

* Changed String.replace() for the StringUtils.replace()(#6607)

* Fixed checkstyle error
@jon-wei jon-wei added this to the 0.14.0 milestone Feb 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants