two-dimensional bi-directional object layout yoav zibin the technion—israel institute of...

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

Upload: shon-cummings

Post on 17-Jan-2016

249 views

Category:

Documents


0 download

TRANSCRIPT

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

Two-Dimensional Bi-Directional Object Layout

Yoav ZibinThe Technion—Israel Institute of

TechnologyJoint work with: Yossi Gil

Page 2: 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

A class A { … };

B class B: public A { … };

h A

h A B

VPTRh

Legend

Ch A B

Add your fields to the layout of your supertype

class C: public B { … };C

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

Object Layout in C++: simple multiple inheritance

class C: public A, public B { … };Ch B C

• Objects may have several VPTRs

• this-adjustment when calling methods of B

BAh B

class A { … };h A

VPTRh

Legend

class B { … };

h A

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

Bv class B: public virtual A { … };

Cv

class C: public virtual A { … };

D

class D: public B, public C { … };h A

h B h A

h C h A

Dh Ch BD has a single

copy of A

Aclass A { … };h A

VPTRh

Legend

VBPTR

• Objects may have several VBPTRs

• Accessing a field in A requires 2 loads (dereferences)

Object Layout in C++: virtual inheritance

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

Standard Layout Schemes

C++ 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

Encapsulate fields in accessor methods

Pros: simple, incremental Cons: dispatching is slow, memory overhead can be

significant

Field access method dispatch

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

Fixed-offsets technique [Pugh & Weddell ‘90]

Assign each field a fixed offset (either positive or negative Bi-Directional) Pros: 1 load for accessing all fields Cons: objects may have holes,

requires knowing the whole hierarchy at compile time

We generalize this technique

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

Fixed-offsets (2/3)

h A

A

D

B C

h A

h A

h A

B

A:

B:

C:

D:

C

B and C are

in conflict

B

CD?

A

A

A

A

B

B

C

C D

h

h

h

A:

B:

C:

D: h

Uni-directional

A hole in objects of

class C

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

Fixed-offsets (2/3)

h AB and C

are

in conflict

A

D

B C

h A

h A

h A

B

A:

B:

C:

D:

C

B

CD?

A

A

A

A

B

B

C

C D

h

h

h

A:

B:

C:

D: h

Bi-directional

How can we determine if there exists a Bi-dir layout without holes?

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

Fixed-offsets (3/3)

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

G

I

E F H

J

B C D

A

Inheritance hierarchy

G

I

E F H

J

B C D

A

Conflict graph

B C

F

B

E

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

Fixed-offsets (3/3)

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

E.g., J:

G

I

E F H

J

B C D

A

G

I

E F H

J

B C D

A

Inheritance hierarchy

G

I

E F H

J

B C D

A

Conflict graph

C AF

DGB E I

H Jh

0 1 2 3 4-1-2

Layout scheme

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

Fixed-offsets (3/3)

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

G H

J

C D

A

E.g., J:

G

I

E F H

J

B C D

A

G

I

E F H

J

B C D

A

Inheritance hierarchy

G

I

E F H

J

B C D

A

Conflict graph

C AF

DGB E I

H Jh

0 1 2 3 4-1-2

Layout scheme

J J

C

A

G H

DC A

DG H J

C A DG H Jh

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

Fixed-offsets (3/3)

Lemma [Pugh & Weddell ‘90]:There exists a bi-directional layout without holes iffthe conflict graph is 2-colorable

G

I

E F H

J

B C D

A

G

I

E F H

J

B C D

A

Inheritance hierarchy

G

I

E F H

J

B C D

A

Conflict graph

C AF

DGB E I

H Jh

0 1 2 3 4-1-2

Layout scheme

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

Our solution: 2D Bi-dir layout

What happens if the graph is not 2-colorable ? Pugh & Weddell answer:

Leave holes in some objects (using a heuristic) Our answer:

2-Dimensional Bi-directional layout (2D Bi-dir)E.g., if the graph is 6-colorable we use 3

layers:

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

