Optimization

This commit is contained in:
Alex Yatskov 2015-11-03 10:13:04 +09:00
parent b5470ba396
commit 38632b1e4a

View File

@ -50,41 +50,43 @@ func New(srcDir, dstDir string) Goldsmith {
refs: make(map[string]bool), refs: make(map[string]bool),
} }
gs.scan() gs.scanFs()
return gs return gs
} }
func (gs *goldsmith) scan() { func (gs *goldsmith) scanFs() {
fileMatches, _, err := scanDir(gs.srcDir) fileMatches, _, err := scanDir(gs.srcDir)
if err != nil { if err != nil {
gs.err = err gs.err = err
return return
} }
s := stage{nil, make(chan *File, len(fileMatches))} s := gs.makeStage()
defer close(s.output)
for _, match := range fileMatches {
relPath, err := filepath.Rel(gs.srcDir, match)
if err != nil {
panic(err)
}
file, _ := gs.NewFile(relPath)
var f *os.File
if f, file.Err = os.Open(match); file.Err == nil {
_, file.Err = file.Buff.ReadFrom(f)
f.Close()
}
s.output <- file
}
gs.stages = append(gs.stages, s) gs.stages = append(gs.stages, s)
go func() {
defer close(s.output)
for _, match := range fileMatches {
relPath, err := filepath.Rel(gs.srcDir, match)
if err != nil {
panic(err)
}
file, _ := gs.NewFile(relPath)
var f *os.File
if f, file.Err = os.Open(match); file.Err == nil {
_, file.Err = file.Buff.ReadFrom(f)
f.Close()
}
s.output <- file
}
}()
} }
func (gs *goldsmith) clean() { func (gs *goldsmith) cleanupFiles() {
fileMatches, _, err := scanDir(gs.dstDir) fileMatches, _, err := scanDir(gs.dstDir)
if err != nil { if err != nil {
gs.err = err gs.err = err
@ -107,7 +109,7 @@ func (gs *goldsmith) clean() {
} }
} }
func (gs *goldsmith) export(file *File) { func (gs *goldsmith) exportFile(file *File) {
defer func() { defer func() {
file.Buff = nil file.Buff = nil
}() }()
@ -132,9 +134,9 @@ func (gs *goldsmith) export(file *File) {
} }
func (gs *goldsmith) makeStage() stage { func (gs *goldsmith) makeStage() stage {
s := stage{ s := stage{output: make(chan *File)}
gs.stages[len(gs.stages)-1].output, if len(gs.stages) > 0 {
make(chan *File), s.input = gs.stages[len(gs.stages)-1].output
} }
gs.stages = append(gs.stages, s) gs.stages = append(gs.stages, s)
@ -228,11 +230,11 @@ func (gs *goldsmith) Complete() ([]*File, error) {
var files []*File var files []*File
for file := range s.output { for file := range s.output {
gs.export(file) gs.exportFile(file)
files = append(files, file) files = append(files, file)
} }
gs.clean() gs.cleanupFiles()
return files, gs.err return files, gs.err
} }