Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
go
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
xorphitus
February 04, 2015
Programming
310
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
go
go な並行処理の中身を紐解いてみました。
(Golang, clojure.core.async)
xorphitus
February 04, 2015
More Decks by xorphitus
See All by xorphitus
オリジナリティのあるGitLabを標準に近づける
xorphitus
1
800
マイクロサービスを作ろう
xorphitus
0
150
コンテナ起動への道
xorphitus
0
170
型システムを学ぼうとした結果
xorphitus
0
82
M-x doctor
xorphitus
0
180
型で数を表そう
xorphitus
0
110
AOT と direct linking
xorphitus
0
92
CFS入門
xorphitus
0
91
HyperLogLog
xorphitus
0
130
Other Decks in Programming
See All in Programming
すこし踏み込む CancellationToken
htkym
2
1.7k
設計次第でAIコードの読む量は減らせる / designing-for-code-reading
minodriven
35
17k
Workers Cache を知る
syumai
0
360
Railsのためのパスキー実践入門 〜マルチテナントSaaSでの意思決定〜
utakaha
2
560
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
180
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Kaigi on Rails 2026, Tokyo, Japan
marcoroth
2
220
Railsバージョンを4倍にした話
optim
0
370
見えないものを探る要求要件定義に必要な基本的思考 / invisible-requirement-thinking
minodriven
15
8.5k
パスワード再設定メールのURLについて知っておこう
uyuki234
0
160
3回つくってわかった、 定期実行ジョブ基盤の本質情報
takai
0
470
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.2k
現場と育てるRailsモデリング 〜Spreadsheetから始めたモデル設計〜
siroemk
2
590
Featured
See All Featured
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
630
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.2k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
730
From π to Pie charts
rasagy
1
400
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.6k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
400
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
Mobile First: as difficult as doing things right
swwweet
225
10k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
480
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
430
Transcript
go @xorphitus (2015-02)
この動物は並行処理が得意らしい
それを、こうじゃ ( )
S式 ( ) go
(let [ch (chan)] (go (while true (let [val (<! ch)]
(println val)))) (go (>! ch “hello world”)))
go は大流行のようです
Go
今日は go な並行処理の 内側を少し覗いてみます
※ 書き方、使い方の話はしない
並行処理の俯瞰図(たぶん) 協調マルチタスク Python、Lua、あたりが主? ECMA6 でもやろうと思えば 共有メモリ ロック 例のあれ STM ClojureやらHaskellやら
メッセージ パッシング アクター Scalaやら Erlangやら CSP/π計算 ベース Goやら Clojureのcore.asyncやら C# async やら
Go の並行処理 (goroutine) CSP (Communicating Sequential Processes) および π計算 (pi-calculus)
の2つを混ぜて作った並行処理、らしい チャネル渡しの概念はπ計算のもの、らしい
CSP と π計算 どちらもプロセス代数の一種 プロセス代数っていうのは • 並行システムを代数で表現したもの • メッセージパッシング •
代数式を解くことで並行処理の恒等的性質を明 らかにする、らしい ◦ これ、デッドロックするんじゃね?とか
CSP 自販機の例 自販機はコインを入れてジュースが出て止まる VendingMachne = coin -> juice -> STOP
人間はコインまたはカードでの支払いを選択し、止まる Person = (coin -> STOP)□(card -> STOP) 自販機と人間を並列化すると VendingMachine|[{coin, card}]|Person ≡ coin -> juice -> STOP 上記を何やかんやすると、自販機・人間の並列プロセスから (juice -> STOP)□STOP ジュースを出して止まる、または単に止まる、非決定性プロセスが得られる
「らしい」
None
商業プログラマから見た goroutine 自分のような商業プログラマには とにかく軽量なスレッド として有り難く使わせてもらえれば十分 背後の計算機科学的理論は、2分で挫折
まさに商業プログラマ!!
ところで 軽量スレッドって 何だろう?
軽量スレッド/文法的なところは • メッセージパッシングにより ◦ レースコンディションの回避 ◦ ロックをとってほげほげ、の回避 • メッセージの授受や sleep
等のイベント処理 が、call back hell なしに書ける
軽量スレッド/もう少し低レイヤ • OSのスレッドよりも更に軽い • 一度に立ち上げられる数がたくさん • コンテキストスイッチのコストが低い • 単一のスレッド内でも複数起動し得る •
ファイバはこれに近いっちゃ近いけど軽量スレッ ドはプリエンプティブ(に見える) • アクターも同じノリだろうか?
何これ どうやって動いてんの?
Go のアプローチ goroutine とは 「スレッドによって多重化されたコルーチン」 らしい 次スライドにイメージ図を描いてみた 参考 http://golang.org/doc/faq#goroutines http://stackoverflow.com/questions/24599645/
goroutine のイメージ(たぶん) coroutine 0 thread 0 coroutine 1 blocking thread
1 coroutine 1 scheduler call call yield 待機状態の coroutineを 移動! 別スレッドにて coroutine 0 を 待たずに続行
goroutine マルチコア対応 環境変数 GOMAXPROCS を変更する この値に応じて実スレッドを起動し goroutine の処理を分散して割り当てる デフォルトは 1
Clojure のアプローチ go はというのはマクロで 中身を停止・再開可能なステートマシンに 変換して 他のタスクにブロックされている間は止める 参考 http://clojure.com/blog/2013/06/28/clojure-core-async-channels.html http://endot.org/notes/2014-02-14-core-async-clojure/
go (Clojure) のイメージ (go (...) (...)) macro expansion (go (...)
(...)) macro expansion thread task 0 task 1 チャネルへの 入力待ち チャネルに 入力 IOC スレッドと言う
go (Clojure) のイメージ thread チャネルへの 入力待ち チャネルに 入力 state 待ちになったら
state を他所で維持しつつ スレッド上の処理から 除外する チャネルへの入力が あったら 元の状態にリジュームして 処理を継続する
go (Clojure) マルチコア対応 実際は、各ステートマシンの処理が 固定長スレッドプール上のスレッドに 割当てられることになる プールサイズはコア数に比例 2 * cores
+ 42
なるほど、軽量スレッド
OSおよび軽量スレッド/イメージ図 multi thread (OS) lightweight thread lightweight thread for multi
core サーバアーキテクチャで、まずイベント駆動が流行して その後コア数に合わせてイベント駆動プロセスを立ち上げるのが出てきた経緯に似てるなあ
※CPUバウンド
IOバウンドな処理だと • OSのスレッドを使った方がよい • 軽量スレッドだと処理がブロックされる • IO waitのコストがコンテキストスイッチのコスト を上回る •
ので、多量に並列するならスレッド上限が多く なっていることは確認しておく core.async なら thread マクロでそれでき(ry
まとめ • go な並行処理はスッキリ記述できて嬉しい • CPUリソースも無駄なく使えて嬉しい • ただしマルチコアによる並「列」化は意識してお く •
そしてIOバウンドな処理にも気をつける • あと、go マクロでステート・マシンに展開すると かヤバい