Скачать презентацию Two-Dimensional Bi-Directional Object Layout Yoav Zibin The Technion Israel Скачать презентацию Two-Dimensional Bi-Directional Object Layout Yoav Zibin The Technion Israel

274d4e4a35076042ffbf731bb3ef9b70.ppt

  • Количество слайдов: 29

Two-Dimensional Bi-Directional Object Layout Yoav Zibin The Technion—Israel Institute of Technology Joint work with: Two-Dimensional Bi-Directional Object Layout Yoav Zibin The Technion—Israel Institute of Technology Joint work with: Yossi Gil

Object Layout in C++: single inheritance Add your fields to the layout of your Object Layout in C++: single inheritance Add your fields to the layout of your supertype Legend A class A { … }; h A B class B: public A { … }; h A B C class C: public B { … }; h A B C h VPTR

Object Layout in C++: simple multiple inheritance class A { … }; h A Object Layout in C++: simple multiple inheritance class A { … }; h A A B C Legend h VPTR class B { … }; h B class C: public A, public B { … }; h A h B C • Objects may have several VPTRs • this-adjustment when calling methods of B

Object Layout in C++: virtual inheritance Legend h VPTR class A { … }; Object Layout in C++: virtual inheritance Legend h VPTR class A { … }; h A A v v B C D D has a single copy of A VBPTR class B: public virtual A { … }; h B h A class C: public virtual A { … }; h C h A class D: public B, public C { … }; h B h C D h A • Objects may have several VBPTRs • Accessing a field in A requires 2 loads (dereferences)

Standard Layout Schemes n C++ n n Pros: incremental, 1 -2 loads for field Standard Layout Schemes n C++ n n Pros: incremental, 1 -2 loads for field access Lot’s of optimizations (many are propriety), often require whole program analysis Cons: requires psychic programmers, this-adjustment in some castings and method calls, object size overhead (many VPTRs, VBPTRs) Java: single inheritance (no fields in interfaces) Cecil & Dylan: field dispatching technique n Encapsulate fields in accessor methods Field access method dispatch n n Pros: simple, incremental Cons: dispatching is slow, memory overhead can be significant

Fixed-offsets technique [Pugh & Weddell ‘ 90] n Assign each field a fixed offset Fixed-offsets technique [Pugh & Weddell ‘ 90] n Assign each field a fixed offset (either positive or negative Bi-Directional) n n n Pros: 1 load for accessing all fields Cons: objects may have holes, requires knowing the whole hierarchy at compile time We generalize this technique

Fixed-offsets (2/3) A: h B C: h A C A B ? C A: Fixed-offsets (2/3) A: h B C: h A C A B ? C A: h C A D: h B A B: h A C: h A D Uni-directional D B and C are in conflict A hole in objects of class C B C D

Fixed-offsets (2/3) A: h B C A B: h A A B C: h Fixed-offsets (2/3) A: h B C A B: h A A B C: h A C A B ? C D: h D A: Bi-directional B: h B h A A C h A C in conflict A h B C: D: D B and C are D How can we determine if there exists a Bi-dir layout without holes?

Fixed-offsets (3/3) n Definition: Types X and Y are in conflict iff (i)i X Fixed-offsets (3/3) n Definition: Types X and Y are in conflict iff (i)i X and Y have a common descendant , and (ii) X and Y are independent, i. e. , neither is a subtype of the other Inheritance hierarchy Conflict graph A A B D E F I G J B H C C E F I D G H J

Fixed-offsets (3/3) n Definition: Types X and Y are in conflict iff (i)i X Fixed-offsets (3/3) n Definition: Types X and Y are in conflict iff (i)i X and Y have a common descendant , and (ii) X and Y are independent, i. e. , neither is a subtype of the other Inheritance hierarchy Conflict graph Layout scheme A A -2 -1 0 1 2 3 4 C F B E I C h A G D H J B C D B D E. g. , J: E F I G H J

Fixed-offsets (3/3) n Definition: Types X and Y are in conflict iff (i)i X Fixed-offsets (3/3) n Definition: Types X and Y are in conflict iff (i)i X and Y have a common descendant , and (ii) X and Y are independent, i. e. , neither is a subtype of the other Inheritance hierarchy Conflict graph Layout scheme A A -2 -1 0 1 2 3 4 C F B E I C h A G D H J B C D B D E. g. , J: E F I G H J G C h A D H J

Fixed-offsets (3/3) n Lemma [Pugh & Weddell ‘ 90]: There exists a bi-directional layout Fixed-offsets (3/3) n Lemma [Pugh & Weddell ‘ 90]: There exists a bi-directional layout without holes iff the conflict graph is 2 -colorable Inheritance hierarchy Conflict graph Layout scheme A A -2 -1 0 1 2 3 4 C F B E I C h A G D H J B D E F I G J B H C E F I D G H J

