You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using regex.MustCompile consumes a significant amount of memory when
importing the package, even if those regular expressions are not used.
This changes compiling the regular expressions to use a lazyregexp
package so that they're only compiled the first time they're used.
There are various regular expressions remaining that are still compiled
on import, but these are exported, so changing them to a sync.OnceValue
would be a breaking change; we can still decide to do so, but leaving
that for a follow-up.
To verify, compile a basic binary importing the package;
package main
import _ "github.com/distribution/reference"
func main() {}
Before:
for i in $(seq 1 5); do GODEBUG=inittrace=1 ./before 2>&1 | grep distribution/reference; done
init github.com/distribution/reference @0.94 ms, 0.22 ms clock, 415712 bytes, 3599 allocs
init github.com/distribution/reference @0.39 ms, 0.22 ms clock, 415712 bytes, 3599 allocs
init github.com/distribution/reference @0.39 ms, 0.23 ms clock, 415712 bytes, 3599 allocs
init github.com/distribution/reference @0.45 ms, 0.27 ms clock, 415712 bytes, 3599 allocs
init github.com/distribution/reference @0.38 ms, 0.24 ms clock, 415712 bytes, 3599 allocs
After:
for i in $(seq 1 5); do GODEBUG=inittrace=1 ./after 2>&1 | grep distribution/reference; done
init github.com/distribution/reference/internal/lazyregexp @0.85 ms, 0 ms clock, 0 bytes, 0 allocs
init github.com/distribution/reference @1.0 ms, 0.16 ms clock, 238680 bytes, 1383 allocs
init github.com/distribution/reference/internal/lazyregexp @0.33 ms, 0 ms clock, 0 bytes, 0 allocs
init github.com/distribution/reference @0.42 ms, 0.16 ms clock, 238680 bytes, 1383 allocs
init github.com/distribution/reference/internal/lazyregexp @0.39 ms, 0 ms clock, 0 bytes, 0 allocs
init github.com/distribution/reference @0.47 ms, 0.19 ms clock, 238680 bytes, 1383 allocs
init github.com/distribution/reference/internal/lazyregexp @0.36 ms, 0 ms clock, 0 bytes, 0 allocs
init github.com/distribution/reference @0.47 ms, 0.14 ms clock, 238680 bytes, 1383 allocs
init github.com/distribution/reference/internal/lazyregexp @0.29 ms, 0 ms clock, 0 bytes, 0 allocs
init github.com/distribution/reference @0.38 ms, 0.15 ms clock, 238680 bytes, 1383 allocs
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
0 commit comments