Skip to content

Commit

Permalink
expand: skip brace expansion when not used
Browse files Browse the repository at this point in the history
Makes expand.Fields noticeably faster, since Braces allocates quite a
bit.

While at it, make interp's execEnv assume that most environments are
larger. For example, on my fairly minimal laptop setup, I already have
about 45 environment variables.

name   old time/op    new time/op    delta
Run-8    1.52ms ± 1%    1.50ms ± 1%  -1.28%  (p=0.002 n=6+6)

name   old alloc/op   new alloc/op   delta
Run-8     185kB ± 0%     182kB ± 0%  -1.76%  (p=0.004 n=6+5)

name   old allocs/op  new allocs/op  delta
Run-8     1.14k ± 0%     1.05k ± 0%  -7.55%  (p=0.002 n=6+6)
  • Loading branch information
mvdan committed Dec 19, 2018
1 parent fc5a791 commit 6c6228a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions expand/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,11 @@ func Fields(cfg *Config, words ...*syntax.Word) ([]string, error) {
fields := make([]string, 0, len(words))
dir := cfg.envGet("PWD")
for _, word := range words {
expWord, _ := syntax.SplitBraces(word)
for _, word2 := range Braces(expWord) {
expBraces := []*syntax.Word{word}
if expWord, any := syntax.SplitBraces(word); any {
expBraces = Braces(expWord)
}
for _, word2 := range expBraces {
wfields, err := cfg.wordFields(word2.Parts)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion interp/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (o overlayEnviron) Each(f func(name string, vr expand.Variable) bool) {
}

func execEnv(env expand.Environ) []string {
list := make([]string, 0, 32)
list := make([]string, 0, 64)
env.Each(func(name string, vr expand.Variable) bool {
if vr.Exported {
list = append(list, name+"="+vr.String())
Expand Down

0 comments on commit 6c6228a

Please sign in to comment.