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

refactor:optimize performance by moving ToLower operation outside loop #7860

Closed

Conversation

Yeatesss
Copy link
Contributor

@Yeatesss Yeatesss commented Nov 4, 2024

Description

This PR optimizes the performance of the keyword matching function by eliminating redundant ToLower operations inside the loop. Previously, content was being converted to lowercase in every loop iteration, which was inefficient. The change moves this operation outside the loop and stores the result in a variable for reuse.

Before:

for _, kw := range r.Keywords {
    if bytes.Contains(bytes.ToLower(content), []byte(strings.ToLower(kw))) {
        return true
    }
}

After
contentLower = bytes.ToLower(content)
for _, kw := range r.Keywords {
    if bytes.Contains(contentLower, []byte(strings.ToLower(kw))) {
        return true
    }
}

## Checklist
- [x ] I've read the [guidelines for contributing](https://aquasecurity.github.io/trivy/latest/community/contribute/pr/) to this repository.
- [x ] I've followed the [conventions](https://aquasecurity.github.io/trivy/latest/community/contribute/pr/#title) in the PR title.
- [ x] I've added tests that prove my fix is effective or that my feature works.
- [ ] I've updated the [documentation](https://github.com/aquasecurity/trivy/blob/main/docs) with the relevant information (if needed).
- [ ] I've added usage information (if the PR introduces new options)
- [ x] I've included a "before" and "after" example to the description (if the PR is a user interface change).

@CLAassistant
Copy link

CLAassistant commented Nov 4, 2024

CLA assistant check
All committers have signed the CLA.

@Yeatesss Yeatesss closed this Nov 4, 2024
@Yeatesss Yeatesss deleted the refactor/optimize-matchkeywords branch November 4, 2024 09:22
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