mirror of
https://github.com/project-zot/zot.git
synced 2026-06-17 12:58:02 +08:00
refactor: optimize code with modern Go patterns and pre-allocation (#3576)
This commit modernizes code across multiple packages by: - Using Go 1.18+ features (slices.IndexFunc, strings.Cut) - Pre-allocating slices and maps with known capacity - Consolidating defensive checks and improving code clarity - Fixing test data and build tag issues CLI client improvements: - Pre-allocate slices in search functions and service methods - Replace strings.Split with strings.Cut for username:password parsing - Use range-based iteration instead of manual index loops Search extension optimizations: - Cache sort functions in pagination modules - Pre-allocate page buffers and maps - Consolidate defensive checks in filterBaseImages/filterDerivedImages - Fix image bas and derived logic allowing out of sequence layers for base images - Fix image pagination reporting images groupped by repos when sorted by update time - Remove duplicate resolver_test.go file Monitoring extension: - Replace manual loops with slices.IndexFunc - Pre-allocate bucketsFloat2String map Sync extension: - Pre-allocate slice in parseRegistryURLs Test utilities: - Fix build tags in oci_layout.go Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>
This commit is contained in:
@@ -445,7 +445,8 @@ func fetchManifestStruct(ctx context.Context, repo, manifestReference string, se
|
||||
imageSize += manifestResp.Config.Size
|
||||
imageSize += manifestSize
|
||||
|
||||
layers := []common.LayerSummary{}
|
||||
// Pre-allocate slice with known capacity
|
||||
layers := make([]common.LayerSummary, 0, len(manifestResp.Layers))
|
||||
|
||||
for _, entry := range manifestResp.Layers {
|
||||
imageSize += entry.Size
|
||||
|
||||
@@ -51,7 +51,8 @@ func SearchAllImagesGQL(config SearchConfig) error {
|
||||
return err
|
||||
}
|
||||
|
||||
imageListData := []imageStruct{}
|
||||
// Pre-allocate slice with known capacity
|
||||
imageListData := make([]imageStruct, 0, len(imageList.Results))
|
||||
|
||||
for _, image := range imageList.Results {
|
||||
imageListData = append(imageListData, imageStruct(image))
|
||||
@@ -103,7 +104,8 @@ func SearchImageByNameGQL(config SearchConfig, imageName string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
imageListData := []imageStruct{}
|
||||
// Pre-allocate slice with known capacity (may be filtered, but worst case is all results)
|
||||
imageListData := make([]imageStruct, 0, len(imageList.Results))
|
||||
|
||||
for _, image := range imageList.Results {
|
||||
if tag == "" || image.Tag == tag {
|
||||
@@ -152,7 +154,8 @@ func SearchDerivedImageListGQL(config SearchConfig, derivedImage string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
imageListData := []imageStruct{}
|
||||
// Pre-allocate slice with known capacity
|
||||
imageListData := make([]imageStruct, 0, len(imageList.DerivedImageList.Results))
|
||||
|
||||
for _, image := range imageList.DerivedImageList.Results {
|
||||
imageListData = append(imageListData, imageStruct(image))
|
||||
@@ -173,7 +176,8 @@ func SearchBaseImageListGQL(config SearchConfig, baseImage string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
imageListData := []imageStruct{}
|
||||
// Pre-allocate slice with known capacity
|
||||
imageListData := make([]imageStruct, 0, len(imageList.BaseImageList.Results))
|
||||
|
||||
for _, image := range imageList.BaseImageList.Results {
|
||||
imageListData = append(imageListData, imageStruct(image))
|
||||
@@ -193,7 +197,8 @@ func SearchImagesForDigestGQL(config SearchConfig, digest string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
imageListData := []imageStruct{}
|
||||
// Pre-allocate slice with known capacity
|
||||
imageListData := make([]imageStruct, 0, len(imageList.Results))
|
||||
|
||||
for _, image := range imageList.Results {
|
||||
imageListData = append(imageListData, imageStruct(image))
|
||||
@@ -344,7 +349,8 @@ func SearchImagesByCVEIDGQL(config SearchConfig, repo, cveid string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
imageListData := []imageStruct{}
|
||||
// Pre-allocate slice with known capacity
|
||||
imageListData := make([]imageStruct, 0, len(imageList.Results))
|
||||
|
||||
for _, image := range imageList.Results {
|
||||
imageListData = append(imageListData, imageStruct(image))
|
||||
@@ -403,13 +409,14 @@ func GlobalSearchGQL(config SearchConfig, query string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
imagesList := []imageStruct{}
|
||||
// Pre-allocate slices with known capacity
|
||||
imagesList := make([]imageStruct, 0, len(globalSearchResult.Images))
|
||||
|
||||
for _, image := range globalSearchResult.Images {
|
||||
imagesList = append(imagesList, imageStruct(image))
|
||||
}
|
||||
|
||||
reposList := []repoStruct{}
|
||||
reposList := make([]repoStruct, 0, len(globalSearchResult.Repos))
|
||||
|
||||
for _, repo := range globalSearchResult.Repos {
|
||||
reposList = append(reposList, repoStruct(repo))
|
||||
|
||||
@@ -403,7 +403,9 @@ func (service searchService) getTagsForCVEGQL(ctx context.Context, config Search
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Pre-allocate filtered results slice with estimated capacity
|
||||
filteredResults := &common.ImagesForCve{}
|
||||
filteredResults.Results = make([]common.ImageSummary, 0, len(result.Results))
|
||||
|
||||
for _, image := range result.Results {
|
||||
if image.RepoName == repo {
|
||||
@@ -476,7 +478,8 @@ func (service searchService) getReferrers(ctx context.Context, config SearchConf
|
||||
return referrersResult{}, err
|
||||
}
|
||||
|
||||
referrersList := referrersResult{}
|
||||
// Pre-allocate referrers list with known capacity
|
||||
referrersList := make(referrersResult, 0, len(referrerResp.Manifests))
|
||||
|
||||
for _, referrer := range referrerResp.Manifests {
|
||||
referrersList = append(referrersList, common.Referrer{
|
||||
@@ -954,7 +957,8 @@ func (ref referrersResult) stringPlainText(maxArtifactTypeLen int) (string, erro
|
||||
var builder strings.Builder
|
||||
|
||||
maxDigestWidth := digestWidth
|
||||
rows := [][]string{}
|
||||
// Pre-allocate rows slice with known capacity
|
||||
rows := make([][]string, 0, len(ref))
|
||||
|
||||
for _, referrer := range ref {
|
||||
artifactType := ellipsize(referrer.ArtifactType, maxArtifactTypeLen, ellipsis)
|
||||
@@ -1390,12 +1394,14 @@ func (service searchService) getRepos(ctx context.Context, config SearchConfig,
|
||||
fmt.Fprintln(config.ResultWriter, "\nREPOSITORY NAME")
|
||||
|
||||
if config.SortBy == SortByAlphabeticAsc {
|
||||
for i := 0; i < len(catalog.Repositories); i++ {
|
||||
fmt.Fprintln(config.ResultWriter, catalog.Repositories[i])
|
||||
for _, repo := range catalog.Repositories {
|
||||
fmt.Fprintln(config.ResultWriter, repo)
|
||||
}
|
||||
} else {
|
||||
for i := len(catalog.Repositories) - 1; i >= 0; i-- {
|
||||
fmt.Fprintln(config.ResultWriter, catalog.Repositories[i])
|
||||
// Iterate in reverse order
|
||||
repos := catalog.Repositories
|
||||
for i := len(repos) - 1; i >= 0; i-- {
|
||||
fmt.Fprintln(config.ResultWriter, repos[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,9 +93,9 @@ func collectResults(config SearchConfig, wg *sync.WaitGroup, imageErr chan strin
|
||||
|
||||
func getUsernameAndPassword(user string) (string, string) {
|
||||
if strings.Contains(user, ":") {
|
||||
split := strings.Split(user, ":")
|
||||
username, password, _ := strings.Cut(user, ":")
|
||||
|
||||
return split[0], split[1]
|
||||
return username, password
|
||||
}
|
||||
|
||||
return "", ""
|
||||
|
||||
Reference in New Issue
Block a user