summaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorSebastiaan van Stijn <thaJeztah@users.noreply.github.com>2022-12-17 18:24:39 +0100
committerGitHub <noreply@github.com>2022-12-17 18:24:39 +0100
commit13b36ce06352ff9853ee5dc2ef1d5b33b85e17f4 (patch)
tree099ffc37a2a28216a4f2bfaf720845c9e911ed46 /pkg
parent6371675bf9de2f38735db36ebe73fba5c9078ffb (diff)
parent54512f218422738013ce4de78e5a0c79f64607b8 (diff)
downloaddocker-13b36ce06352ff9853ee5dc2ef1d5b33b85e17f4.tar.gz
Merge pull request #44661 from thaJeztah/optimize_stringid
pkg/stringid: optimize performance
Diffstat (limited to 'pkg')
-rw-r--r--pkg/stringid/stringid.go22
-rw-r--r--pkg/stringid/stringid_test.go27
2 files changed, 42 insertions, 7 deletions
diff --git a/pkg/stringid/stringid.go b/pkg/stringid/stringid.go
index 5fe071d628..d3d1014acf 100644
--- a/pkg/stringid/stringid.go
+++ b/pkg/stringid/stringid.go
@@ -4,21 +4,28 @@ package stringid // import "github.com/docker/docker/pkg/stringid"
import (
"crypto/rand"
"encoding/hex"
- "fmt"
+ "errors"
"regexp"
"strconv"
"strings"
)
-const shortLen = 12
+const (
+ shortLen = 12
+ fullLen = 64
+)
var (
validShortID = regexp.MustCompile("^[a-f0-9]{12}$")
validHex = regexp.MustCompile(`^[a-f0-9]{64}$`)
)
-// IsShortID determines if an arbitrary string *looks like* a short ID.
+// IsShortID determines if id has the correct format and length for a short ID.
+// It checks the IDs length and if it consists of valid characters for IDs (a-f0-9).
func IsShortID(id string) bool {
+ if len(id) != shortLen {
+ return false
+ }
return validShortID.MatchString(id)
}
@@ -54,10 +61,13 @@ func GenerateRandomID() string {
}
}
-// ValidateID checks whether an ID string is a valid image ID.
+// ValidateID checks whether an ID string is a valid, full-length image ID.
func ValidateID(id string) error {
- if ok := validHex.MatchString(id); !ok {
- return fmt.Errorf("image ID %q is invalid", id)
+ if len(id) != fullLen {
+ return errors.New("image ID '" + id + "' is invalid")
+ }
+ if !validHex.MatchString(id) {
+ return errors.New("image ID '" + id + "' is invalid")
}
return nil
}
diff --git a/pkg/stringid/stringid_test.go b/pkg/stringid/stringid_test.go
index 2660d2e65f..e2d0d7906f 100644
--- a/pkg/stringid/stringid_test.go
+++ b/pkg/stringid/stringid_test.go
@@ -8,7 +8,7 @@ import (
func TestGenerateRandomID(t *testing.T) {
id := GenerateRandomID()
- if len(id) != 64 {
+ if len(id) != fullLen {
t.Fatalf("Id returned is incorrect: %s", id)
}
}
@@ -62,3 +62,28 @@ func TestIsShortIDNotCorrectSize(t *testing.T) {
t.Fatalf("%s is not a short ID", id)
}
}
+
+var testIDs = []string{
+ "4e38e38c8ce0",
+ strings.Repeat("a", shortLen+1),
+ strings.Repeat("a", 16000),
+ "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2",
+}
+
+func BenchmarkIsShortID(b *testing.B) {
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ for _, id := range testIDs {
+ _ = IsShortID(id)
+ }
+ }
+}
+
+func BenchmarkValidateID(b *testing.B) {
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ for _, id := range testIDs {
+ _ = ValidateID(id)
+ }
+ }
+}