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 isCloudEventsHeader check #445

Merged

Conversation

pierDipi
Copy link
Member

@pierDipi pierDipi commented Feb 7, 2022

Instead of using:

key.substring(0, CE_PREFIX.length()).toLowerCase().startsWith(CE_PREFIX);

that allocates 2 new strings, we can just use:

key.regionMatches(true /* ignoreCase */, 0, CE_PREFIX, 0, CE_PREFIX.length());

Signed-off-by: Pierangelo Di Pilato pierdipi@redhat.com

Instead of using:
```java
key.substring(0, CE_PREFIX.length()).toLowerCase().startsWith(CE_PREFIX);
```
we can just use:
```java
key.regionMatches(true /* ignoreCase */, 0, CE_PREFIX, 0, CE_PREFIX.length());
```

Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com>
@pierDipi
Copy link
Member Author

pierDipi commented Feb 7, 2022

/cc @JemDay @aliok @matzew

matzew
matzew previously approved these changes Feb 7, 2022
Copy link
Member

@matzew matzew left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com>
Copy link
Contributor

@JemDay JemDay left a comment

Choose a reason for hiding this comment

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

While you're here .. is there value in adding a generic implementation of isCloudEventsHeader(prefix, key).

Then all of these implementations can be simplified to use that method and remove all the duplicated code

@@ -44,7 +44,7 @@ protected boolean isContentTypeHeader(String key) {

@Override
protected boolean isCloudEventsHeader(String key) {
return key.length() > 3 && key.substring(0, KafkaHeaders.CE_PREFIX.length()).startsWith(KafkaHeaders.CE_PREFIX);
return key.length() > KafkaHeaders.CE_PREFIX.length() && key.startsWith(KafkaHeaders.CE_PREFIX);
Copy link
Contributor

Choose a reason for hiding this comment

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

Any particular reason this isn't using your new method ?

Also ... Might be worth adding the null-check on key (most of the other readers do this)

Copy link
Member Author

Choose a reason for hiding this comment

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

Any particular reason this isn't using your new method ?

it wasn't case-insensitive before like other ones and I think the reason is that other methods are HTTP based (headers normalization) vs this one being Kafka based.

Do you know if the spec is case-insensitive for every defined protocol binding?

Honestly, from a user perspective, I would be surprised if a header isn't considered a CE header due to the case, so I agree with you.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hey @JemDay since most of the comments aren't strictly related to the original goal of the PR and the code is strictly equivalent to the existing one, I think, we can follow up on these comments in a different PR/issue.

@pierDipi pierDipi requested a review from matzew July 13, 2022 07:50
@pierDipi pierDipi added this to the 2.4.0 milestone Jul 13, 2022
@pierDipi pierDipi requested a review from JemDay July 13, 2022 07:53
Copy link
Member

@matzew matzew left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

@pierDipi pierDipi merged commit 45ec85f into cloudevents:master Jul 13, 2022
@pierDipi pierDipi deleted the optimize-prefix-match-ignoring-case branch July 13, 2022 08:18
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.

3 participants