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

Bug 1937972: router/template: Cache compiled regular expressions #268

Merged

Conversation

frobware
Copy link
Contributor

This is an optimisation that caches compiled regular expressions.

When the template is written calls to matchString and firstMatch
would recompile the regular expression on every call. When you are
processing ~13,000 routes the time to compile on each invocation becomes
significant. This change brings a reduction of 60% when processing and
writing haproxy.config.

router.go:456] template "msg"="writeConfig"  "duration"="10.663160501s"
router.go:456] template "msg"="writeConfig"  "duration"="10.328840858s"
router.go:456] template "msg"="writeConfig"  "duration"="10.762876672s"

And if we cache the REs:

template "msg"="writeConfig"  "duration"="3.659229627s"
template "msg"="writeConfig"  "duration"="3.718457818s"
template "msg"="writeConfig"  "duration"="3.672131941s"

Related to: https://bugzilla.redhat.com/show_bug.cgi?id=1929821

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 11, 2021
@knobunc
Copy link
Contributor

knobunc commented Mar 11, 2021

Looks good to me! But waiting for a second reviewer.

Copy link
Contributor

@Miciah Miciah left a comment

Choose a reason for hiding this comment

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

This is terrific.
/lgtm
/hold
in case you want to wait for further reviews or do any follow-up work such as comments or adding a BZ reference.

@@ -80,7 +105,7 @@ func matchValues(s string, allowedValues ...string) bool {

func matchPattern(pattern, s string) bool {
log.V(7).Info("matchPattern called", "pattern", pattern, "s", s)
status, err := regexp.MatchString(`\A(?:`+pattern+`)\z`, s)
status, err := matchString(`\A(?:`+pattern+`)\z`, s)
Copy link
Contributor

Choose a reason for hiding this comment

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

matchPattern isn't used in the template, so this shouldn't affect performance (but it shouldn't hurt anything either).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was explicit as MatchString compiles a regexp too:

// MatchString reports whether the string s
// contains any match of the regular expression pattern.
// More complicated queries need to use Compile and the full Regexp interface.
func MatchString(pattern string, s string) (matched bool, err error) {
	re, err := Compile(pattern)
	if err != nil {
		return false, err
	}
	return re.MatchString(s), nil
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean, this entire function is effectively dead code anyway, unless someone is using a custom template that uses matchPattern. That said, it is possible that someone is using a custom template that uses matchPattern, so we might as well optimize it.

@@ -31,9 +32,33 @@ func isTrue(s string) bool {
return v
}

var compiledRegexp sync.Map
Copy link
Contributor

Choose a reason for hiding this comment

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

I was wondering if we needed garbage collection for this map. I believe we do not as every use of firstMatch specifies a pattern that is a hard-coded in the template itself (as opposed to specifying a pattern from an annotation, for example), so the size of the map is bounded by a function of the static set of patterns that are used in haproxy-config.template.

pkg/router/template/template_helper.go Show resolved Hide resolved
@openshift-ci-robot openshift-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Mar 11, 2021
@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Mar 11, 2021
@sgreene570
Copy link

/retitle Bug 1937972: router/template: Cache compiled regular expressions

@openshift-ci-robot openshift-ci-robot changed the title router/template: Cache compiled regular expressions Bug 1937972: router/template: Cache compiled regular expressions Mar 11, 2021
@openshift-ci-robot openshift-ci-robot added the bugzilla/severity-high Referenced Bugzilla bug's severity is high for the branch this PR is targeting. label Mar 11, 2021
@openshift-ci-robot
Copy link
Contributor

@frobware: This pull request references Bugzilla bug 1937972, which is valid. The bug has been moved to the POST state. The bug has been updated to refer to the pull request using the external bug tracker.

3 validation(s) were run on this bug
  • bug is open, matching expected state (open)
  • bug target release (4.8.0) matches configured target release for branch (4.8.0)
  • bug is in the state ASSIGNED, which is one of the valid states (NEW, ASSIGNED, ON_DEV, POST, POST)

Requesting review from QA contact:
/cc @lihongan

In response to this:

Bug 1937972: router/template: Cache compiled regular expressions

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@openshift-ci-robot openshift-ci-robot added the bugzilla/valid-bug Indicates that a referenced Bugzilla bug is valid for the branch this PR is targeting. label Mar 11, 2021
@frobware frobware force-pushed the cache-compiled-regexps branch from dafee34 to e9d22d6 Compare March 11, 2021 19:55
@openshift-ci-robot openshift-ci-robot removed the lgtm Indicates that a PR is ready to be merged. label Mar 11, 2021
@frobware frobware force-pushed the cache-compiled-regexps branch from e9d22d6 to b96fe3b Compare March 11, 2021 20:07
}

func matchString(pattern string, s string) (bool, error) {
status, err := regexp.MatchString(`\A(?:`+pattern+`)\z`, s)
Copy link
Contributor

Choose a reason for hiding this comment

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

What's this line for? Copy and paste error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Errr. yes. Removing. :)

@frobware frobware force-pushed the cache-compiled-regexps branch from b96fe3b to 3fd480f Compare March 11, 2021 20:38
@Miciah
Copy link
Contributor

Miciah commented Mar 11, 2021

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Mar 11, 2021
@openshift-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: frobware, knobunc, Miciah

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [Miciah,frobware,knobunc]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@frobware
Copy link
Contributor Author

/hold cancel

@openshift-ci-robot openshift-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Mar 12, 2021
@openshift-merge-robot openshift-merge-robot merged commit f817470 into openshift:master Mar 12, 2021
@openshift-ci-robot
Copy link
Contributor

@frobware: All pull requests linked via external trackers have merged:

Bugzilla bug 1937972 has been moved to the MODIFIED state.

In response to this:

Bug 1937972: router/template: Cache compiled regular expressions

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@frobware
Copy link
Contributor Author

/cherry-pick release-4.7

@openshift-cherrypick-robot

@frobware: new pull request created: #269

In response to this:

/cherry-pick release-4.7

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@frobware
Copy link
Contributor Author

/cherry-pick release-4.6

@openshift-cherrypick-robot

@frobware: new pull request created: #270

In response to this:

/cherry-pick release-4.6

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@frobware
Copy link
Contributor Author

/cherry-pick release-4.5

@openshift-cherrypick-robot

@frobware: new pull request created: #271

In response to this:

/cherry-pick release-4.5

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. bugzilla/severity-high Referenced Bugzilla bug's severity is high for the branch this PR is targeting. bugzilla/valid-bug Indicates that a referenced Bugzilla bug is valid for the branch this PR is targeting. lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants