Slide 1

Slide 1 text

#ccc_c4 CLRのValue Typeを起点にProject Valhallaを覗いてみる Look into Project Valhalla from CLR viewpoint

Slide 2

Slide 2 text

https://speakerdeck.com/logico/look-into- project-Valhalla-from-clr-viewpoint

Slide 3

Slide 3 text

このセッションで持ち帰ってもらいたいこと

Slide 4

Slide 4 text

Who am I? https://logico-jp.io

Slide 5

Slide 5 text

Agenda

Slide 6

Slide 6 text

メモリレイアウトの重要性

Slide 7

Slide 7 text

CPUはデータの局所性 (Data Locality) の原則に従う

Slide 8

Slide 8 text

CPUからのメモリアクセス L1 L2 Main Memory L3 Cache

Slide 9

Slide 9 text

レイテンシ Intel Broadwell-EPを例に L1 L2 Main Memory Intel® 64 and IA-32 Architectures Optimization Reference Manual https://software.intel.com/sites/default/files/managed/9e/bc/64-ia-32-architectures-optimization-manual.pdf L3 Cache

Slide 10

Slide 10 text

CLRのValue Type

Slide 11

Slide 11 text

CLR (Common Language Runtime) https://docs.microsoft.com/ja-jp/dotnet/standard/clr

Slide 12

Slide 12 text

CLRの型の種類

Slide 13

Slide 13 text

Value Type (値型)  enum  struct https://docs.microsoft.com/ja-jp/dotnet/csharp/language-reference/keywords/value-types

Slide 14

Slide 14 text

Nullable Value Type (Null許容値型) https://docs.microsoft.com/ja-jp/dotnet/csharp/programming-guide/nullable-types/

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

static void Main() { Point a = new Point(2, 4); Point b = a; Point c = a; ... } struct Point { public int x, y; public Point(int x, int y) { this.x = x; this.y = y; } ... }

Slide 17

Slide 17 text

static void Main() { Point a = new Point(2, 4); Point b = a; Point c = a; ... } struct Point { public int x, y; public Point(int x, int y) { this.x = x; this.y = y; } ... } Stack Heap a = Point(2,4) b = Point(2,4) c = Point(2,4)

Slide 18

Slide 18 text

Reference Type (参照型)  class  配列 https://docs.microsoft.com/ja-jp/dotnet/csharp/language-reference/keywords/reference-types

Slide 19

Slide 19 text

O M Method table pointer Object header AbcD..

Slide 20

Slide 20 text

Nullable Reference Type (Null許容参照型) https://docs.microsoft.com/ja-jp/dotnet/csharp/nullable-references

Slide 21

Slide 21 text

static void Main() { Point a = new Point(2, 4); Point b = a; Point c = a; ... } class Point { public int x, y; public Point(int x, int y) { this.x = x; this.y = y; } }

Slide 22

Slide 22 text

static void Main() { Point a = new Point(2, 4); Point b = a; Point c = a; ... } class Point { public int x, y; public Point(int x, int y) { this.x = x; this.y = y; } } Stack Heap a(ref) b(ref) c(ref) a Point(2,4)

Slide 23

Slide 23 text

3 12 24 O M 53 値型の配列 O M -1 9 1 -1 -8 2 5 8 5 2 3

Slide 24

Slide 24 text

O M 5 参照型の配列

Slide 25

Slide 25 text

O M 5 O M 3 2 O M -1 9 O M -8 2 O M 1 -1 O M 5 8

Slide 26

Slide 26 text

値型のメリット

Slide 27

Slide 27 text

値型のデメリット

Slide 28

Slide 28 text

Project Valhallaとは

Slide 29

Slide 29 text

Project Valhalla ― Reboot JVM relationship with data in memory https://wiki.openjdk.java.net/display/valhalla/Main

Slide 30

Slide 30 text

スコープ

Slide 31

Slide 31 text

スコープ

Slide 32

Slide 32 text

マイルストン

Slide 33

Slide 33 text

マイルストン  L2 (LW2) https://wiki.openjdk.java.net/display/valhalla/LW2

Slide 34

Slide 34 text

LW2 http://jdk.java.net/valhalla/

Slide 35

Slide 35 text

様々な最適化

Slide 36

Slide 36 text

Project Valhalla のこれまでのあゆみ JVMとプログラミングモデルの両方から調査 L World Early prototypes (M1-M3) コンパイラのみ 調査対象は言語側 • Genericsの特殊化や ワイルドカード • VMへの要件 • 多数の検討課題があること を確認 “MVT” prototype VMのみ 調査対象は“Q-World”モデル 詳細はJVM Language Summit 2018の動画を 参照 Current prototype • VMとコンパイラをサポート • Genericsの特殊化、 参照型から値型への移行は 未サポート • まだまだ調査中

Slide 37

Slide 37 text

* type

Slide 38

Slide 38 text

Q World Q qload...

Slide 39

Slide 39 text

Q World Q qload... 動きはしたが、 オーバーヘッドが大きく 使い物にならず...

Slide 40

Slide 40 text

L World a*

Slide 41

Slide 41 text

L World

Slide 42

Slide 42 text

メモリレイアウトの効率化が必要な理由 (再掲) Intel Broadwell-EPを例に L1 L2 Main Memory Intel® 64 and IA-32 Architectures Optimization Reference Manual https://software.intel.com/sites/default/files/managed/9e/bc/64-ia-32-architectures-optimization-manual.pdf L3 Cache

Slide 43

Slide 43 text

Javaでは?

Slide 44

Slide 44 text

