Skip to content

Commit 15a5643

Browse files
authored
Nonfunctional changes (#1017)
* non-functional changes * remove additional allocation from autoconf expand * use slices.sort for sorting in autofconf * slices.Clone instead of make + copy
1 parent 6c1afb8 commit 15a5643

File tree

9 files changed

+41
-49
lines changed

9 files changed

+41
-49
lines changed

autoconf/client.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"net/http"
99
"os"
1010
"path/filepath"
11-
"sort"
11+
"slices"
1212
"strings"
1313
"sync"
1414
"time"
@@ -256,9 +256,8 @@ func (c *Client) getCacheDir() (string, error) {
256256
}
257257

258258
// Sort URLs for consistent hashing regardless of order
259-
sortedURLs := make([]string, len(c.urls))
260-
copy(sortedURLs, c.urls)
261-
sort.Strings(sortedURLs)
259+
sortedURLs := slices.Clone(c.urls)
260+
slices.Sort(sortedURLs)
262261

263262
// Hash all URLs together for a single cache directory
264263
h := fnv.New64a()

autoconf/expansion.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ package autoconf
22

33
import (
44
"math/rand"
5+
"slices"
56
"strings"
67
)
78

89
// normalizeURLs ensures all URLs in the slice have no trailing slashes
9-
func normalizeURLs(urls []string) []string {
10-
normalized := make([]string, len(urls))
10+
func normalizeURLs(urls []string) {
1111
for i, url := range urls {
12-
normalized[i] = strings.TrimRight(url, "/")
12+
urls[i] = strings.TrimRight(url, "/")
1313
}
14-
return normalized
1514
}
1615

1716
// expandAutoConfSlice is a generic helper for expanding "auto" placeholders in string slices.
@@ -163,33 +162,30 @@ func ExpandDelegatedEndpoints(configEndpoints []string, autoConf *Config, native
163162
return result
164163
}
165164

166-
var routers []string
167165
seen := make(map[string]struct{})
168-
169166
for baseURL, config := range endpoints {
170167
// Combine both Read and Write paths (already filtered by WithSupportedPathsOnly)
171-
allPaths := append(config.Read, config.Write...)
172-
168+
uniquePaths := slices.Concat(config.Read, config.Write)
173169
// Deduplicate paths (in case same path appears in both Read and Write)
174-
uniquePaths := make(map[string]struct{}, len(allPaths))
175-
for _, path := range allPaths {
176-
uniquePaths[path] = struct{}{}
177-
}
170+
slices.Sort(uniquePaths)
171+
uniquePaths = slices.Compact(uniquePaths)
178172

179-
for path := range uniquePaths {
173+
for _, path := range uniquePaths {
180174
url := buildEndpointURL(baseURL, path)
181-
if _, exists := seen[url]; !exists {
182-
routers = append(routers, url)
183-
seen[url] = struct{}{}
184-
}
175+
seen[url] = struct{}{}
185176
}
186177
}
187178

179+
routers := make([]string, 0, len(seen))
180+
for url := range seen {
181+
routers = append(routers, url)
182+
}
183+
188184
resolved := expandAutoConfSlice(configEndpoints, routers)
189185

190186
// Normalize all URLs to ensure no trailing slashes
191187
// (autoconf URLs are already normalized, but user-provided custom URLs might not be)
192-
resolved = normalizeURLs(resolved)
188+
normalizeURLs(resolved)
193189

194190
log.Debugf("ExpandDelegatedEndpoints: final result contains %d endpoints", len(resolved))
195191
return resolved

autoconf/fetch.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"net/url"
1111
"os"
1212
"path/filepath"
13-
"sort"
13+
"slices"
1414
"strings"
1515
"time"
1616

@@ -390,8 +390,8 @@ func (c *Client) listCacheFiles(cacheDir string) ([]string, error) {
390390
}
391391

392392
// Sort by filename (which contains unix timestamp) in descending order
393-
sort.Slice(files, func(i, j int) bool {
394-
return filepath.Base(files[i]) > filepath.Base(files[j])
393+
slices.SortFunc(files, func(a, b string) int {
394+
return strings.Compare(filepath.Base(b), filepath.Base(a))
395395
})
396396

397397
return files, nil

autoconf/fetch_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,7 @@ func TestCalculateEffectiveRefreshInterval(t *testing.T) {
633633
// Verify the result is always the minimum of the two values (when server TTL > 0)
634634
if tt.cacheTTL > 0 {
635635
serverTTL := time.Duration(tt.cacheTTL) * time.Second
636-
expectedMin := tt.userInterval
637-
if serverTTL < tt.userInterval {
638-
expectedMin = serverTTL
639-
}
636+
expectedMin := min(serverTTL, tt.userInterval)
640637
assert.Equal(t, expectedMin, result, "result should be minimum of user interval and server TTL")
641638
} else {
642639
// When server TTL <= 0, should always return user interval

bitswap/network/httpnet/msg_sender.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ func (sender *httpMsgSender) sortURLS() []*senderURL {
9191

9292
// sender.urls must be read-only as multiple workers
9393
// attempt to sort it.
94-
urlCopy := make([]*senderURL, len(sender.urls))
95-
copy(urlCopy, sender.urls)
94+
urlCopy := slices.Clone(sender.urls)
9695

9796
slices.SortFunc(urlCopy, func(a, b *senderURL) int {
9897
// urls without exhausted retries come first

ipld/unixfs/hamt/hamt.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"errors"
2828
"fmt"
2929
"os"
30+
"slices"
3031
"sync"
3132

3233
"github.com/gammazero/deque"
@@ -796,8 +797,7 @@ func (s *childer) makeChilder(data []byte, links []*ipld.Link) *childer {
796797
s.children = make([]*Shard, len(links))
797798
s.bitfield.SetBytes(data)
798799
if len(links) > 0 {
799-
s.links = make([]*ipld.Link, len(links))
800-
copy(s.links, links)
800+
s.links = slices.Clone(links)
801801
}
802802

803803
return s
@@ -829,10 +829,12 @@ func (s *childer) insert(key string, lnk *ipld.Link, idx int) error {
829829
return err
830830
}
831831

832-
s.children = append(s.children[:i], append([]*Shard{sd}, s.children[i:]...)...)
833-
s.links = append(s.links[:i], append([]*ipld.Link{nil}, s.links[i:]...)...)
832+
s.children = slices.Insert(s.children, i, sd)
833+
834834
// Add a `nil` placeholder in `links` so the rest of the entries keep the same
835835
// index as `children`.
836+
s.links = slices.Insert(s.links, i, nil)
837+
836838
s.bitfield.SetBit(idx)
837839

838840
return nil
@@ -855,11 +857,8 @@ func (s *childer) rm(childIndex int) error {
855857
return err
856858
}
857859

858-
copy(s.children[i:], s.children[i+1:])
859-
s.children = s.children[:len(s.children)-1]
860-
861-
copy(s.links[i:], s.links[i+1:])
862-
s.links = s.links[:len(s.links)-1]
860+
s.children = slices.Delete(s.children, i, i+1)
861+
s.links = slices.Delete(s.links, i, i+1)
863862

864863
s.bitfield.UnsetBit(childIndex)
865864

ipld/unixfs/mod/dagmodifier.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ func (dm *DagModifier) Size() (int64, error) {
167167
if err != nil {
168168
return 0, err
169169
}
170-
if dm.wrBuf != nil && int64(dm.wrBuf.Len())+int64(dm.writeStart) > int64(fileSize) {
171-
return int64(dm.wrBuf.Len()) + int64(dm.writeStart), nil
170+
if dm.wrBuf == nil {
171+
return int64(fileSize), nil
172172
}
173-
return int64(fileSize), nil
173+
return max(int64(fileSize), int64(dm.wrBuf.Len())+int64(dm.writeStart)), nil
174174
}
175175

176176
func fileSize(n ipld.Node) (uint64, error) {

path/resolver/resolver.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ func (r *basicResolver) ResolveToLastNode(ctx context.Context, fpath path.Immuta
101101

102102
if len(nodes) < 1 {
103103
return cid.Cid{}, nil, fmt.Errorf("path %v did not resolve to a node", fpath)
104-
} else if len(nodes) < len(remainder) {
104+
}
105+
if len(nodes) < len(remainder) {
105106
return cid.Undef, nil, &ErrNoLink{Name: remainder[len(nodes)-1], Node: lastCid}
106107
}
107108

peering/peering.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"math/rand"
7+
"slices"
78
"strconv"
89
"sync"
910
"time"
@@ -68,8 +69,7 @@ type peerHandler struct {
6869
// setAddrs sets the addresses for this peer.
6970
func (ph *peerHandler) setAddrs(addrs []multiaddr.Multiaddr) {
7071
// Not strictly necessary, but it helps to not trust the calling code.
71-
addrCopy := make([]multiaddr.Multiaddr, len(addrs))
72-
copy(addrCopy, addrs)
72+
addrCopy := slices.Clone(addrs)
7373

7474
ph.mu.Lock()
7575
defer ph.mu.Unlock()
@@ -265,9 +265,10 @@ func (ps *PeeringService) ListPeers() []peer.AddrInfo {
265265

266266
out := make([]peer.AddrInfo, 0, len(ps.peers))
267267
for id, addrs := range ps.peers {
268-
ai := peer.AddrInfo{ID: id}
269-
ai.Addrs = append(ai.Addrs, addrs.addrs...)
270-
out = append(out, ai)
268+
out = append(out, peer.AddrInfo{
269+
ID: id,
270+
Addrs: slices.Clone(addrs.addrs),
271+
})
271272
}
272273
return out
273274
}

0 commit comments

Comments
 (0)