Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use new built-in methods introduced before go 1.22 #726

Merged
merged 4 commits into from Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions content/oci/oci.go
Expand Up @@ -23,6 +23,7 @@ import (
"errors"
"fmt"
"io"
"maps"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -333,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.
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions content/oci/readonlyoci.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
Expand Down
13 changes: 3 additions & 10 deletions internal/resolver/memory.go
Expand Up @@ -17,6 +17,7 @@ package resolver

import (
"context"
"maps"
"sync"

"github.com/opencontainers/go-digest"
Expand Down Expand Up @@ -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.
Expand All @@ -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)
}
24 changes: 0 additions & 24 deletions internal/slices/slice.go

This file was deleted.

6 changes: 3 additions & 3 deletions pack.go
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"maps"
"regexp"
"time"

Expand Down Expand Up @@ -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()
Expand Down
9 changes: 4 additions & 5 deletions registry/remote/auth/scope.go
Expand Up @@ -17,10 +17,9 @@ package auth

import (
"context"
"sort"
"slices"
"strings"

"oras.land/oras-go/v2/internal/slices"
"oras.land/oras-go/v2/registry"
)

Expand Down Expand Up @@ -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
}

Expand All @@ -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] == "*" {
Expand Down
2 changes: 1 addition & 1 deletion registry/remote/repository.go
Expand Up @@ -24,6 +24,7 @@ import (
"io"
"mime"
"net/http"
"slices"
"strconv"
"strings"
"sync"
Expand All @@ -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"
Expand Down