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

Replace Pattern with CharMatcher for valid/clean key checks #20528

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kroepke
Copy link
Member

@kroepke kroepke commented Sep 24, 2024

Description

Replace Pattern with Guava's CharMatcher in the validKey and cleanKey methods. validKey is in the hot path when adding fields to messages.

Motivation and Context

Restoring archives or other activity that re-creates messages in bulk will cause validKey to be called very often. In various scenarios, it accounted for 25% of the time spent restoring messages, for example.

Benchmarking showed that replacing the Pattern with the corresponding CharMatcher is 3-4x faster due to how inefficient java.util.regex is in general.

cleanKey is called rarely and only from pipeline functions, but it can easily be replaced with a CharMatcher that is derived from the one used in validKey, so for consistency I've adapted it too (and added another test case for multiple replacements.

/nocl refactoring

How Has This Been Tested?

Unit tests for the corresponding methods.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Refactoring (non-breaking change)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.

@kroepke kroepke requested a review from a team September 24, 2024 12:36
Copy link
Contributor

@boosty boosty left a comment

Choose a reason for hiding this comment

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

Nice!

I checked out the code to verify if the behavior for leading and trailing whitespace stays the same, which is the case 👍

assertFalse(Message.validKey(" foo123"));
assertFalse(Message.validKey("foo123 "));

Maybe these are worth adding to testValidKeys()?

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