Docsity

Performance Issues & Techniques in Object-Oriented Language Compilation, Study notes of Computer Science

Toggle sidebar
bg1
CS553 Lecture Compiling Object Oriented Languages 2
Compiling Object Oriented Languages
!Last time
–!Undergraduate compiler review
!Today
–!Introduction to compiling object oriented languages
–!What are the performance issues?
CS553 Lecture Compiling Object Oriented Languages 3
What is an Object-Oriented Programming Language?
!Objects
–!Encapsulate code and data
!Inheritance
–!Supports code reuse and software
evolution
!Subtype polymorphism
–!Can use a subclass wherever a parent
class is expected
!Dynamic binding (message sends)
–!Binding of method name to code is done
dynamically based on the dynamic type
of the (receiver) object
Person
Student Teacher
Person p = new Person;
Student s = new Student;
PrintName(p);
PrintName(s);
PrintName(Person p);
p.reprimand();
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Performance Issues & Techniques in Object-Oriented Language Compilation and more Study notes Computer Science in PDF only on Docsity!

CS553 Lecture Compiling Object Oriented Languages 2

Compiling Object Oriented Languages

! Last time

-! Undergraduate compiler review ! Today -! Introduction to compiling object oriented languages -! What are the performance issues?

What is an Object-Oriented Programming Language?

! Objects

-! Encapsulate code and data ! Inheritance -! Supports code reuse and software evolution ! Subtype polymorphism -! Can use a subclass wherever a parent class is expected ! Dynamic binding ( message sends ) -! Binding of method name to code is done dynamically based on the dynamic type of the (receiver) object Person Student Teacher Person p = new Person; Student s = new Student; PrintName(p); PrintName(s); PrintName(Person p); p.reprimand();

CS553 Lecture Compiling Object Oriented Languages 4

Implementation: Inheritance of Instance Variables

! Goal

-! Lay out object for type-independent instance variable access ! Solution (single inheritance) -! Prefixing: super-class fields are at beginning of object ! Example Student Name ID Person Name Teacher Name Salary

Implementation: Dynamic Binding

! Problem

-! The appropriate method depends on the dynamic type of the object e.g., p.reprimand() ! Solution -! Create descriptor for each class ( not object) encoding available methods -! Encode pointer to class descriptor in each object -! Lay out methods in class descriptor just like instance variables ! Usage summary -! Load class descriptor pointer from object -! Load method address from descriptor -! Jump to method Student getName reprimand workhard Person getName Teacher getName reprimand party

CS553 Lecture Compiling Object Oriented Languages 8

Cost of Dynamic Binding

! Direct cost

-! Overhead of performing dynamic method invocation ! Indirect cost -! Inhibits static analysis of the code ! Example ! class rectangle:shape { ! int length() { ... } ! int width() { ... } ! int area() { return (length() * width()); } ! } ! Driessen and Holzle (OOPSLA 96), in C++ programs median of 5.2% total execution time spent on dynamic dispatch Want to inline and assign to registers, etc.

Dynamism: Data

! Object instance types are not statically apparent

-! Need to be able to manipulate uniformly -! Boxing: wrap up all data and reference it with a pointer ! Example ! Integer n = new Integer(33); type descriptor data (int) pointer n

CS553 Lecture Compiling Object Oriented Languages 10

Cost of Dynamism: Data

! Direct cost

-! Overhead of actually extracting data -! e.g., 2 loads versus 0 (if data already in a register) ! Indirect cost -! More difficult to statically reason about data

Style

! Sometimes programmers write C-style code in OO languages

-! Easy: just “optimize” it away ! Sometimes programmers actually exploit dynamism -! Hard: it can’t just be “optimized away” ! Programmers create many small objects -! Thwarts local analysis -! Exacerbates dynamism problem -! Huge problem for pure OO languages ! Programmers create many small methods -! Methods to encapsulate data -! e.g. Methods to get and set member fields

CS553 Lecture Compiling Object Oriented Languages 14

Approaches to Implementing Java (cont)

! JIT compilation

-! Still supports mobile code (with more effort) -! Can have very good performance -! Compilation time is critical -! Compiler can exploit dynamic information ! JIT/Dynamic compilation -! Compiler gets several chances on the same code -! Compiler can exploit changes in dynamic information

Why is Java Slow?

! Bytecode interpretation?

-! Not a good answer

CS553 Lecture Compiling Object Oriented Languages 16

Why is Java Slow?

! Impediments to performance

-! Dynamic class loading thwarts optimization -! Even the class hierarchy is dynamic -! Flexible array semantics -! Run-time checks (null pointers, array bounds, types) -! Precise exception semantics thwart optimization -! Multithreading introduces synchronization overhead -! Lots of memory references (poor cache performance) ... and all the usual OO challenges

Analysis with a Dynamic Class Hierarchy

! Approaches

-! Ignore it ( i.e., disable dynamic class loading) -! Exploit final classes & methods -! Conservative optimization ( e.g., guarded devirtualization) -! Track validity of current code fragments and rebuild as necessary -! e.g., Resolution dependence graph -! Necessitates JIT/dynamic compilation

CS553 Lecture Compiling Object Oriented Languages 20

Exceptions in Java

! Exceptions in Java are precise

-! The effects of all statements and expressions before a thrown exception must appear to have taken place, and -! The effects of all statements or expressions after a thrown exception must appear not to have taken place ! Implications -! Must be very careful or clever when -! Eliminating checks or -! Reordering statements CS553 Lecture Compiling Object Oriented Languages 21 ! Idea -! Create two versions of a block of code -! One is guaranteed not to except and is optimized accordingly -! The other is used when the code might except if (m <= C.size(0) && p <= C.size(1) && m <= A.size(0) && n <= A.size(1) && n <= B.size(0) && p <= B.size(1)) { for (i=0; i<m; i++) // safe region ! for (j=0; j<p; j++) ! for (k=0; k<n; k++) ! C[i][j] += A[i][k] * B[k][j]; ! } else { for (i=0; i<m; i++) // unsafe region ! for (j=0; j<p; j++) ! for (k=0; k<n; k++) ! C[i][j] += A[i][k] * B[k][j]; ! }

Safe Regions [Moreira et al. TOPLAS 2000]

CS553 Lecture Compiling Object Oriented Languages 22

Java Arrays and Loop Transformations

! Java arrays

-! No multidimensional arrays -! Instead use arrays of arrays (can be ragged) -! Requires one memory reference for each array dimension -! Rows may alias with one another ! Arrays are common in scientific applications -! Their use requires optimization for good performance -! Large body of work on loop transformations makes assumptions -! Arrays stored in contiguous memory -! No aliasing among array elements -! (Arrays are not ragged)

Java Arrays

IBM Systems Journal 39(1) © 2000 IBM