pony concurrency built into the type system

81
Pony concurrency built into the type system @matsu_chara 2016/3/20 kbkz_tech#9

Upload: matsuchara

Post on 16-Apr-2017

4.425 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Pony concurrency built into the type system

Pony concurrency built into the type system@matsu_chara 2016/3/20 kbkz_tech#9

Page 2: Pony concurrency built into the type system

スライドが81ページあるので

Page 3: Pony concurrency built into the type system

爆速でやります。

Page 4: Pony concurrency built into the type system

今日のスライド https://www.slideshare.net/matsu_chara/pony-concurrency-built-into-the-type-system-59778750

Page 5: Pony concurrency built into the type system

タイトルはパクリ

ツイート: https://twitter.com/debasishg/status/701675442793615360

元ブログ(別の人): http://blog.acolyer.org/2016/02/17/deny-capabilities/

Page 6: Pony concurrency built into the type system

自己紹介

• @matsu_chara

• ドワンゴ一年目

• 好きな言語:PHP

• ブログ: http://matsu-chara.hatenablog.com/

Page 7: Pony concurrency built into the type system

今日は

Page 8: Pony concurrency built into the type system

最強言語PHPの話

Page 9: Pony concurrency built into the type system

ではなく

Page 10: Pony concurrency built into the type system

超高速で型安全な アクターモデル言語Ponyの話をします

Page 11: Pony concurrency built into the type system

Ponyとは

• 2015年4月に0.1がリリースされた言語

• Causalityというロンドンの会社が開発。

• 元はImperial Collegeで研究されていた。

• 論文 A String of Ponies Transparent Distributed Programming with Actors s.blessing 2013

Page 12: Pony concurrency built into the type system

商用サポートあり

http://www.causality.io/

Page 13: Pony concurrency built into the type system

気合入ってる

Page 14: Pony concurrency built into the type system

で、一体どんな言語なんだ・・・

Page 15: Pony concurrency built into the type system

Ponyとは

• アクターモデルに特化

• 速さと(型による)安全性を追求

• pony philosophy

• capability-secure

Page 16: Pony concurrency built into the type system

さらに

Page 17: Pony concurrency built into the type system

distributed pony

http://www.doc.ic.ac.uk/teaching/distinguished-projects/2013/s.blessing.pdf論文での提案手法リスト

• 真の狙いは並行処理ではなく分散システムの構築にある • まだ実装は未公開(論文はある)

Page 18: Pony concurrency built into the type system

まだ出てないので今日はconcurrent ponyの話に限定

Page 19: Pony concurrency built into the type system

Ponyとは

• アクターモデルに特化

• 速さと(型による)安全性を追求

• pony philosophy

• capability-secure

Page 20: Pony concurrency built into the type system

Pony哲学(抜粋) https://github.com/ponylang/ponyc/wiki/Philosophy

• Fully type safe. There is no "trust me, I know what I'm doing" coercion.

• 型安全!

• Fully memory safe. There is no "this random number is really a pointer, honest.”

• 危険なポインタ操作とかは無し!

• No crashes. A program that compiles should never crash (although it may hang or do something unintended).

• コンパイル通ったらクラッシュもハングもしない!

Page 21: Pony concurrency built into the type system

コンパイルが通ったら絶対に クラッシュさせないという 熱い気持ち

Page 22: Pony concurrency built into the type system

Ponyとは(再掲)

• アクターモデルに特化

• 速さと(型による)安全性を追求

• pony philosophy

• capability-secure

Page 23: Pony concurrency built into the type system

capability secureの前に アクターモデルについて

Page 24: Pony concurrency built into the type system

アクターモデル(かなり省略版)

Actor

Actor ActorActor

Actorを用意

Page 25: Pony concurrency built into the type system

各アクターにメッセージを配信

Actor

Actor ActorActor

Page 26: Pony concurrency built into the type system

各アクターにメッセージを配信

Actor

Actor ActorActor

Page 27: Pony concurrency built into the type system

各アクターにメッセージを配信

Actor

Actor ActorActor

Page 28: Pony concurrency built into the type system

メッセージを処理

Actor

Actor ActorActor

一つのアクターはシングルスレッドで メッセージを1つずつ処理

Page 29: Pony concurrency built into the type system

メッセージドリブン

Actor

Actor ActorActor

Actor

処理が終わったらまた別のアクターに メッセージを投げる

Page 30: Pony concurrency built into the type system

メッセージドリブン

Actor

Actor ActorActor

Actor

処理が終わったらまた別のアクターに メッセージを投げる

Page 31: Pony concurrency built into the type system

メッセージドリブン

Actor

