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

chore: Improve containsLower performance using quick rejection #15076

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cyriltovena
Copy link
Contributor

This pull request includes improvements to the containsLower function in pkg/logql/log/filter.go for better performance and adds benchmark tests to ensure the changes are effective. The key changes include optimizing the case-insensitive substring search and introducing comprehensive benchmark tests.

Optimizations to containsLower function:

  • pkg/logql/log/filter.go: Optimized the containsLower function by implementing a more efficient search algorithm that first locates the potential starting byte of the substring and then verifies the rest of the substring with both fast ASCII and slower Unicode comparisons.

Addition of benchmark tests:

  • pkg/logql/log/filter_test.go: Added a new benchmark function BenchmarkContainsLower with various test cases to evaluate the performance of the containsLower function under different scenarios, including short and long lines, matches and non-matches, and lines with Unicode characters.

Result:

benchstat before.txt before2.txt
name                                             old time/op    new time/op    delta
ContainsLower/short_line_no_match-10               43.2ns ± 1%    20.9ns ± 0%  -51.53%  (p=0.008 n=5+5)
ContainsLower/short_line_with_match-10             27.7ns ± 1%    26.3ns ± 0%   -5.12%  (p=0.008 n=5+5)
ContainsLower/long_line_no_match-10                 405ns ± 0%     249ns ± 1%  -38.36%  (p=0.008 n=5+5)
ContainsLower/long_line_match_start-10             7.04ns ± 0%    8.55ns ± 1%  +21.50%  (p=0.016 n=4+5)
ContainsLower/long_line_match_middle-10             181ns ± 0%     120ns ± 3%  -33.59%  (p=0.016 n=4+5)
ContainsLower/long_line_match_end-10                426ns ± 2%     272ns ± 0%  -36.05%  (p=0.008 n=5+5)
ContainsLower/short_unicode_line_no_match-10        178ns ± 0%      60ns ± 1%  -66.25%  (p=0.016 n=4+5)
ContainsLower/short_unicode_line_with_match-10     65.1ns ± 3%    37.9ns ± 0%  -41.80%  (p=0.016 n=5+4)
ContainsLower/long_unicode_line_no_match-10        1.98µs ± 1%    0.31µs ± 1%  -84.54%  (p=0.008 n=5+5)
ContainsLower/long_unicode_line_match_start-10      808ns ± 4%     243ns ± 0%  -69.94%  (p=0.016 n=5+4)
ContainsLower/long_unicode_line_match_middle-10     760ns ± 2%     108ns ± 3%  -85.77%  (p=0.008 n=5+5)
ContainsLower/long_unicode_line_match_end-10       3.89µs ± 1%    0.43µs ± 4%  -88.85%  (p=0.008 n=5+5)

*What this PR does / why we need it:

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Checklist

  • Reviewed the CONTRIBUTING.md guide (required)
  • Documentation added
  • Tests updated
  • Title matches the required conventional commits format, see here
    • Note that Promtail is considered to be feature complete, and future development for logs collection will be in Grafana Alloy. As such, feat PRs are unlikely to be accepted unless a case can be made for the feature actually being a bug fix to existing behavior.
  • Changes that require user attention or interaction to upgrade are documented in docs/sources/setup/upgrade/_index.md
  • If the change is deprecating or removing a configuration option, update the deprecated-config.yaml and deleted-config.yaml files respectively in the tools/deprecated-config-checker directory. Example PR

@cyriltovena cyriltovena requested a review from a team as a code owner November 22, 2024 15:20
@cyriltovena
Copy link
Contributor Author

Sounds like test failure so will need to check

@cyriltovena cyriltovena marked this pull request as draft November 22, 2024 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant