1
0
mirror of https://github.com/golang/go synced 2024-09-30 03:34:51 -06:00

os/exec: less allocs in the common case

When Stdin, Stdout, and Stderr are nil, there are no goroutines to keep
track of, so we don't need a channel.

And in startProcess, preallocate the right size for sysattr.Files,
saving a bit of space and a couple of slice growth allocs.

name            old time/op    new time/op    delta
ExecHostname-8     419µs ± 0%     417µs ± 1%    ~     (p=0.093 n=6+6)

name            old alloc/op   new alloc/op   delta
ExecHostname-8    6.40kB ± 0%    6.28kB ± 0%  -1.86%  (p=0.002 n=6+6)

name            old allocs/op  new allocs/op  delta
ExecHostname-8      34.0 ± 0%      31.0 ± 0%  -8.82%  (p=0.002 n=6+6)

Change-Id: Ic1d617f29e9c6431cdcadc7f9bb992750a6d5f48
Reviewed-on: https://go-review.googlesource.com/c/164801
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Daniel Martí 2019-03-03 23:52:00 +00:00
parent 0db5534d7a
commit d1887676d9
3 changed files with 10 additions and 5 deletions

View File

@ -404,11 +404,14 @@ func (c *Cmd) Start() error {
c.closeDescriptors(c.closeAfterStart)
c.errch = make(chan error, len(c.goroutine))
for _, fn := range c.goroutine {
go func(fn func() error) {
c.errch <- fn()
}(fn)
// Don't allocate the channel unless there are goroutines to fire.
if len(c.goroutine) > 0 {
c.errch = make(chan error, len(c.goroutine))
for _, fn := range c.goroutine {
go func(fn func() error) {
c.errch <- fn()
}(fn)
}
}
if c.ctx != nil {

View File

@ -27,6 +27,7 @@ func startProcess(name string, argv []string, attr *ProcAttr) (p *Process, err e
Sys: attr.Sys,
}
sysattr.Files = make([]uintptr, 0, len(attr.Files))
for _, f := range attr.Files {
sysattr.Files = append(sysattr.Files, f.Fd())
}

View File

@ -40,6 +40,7 @@ func startProcess(name string, argv []string, attr *ProcAttr) (p *Process, err e
if sysattr.Env == nil {
sysattr.Env = Environ()
}
sysattr.Files = make([]uintptr, 0, len(attr.Files))
for _, f := range attr.Files {
sysattr.Files = append(sysattr.Files, f.Fd())
}