Our solution: 2 D Bi-dir layout n What happens if the graph is not Our solution: 2 D Bi-dir layout n What happens if the graph is not 2 -colorable ? n n Pugh & Weddell answer: Leave holes in some objects (using a heuristic) Our answer: 2 -Dimensional Bi-directional layout (2 D Bi-dir) E. g. , if the graph is 6 -colorable we use 3 layers:

2 D Bi-dir n All objects use the same basic layout scheme Scheme n 2 D Bi-dir n All objects use the same basic layout scheme Scheme n n Layout of A Layout of B Layout of C Layers are Bi-directional Can be empty #layers = #colors / 2 Independently discovered by Pugh & Weddell [Tech report ’ 93]

Mapping 2 D Bi-dir to linear memory n Using an array of pointers to Mapping 2 D Bi-dir to linear memory n Using an array of pointers to the layers origins Scheme: n Example: n

Layout optimization n n Sharing an array of offsets (instead of pointers) 1 load Layout optimization n n Sharing an array of offsets (instead of pointers) 1 load for fields in the first layer (colors 1 & 2) n 3 loads for the other layers n Scheme: h n Example: h +8 +7

Experiments n Data set: 28 hierarchies with 49, 379 types n n n Large Experiments n Data set: 28 hierarchies with 49, 379 types n n n Large hierarchies used in real life programs Taken from eight different programming languages Resemble trees 67 #types 8, 793 #colors 24 #layers 12 We compared 2 D Bi-dir with: n n n Fixed-offsets [Pugh & Weddell ‘ 90] Standard C++ Optimized C++: Simple-inline, Aggressive-inline & Sweeney ‘ 99, Eckel & Gil ‘ 2000] [Gil

E. g. , Core of Flavors hierarchy (1/3) 7 classes require 3 loads 60 E. g. , Core of Flavors hierarchy (1/3) 7 classes require 3 loads 60 classes require 1 load

E. g. , Flavors conflict graph (2/3) The conflict graph is usually more close E. g. , Flavors conflict graph (2/3) The conflict graph is usually more close to linear than quadratic

E. g. , Flavors cont. (3/3) n Object size overhead n n 2 D E. g. , Flavors cont. (3/3) n Object size overhead n n 2 D Bi-dir: C++ techniques: Fixed-offsets: 1 type-identifier 2. 4 – 3. 4 VPTRs 6% holes Access Efficiency: percentage of accesses that require 1 load n n n 2 D Bi-dir: C++ techniques : Fixed-offsets: 81% 50 -77% 100%

Avg. #layers in different hierarchies Avg. #layers in different hierarchies

Summary: Advantages of 2 D Bi-Dir n Dynamic memory overhead (per object) n n Summary: Advantages of 2 D Bi-Dir n Dynamic memory overhead (per object) n n Static memory overheads (per type) n n | array of offsets | 11 bytes Field access efficiency (run-time) n n A single type-identifier 1 or 3 load instructions to access a field (on average, 82% of field accesses require 1 load) Time for computing the layout (compile-time) n n usually 10 -50 m. Sec, worst case 400 m. Sec 13 micro-seconds / type

Comparison with 2 D Bi-dir Dynamic memory overhead Field dispatching Fixed-offsets C++ 2 D Comparison with 2 D Bi-dir Dynamic memory overhead Field dispatching Fixed-offsets C++ 2 D Bi-dir #load Incremental instructions 0 3 holes × 1 VPTRs & VBPTRs 1 -2 0 × 1 or 3

Future Research n n n Empirical study of field access frequencies Further reducing the Future Research n n n Empirical study of field access frequencies Further reducing the static memory overheads Incremental algorithms

The End n Any questions? The End n Any questions?

Object Layout in C++ class A { … }; h A A h VPTR Object Layout in C++ class A { … }; h A A h VPTR VBPTR class B: public A { … }; h A B v v E Legend F G G has a single copy of A B C D class C { … }; h C class D: public B, public C { … }; h A B h C D class E: public virtual A { … }; h E h A class F: public virtual A { … }; h F h A class G: public E, public F { … }; h E h F G h A

Inheritance hierarchy Conflict graph Layout scheme A A -2 -1 0 1 2 3 Inheritance hierarchy Conflict graph Layout scheme A A -2 -1 0 1 2 3 4 C F B E I C h A G D H J B C D B D E. g. , J: E F I G H J

Inheritance hierarchy Conflict graph Layout scheme A A -2 -1 0 1 2 3 Inheritance hierarchy Conflict graph Layout scheme A A -2 -1 0 1 2 3 4 C F B E I C h A G D H J B C D B D E. g. , J: E F I G H J

A A