Actor ActorActor

Actor

処理が終わったらまた別のアクターに メッセージを投げる

Page 32: Pony concurrency built into the type system

shared nothing is 神

• 各アクターでデータを共有しないからデータ競合しない!

• ロックも必要ないからデッドロックのリスクもない!

Page 33: Pony concurrency built into the type system

アクターモデルで書ける言語

• 色々ある

• Erlang/Elixir, Scala, C++,…

• Erlangは神

• 軽量スレッドに最適化されたVM

• ノンブロッキングな標準ライブラリ群

Page 34: Pony concurrency built into the type system

一方で

Page 35: Pony concurrency built into the type system

shared nothing の課題

• Erlangなどの言語ではメッセージをコピーして渡す必要がある。間違いでした(※1, ※2)

• コピーしなくても良い物も含まれるので当然オーバーヘッドがある。

• ※1: 特定Byte数以上のBinaryは参照とのことです。 https://twitter.com/voluntas/status/711486384406552576

• ※2: http://www.ponylang.org/papers/fast-cheap.pdf のintroductionを読んで書いたのですが”全部”のデータがコピーされるわけではないようです。

Page 36: Pony concurrency built into the type system

• Scala/akkaではアクターで書きつつshared memoryっぽくデータを共有できる

• しかしデータを共有したが最後、データ競合・デッドロックのリスクが発生してしまう

• アクターモデルとは何だったのか・・・

shared nothing の課題

Page 37: Pony concurrency built into the type system

全部コンパイル時に保証して欲しい・・・

• 「shared memory方式の効率の良さ」と「shared nothingの安全性」を両立したい。

• data-raceとdead-lockが二大天敵

• shared memoryなんだけどコンパイル時にdata-raceとdead-lockが無いことを保証してくれる言語があればできそう・・・

Page 38: Pony concurrency built into the type system

というわけで Ponyの話に戻ります

Page 39: Pony concurrency built into the type system

Ponyとは(再掲)

• アクターモデルに特化

• 速さと(型による)安全性を追求

• pony philosophy

• capability-secure

Page 40: Pony concurrency built into the type system

capabilities-secure

以下の要素を満たす(という定義)http://tutorial.ponylang.org/

• 型安全

• メモリー安全

• 例外安全

• デッドロックなし

• データ競合なし

Page 41: Pony concurrency built into the type system

capabilities-secure

以下の要素を満たす(という定義)http://tutorial.ponylang.org/

• 型安全

• メモリー安全

• 例外安全

• デッドロックなし

• データ競合なし ←コンパイル時に保障

Page 42: Pony concurrency built into the type system

データ競合させないための 型修飾子

Page 43: Pony concurrency built into the type system

object capabilities

• オブジェクトへのアクセス権を細かく分割して型で表現

• 全6種

• 例)複数アクターから同時にWriteできる参照があったらコンパイルエラー

論文 http://www.ponylang.org/papers/fast-cheap.pdf

Page 44: Pony concurrency built into the type system

Main Worker

object capabilities

Page 45: Pony concurrency built into the type system

Main Worker

状態を初期化

object capabilities

Page 46: Pony concurrency built into the type system

Main Worker

状態を初期化

初期化した状態を送信

object capabilities

Page 47: Pony concurrency built into the type system

object capabilities

Page 48: Pony concurrency built into the type system

文字サイズが小さいので拡大

object capabilities

Page 49: Pony concurrency built into the type system

new iso createという メソッド定義により 作成されたインスタンスに isoという型修飾子が ついて返ってくる isoについては後述

object capabilities

Page 50: Pony concurrency built into the type system

fun refとすると 状態を変更するメソッドが 定義できる

object capabilities

Page 51: Pony concurrency built into the type system

refを忘れて 状態を変更すると コンパイルエラー

object capabilities

Page 52: Pony concurrency built into the type system

iso: read/writeユニークな参照

Page 53: Pony concurrency built into the type system

iso: read/writeユニークな参照

isoを受け取るメソッド (正確にはbehavior)

Page 54: Pony concurrency built into the type system

iso: read/writeユニークな参照

• 他の参照が無いので自由に書き換え可能

• 参照を破棄することで、他アクターに安全にread/write権限を渡すことが出来る

Page 55: Pony concurrency built into the type system

iso: read/writeユニークな参照

Page 56: Pony concurrency built into the type system

iso: read/writeユニークな参照

isoな参照型は推論されるので 書かなくてもOK

Page 57: Pony concurrency built into the type system

iso: read/writeユニークな参照

consumeで参照を破棄 & 他アクターに渡す

Page 58: Pony concurrency built into the type system

iso: read/writeユニークな参照