Object Identity フラットなメモリレイアウトを実現する上での課題 class Point { int x; int y; Point(int _x, int _y) { this.x = _x; this.y = _y; } } ... Point p1 = Point(1,2); Point p2 = Point(1,2);

Slide 45

Slide 45 text

Object Identity フラットなメモリレイアウトを実現する上での課題 class Point { int x; int y; Point(int _x, int _y) { this.x = _x; this.y = _y; } } ... Point p1 = Point(1,2); Point p2 = Point(1,2); p1とp2は完全に 一致するか?

Slide 46

Slide 46 text

M K 53 klass (同一クラスを使うインスタンス共通のmetadataへのPointer) mark (インスタンス固有のmetadata) Metaspace HotSpotの場合

Slide 47

Slide 47 text

M K 3 12 24 53 配列

Slide 48

Slide 48 text

M K 3 12 24 M K 3 M K 12 M K 24 M K 53 53

Slide 49

Slide 49 text

例 public class Point { int x; int y; } public class App { ... Point[] points = new Point[5]; points[0] = new Point(); ... }

Slide 50

Slide 50 text

M K 5

Slide 51

Slide 51 text

M K 5 M K 3 2 M K -1 9 M K -8 2 M K 1 -1 M K 5 8

Slide 52

Slide 52 text

M K 3 2 M K -1 9 M K -8 2 M K 1 -1 M K 5 8 M K -1 9 1 -1 -8 2 5 8 5 2 3

Slide 53

Slide 53 text

M K 3 2 M K -1 9 M K -8 2 M K 1 -1 M K 5 8 inline class M K -1 9 1 -1 -8 2 5 8 5 2 3

Slide 54

Slide 54 text

現在のプロトタイプでできること

Slide 55

Slide 55 text

Inline class public inline class Point { private int x; private int y; private Point(int _x, int _y) { this.x = _x; this.y = _y; } public static Point of(int _x, int _y) { return new Point(_x,_y); } }

Slide 56

Slide 56 text

コンストラクタ  defaultvalue  withfield

Slide 57

Slide 57 text

public class Point { private int x; private int y; private Point(int _x, int _y) { this.x = _x; this.y = _y; } public static Point of(int _x, int _y) { return new Point(_x,_y); } } public inline class Point { private int x; private int y; private Point(int _x, int _y) { this.x = _x; this.y = _y; } public static Point of(int _x, int _y) { return new Point(_x,_y); } }

Slide 58

Slide 58 text

配列

Slide 59

Slide 59 text

デフォルト値 Foo.default と表記 x = T.default x = null

Slide 60

Slide 60 text

メモリレイアウトのフラット化 常にフラットでなければならないか?

Slide 61

Slide 61 text

メモリレイアウトのフラット化 LW2 では V? を使う (Null許容) http://hg.openjdk.java.net/valhalla/valhalla /file/a32457198d3e/test/jdk/valhalla/valuet ypes/NonFlattenValue.java public inline class NonFlattenValue { Point? nfp; NonFlattenValue() { this.nfp = Point.makePoint(0,0); } NonFlattenValue(Point p) { this.nfp = p; } public Point? point() { return nfp; } public Point pointValue() { return (Point) nfp; } ... }

Slide 62

Slide 62 text

Generics OptionalInt?

Slide 63

Slide 63 text

今後検討していくもの (LW2 では未検討のもの)

Slide 64

Slide 64 text

新しい上位型 – RefObject と ValObject

Slide 65

Slide 65 text

新しい上位型 – なぜ必要か? x instance of RefObject RefObject RefObject o extents RefObject

Slide 66

Slide 66 text

Nullの許容

Slide 67

Slide 67 text

Nullの許容 (L3での調査の中心部分)

Slide 68

Slide 68 text

同一性 (==)

Slide 69

Slide 69 text

同一性 (==)

Slide 70

Slide 70 text

移行 https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/doc- files/ValueBased.html https://docs.oracle.com/javase/jp/13/docs/api/java.base/java/lang/doc-files/ValueBased.html

Slide 71

Slide 71 text

その他のIdentityに関連する操作

Slide 72

Slide 72 text

Conclusion

Slide 73

Slide 73 text

LW2時点でのInline Classの制限 基底クラス java.lang.Object 含められるもの Top level クラス、innerクラス、nestedクラス、localクラス 含められないもの Interface、annotation type、enum 宣言できるもの innerクラス、nestedクラス、localクラス Nullを許容するか しない 必ずデフォルト値を持つ (全ビットゼロ) abstractにできるか できない Inline Classは暗黙のうちにfinal Inline Classのメンバ 全てのインスタンスフィールドは暗黙のうちにfinal 明示的にregular interfaceを実装できる 再帰的に自インスタンスのフィールドの型として宣言できない javacによる操作 hashCode()、equals()、toString()を自動生成する clone()、finalize()、wait()、notify()を許可しない

Slide 74

Slide 74 text

現在検討しているもの (一例)

Slide 75

Slide 75 text

まとめ

Slide 76

Slide 76 text

セッションアンケート https://aka.ms/AA6m7dq

Slide 77

Slide 77 text

Microsoft Learnや 開発者ニュースレター の詳細情報 https://aka.ms/AA6lx6o

Slide 78

Slide 78 text

Resources

Slide 79

Slide 79 text

Resources https://youtu.be/1H4vmT-Va4o https://youtu.be/_26KZAegYRM https://youtu.be/49GUljUmGHg https://www.slideshare.net/DavidBuck7/valhalla-update-jjug-ccc-spring-2019

Slide 80

Slide 80 text

No content