Skip to content

Commit 8843929

Browse files
author
Tamás Gulácsi
committed
activation: allocate slice if we know the size
Use for range, and use the knowledge the everything is zeroed on allocation, so gaps will be nil in the slice.
1 parent a317b84 commit 8843929

File tree

3 files changed

+10
-15
lines changed

3 files changed

+10
-15
lines changed

activation/files.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func Files(unsetEnv bool) []*os.File {
4242
return nil
4343
}
4444

45-
var files []*os.File
45+
files := make([]*os.File, 0, nfds)
4646
for fd := listenFdsStart; fd < listenFdsStart+nfds; fd++ {
4747
syscall.CloseOnExec(fd)
4848
files = append(files, os.NewFile(uintptr(fd), "LISTEN_FD_"+strconv.Itoa(fd)))

activation/listeners.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@ import (
2626
// corresponding with "udp, tcp, tcp", then the slice would contain {nil, net.Listener, net.Listener}
2727
func Listeners(unsetEnv bool) ([]net.Listener, error) {
2828
files := Files(unsetEnv)
29-
listeners := make([]net.Listener, 0)
29+
listeners := make([]net.Listener, len(files))
3030

31-
for i := 0; i < len(files); i++ {
32-
if pc, err := net.FileListener(files[i]); err == nil {
33-
listeners = append(listeners, pc)
34-
continue
35-
} else {
36-
listeners = append(listeners, nil)
31+
for i, f := range files {
32+
if pc, err := net.FileListener(f); err == nil {
33+
listeners[i] = pc
3734
}
3835
}
3936
return listeners, nil

activation/packetconns.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ import (
2626
// corresponding with "udp, tcp, udp", then the slice would contain {net.PacketConn, nil, net.PacketConn}
2727
func PacketConns(unsetEnv bool) ([]net.PacketConn, error) {
2828
files := Files(unsetEnv)
29-
conns := make([]net.PacketConn, 0)
30-
for i := 0; i < len(files); i++ {
31-
if pc, err := net.FilePacketConn(files[i]); err == nil {
32-
conns = append(conns, pc)
33-
continue
34-
} else {
35-
conns = append(conns, nil)
29+
conns := make([]net.PacketConn, len(files))
30+
31+
for i, f := range files {
32+
if pc, err := net.FilePacketConn(f); err == nil {
33+
conns[i] = pc
3634
}
3735
}
3836
return conns, nil

0 commit comments

Comments
 (0)