Skip to content

Commit

Permalink
refactor: prealloc slices with known sizes (#8892)
Browse files Browse the repository at this point in the history
Often those are small slices and that cuts down the number
of (small) allocations from 2~7 to 1. 
This is consistent too, always only gets 1.
  • Loading branch information
Jorropo authored Jun 24, 2022
1 parent a3a6a59 commit 6602dd7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
13 changes: 7 additions & 6 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,22 +704,23 @@ func printSwarmAddrs(node *core.IpfsNode) {
return
}

var lisAddrs []string
ifaceAddrs, err := node.PeerHost.Network().InterfaceListenAddresses()
if err != nil {
log.Errorf("failed to read listening addresses: %s", err)
}
for _, addr := range ifaceAddrs {
lisAddrs = append(lisAddrs, addr.String())
lisAddrs := make([]string, len(ifaceAddrs))
for i, addr := range ifaceAddrs {
lisAddrs[i] = addr.String()
}
sort.Strings(lisAddrs)
for _, addr := range lisAddrs {
fmt.Printf("Swarm listening on %s\n", addr)
}

var addrs []string
for _, addr := range node.PeerHost.Addrs() {
addrs = append(addrs, addr.String())
nodePhostAddrs := node.PeerHost.Addrs()
addrs := make([]string, len(nodePhostAddrs))
for i, addr := range nodePhostAddrs {
addrs[i] = addr.String()
}
sort.Strings(addrs)
for _, addr := range addrs {
Expand Down
6 changes: 3 additions & 3 deletions core/commands/sysdiag.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ func netInfo(online bool, out map[string]interface{}) error {
return err
}

var straddrs []string
for _, a := range addrs {
straddrs = append(straddrs, a.String())
straddrs := make([]string, len(addrs))
for i, a := range addrs {
straddrs[i] = a.String()
}

n["interface_addresses"] = straddrs
Expand Down
6 changes: 3 additions & 3 deletions core/node/libp2p/addrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ func AddrsFactory(announce []string, appendAnnouce []string, noAnnounce []string
}

func listenAddresses(addresses []string) ([]ma.Multiaddr, error) {
var listen []ma.Multiaddr
for _, addr := range addresses {
listen := make([]ma.Multiaddr, len(addresses))
for i, addr := range addresses {
maddr, err := ma.NewMultiaddr(addr)
if err != nil {
return nil, fmt.Errorf("failure to parse config.Addresses.Swarm: %s", addresses)
}
listen = append(listen, maddr)
listen[i] = maddr
}

return listen, nil
Expand Down
8 changes: 4 additions & 4 deletions fuse/ipns/ipns_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (r *Root) Forget() {
func (r *Root) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
log.Debug("Root ReadDirAll")

var listing []fuse.Dirent
listing := make([]fuse.Dirent, 0, len(r.Keys)*2)
for alias, k := range r.Keys {
ent := fuse.Dirent{
Name: k.ID().Pretty(),
Expand Down Expand Up @@ -291,12 +291,12 @@ func (s *Directory) Lookup(ctx context.Context, name string) (fs.Node, error) {

// ReadDirAll reads the link structure as directory entries
func (dir *Directory) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
var entries []fuse.Dirent
listing, err := dir.dir.List(ctx)
if err != nil {
return nil, err
}
for _, entry := range listing {
entries := make([]fuse.Dirent, len(listing))
for i, entry := range listing {
dirent := fuse.Dirent{Name: entry.Name}

switch mfs.NodeType(entry.Type) {
Expand All @@ -306,7 +306,7 @@ func (dir *Directory) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
dirent.Type = fuse.DT_File
}

entries = append(entries, dirent)
entries[i] = dirent
}

if len(entries) > 0 {
Expand Down
6 changes: 3 additions & 3 deletions profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ func (p *profiler) runProfile(ctx context.Context) error {
ctx, cancelFn := context.WithCancel(ctx)
defer cancelFn()

var collectorsToRun []collector
for _, name := range p.opts.Collectors {
collectorsToRun := make([]collector, len(p.opts.Collectors))
for i, name := range p.opts.Collectors {
c, ok := collectors[name]
if !ok {
return fmt.Errorf("unknown collector '%s'", name)
}
collectorsToRun = append(collectorsToRun, c)
collectorsToRun[i] = c
}

results := make(chan profileResult, len(p.opts.Collectors))
Expand Down

0 comments on commit 6602dd7

Please sign in to comment.