Explorar o código

Merge pull request #13190 from unclejack/precompile_regexp

Precompile regexp
Alexander Morozov %!s(int64=10) %!d(string=hai) anos
pai
achega
877461bc20
Modificáronse 2 ficheiros con 5 adicións e 2 borrados
  1. 2 1
      image/image.go
  2. 3 1
      pkg/stringid/stringid.go

+ 2 - 1
image/image.go

@@ -19,6 +19,8 @@ import (
 // For more information see: http://sourceforge.net/p/aufs/aufs3-standalone/ci/aufs3.12/tree/config.mk
 // For more information see: http://sourceforge.net/p/aufs/aufs3-standalone/ci/aufs3.12/tree/config.mk
 const MaxImageDepth = 127
 const MaxImageDepth = 127
 
 
+var validHex = regexp.MustCompile(`^([a-f0-9]{64})$`)
+
 type Image struct {
 type Image struct {
 	ID              string            `json:"id"`
 	ID              string            `json:"id"`
 	Parent          string            `json:"parent,omitempty"`
 	Parent          string            `json:"parent,omitempty"`
@@ -266,7 +268,6 @@ func NewImgJSON(src []byte) (*Image, error) {
 
 
 // Check wheather id is a valid image ID or not
 // Check wheather id is a valid image ID or not
 func ValidateID(id string) error {
 func ValidateID(id string) error {
-	validHex := regexp.MustCompile(`^([a-f0-9]{64})$`)
 	if ok := validHex.MatchString(id); !ok {
 	if ok := validHex.MatchString(id); !ok {
 		return fmt.Errorf("image ID '%s' is invalid", id)
 		return fmt.Errorf("image ID '%s' is invalid", id)
 	}
 	}

+ 3 - 1
pkg/stringid/stringid.go

@@ -10,9 +10,11 @@ import (
 
 
 const shortLen = 12
 const shortLen = 12
 
 
+var validShortID = regexp.MustCompile("^[a-z0-9]{12}$")
+
 // Determine if an arbitrary string *looks like* a short ID.
 // Determine if an arbitrary string *looks like* a short ID.
 func IsShortID(id string) bool {
 func IsShortID(id string) bool {
-	return regexp.MustCompile("^[a-z0-9]{12}$").MatchString(id)
+	return validShortID.MatchString(id)
 }
 }
 
 
 // TruncateID returns a shorthand version of a string identifier for convenience.
 // TruncateID returns a shorthand version of a string identifier for convenience.