Repository: syncthing/syncthing
File: cmd/syncthing/gui.go (Go)

Code before:
	// The searchFile should be the last component of search, or empty if it
	// ends with a path separator
	var searchFile string
	if !strings.HasSuffix(search, pathSeparator) {
		searchFile = filepath.Base(search)
	}

	fs := fs.NewFilesystem(fsType, searchDir)

	subdirectories, _ := fs.DirNames(".")

	matches := make([]match, 0, len(subdirectories))

	for _, subdirectory := range subdirectories {
		info, err := fs.Stat(subdirectory)
		if !(err == nil && info.IsDir()) {
			continue
		}

		m := match{
			path:            filepath.Join(searchDir, subdirectory) + pathSeparator,
			caseDifferences: prefixCaseDifferences(subdirectory, searchFile),
		}

		if m.caseDifferences >= 0 {
			matches = append(matches, m)
		}
	}

	// sort by number of differences with pattern
	// ties are resolved by alphabetical order
	sort.Slice(matches, func(i, j int) bool {
		if matches[i].caseDifferences == matches[j].caseDifferences {
			return strings.ToLower(matches[i].path) < strings.ToLower(matches[j].path)
		}
		return matches[i].caseDifferences < matches[j].caseDifferences
	})

	ret := make([]string, 0, len(matches))
	for _, m := range matches {
		ret = append(ret, m.path)
	}
	return ret
}

func (s *apiService) getCPUProf(w http.ResponseWriter, r *http.Request) {
	duration, err := time.ParseDuration(r.FormValue("duration"))
	if err != nil {
		duration = 30 * time.Second
	}


Review comment (suggestion):
You could store the prefifDiff on the stack, and if that is not negative, only then create match in the append statement.

Code after:
			caseInsMatches = append(caseInsMatches, filepath.Join(searchDir, subdirectory)+pathSeparator)
		}
	}

	// sort to return matches in deterministic order (don't depend on file system order)
	sort.Strings(exactMatches)
	sort.Strings(caseInsMatches)
	return append(exactMatches, caseInsMatches...)
}

func (s *apiService) getCPUProf(w http.ResponseWriter, r *http.Request) {
	duration, err := time.ParseDuration(r.FormValue("duration"))
	if err != nil {
		duration = 30 * time.Second
	}

	filename := fmt.Sprintf("syncthing-cpu-%s-%s-%s-%s.pprof", runtime.GOOS, runtime.GOARCH, Version, time.Now().Format("150405")) // hhmmss

	w.Header().Set("Content-Type", "application/octet-stream")
	w.Header().Set("Content-Disposition", "attachment; filename="+filename)

	pprof.StartCPUProfile(w)
	time.Sleep(duration)
	pprof.StopCPUProfile()
}

func (s *apiService) getHeapProf(w http.ResponseWriter, r *http.Request) {
	filename := fmt.Sprintf("syncthing-heap-%s-%s-%s-%s.pprof", runtime.GOOS, runtime.GOARCH, Version, time.Now().Format("150405")) // hhmmss

	w.Header().Set("Content-Type", "application/octet-stream")
	w.Header().Set("Content-Disposition", "attachment; filename="+filename)

	runtime.GC()
	pprof.WriteHeapProfile(w)
}

func toNeedSlice(fs []db.FileInfoTruncated) []jsonDBFileInfo {
	res := make([]jsonDBFileInfo, len(fs))
	for i, f := range fs {
		res[i] = jsonDBFileInfo(f)
	}
	return res
}

// Type wrappers for nice JSON serialization

type jsonFileInfo protocol.FileInfo

func (f jsonFileInfo) MarshalJSON() ([]byte, error) {
	return json.Marshal(map[string]interface{}{
		"name":          f.Name,