2D Bi-dir

All objects use the same basic layout scheme

Layers are Bi-directional Can be empty #layers = #colors / 2

Independently discovered by Pugh & Weddell [Tech report ’93]

Layout of A

Scheme Layout of B

Layout of C

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

Mapping 2D Bi-dir to linear memory

Using an array of pointers to the layers origins

Scheme:

Example:

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

Layout optimization

Sharing an array of offsets (instead of pointers)

1 load for fields in the first layer (colors 1 & 2) 3 loads for the other layers

Scheme:

Example:

h

h

+8 +7

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

Experiments

Data set: 28 hierarchies with 49,379 types 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 2D Bi-dir with: Fixed-offsets [Pugh & Weddell ‘90] Standard C++ Optimized C++: Simple-inline, Aggressive-inline

[Gil & Sweeney ‘99, Eckel & Gil ‘2000]

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

E.g., Core of Flavors hierarchy (1/3)

7 classes require 3 loads

60 classes require 1 load

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

E.g., Flavors conflict graph (2/3)

The conflict graph is usually more close to linear than

quadratic

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

E.g., Flavors cont. (3/3)

Object size overhead 2D Bi-dir: 1 type-identifier C++ techniques: 2.4 – 3.4 VPTRs Fixed-offsets: 6% holes

Access Efficiency: percentage of accesses that require 1 load 2D Bi-dir: 81% C++ techniques :50-77% Fixed-offsets: 100%

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

Flav

ors

IDL.

type

sM

I-IBM

-XM

LJA

V.ty

pes

LAU.

type

sED

.type

slov

-obje

ct-e

ditor ce

cil2

cecil

-un

idraw

-inte

rview

sha

rlequ

inM

I-Orb

acus

-test

MI-H

otJa

vady

lance

cilge

ode

MI-O

rbac

us vor3

MI-C

orba

java1

.18.

type

sse

lf-sy

stem

vorte

x3EI

F.ty

pes

MI-O

rbix

java.

1.22

.type

sjav

a.1.

30.ty

pes

MI-j

dk1.

3.1

MI-I

BM-S

F-sa

mple

s

0.0

2.0

4.0

6.0

8.0

10.0

C++: Standard

C++: Simple inline

C++: Aggressive inline

2D Bi-dir

Avg. #layers in different hierarchies

Avg. #

layers

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

Summary: Advantages of 2D Bi-Dir

Dynamic memory overhead (per object) A single type-identifier

Static memory overheads (per type) | array of offsets | 11 bytes

Field access efficiency (run-time) 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)

usually 10-50 mSec, worst case 400mSec 13 micro-seconds / type

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

Comparison with 2D Bi-dir

×

×

Incremental

1 or 302D Bi-dir

1 - 2VPTRs & VBPTRs

C++

1holesFixed-offsets

30Field dispatching

#load instruction

s

Dynamic memory overhead

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

Future Research

Empirical study of field access frequencies Further reducing the static memory

overheads Incremental algorithms

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

The End

Any questions?

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

Object Layout in C++

A

B

G

Ev

Fv

class A { … };

class C { … };

class D: public B,public C { … };

class E: public virtual A { … };

class B: public A { … };

class F: public virtual A { … };

class G: public E,public F { … };

h A

C

D

h A B

h C

h A B h C

h A

D

h E h A

h F h A

Gh Fh E

G has a single copy

of A

VPTRh

Legend

VBPTR

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

E.g., J:

G

I

E F H

J

B C D

A

G

I

E F H

J

B C D

A

Inheritance hierarchy

G

I

E F H

J

B C D

A

Conflict graph

C AF

DGB E I

H Jh

0 1 2 3 4-1-2

Layout scheme

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

E.g., J:

G

I

E F H

J

B C D

A

G

I

E F H

J

B C D

A

Inheritance hierarchy

G

I

E F H

J

B C D

A

Conflict graph

C AF

DGB E I

H Jh

0 1 2 3 4-1-2

Layout scheme

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

A