Upgrade to Pro — share decks privately, control downloads, hide ads and more …

pt&Goroutines

 pt&Goroutines

pt(the_platinum_searcher) を高速化するために Goroutines まわりで試したことを発表しました。
http://connpass.com/event/6370/

monochromegane

May 31, 2014
Tweet

More Decks by monochromegane

Other Decks in Technology

Transcript

  1. ag?

  2. AND

  3. fast ! ack go 6.24s user 1.06s system 99% cpu

    7.304 total # ack ag go 0.88s user 1.39s system 221% cpu 1.027 total # ag pt go 1.09s user 1.01s system 235% cpu 0.892 total # pt
  4. // find find := find.Find{Option: self.Option} find.Do(self.Root) ! // grep

    grep := grep.Grep{ Files: find.Files, // result Pattern: self.Pattern, Option: self.Option} grep.Do() ! // print print := print.Print{ Matches: grep.Matches, // result Pattern: self.Pattern, Option: self.Option} print.Do()
  5. // channel files := make(chan *string, self.Option.Cap) matches := make(chan

    *grep.Match, self.Option.Cap) done := make(chan bool) ! // find find := find.Find{Files: files, Option: self.Option} go find.Do(self.Root) ! // grep grep := grep.Grep{ Files: files, Matches: matches, Pattern: self.Pattern, Option: self.Option} go grep.Do() ! // print print := print.Print{ Done: done, Matches: matches, Pattern: self.Pattern, Option: self.Option} go print.Do() ! <-done // block
  6. walkFunc := func(path string, info os.FileInfo, err error) error {

    if info.IsDir() { return nil } self.Files <- &path // send return nil } ! filepath.Walk(root, walkFunc) close(self.Files) // close
  7. for file := range self.Files { // receive ( <-self.Files

    ) fh, err := os.Open(*file) if err != nil { panic(err) } ! f := bufio.NewReader(fh) ! var buf []byte var lineNum = 1 for { buf, _, err = f.ReadLine() if err != nil { break } line := string(buf) if strings.Contains(line, self.Pattern) { self.Matches <- &Match{*file, lineNum, line} // send } lineNum++ } fh.Close() } close(self.Matches) // close
  8. for match := range self.Matches { // receive fmt.Printf("%s:%d:%s\n", match.Path,

    match.Num, match.Match) } self.Done <- true // send
  9. ?

  10. • Channelͷड෇༰ྔ • ch := make(chan ܕ, ༰ྔ) • ༰ྔ·Ͱ͸ड෇

    • ༰ྔ௒͑Δͱૹ৴ଆ͸ड෇଴ͪ • ड৴͢Δͱ༰ྔ͕ͻͱۭͭ͘ • ༰ྔ͕0ͷ৔߹ɺৗʹ଴ͭ
  11. // channel with buffer files := make(chan *string, self.Option.Cap) matches

    := make(chan *grep.Match, self.Option.Cap) done := make(chan bool) // always wait
  12. var wg sync.WaitGroup for file := range self.Files { wg.Add(1)

    // goroutineͷىಈ਺ΛΠϯΫϦϝϯτ (தུ) go func(self *Grep, file *string) { defer wg.Done() // goroutine͕׬ྃͨ͠Βىಈ਺ΛσΫϦϝϯτ for { ɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹ(தུ) } fh.Close() ! }(self, file) // ΫϩʔδϟΛgoroutineʹ͢Δͱ͖͸ม਺ͷڞ༗ʹ஫ҙ ! } wg.Wait() // ෆಛఆ਺ͷgoroutine͕શͯऴྃ͢ΔͷΛ଴ͭ close(self.Matches)
  13. var wg sync.WaitGroup sem := make(chan bool, self.Option.Cap) // ىಈ͢Δgoroutineͷ਺Λ੍ޚ͢Δchannel

    for file := range self.Files { sem <- true // goroutineͷىಈ਺(channelͷbuffer)͕͍ͬͺ͍ͳΒ଴ͭ wg.Add(1) (தུ) go func(self *Grep, file *string) { defer wg.Done() for { ɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹ(தུ) } fh.Close() <-sem // ಉ࣌ىಈ਺channelͷbufferʹۭ͖Λͭ͘Δ ! }(self, file) ! } wg.Wait() close(self.Matches)
  14. –Rob Pike • Concurrency is powerful. • Concurrency is not

    parallelism. • Concurrency enables parallelism. • Concurrency makes parallelism (and scaling and everything else) easy.