Optimization

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

View File

@ -50,18 +50,21 @@ 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()
gs.stages = append(gs.stages, s)
go func() {
defer close(s.output) defer close(s.output)
for _, match := range fileMatches { for _, match := range fileMatches {
@ -80,11 +83,10 @@ func (gs *goldsmith) scan() {
s.output <- file s.output <- file
} }
}()
gs.stages = append(gs.stages, s)
} }
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
} }