From b41114e9dcbddee4eb6b6af1554c82bbbccf9fb3 Mon Sep 17 00:00:00 2001 From: Shiwei Zhang Date: Tue, 19 Mar 2024 17:30:47 +0800 Subject: [PATCH 1/4] chore: use built-in slices Signed-off-by: Shiwei Zhang --- internal/slices/slice.go | 24 ------------------------ registry/remote/auth/scope.go | 2 +- registry/remote/repository.go | 2 +- 3 files changed, 2 insertions(+), 26 deletions(-) delete mode 100644 internal/slices/slice.go diff --git a/internal/slices/slice.go b/internal/slices/slice.go deleted file mode 100644 index cf22d36d..00000000 --- a/internal/slices/slice.go +++ /dev/null @@ -1,24 +0,0 @@ -/* -Copyright The ORAS Authors. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - -http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package slices - -// Clone returns a shallow copy of the slice. -func Clone[S ~[]E, E any](s S) S { - if s == nil { - return nil - } - return append(make(S, 0, len(s)), s...) -} diff --git a/registry/remote/auth/scope.go b/registry/remote/auth/scope.go index fabc2af2..adf79791 100644 --- a/registry/remote/auth/scope.go +++ b/registry/remote/auth/scope.go @@ -17,10 +17,10 @@ package auth import ( "context" + "slices" "sort" "strings" - "oras.land/oras-go/v2/internal/slices" "oras.land/oras-go/v2/registry" ) diff --git a/registry/remote/repository.go b/registry/remote/repository.go index ac4efcc5..7c36dc1c 100644 --- a/registry/remote/repository.go +++ b/registry/remote/repository.go @@ -24,6 +24,7 @@ import ( "io" "mime" "net/http" + "slices" "strconv" "strings" "sync" @@ -37,7 +38,6 @@ import ( "oras.land/oras-go/v2/internal/cas" "oras.land/oras-go/v2/internal/httputil" "oras.land/oras-go/v2/internal/ioutil" - "oras.land/oras-go/v2/internal/slices" "oras.land/oras-go/v2/internal/spec" "oras.land/oras-go/v2/internal/syncutil" "oras.land/oras-go/v2/registry" From ada641c7b4c9d808935632ff851235c260fc2f14 Mon Sep 17 00:00:00 2001 From: Shiwei Zhang Date: Tue, 19 Mar 2024 17:41:49 +0800 Subject: [PATCH 2/4] perf: use built-in map clone Signed-off-by: Shiwei Zhang --- internal/resolver/memory.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/internal/resolver/memory.go b/internal/resolver/memory.go index 80843e43..092a29e9 100644 --- a/internal/resolver/memory.go +++ b/internal/resolver/memory.go @@ -17,6 +17,7 @@ package resolver import ( "context" + "maps" "sync" "github.com/opencontainers/go-digest" @@ -90,11 +91,7 @@ func (m *Memory) Map() map[string]ocispec.Descriptor { m.lock.RLock() defer m.lock.RUnlock() - res := make(map[string]ocispec.Descriptor, len(m.index)) - for key, value := range m.index { - res[key] = value - } - return res + return maps.Clone(m.index) } // TagSet returns the set of tags of the descriptor. @@ -103,9 +100,5 @@ func (m *Memory) TagSet(desc ocispec.Descriptor) set.Set[string] { defer m.lock.RUnlock() tagSet := m.tags[desc.Digest] - res := make(set.Set[string], len(tagSet)) - for tag := range tagSet { - res.Add(tag) - } - return res + return maps.Clone(tagSet) } From 8cbf8dde8340984a812927ea96772e2673a0a183 Mon Sep 17 00:00:00 2001 From: Shiwei Zhang Date: Tue, 19 Mar 2024 17:45:10 +0800 Subject: [PATCH 3/4] chore: use built-in map copy Signed-off-by: Shiwei Zhang --- content/oci/oci.go | 5 ++--- pack.go | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/content/oci/oci.go b/content/oci/oci.go index dac299ec..4c25af80 100644 --- a/content/oci/oci.go +++ b/content/oci/oci.go @@ -23,6 +23,7 @@ import ( "errors" "fmt" "io" + "maps" "os" "path" "path/filepath" @@ -418,9 +419,7 @@ func (s *Store) saveIndex() error { for ref, desc := range refMap { if ref != desc.Digest.String() { annotations := make(map[string]string, len(desc.Annotations)+1) - for k, v := range desc.Annotations { - annotations[k] = v - } + maps.Copy(annotations, desc.Annotations) annotations[ocispec.AnnotationRefName] = ref desc.Annotations = annotations manifests = append(manifests, desc) diff --git a/pack.go b/pack.go index 5aea387a..1b995612 100644 --- a/pack.go +++ b/pack.go @@ -21,6 +21,7 @@ import ( "encoding/json" "errors" "fmt" + "maps" "regexp" "time" @@ -420,9 +421,8 @@ func ensureAnnotationCreated(annotations map[string]string, annotationCreatedKey // copy the original annotation map copied := make(map[string]string, len(annotations)+1) - for k, v := range annotations { - copied[k] = v - } + maps.Copy(copied, annotations) + // set creation time in RFC 3339 format // reference: https://github.com/opencontainers/image-spec/blob/v1.1.0-rc2/annotations.md#pre-defined-annotation-keys now := time.Now().UTC() From a1d93c097cb5e49f02a6c2ff9a79640d1e64cbb0 Mon Sep 17 00:00:00 2001 From: Shiwei Zhang Date: Tue, 19 Mar 2024 17:55:40 +0800 Subject: [PATCH 4/4] perf: use slices.Sort instead of sort.Strings Signed-off-by: Shiwei Zhang --- content/oci/oci.go | 2 +- content/oci/readonlyoci.go | 8 ++++---- registry/remote/auth/scope.go | 7 +++---- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/content/oci/oci.go b/content/oci/oci.go index 4c25af80..748aeecc 100644 --- a/content/oci/oci.go +++ b/content/oci/oci.go @@ -334,7 +334,7 @@ func (s *Store) Tags(ctx context.Context, last string, fn func(tags []string) er s.sync.RLock() defer s.sync.RUnlock() - return listTags(ctx, s.tagResolver, last, fn) + return listTags(s.tagResolver, last, fn) } // ensureOCILayoutFile ensures the `oci-layout` file. diff --git a/content/oci/readonlyoci.go b/content/oci/readonlyoci.go index 2f390bb3..3f1ee4ee 100644 --- a/content/oci/readonlyoci.go +++ b/content/oci/readonlyoci.go @@ -22,7 +22,7 @@ import ( "fmt" "io" "io/fs" - "sort" + "slices" "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" @@ -125,7 +125,7 @@ func (s *ReadOnlyStore) Predecessors(ctx context.Context, node ocispec.Descripto // // See also `Tags()` in the package `registry`. func (s *ReadOnlyStore) Tags(ctx context.Context, last string, fn func(tags []string) error) error { - return listTags(ctx, s.tagResolver, last, fn) + return listTags(s.tagResolver, last, fn) } // validateOCILayoutFile validates the `oci-layout` file. @@ -216,7 +216,7 @@ func resolveBlob(fsys fs.FS, dgst string) (ocispec.Descriptor, error) { // list. // // See also `Tags()` in the package `registry`. -func listTags(ctx context.Context, tagResolver *resolver.Memory, last string, fn func(tags []string) error) error { +func listTags(tagResolver *resolver.Memory, last string, fn func(tags []string) error) error { var tags []string tagMap := tagResolver.Map() @@ -229,7 +229,7 @@ func listTags(ctx context.Context, tagResolver *resolver.Memory, last string, fn } tags = append(tags, tag) } - sort.Strings(tags) + slices.Sort(tags) return fn(tags) } diff --git a/registry/remote/auth/scope.go b/registry/remote/auth/scope.go index adf79791..d81cc0d4 100644 --- a/registry/remote/auth/scope.go +++ b/registry/remote/auth/scope.go @@ -18,7 +18,6 @@ package auth import ( "context" "slices" - "sort" "strings" "oras.land/oras-go/v2/registry" @@ -276,14 +275,14 @@ func CleanScopes(scopes []string) []string { } actions = append(actions, action) } - sort.Strings(actions) + slices.Sort(actions) scope := resourceType + ":" + resourceName + ":" + strings.Join(actions, ",") result = append(result, scope) } } // sort and return - sort.Strings(result) + slices.Sort(result) return result } @@ -302,7 +301,7 @@ func cleanActions(actions []string) []string { } // slow path - sort.Strings(actions) + slices.Sort(actions) n := 0 for i := 0; i < len(actions); i++ { if actions[i] == "*" {