Skip to content

Commit

Permalink
perf: use new built-in methods introduced before go 1.22 (#726)
Browse files Browse the repository at this point in the history
Changes:
* chore: use built-in slices
* perf: use built-in map clone
* chore: use built-in map copy
* perf: use slices.Sort instead of sort.Strings

Resolves: #713

---------

Signed-off-by: Shiwei Zhang <shizh@microsoft.com>
  • Loading branch information
shizhMSFT authored Mar 20, 2024
1 parent 8d139f0 commit 4503c31
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 51 deletions.
7 changes: 3 additions & 4 deletions content/oci/oci.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 4503c31

Please sign in to comment.