Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
pt&Goroutines
Search
monochromegane
May 31, 2014
Technology
20
15k
pt&Goroutines
pt(the_platinum_searcher) を高速化するために Goroutines まわりで試したことを発表しました。
http://connpass.com/event/6370/
monochromegane
May 31, 2014
Tweet
Share
More Decks by monochromegane
See All by monochromegane
Go言語でターミナルフレンドリーなAIコマンド、afaを作った/fukuokago20_afa
monochromegane
2
150
多様かつ継続的に変化する環境に適応する情報システム/thesis-defense-presentation
monochromegane
1
540
Online Nonstationary and Nonlinear Bandits with Recursive Weighted Gaussian Process
monochromegane
0
280
AIを前提とした体験の実現に向けて/toward_ai_based_experiences
monochromegane
1
640
Go言語でMac GPUプログラミング
monochromegane
1
390
Contextual and Nonstationary Multi-armed Bandits Using the Linear Gaussian State Space Model for the Meta-Recommender System
monochromegane
1
840
迅速な学習機構を用いて逐次適応性を損なうことなく非線形性を扱う文脈付き多腕バンディット手法/extreme_neural_linear_bandits
monochromegane
0
1.9k
再帰化への認知的転回/the-turn-to-recursive-system
monochromegane
0
720
仮想的な探索を用いて文脈や時間の経過による番狂わせにも迅速に追従する多腕バンディット手法/wi2_lkf_bandits
monochromegane
0
660
Other Decks in Technology
See All in Technology
10XにおけるData Contractの導入について: Data Contract事例共有会
10xinc
6
660
B2B SaaSから見た最近のC#/.NETの進化
sansantech
PRO
0
890
TypeScriptの次なる大進化なるか!? 条件型を返り値とする関数の型推論
uhyo
2
1.7k
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
3.9k
ISUCONに強くなるかもしれない日々の過ごしかた/Findy ISUCON 2024-11-14
fujiwara3
8
870
マルチプロダクトな開発組織で 「開発生産性」に向き合うために試みたこと / Improving Multi-Product Dev Productivity
sugamasao
1
310
日経電子版のStoreKit2フルリニューアル
shimastripe
1
140
AWS Media Services 最新サービスアップデート 2024
eijikominami
0
200
SRE×AIOpsを始めよう!GuardDutyによるお手軽脅威検出
amixedcolor
0
170
誰も全体を知らない ~ ロールの垣根を超えて引き上げる開発生産性 / Boosting Development Productivity Across Roles
kakehashi
1
230
SREが投資するAIOps ~ペアーズにおけるLLM for Developerへの取り組み~
takumiogawa
1
430
複雑なState管理からの脱却
sansantech
PRO
1
150
Featured
See All Featured
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Speed Design
sergeychernyshev
25
620
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
The Cult of Friendly URLs
andyhume
78
6k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
GraphQLとの向き合い方2022年版
quramy
43
13k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
420
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Transcript
pt &Goroutine - GoCon 2014 spring -
MIYAKE Yusuke (@monochromegane)
GMO Pepabo, Inc.
grep ͯ͠·͔͢ʁ
grep?
ack?
ag?
pt The Platinum Searcher
Written in Golang
Mac OSX Linux Windows
UTF-8 EUC-JP Shift-JIS
AND
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
How?
Goroutine & Channel
͍ͬ͠ΐʹߴԽͯ͠Έ·͠ΐ͏
1. ϑΝΠϧΛݕࡧͯ͠(find) 2. จࣈྻΛݕࡧͯ͠(grep) 3. ݁ՌΛදࣔ͢Δ(print) ύλʔϯݕࡧͱ
Approach-0 ! ॱ൪ʹ
find
find grep
find grep print
// 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()
> the_simple_searcher go $GOROOT > /dev/null
0.79 seconds
Approach-1 ! ฒߦʹ
Goroutine
• GoݴޠͰฒߦॲཧΛ࣮ݱ͢Δ • εϨουɺίϧʔνϯͱҧ͏ • Concurrency(ฒߦ)ͱParallelism(ฒྻ) • ܰྔ • go
f()
find grep print go go go
Channel
• Goroutineؒͷϝοηʔδϯά • ͷૹड৴ • όοϑΝʹΑΔϒϩοΫ
find grep print go go go
$IBOFM find grep print $IBOFM go go go
$IBOFM find grep print $IBOFM go go go
// 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
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
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
for match := range self.Matches { // receive fmt.Printf("%s:%d:%s\n", match.Path,
match.Num, match.Match) } self.Done <- true // send
> the_simple_searcher go $GOROOT > /dev/null
0.79 -> 0.87 seconds
?
buffer
• Channelͷड༰ྔ • ch := make(chan ܕ, ༰ྔ) • ༰ྔ·Ͱड
• ༰ྔ͑Δͱૹ৴ଆडͪ • ड৴͢Δͱ༰ྔ͕ͻͱۭͭ͘ • ༰ྔ͕0ͷ߹ɺৗʹͭ
// channel with buffer files := make(chan *string, self.Option.Cap) matches
:= make(chan *grep.Match, self.Option.Cap) done := make(chan bool) // always wait
> the_simple_searcher go $GOROOT > /dev/null
0.79 -> 0.8 seconds
Approach-2 ! ͬͱฒߦʹ
$IBOFM find grep print $IBOFM go go go
$IBOFM find grep print $IBOFM go go go grep grep
grep
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)
> the_simple_searcher go $GOROOT > /dev/null
panic ! too many open files
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)
> the_simple_searcher go $GOROOT > /dev/null
0.79 -> 0.8 seconds
Approach-3 ! ฒྻʹ
GOMAXPROCS
• Goroutineͷฒྻ • σϑΥϧτ1 • runtime.NumCPU()ͰίΞΛऔಘ • runtime.GOMAXPROCS()ͰฒྻΛઃఆ
> the_simple_searcher go $GOROOT > /dev/null
0.79 -> 0.55 ! seconds
benchmark ! • Mac OSX(10.9.3) • CPU: 2.5GHz Core i5(2Core)
• Memory: 8GB • Go: 1.2.2
#V⒎FS (0."9130$4 "QQSPBDI
ฒߦԽͯ͠ͳ͍ͷͰ ฒྻԽͯ͠มΘΒͣ ίΞҎ্ͷࢦఆ ޮՌͳ͠ ଌఆͯ͠ௐ͠ͳ͍ͱ ߹ʹΑͬͯ͘ͳΔ #V⒎FS (0."9130$4 "QQSPBDI
–Rob Pike • Concurrency is powerful. • Concurrency is not
parallelism. • Concurrency enables parallelism. • Concurrency makes parallelism (and scaling and everything else) easy.
એ ϖύϘͰΤϯδχΞΛืू͍ͯ͠·͢ɻ ڞʹαʔϏεΛੜΈग़͠ҭͯͯ͘ΕΔ৽͍ؒ͠ Λ͍ͬͯ·͢ɻ ! http://pepabo.com/recruit/career/engineer/
͓ΘΓ