Skip to content

Commit

Permalink
interp: avoid dirStack allocation at start-up
Browse files Browse the repository at this point in the history
An easy win for programs that don't use pushd. We also avoid the
allocation in Runner.sub, when creating subshells.

name   old time/op    new time/op    delta
Run-8    1.45ms ± 1%    1.45ms ± 1%    ~     (p=0.818 n=6+6)

name   old alloc/op   new alloc/op   delta
Run-8     180kB ± 0%     180kB ± 0%    ~     (p=0.931 n=5+6)

name   old allocs/op  new allocs/op  delta
Run-8     1.05k ± 0%     1.04k ± 0%  -0.38%  (p=0.002 n=6+6)
  • Loading branch information
mvdan committed Dec 19, 2018
1 parent 6c6228a commit 13a9691
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions interp/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
// standard output writer means that the output will be discarded.
func New(opts ...func(*Runner) error) (*Runner, error) {
r := &Runner{usedNew: true}
r.dirStack = r.dirBootstrap[:0]
for _, opt := range opts {
if err := opt(r); err != nil {
return nil, err
Expand Down Expand Up @@ -385,7 +386,10 @@ type Runner struct {

opts [len(shellOptsTable) + len(bashOptsTable)]bool

dirStack []string
// Most scripts don't use pushd/popd, so make space for the initial PWD
// without requiring an extra allocation.
dirStack []string
dirBootstrap [1]string

optState getopts

Expand Down Expand Up @@ -699,7 +703,7 @@ func (r *Runner) sub() *Runner {
for k, v := range r.cmdVars {
r2.cmdVars[k] = v
}
r2.dirStack = append([]string(nil), r.dirStack...)
r2.dirStack = append(r2.dirBootstrap[:0], r.dirStack...)
r2.fillExpandConfig(r.ectx)
r2.didReset = true
return r2
Expand Down

0 comments on commit 13a9691

Please sign in to comment.