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

Squash allocations in selector parsing and other hotspots #9564

Merged
merged 5 commits into from
Dec 12, 2024

Conversation

fasaxc
Copy link
Member

@fasaxc fasaxc commented Dec 5, 2024

Description

Noticed that selector parsing was a memory allocation hotspot in clusters with lots of policy. Most of that was down to validating selectors so we were just throwing away the parsed selector.

  • Check log level in various places before logging to avoid allocs in logrus.
  • Add tokenizer.AppendTokens() to do tokenization into a pre-allocated buffer. Use a shared instance of the parser to hold the shared token buffer (protected my mutex).
  • Add dedicatied Validate() function that calls parser with a flag telling it not to allocate the selector.
  • Replace regexes with custom code. The regex engine is relatively slow for these simple matches and it allocates for every call.
  • Modernise some of the tokenizer code using strings.Cut and friends; makes for easier reading and less string slicing.
  • Generate a String() method for the token constants.
  • Add more coverage tests.

Before:

goos: linux
goarch: amd64
pkg: github.com/projectcalico/calico/libcalico-go/lib/selector
cpu: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
BenchmarkParse-12    	  162223	      6416 ns/op	    2633 B/op	      46 allocs/op
PASS

After:

goos: linux
goarch: amd64
pkg: github.com/projectcalico/calico/libcalico-go/lib/selector
cpu: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
BenchmarkParse-12       	  919357	      1209 ns/op	     520 B/op	      22 allocs/op
BenchmarkValidate-12    	 3878425	       293.8 ns/op	       0 B/op	       0 allocs/op
PASS

Related issues/PRs

CORE-10829

Todos

  • Tests
  • Documentation
  • Release note

Release Note

Improve performance of selector parsing and validation.

Reminder for the reviewer

Make sure that this PR has the correct labels and milestone set.

Every PR needs one docs-* label.

  • docs-pr-required: This change requires a change to the documentation that has not been completed yet.
  • docs-completed: This change has all necessary documentation completed.
  • docs-not-required: This change has no user-facing impact and requires no docs.

Every PR needs one release-note-* label.

  • release-note-required: This PR has user-facing changes. Most PRs should have this label.
  • release-note-not-required: This PR has no user-facing changes.

Other optional labels:

  • cherry-pick-candidate: This PR should be cherry-picked to an earlier release. For bug fixes only.
  • needs-operator-pr: This PR is related to install and requires a corresponding change to the operator.

@marvin-tigera marvin-tigera added this to the Calico v3.30.0 milestone Dec 5, 2024
@marvin-tigera marvin-tigera added release-note-required Change has user-facing impact (no matter how small) docs-pr-required Change is not yet documented labels Dec 5, 2024
@fasaxc fasaxc force-pushed the squash-allocs branch 2 times, most recently from e57d003 to 51d7220 Compare December 5, 2024 18:20
@fasaxc fasaxc added docs-not-required Docs not required for this change and removed docs-pr-required Change is not yet documented labels Dec 5, 2024
Reduce usage of WithFields on the hot path; it allocates heavily.
@fasaxc fasaxc marked this pull request as ready for review December 6, 2024 15:34
@fasaxc fasaxc requested a review from a team as a code owner December 6, 2024 15:34
Copy link
Member

@mazdakn mazdakn left a comment

Choose a reason for hiding this comment

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

LGTM. Just left a few questions and nits.

log.Debug("Remaining input: ", input)
}
startLen := len(input)
input = strings.TrimLeft(input, whitespace)
input = trimWhitespace(input)
Copy link
Member

@mazdakn mazdakn Dec 11, 2024

Choose a reason for hiding this comment

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

I assume we can do this because of the limited charset we recognise, right? But what's the main motivation it?

Copy link
Member Author

@fasaxc fasaxc Dec 12, 2024

Choose a reason for hiding this comment

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

Yes, I think TrimLeft was allocating/doing work to calculate the cut set so this was faster.

)
func (t Token) String() string {
return fmt.Sprintf("%s(%s)", t.Kind, t.Value)
}

// Tokenize transforms string to token slice
func Tokenize(input string) (tokens []Token, err error) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: no need for named return values.

if len(input) > 1 && input[1] == '=' {
tokens = append(tokens, Token{TokEq, nil})
input = input[2:]
if input, found = strings.CutPrefix(input, "=="); found {
Copy link
Member

Choose a reason for hiding this comment

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

Using CutPrefix is definitely easier to read. But I wonder why here we move to a cleaner version, while for TrimLeft the decision is to implement a new function?

@fasaxc
Copy link
Member Author

fasaxc commented Dec 12, 2024

/merge-when-ready squash-commits

@marvin-tigera
Copy link
Contributor

OK, I will merge the pull request when it's ready, squash the commits when I merge it, and leave the branch after I've merged it.

@marvin-tigera marvin-tigera merged commit 927bb3e into projectcalico:master Dec 12, 2024
5 checks passed
sridhartigera pushed a commit to sridhartigera/calico that referenced this pull request Dec 18, 2024
…ico#9564)

* Squash some chatty allocations.

Reduce usage of WithFields on the hot path; it allocates heavily.

* Avoid allocations when validating selectors.

* Clean ups in tokenizer.

* Tweaks.

* Markups.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs-not-required Docs not required for this change merge-when-ready release-note-required Change has user-facing impact (no matter how small) squash-commits
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants