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
Golang と Erlang
Search
鹿
October 20, 2024
Programming
8
2.2k
Golang と Erlang
2024-10-20 (日) に Kyoto.go remote #54 リモートLT会 で発表したスライドです。
鹿
October 20, 2024
Tweet
Share
More Decks by 鹿
See All by 鹿
なぜselectはselectではないのか
taiyow
1
410
Go の GC の不得意な部分を克服したい
taiyow
4
1.5k
Other Decks in Programming
See All in Programming
Codexに役割を持たせる 他のAIエージェントと組み合わせる実務Tips
o8n
1
610
ご飯食べながらエージェントが開発できる。そう、Agentic Engineeringならね。
yokomachi
1
280
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
380
今更考える「単一責任原則」 / Thinking about the Single Responsibility Principle
tooppoo
3
1.4k
The Ralph Wiggum Loop: First Principles of Autonomous Development
sembayui
0
3.7k
Railsの気持ちを考えながらコントローラとビューを整頓する/tidying-rails-controllers-and-views-as-rails-think
moro
4
370
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
240
浮動小数の比較について
kishikawakatsumi
0
380
new(1.26) ← これすき / kamakura.go #8
utgwkk
0
1.7k
朝日新聞のデジタル版を支えるGoバックエンド ー価値ある情報をいち早く確実にお届けするために
junkiishida
1
370
Rubyと楽しいをつくる / Creating joy with Ruby
chobishiba
0
210
猫の手も借りたい!ので AIエージェント猫を作って社内に放した話 Claude Code × Container Lambda の Slack Bot "DevNeko"
naramomi7
0
250
Featured
See All Featured
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
280
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
170
Raft: Consensus for Rubyists
vanstee
141
7.3k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.1k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Accessibility Awareness
sabderemane
0
73
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
460
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
200
A Modern Web Designer's Workflow
chriscoyier
698
190k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.1k
Abbi's Birthday
coloredviolet
2
5.1k
Being A Developer After 40
akosma
91
590k
Transcript
Golang(*)と Erlang 鹿 @mizushika1 (*)正式名称は「Go」ですが、並べたときの響きの良さのために、 このスライドでは Golang と表記しています
名前は似てるけど、全然似てない Golang Erlang プログラミングモデル 手続き型 関数型 代入 何回でも 単一代入 コンパイル結果
バイナリ 中間言語 コンパイル結果 1ファイル moduleごとに別ファイル panic プロセス全体が死ぬ 軽量プロセスだけが死ぬ
スレッドを起動して Hello World の例 -module(hello). -export([start/0, printer/0]). start() -> Pid
= spawn(fun printer/0), Pid ! "Hello World~n". printer() -> receive msg -> io:fwrite(msg) end. package main import "fmt" func main() { ch := make(chan string) go printer(ch) ch <- "Hello World" <-ch } func printer(ch chan string) { msg := <-ch fmt.Println(msg) close(ch) }
runtime では似ている部分もある • スレッドモデルはどちらも M:N • Golang で言う goroutine •
Erlang で言う軽量プロセス • GC • ある • Golang は Mark and Sweep 方式 • Erlang は 世代別 GC も採用 • スレッド間の共有方法 • 変数をキュー(チャンネル)にコピーして通信 • Share memory by communication
runtime で似ているけど違う部分を紹介 • スレッドモデル • GC • スレッド間の共有方法
スレッドモデル • M:N = OSスレッド:ランタイムのスレッド • スケジューラスレッドがOSスレッドとして存在 • ランタイムのスレッドを順番に実行していく •
スケジューラが空いたら他のスケジューラから奪ったりもする • Go で言う G, P, M の話 • 優先度設定は限定的 • Linuxカーネルだと、SCHED_** とか nice 値とかが指定できるが、 • Golang は優先度設定一切無し • Erlang は実質2レベルあり、ランキューが2本だけある • Golang は goroutine に名前を付けられない • 名前が無いので、外から状態を把握したり止めたりできない • Erlang は VM で動くのでリモートから接続してトレースができる
GC • Garbage Collector • プログラマがヒープメモリの開放を気にしなくて良い仕組み • 通常の人類には必須の機能 • Golang
はプロセス全体で GC をかける • Mark and Sweep という昔ながらの方式 • Mark のときに STW (Stop-the-World) が発生して、全goroutineが止まる • Erlangは軽量プロセス(runtimeスレッド)ごとにGCをかける • 他の軽量プロセスは影響を受けない • グローバル変数やポインタが無いからできる技 • 実行タイミングは「関数呼び出しの回数」
スレッド間の共有方法 • チャンネル or メッセージキューを推奨している • データがコピーして格納されるので shared nothing にできる
• Golang • チャンネルは自分で作る • 段数があり、それを超える write はブロックされる • close 済みのチャンネルに write すると panic になる • Erlang • 軽量プロセスを作ると自動でそれ用のメッセージキューも作られる • 段数は無限で、メモリの許す限り write できる • 存在しない相手に write してもエラーにならない • 1プロセス1キューなので、fan-out 構造は工夫が必要
まとめ • Golang と Erlang の、 似てないようで似てるようでやっぱり違う点の紹介をした • どちらも M:N
スレッドモデルを採用しており、 設計思想が共有できる点が多い(かも) • M:N スレッドモデルは今後広まっていきそう • Ruby の Ractor とか • Java の VirtualThread とか • 個人的感想 • Golang の GC も大量の goroutine 利用に適したものになってほしい • 世代別 GC を熱望している