Merge pull request #147 from qiniu/revert-146-perf
Revert "Improve performance of getting filelist to copy"
This commit is contained in:
commit
f9e4d1c85b
@ -20,9 +20,10 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
cc "github.com/otiai10/copy" // conflit with builtin copy
|
|
||||||
"github.com/qiniu/goc/pkg/cover"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/otiai10/copy"
|
||||||
|
"github.com/qiniu/goc/pkg/cover"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *Build) cpProject() {
|
func (b *Build) cpProject() {
|
||||||
@ -47,13 +48,13 @@ func (b *Build) cpProject() {
|
|||||||
if v.Name == "main" {
|
if v.Name == "main" {
|
||||||
dst := filepath.Join(b.TmpDir, "go.mod")
|
dst := filepath.Join(b.TmpDir, "go.mod")
|
||||||
src := filepath.Join(v.Module.Dir, "go.mod")
|
src := filepath.Join(v.Module.Dir, "go.mod")
|
||||||
if err := cc.Copy(src, dst); err != nil {
|
if err := copy.Copy(src, dst); err != nil {
|
||||||
log.Errorf("Failed to Copy the go mod file from %v to %v, the error is: %v ", src, dst, err)
|
log.Errorf("Failed to Copy the go mod file from %v to %v, the error is: %v ", src, dst, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dst = filepath.Join(b.TmpDir, "go.sum")
|
dst = filepath.Join(b.TmpDir, "go.sum")
|
||||||
src = filepath.Join(v.Module.Dir, "go.sum")
|
src = filepath.Join(v.Module.Dir, "go.sum")
|
||||||
if err := cc.Copy(src, dst); err != nil && !strings.Contains(err.Error(), "no such file or directory") {
|
if err := copy.Copy(src, dst); err != nil && !strings.Contains(err.Error(), "no such file or directory") {
|
||||||
log.Errorf("Failed to Copy the go mod file from %v to %v, the error is: %v ", src, dst, err)
|
log.Errorf("Failed to Copy the go mod file from %v to %v, the error is: %v ", src, dst, err)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
@ -67,7 +68,18 @@ func (b *Build) cpProject() {
|
|||||||
func (b *Build) copyDir(pkg *cover.Package) error {
|
func (b *Build) copyDir(pkg *cover.Package) error {
|
||||||
fileList := []string{}
|
fileList := []string{}
|
||||||
dir := pkg.Dir
|
dir := pkg.Dir
|
||||||
fileList = getFileListNeedsCopy(pkg)
|
fileList = append(fileList, pkg.GoFiles...)
|
||||||
|
fileList = append(fileList, pkg.CompiledGoFiles...)
|
||||||
|
fileList = append(fileList, pkg.IgnoredGoFiles...)
|
||||||
|
fileList = append(fileList, pkg.CFiles...)
|
||||||
|
fileList = append(fileList, pkg.CXXFiles...)
|
||||||
|
fileList = append(fileList, pkg.MFiles...)
|
||||||
|
fileList = append(fileList, pkg.HFiles...)
|
||||||
|
fileList = append(fileList, pkg.FFiles...)
|
||||||
|
fileList = append(fileList, pkg.SFiles...)
|
||||||
|
fileList = append(fileList, pkg.SwigCXXFiles...)
|
||||||
|
fileList = append(fileList, pkg.SwigFiles...)
|
||||||
|
fileList = append(fileList, pkg.SysoFiles...)
|
||||||
for _, file := range fileList {
|
for _, file := range fileList {
|
||||||
p := filepath.Join(dir, file)
|
p := filepath.Join(dir, file)
|
||||||
var src, root string
|
var src, root string
|
||||||
@ -78,69 +90,10 @@ func (b *Build) copyDir(pkg *cover.Package) error {
|
|||||||
}
|
}
|
||||||
src = strings.TrimPrefix(pkg.Dir, root) // get the relative path of the files
|
src = strings.TrimPrefix(pkg.Dir, root) // get the relative path of the files
|
||||||
dst := filepath.Join(b.TmpDir, src, file) // it will adapt the case where src is ""
|
dst := filepath.Join(b.TmpDir, src, file) // it will adapt the case where src is ""
|
||||||
if err := cc.Copy(p, dst); err != nil {
|
if err := copy.Copy(p, dst); err != nil {
|
||||||
log.Errorf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err)
|
log.Errorf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// original implementation
|
|
||||||
// goos: darwin
|
|
||||||
// goarch: amd64
|
|
||||||
// pkg: github.com/qiniu/goc/pkg/build
|
|
||||||
// BenchmarkGetFileList-4 1535029 778 ns/op 1488 B/op 5 allocs/op
|
|
||||||
// func getFileListNeedsCopy(pkg *cover.Package) []string {
|
|
||||||
// fileList := []string{}
|
|
||||||
|
|
||||||
// fileList = append(fileList, pkg.GoFiles...)
|
|
||||||
// fileList = append(fileList, pkg.CompiledGoFiles...)
|
|
||||||
// fileList = append(fileList, pkg.IgnoredGoFiles...)
|
|
||||||
// fileList = append(fileList, pkg.CFiles...)
|
|
||||||
// fileList = append(fileList, pkg.CXXFiles...)
|
|
||||||
// fileList = append(fileList, pkg.MFiles...)
|
|
||||||
// fileList = append(fileList, pkg.HFiles...)
|
|
||||||
// fileList = append(fileList, pkg.FFiles...)
|
|
||||||
// fileList = append(fileList, pkg.SFiles...)
|
|
||||||
// fileList = append(fileList, pkg.SwigCXXFiles...)
|
|
||||||
// fileList = append(fileList, pkg.SwigFiles...)
|
|
||||||
// fileList = append(fileList, pkg.SysoFiles...)
|
|
||||||
|
|
||||||
// return fileList
|
|
||||||
// }
|
|
||||||
|
|
||||||
// new implementation
|
|
||||||
// goos: darwin
|
|
||||||
// goarch: amd64
|
|
||||||
// pkg: github.com/qiniu/goc/pkg/build
|
|
||||||
// BenchmarkGetFileList-4 3884557 298 ns/op 576 B/op 1 allocs/op
|
|
||||||
func getFileListNeedsCopy(pkg *cover.Package) []string {
|
|
||||||
fileSlices := [][]string{
|
|
||||||
pkg.GoFiles,
|
|
||||||
pkg.CompiledGoFiles,
|
|
||||||
pkg.IgnoredGoFiles,
|
|
||||||
pkg.CFiles,
|
|
||||||
pkg.CXXFiles,
|
|
||||||
pkg.MFiles,
|
|
||||||
pkg.HFiles,
|
|
||||||
pkg.FFiles,
|
|
||||||
pkg.SFiles,
|
|
||||||
pkg.SwigCXXFiles,
|
|
||||||
pkg.SwigFiles,
|
|
||||||
pkg.SysoFiles,
|
|
||||||
}
|
|
||||||
|
|
||||||
var totalLen int
|
|
||||||
for _, s := range fileSlices {
|
|
||||||
totalLen += len(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp := make([]string, totalLen)
|
|
||||||
var i int
|
|
||||||
for _, s := range fileSlices {
|
|
||||||
i += copy(tmp[i:], s)
|
|
||||||
}
|
|
||||||
|
|
||||||
return tmp
|
|
||||||
}
|
|
||||||
|
@ -17,12 +17,12 @@
|
|||||||
package build
|
package build
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/qiniu/goc/pkg/cover"
|
"github.com/qiniu/goc/pkg/cover"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// copy in cpProject of invalid src, dst name
|
// copy in cpProject of invalid src, dst name
|
||||||
@ -80,34 +80,3 @@ func TestCopyDir(t *testing.T) {
|
|||||||
defer os.RemoveAll(tmpDir)
|
defer os.RemoveAll(tmpDir)
|
||||||
assert.NoError(t, b.copyDir(pkg))
|
assert.NoError(t, b.copyDir(pkg))
|
||||||
}
|
}
|
||||||
|
|
||||||
func initSlicesForTest() *cover.Package {
|
|
||||||
var pkg cover.Package
|
|
||||||
pkg.GoFiles = []string{"a1.go", "b1.go", "c1.go"}
|
|
||||||
pkg.CompiledGoFiles = []string{"a2.go", "b2.go", "c2.go"}
|
|
||||||
pkg.IgnoredGoFiles = []string{"a3.go", "b3.go", "c3.go"}
|
|
||||||
pkg.CFiles = []string{"a4.go", "b4.go", "c4.go"}
|
|
||||||
pkg.CXXFiles = []string{"a5.go", "b5.go", "c5.go"}
|
|
||||||
pkg.MFiles = []string{"a6.go", "b6.go", "c6.go"}
|
|
||||||
pkg.HFiles = []string{"a7.go", "b7.go", "c7.go"}
|
|
||||||
pkg.FFiles = []string{"a8.go", "b8.go", "c8.go"}
|
|
||||||
pkg.SFiles = []string{"a9.go", "b9.go", "c9.go"}
|
|
||||||
pkg.SwigCXXFiles = []string{"a10.go", "b10.go", "c10.go"}
|
|
||||||
pkg.SwigFiles = []string{"a11.go", "b11.go", "c11.go"}
|
|
||||||
pkg.SysoFiles = []string{"a12.go", "b12.go", "c12.go"}
|
|
||||||
|
|
||||||
return &pkg
|
|
||||||
}
|
|
||||||
|
|
||||||
// benchmark getFileListNeedsCopy
|
|
||||||
// goos: darwin
|
|
||||||
// goarch: amd64
|
|
||||||
// pkg: github.com/qiniu/goc/pkg/build
|
|
||||||
// BenchmarkGetFileList-4 3884557 298 ns/op 576 B/op 1 allocs/op
|
|
||||||
func BenchmarkGetFileList(b *testing.B) {
|
|
||||||
//var files []string
|
|
||||||
pkg := initSlicesForTest()
|
|
||||||
for n := 0; n < b.N; n++ {
|
|
||||||
getFileListNeedsCopy(pkg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user