Skip to content

Commit

Permalink
🥕 dns, slicehelper: prefer slices package
Browse files Browse the repository at this point in the history
slices.Grow is better because it exposes all additional capacity from rounding up to the next size class.
  • Loading branch information
database64128 committed Feb 16, 2025
1 parent 99e4174 commit e2bd5ff
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
6 changes: 1 addition & 5 deletions dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,11 +749,7 @@ func (r *Resolver) LookupIPs(ctx context.Context, name string) ([]netip.Addr, er
if err != nil {
return nil, err
}

ips := make([]netip.Addr, 0, len(result.IPv6)+len(result.IPv4))
ips = append(ips, result.IPv6...)
ips = append(ips, result.IPv4...)
return ips, nil
return slices.Concat(result.IPv6, result.IPv4), nil
}

// SystemResolver resolves names using [net.DefaultResolver].
Expand Down
12 changes: 4 additions & 8 deletions slicehelper/slicehelper.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package slicehelper

import "slices"

// Extend extends the input slice by n elements. head is the full extended
// slice, while tail is the appended part. If the original slice has sufficient
// capacity no allocation is performed.
func Extend[S ~[]E, E any](in S, n int) (head, tail S) {
if total := len(in) + n; cap(in) >= total {
head = in[:total]
} else {
head = make(S, total)
copy(head, in)
}
tail = head[len(in):]
return
head = slices.Grow(in, n)[:len(in)+n]
return head, head[len(in):]
}

0 comments on commit e2bd5ff

Please sign in to comment.