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

Create pkg/regexp to better handle init regex #1465

Merged
merged 1 commit into from Jan 12, 2023

Conversation

rhatdan
Copy link
Member

@rhatdan rhatdan commented Jan 12, 2023

The new Reexp structure, will handle the initialization of regexp and then will do the MustCompile within a sync.Once field.

Signed-off-by: Daniel J Walsh dwalsh@redhat.com

@rhatdan
Copy link
Member Author

rhatdan commented Jan 12, 2023

@alexlarsson
Copy link
Contributor

looks great to me. Should potentially be somewhere not in containers/storage though... Maybe containers/common?

@giuseppe
Copy link
Member

LGTM, I like the idea. The only risk is that the regex using the new package can panic at any point during the program execution (potentially causing db corruptions), while before we'd catch the error immediately; but I don't see an immediate way to address it. Maybe an env variable that causes the regexp to be compiled immediately and enable it in the CI?

@alexlarsson
Copy link
Contributor

The reonce package has:

The reoncetest build tag forces reonce to immediately compile regexes and panic on error.

If we did this, the CI could enable this.

@rhatdan
Copy link
Member Author

rhatdan commented Jan 12, 2023

@alexlarsson Containers/storage is vendored into containers/common, not the other way around

This is how packages are vendored Each package vendors the packages above.

containers/storage
containers/image
containers/common
containers/buildah
containers/podman

@rhatdan
Copy link
Member Author

rhatdan commented Jan 12, 2023

@alexlarsson @giuseppe added regex_precompile build tag

Copy link
Member

@giuseppe giuseppe left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@vrothberg vrothberg left a comment

Choose a reason for hiding this comment

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

Other than the nit LGTM.

I am not sure about the precompile build tag. If users want to precompile, they can use the stdlib directly.


// Regexp is a wrapper struct used for wrapping MustCompile regex expressions
// used init. Using this stucture should help speed up startup time of apps
// that want to initialize their regex at init time.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// that want to initialize their regex at init time.
// that do not want to initialize their regex at init time.

@rhatdan
Copy link
Member Author

rhatdan commented Jan 12, 2023

The goal of the tag is to blow up if the REGEX is bad, during testing versus production, not likely, but might as well provide functionality.


package regexp

var precompile = false
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be const?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes (in both cases); this decision should have zero run-time cost.

Copy link
Collaborator

@mtrmac mtrmac left a comment

Choose a reason for hiding this comment

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

Makes sense overall, some design points to perhaps discuss (but I don’t feel strongly about any of them).

@@ -0,0 +1,210 @@
package regexp
Copy link
Collaborator

Choose a reason for hiding this comment

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

I’m not sure about using the same name as the standard library package, the two are not interoperable in any sense. OTOH there’s a certain elegance to this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Having the same package name does make it tricky to use this and call regular regexp global functions from the same file (you'd have to rename one of them), so i agree with this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OTOH that could be useful as a speed bump to make the user consider whether that call should use the delayed package as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, for dynaminc use (i.e. create a regexp in a function) you never want the lazy stuff.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That’s a good point.

pkg/regexp/regexp.go Outdated Show resolved Hide resolved
}

func (re *Regexp) compile() {
re.once.Do(func() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don’t do this if precompile?

On the one hand, the current version makes run-time profiling of the precompile version representative of the other one.

On the other hand, making compilation conditional here would allow an easy experiment to see if precompilation, in total, pays off in a particular situation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, so you want the re.once.Do command not to be executed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

My very weak preference would be to not execute it, but I’m completely fine with either one.

pkg/regexp/regexp.go Outdated Show resolved Hide resolved
@rhatdan rhatdan force-pushed the regexp branch 2 times, most recently from 58c9b92 to 776147b Compare January 12, 2023 17:04
pkg/regexp/regexp.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@mtrmac mtrmac left a comment

Choose a reason for hiding this comment

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

LGTM; there are still some discussions going on but I’m fine with any outcome of them

The new Reexp structure, will handle the initialization of regexp
and then will do the MustCompile within a sync.Once field.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
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.

None yet

5 participants