| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
|
Skipping CI for Draft Pull Request. |
Sorry, something went wrong.
There was a problem hiding this comment.
Changes in this file will conflict with another PR currently pending;
Sorry, something went wrong.
|
Does this have a significant benefit? |
Sorry, something went wrong.
|
I don't have benchmarks for this case (and it may depend where these packages are used); I know both @kolyshkin and @tonistiigi have looked at use of regexp.MustCompile in library code in various projects, as they tended to bring additional overhead even when not used; some related discussions with some examples; |
Sorry, something went wrong.
|
@AkihiroSuda OK; did some rudimentary comparisons based on just the github.com/containerd/containerd/v2 packages (I didn't compare (clock) time, as it fluctuated too much);
For those, I ran (e.g.); Before: GODEBUG=inittrace=1 containerd-shim-runc-v2 --version 2>&1 | grep github.com/containerd/containerd/v2 init github.com/containerd/containerd/v2/version @0.48 ms, 0.043 ms clock, 0 bytes, 0 allocs init github.com/containerd/containerd/v2/pkg/identifiers @3.6 ms, 0.063 ms clock, 5184 bytes, 73 allocs init github.com/containerd/containerd/v2/pkg/protobuf @4.2 ms, 0.007 ms clock, 128 bytes, 3 allocs init github.com/containerd/containerd/v2/core/mount @4.7 ms, 0.001 ms clock, 0 bytes, 0 allocs init github.com/containerd/containerd/v2/pkg/sys/reaper @4.7 ms, 0 ms clock, 48 bytes, 1 allocs init github.com/containerd/containerd/v2/core/runtime @4.8 ms, 0.007 ms clock, 992 bytes, 11 allocs init github.com/containerd/containerd/v2/cmd/containerd-shim-runc-v2/task/plugin @4.8 ms, 0.001 ms clock, 136 bytes, 3 allocs After: GODEBUG=inittrace=1 containerd-shim-runc-v2 --version 2>&1 | grep github.com/containerd/containerd/v2 init github.com/containerd/containerd/v2/version @0.30 ms, 0.004 ms clock, 0 bytes, 0 allocs init github.com/containerd/containerd/v2/internal/lazyregexp @2.1 ms, 0 ms clock, 0 bytes, 0 allocs init github.com/containerd/containerd/v2/pkg/identifiers @2.1 ms, 0.001 ms clock, 144 bytes, 3 allocs init github.com/containerd/containerd/v2/pkg/protobuf @2.8 ms, 0.006 ms clock, 128 bytes, 3 allocs init github.com/containerd/containerd/v2/core/mount @3.3 ms, 0.001 ms clock, 0 bytes, 0 allocs init github.com/containerd/containerd/v2/pkg/sys/reaper @3.3 ms, 0 ms clock, 48 bytes, 1 allocs init github.com/containerd/containerd/v2/core/runtime @3.4 ms, 0.006 ms clock, 992 bytes, 11 allocs init github.com/containerd/containerd/v2/cmd/containerd-shim-runc-v2/task/plugin @3.5 ms, 0.001 ms clock, 136 bytes, 3 allocs |
Sorry, something went wrong.
There was a problem hiding this comment.
minor comments, LGTM overall
Sorry, something went wrong.
|
It might make sense to drop the use of regexp altogether at least in some cases. Regexp is good to have when the pattern can be different (for example, supplied from config). It can be useful when the parsing logic is very complex. In other cases (relatively simple pattern which won't change) it's better to reimplement the logic. For example, here is Validate from pkg/indentifiers which don't use regexp, mostly written by Claude. It's faster, cleaner, and the logic is easy to follow: func Validate(s string) error {
if len(s) == 0 {
return fmt.Errorf("identifier must not be empty: %w", errdefs.ErrInvalidArgument)
}
if len(s) > maxLength {
return fmt.Errorf("identifier %q greater than maximum length (%d characters): %w", s, maxLength, errdefs.ErrInvalidArgument)
}
// Must start with alphanumeric character.
if !isAlphaNum(s[0]) {
return fmt.Errorf("identifier %q must begin with alphanumeric character: %w", s, errdefs.ErrInvalidArgument)
}
// Now check the pattern: alphanumeric followed by optional (separator + alphanumeric).
i := 0
// Parse first alphanumeric segment.
for i < len(s) && isAlphaNum(s[i]) {
i++
}
// Now we should have alternating separator and alphanumeric segments.
for i < len(s) {
// Must be a separator.
if !isSep(s[i]) {
return fmt.Errorf("identifier %q must match pattern of alphanumeric segments separated by '.', '_', or '-': %w", s, errdefs.ErrInvalidArgument)
}
i++
// Separator must be followed by at least one alphanumeric.
if i >= len(s) || !isAlphaNum(s[i]) {
return fmt.Errorf("identifier %q must have alphanumeric characters after each separator: %w", s, errdefs.ErrInvalidArgument)
}
// Parse alphanumeric segment.
for i < len(s) && isAlphaNum(s[i]) {
i++
}
}
return nil
}
func isAlphaNum(c byte) bool {
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')
}
func isSep(c byte) bool {
return c == '.' || c == '_' || c == '-'
} |
Sorry, something went wrong.
|
Can you rebase to pick up the CI fixes |
Sorry, something went wrong.
|
rebased 👍 @kolyshkin thanks! Yes, I think rewriting some of these could make sense. I did this one as I had done this approach in some of the other repositories, and it at least addresses the immediate issue, but yeah, sometimes regexes have been used a bit too early. I might spend some time on those on a rainy afternoon |
Sorry, something went wrong.
|
Looks good, can you squash the package updates into a single commit |
Sorry, something went wrong.
Based on the "lazyregexp" package in golang.org/x/mod; https://cs.opensource.google/go/x/mod/+/refs/tags/v0.19.0:internal/lazyregexp/lazyre.go;l=66-78 This package allows defining regular expressions that should not be compiled until used, but still providing validation to prevent invalid regular expressions from producing a panic at runtime. The lazyregexp package provides a subset of the methods provided by "regexp" and only implements the methods used in the codebase. Additional methods can be added when needed. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
- internal/cri/bandwidth: use lazyregexp to compile regexes on first use - pkg/identifiers: use lazyregexp to compile regexes on first use - pkg/progress: use lazyregexp to compile regexes on first use - pkg/reference: use lazyregexp to compile regexes on first use - pkg/sys: use lazyregexp to compile regexes on first use Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
|
done; let me know if this is what you meant |
Sorry, something went wrong.
|
/retest |
Sorry, something went wrong.
|
all green again 👍 |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
implement lazyregexp package
Based on the "lazyregexp" package in golang.org/x/mod;
https://cs.opensource.google/go/x/mod/+/refs/tags/v0.19.0:internal/lazyregexp/lazyre.go;l=66-78
This package allows defining regular expressions that should not be
compiled until used, but still providing validation to prevent
invalid regular expressions from producing a panic at runtime.
The lazyregexp package provides a subset of the methods provided
by "regexp" and only implements the methods used in the codebase.
Additional methods can be added when needed.