Skip to content

MINOR: Make regular expressions into static #5168

Merged
hachikuji merged 1 commit intoapache:trunkfrom
KoenDG:staticpattern3
Aug 14, 2018
Merged

MINOR: Make regular expressions into static #5168
hachikuji merged 1 commit intoapache:trunkfrom
KoenDG:staticpattern3

Conversation

@KoenDG
Copy link
Copy Markdown
Contributor

@KoenDG KoenDG commented Jun 8, 2018

@ijuma I made a new PR after several rebases and merges of trunk. It's the same as this: #5042 except only 1 commit.

This should improve performance, as in-place means that the regex has to be compiled again each time the code path is traversed. Which is not performant.

A nice writeup on this topic can be found here: https://softwareengineering.stackexchange.com/questions/216320/java-regex-patterns-compile-time-constants-or-instance-members

And it's also recommended in Joshua Bloch's "Effective Java 3rd Edition" (Item 6: Avoid creating unnecessary objects)

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status (Are there code coverage reports?)
  • Verify documentation (including upgrade notes)

@KoenDG
Copy link
Copy Markdown
Contributor Author

KoenDG commented Jul 11, 2018

ping @ijuma sorry for dropping this for so long. Merged in trunk and pushed, all builds passing, should be good.

Copy link
Copy Markdown
Contributor

@hachikuji hachikuji left a comment

Choose a reason for hiding this comment

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

Thanks for the patch. Just had a minor nit.

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.

nit: comments like this seem redundant. Can we just remove them?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If that's what you want, I'll remove them. My reason for adding was that it wasn't immediately obvious to me what the regex did. So I went to regex101.com and it explained the details, which I then added. It might be redundant if you're more fluent at regex than me, but it wasn't for me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Also, it's more for other people than for me. I want the people to come after me to be able to read this and find use out of it, instead of having to google it. I already did that, I might as well save the people after me the trouble.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@hachikuji Just to clarify, I'm waiting on your confirmation that you still want it gone after reading my reasoning.

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.

@KoenDG I agree with @hachikuji. Name of the constant should be explanatory enough (and it usually is). I don't see the need to explain usage further.

Copy link
Copy Markdown
Contributor

@andrasbeni andrasbeni left a comment

Choose a reason for hiding this comment

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

@KoenDG Thanks for you contribution. I have a few questions and remarks.

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.

@KoenDG I agree with @hachikuji. Name of the constant should be explanatory enough (and it usually is). I don't see the need to explain usage further.

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.

Is it necessary to add SPLIT_ and REPLACE_ to the names of patterns? The operation performed with the pattern is not part of it. In theory one pattern could be used to both split and replace.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point.

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.

This one matches a double quote, not a backslash and a double quote.

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.

This one matches a single backslash, not a double backslash. Also, I think you don't need to abbreviate 'backslash'.

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.

This nested invocation is much harder to read than the original version. I believe storing the result of the first replace in a local variable would make it clearer.

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.

In OAuthBearerUnsecuredLoginCallbackHandler.java you use Matcher.quoteReplacement. Why do you do it differently here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Looks like an oversight. The first thing the source code of Matcher.quoteReplacement does is this:

if ((s.indexOf('\\') == -1) && (s.indexOf('$') == -1))
            return s;

So anything with a \ gets processed and therefor needs it.

That being said, one should not rely on knowledge of source code to assume behavior. I'll add them everywhere to be consistent.

Copy link
Copy Markdown
Contributor Author

@KoenDG KoenDG Jul 25, 2018

Choose a reason for hiding this comment

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

Scratch that, now that I'm looking at the actual code, it because of the difference between replace and replaceAll in String.java.

replace will do Matcher.quoteReplacelemt, replaceAll doesn't. And the javadoc for replaceAll warns the user of this and suggests using it beforehand if desired.

Knowing that, I'll make it so that the behavior is that of the function that originally got called.

EDIT: And there's more. replace interprets the regex as LITERAL. replaceAll doesn't.

@KoenDG
Copy link
Copy Markdown
Contributor Author

KoenDG commented Jul 25, 2018

PR updated, as per comments.

@harshach
Copy link
Copy Markdown

harshach commented Aug 7, 2018

+1. LGTM.
cc @hachikuji

Copy link
Copy Markdown
Contributor

@hachikuji hachikuji left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the patch and also the additional reviews.

@KoenDG
Copy link
Copy Markdown
Contributor Author

KoenDG commented Aug 13, 2018

@hachikuji Thanks. Is this good for merge now? I don't have an option to do that.

@hachikuji
Copy link
Copy Markdown
Contributor

Sorry, got sidetracked when I was about to merge. Will do so shortly

@hachikuji hachikuji merged commit 9e0e29a into apache:trunk Aug 14, 2018
@KoenDG KoenDG deleted the staticpattern3 branch August 14, 2018 12:30
@KoenDG
Copy link
Copy Markdown
Contributor Author

KoenDG commented Aug 14, 2018

Thanks you for your time.

pengxiaolong pushed a commit to pengxiaolong/kafka that referenced this pull request Jun 14, 2019
…ache#5168)

Reviewers: Andras Beni <andrasbeni@cloudera.com>, Sriharsha Chintalapani <sriharsha@apache.org>, Jason Gustafson <jason@confluent.io>
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.

4 participants