Skip to content

[skip changelog] Sliglthly optimize searchConfigTree #515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -124,15 +123,19 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
func searchConfigTree(cwd string) string {
// go back up to root and search for the config file
for {
if _, err := os.Stat(path.Join(cwd, "arduino-cli.yaml")); os.IsNotExist(err) {
if _, err := os.Stat(filepath.Join(cwd, "arduino-cli.yaml")); err == nil {
// config file found
return cwd
} else if os.IsNotExist(err) {
// no config file found
next := path.Join(cwd, "..")
if filepath.Clean(next) == filepath.Clean(cwd) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a quick profiling session it seems that on Windows the filepath.Clean() was responsible for the performance degradation. Removing this check fixed the CLI slow response (even with the version command) in case of missing arduino-cli.yaml configuration file on the machine.

next := filepath.Dir(cwd)
if next == cwd {
return ""
}
cwd = next
} else {
return cwd
// some error we can't handle happened
return ""
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,18 @@ func TestSearchConfigTreeInParent(t *testing.T) {
require.Nil(t, err)
require.Equal(t, searchConfigTree(target), tmp)
}

var result string

func BenchmarkSearchConfigTree(b *testing.B) {
tmp := tmpDirOrDie()
defer os.RemoveAll(tmp)
target := filepath.Join(tmp, "foo", "bar", "baz")
os.MkdirAll(target, os.ModePerm)

var s string
for n := 0; n < b.N; n++ {
s = searchConfigTree(target)
}
result = s
}