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
170
多様かつ継続的に変化する環境に適応する情報システム/thesis-defense-presentation
monochromegane
1
590
Online Nonstationary and Nonlinear Bandits with Recursive Weighted Gaussian Process
monochromegane
0
330
AIを前提とした体験の実現に向けて/toward_ai_based_experiences
monochromegane
1
680
Go言語でMac GPUプログラミング
monochromegane
1
430
Contextual and Nonstationary Multi-armed Bandits Using the Linear Gaussian State Space Model for the Meta-Recommender System
monochromegane
1
860
迅速な学習機構を用いて逐次適応性を損なうことなく非線形性を扱う文脈付き多腕バンディット手法/extreme_neural_linear_bandits
monochromegane
0
2k
再帰化への認知的転回/the-turn-to-recursive-system
monochromegane
0
730
仮想的な探索を用いて文脈や時間の経過による番狂わせにも迅速に追従する多腕バンディット手法/wi2_lkf_bandits
monochromegane
0
670
Other Decks in Technology
See All in Technology
レンジャーシステムズ | 会社紹介(採用ピッチ)
rssytems
0
150
watsonx.ai Dojo #5 ファインチューニングとInstructLAB
oniak3ibm
PRO
0
160
NW-JAWS #14 re:Invent 2024(予選落ち含)で 発表された推しアップデートについて
nagisa53
0
250
開発生産性向上! 育成を「改善」と捉えるエンジニア育成戦略
shoota
1
230
サーバレスアプリ開発者向けアップデートをキャッチアップしてきた #AWSreInvent #regrowth_fuk
drumnistnakano
0
190
NilAway による静的解析で「10 億ドル」を節約する #kyotogo / Kyoto Go 56th
ytaka23
3
370
社外コミュニティで学び社内に活かす共に学ぶプロジェクトの実践/backlogworld2024
nishiuma
0
250
Fanstaの1年を大解剖! 一人SREはどこまでできるのか!?
syossan27
2
160
Amazon Kendra GenAI Index 登場でどう変わる? 評価から学ぶ最適なRAG構成
naoki_0531
0
100
生成AIのガバナンスの全体像と現実解
fnifni
1
180
大幅アップデートされたRagas v0.2をキャッチアップ
os1ma
2
520
WACATE2024冬セッション資料(ユーザビリティ)
scarletplover
0
190
Featured
See All Featured
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Designing for humans not robots
tammielis
250
25k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Reflections from 52 weeks, 52 projects
jeffersonlam
347
20k
Six Lessons from altMBA
skipperchong
27
3.5k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.4k
What's in a price? How to price your products and services
michaelherold
243
12k
Testing 201, or: Great Expectations
jmmastey
40
7.1k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
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/
͓ΘΓ