isoな参照を 増やそうとすると コンパイルエラー

Page 59: Pony concurrency built into the type system

iso: read/writeユニークな参照

consume後の 参照は コンパイルエラー

Page 60: Pony concurrency built into the type system

trn: writeユニーク

• 他アクターからはreadできない

• 自アクターからはreadできる

• 権限を破棄すると、writeがもう存在しなくなることを保証できる。=> (readする参照が別にあっても)他アクターに安全に渡せる

Page 61: Pony concurrency built into the type system

trn: writeユニーク

Page 62: Pony concurrency built into the type system

trn: writeユニーク

new isoからnew trn に書き換えただけなので略

Page 63: Pony concurrency built into the type system

trn: writeユニーク

Page 64: Pony concurrency built into the type system

trn: writeユニーク

trnな変数

Page 65: Pony concurrency built into the type system

trn: writeユニーク

参照を放棄 writeできる参照が なくなる

Page 66: Pony concurrency built into the type system

trn: writeユニーク

Page 67: Pony concurrency built into the type system

trn: writeユニーク

val(定数)になる。

Page 68: Pony concurrency built into the type system

• read専用(not ユニーク)

• 自分のアクターでtrnが書き換えるかも

• 他のアクター内にある定数かも

• とりあえず値が読めれば気にしない

box: read参照

Page 69: Pony concurrency built into the type system

box: read参照

Page 70: Pony concurrency built into the type system

box: read参照

trnと全く同じなので略

Page 71: Pony concurrency built into the type system

box: read参照

trnを参照して 読み取りしてOK

Page 72: Pony concurrency built into the type system

box: read参照

trn参照の末路を 気にせず読み取ってOK

Page 73: Pony concurrency built into the type system

val, ref, tag (説明略)

• val: immutable

• ref: mutable

• tag: 参照が同じかどうかの比較しかできない。(read/writeどちらもNG)

Page 74: Pony concurrency built into the type system

型システムがアクターを前提とするメリット

• data-race freeをコンパイル時に保障できるようになる

• writeするなら他アクターはreadしちゃダメ。自アクターからはread OK

• writeしないなら他アクターもreadしてOK。もちろん自分もread OK

• write uniqueなら書き換え件を放棄したら他のアクターに定数として渡してOK

• read/write uniqueなら権限放棄しつつ他アクターに渡せば、そっちでまたwriteできる

• 別のアクターに渡す・自分のアクター内で処理するなどの情報が型システムで扱える。

Page 75: Pony concurrency built into the type system

型システムがアクターを前提とするメリット

• zero-copyなので大きいデータをメッセージとしてやり取りしてもオーバーヘットがない

• 標準ライブラリがこの機能をガンガン使っていることによる恩恵も受けられる

• 処理系がデータ競合のケアをしなくて良くなる。 GCでの性能改善などに寄与している(らしい) https://

www.youtube.com/watch?v=KvLjy8w1G_U

Page 76: Pony concurrency built into the type system

コンパイルが通ったら絶対に クラッシュさせないという 熱い気持ち安全な型システム

Page 77: Pony concurrency built into the type system

パフォーマンス (付録) https://github.com/ponylang/ponylang.github.io/blob/master/benchmarks_all.pdf

• それなりに速そう

• スケジューラーとキューも工夫あり

pony

Page 78: Pony concurrency built into the type system

GCもすごい (付録)

• Pony-ORCA (Ownership and Reference Counting based gc for

Actor?)

• stop-the-world lessな並行GC

• one Actor GC/cross Actor GC/Actor GC

• data-race freeを前提に出来る強さ。

• 詳しくは論文(https://github.com/ponylang/ponylang.github.io/blob/master/papers/OGC.pdf)

Page 79: Pony concurrency built into the type system

他の言語との比較 (付録)

Page 80: Pony concurrency built into the type system

Ponyお役立ち資料集 (付録)

• とりあえず公式Tutorial & 発表

• http://tutorial.ponylang.org/

• https://www.youtube.com/watch?v=KvLjy8w1G_U

• 論文

• http://www.doc.ic.ac.uk/teaching/distinguished-projects/2013/s.blessing.pdf

• https://github.com/ponylang/ponylang.github.io/tree/master/papers

Page 81: Pony concurrency built into the type system

まとめ

• shared nothingとshared memoryを統合して高速化を図りつつ安全に並行処理を記述できる

• アクターを前提にした言語&処理系のメリット

• zero-copy & data-race free & dead-lock free

• Pony-ORCA GC

• 完全な研究用ではなく実用を目指している。

• 真の狙いは分散システムにある。(distributed ponyに期待)