]> Cypherpunks repositories - gostls13.git/commitdiff
os/exec: less allocs in the common case
authorDaniel Martí <mvdan@mvdan.cc>
Sun, 3 Mar 2019 23:52:00 +0000 (23:52 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Mon, 4 Mar 2019 09:45:46 +0000 (09:45 +0000)
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>
src/os/exec/exec.go
src/os/exec_plan9.go
src/os/exec_posix.go

index 30fd64a4b1f1970f226a20e9ef4bb20713deba98..424b49cf061dbfae39711e4ef0a688c31027c85a 100644 (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 {
index bab16ccad34ccf3f18bf1c1e0a63775928c6c144..b0abf743ddb90f5b59607326ee3cba70807728ed 100644 (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())
        }
index 4c8261295c009dfad86065ce2fe6a6b8307422f5..7b1ef67d1cd95c7ea98f306b4c8b39932af81ac2 100644 (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())
        }