Dynamic Languages Hal Perkins Autumn 2009

Slides:



Advertisements
Similar presentations
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Advertisements

Intermediate Code Generation
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
The Assembly Language Level
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Aarhus University, 2005Esmertec AG1 Implementing Object-Oriented Virtual Machines Lars Bak & Kasper Lund Esmertec AG
Previous finals up on the web page use them as practice problems look at them early.
Scripting Languages For Virtual Worlds. Outline Necessary Features Classes, Prototypes, and Mixins Static vs. Dynamic Typing Concurrency Versioning Distribution.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Schedule Midterm out tomorrow, due by next Monday Final during finals week Project updates next week.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
C++ fundamentals.
Self: The Power of Simplicity David Ungar and Randall B. Smith Presenter: Jonathan Aldrich
Comparison of OO Programming Languages © Jason Voegele, 2003.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Sadegh Aliakbary Sharif University of Technology Fall 2011.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
JIT in webkit. What’s JIT See time_compilation for more info. time_compilation.
7. Just In Time Compilation Prof. O. Nierstrasz Jan Kurs.
CSC 310 – Imperative Programming Languages, Spring, 2009 Virtual Machines and Threaded Intermediate Code (instead of PR Chapter 5 on Target Machine Architecture)
Sadegh Aliakbary Sharif University of Technology Fall 2012.
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
Chapter 12 Support for Object oriented Programming.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
CS 598 Scripting Languages Design and Implementation 14. Self Compilers.
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
Introduction to OOP CPS235: Introduction.
ECE 750 Topic 8 Meta-programming languages, systems, and applications Automatic Program Specialization for J ava – U. P. Schultz, J. L. Lawall, C. Consel.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSCE 240 – Intro to Software Engineering Lecture 3.
ISBN Chapter 12 Support for Object-Oriented Programming.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
1 Case Study: Meta Classes  Class representation in memory  Class variables and class methods  Meta Classes  3 Level System  4 Level System  5 Level.
Smalltalk Implementation Harry Porter, October 2009 Smalltalk Implementation: Optimization Techniques Prof. Harry Porter Portland State University 1.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Visit for more Learning Resources
Object-Oriented Analysis and Design
Java Primer 1: Types, Classes and Operators
Type Systems Terms to learn about types: Related concepts: Type
Multi-Dispatch in the Java™ Virtual Machine
Optimizing Transformations Hal Perkins Autumn 2011
The Procedure Abstraction Part V: Run-time Structures for OOLs
CSC 533: Programming Languages Spring 2015
Inlining and Devirtualization Hal Perkins Autumn 2011
Towards JIT compiler for IO language Dynamic mixin optimization
Inlining and Devirtualization Hal Perkins Autumn 2009
More Object-Oriented Programming
Optimizing Transformations Hal Perkins Winter 2008
Support for Object-Oriented Programming
(Computer fundamental Lab)
Data Structures & Algorithms
Course Overview PART I: overview material PART II: inside a compiler
Type Systems Terms to learn about types: Related concepts: Type
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
CSC 533: Programming Languages Spring 2018
Procedure Linkages Standard procedure linkage Procedure has
CSc 453 Interpreters & Interpretation
CSC 533: Programming Languages Spring 2019
Dynamic Binary Translators and Instrumenters
Presentation transcript:

Dynamic Languages Hal Perkins Autumn 2009 CSE P 501 – Compilers Dynamic Languages Hal Perkins Autumn 2009 12/5/2018 © 2002-09 Hal Perkins & UW CSE

References An Efficient Implementation of Self, a dynamically-typed object-oriented language based on prototypes Chambers, Unger, Lee, OOPSLA 1989 Slides by Vijay Menon, CSE 501, Sp09, adapted from slides by Kathleen Fisher 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Dynamic Typing JavaScript: function foo(a, b) { t1 = a.x; // runtime field lookup t2 = b.y(); // runtime method lookup t3 = t1 + t2; // runtime dispatch on ‘+’ return t3; } 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Overview Self JavaScript 20+ year old research language One of earliest JIT compilation systems Pioneered techniques used today JavaScript Self with a Java syntax Much recent work to optimize 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Self Prototype-based pure object-oriented language. Designed by Randall Smith (Xerox PARC) and David Ungar (Stanford University). Successor to Smalltalk-80. “Self: The power of simplicity” appeared at OOPSLA ‘87. Initial implementation done at Stanford; then project shifted to Sun Microsystems Labs. Vehicle for implementation research. Self 4.3 available from Sun web site 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Design Goals Occam’s Razor: Conceptual economy Concreteness Everything is an object. Everything done using messages. No classes No variables Concreteness Objects should seem “real.” GUI to manipulate objects directly 12/5/2018 © 2002-09 Hal Perkins & UW CSE

How successful? Self is a very well-designed language. Few users: not a popular success Not clear why. However, many research innovations Very simple computational model. Enormous advances in compilation techniques. Influenced the design of Java compilers. 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Language Overview Dynamically typed. Everything is an object. All computation via message passing. Creation and initialization done by copying example object. Operations on objects: send messages add new slots replace old slots remove slots 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Objects and Slots Object consists of named slots. Data Assignment Such slots return contents upon evaluation; so act like variables Assignment Set the value of associated slot Method Slot contains Self code Parent References existing object to inherit slots 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Messages and Methods clone … When message is sent, object searched for slot with name. If none found, all parents are searched. Runtime error if more than one parent has a slot with the same name. If slot is found, its contents evaluated and returned. Runtime error if no slot found. parent* print … parent* x 3 x:  12/5/2018 © 2002-09 Hal Perkins & UW CSE

Messages and Methods clone … obj x 3 parent* obj print print point object print … obj x: 4 obj after setting x to 4. parent* x 3 x:  12/5/2018 © 2002-09 Hal Perkins & UW CSE

Mixing State and Behavior parent* … + add points x 4 y 17 x:  parent* y: x random number generator y o parent* y:  12/5/2018 © 2002-09 Hal Perkins & UW CSE

Object Creation To create an object, we copy an old one. We can add new methods, override existing ones, or even remove methods. These operations also apply to parent slots. 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Changing Parent Pointers frog jump … prince dance … eatFly … eatCake … p parent* parent*:  p jump. p eatFly. p parent: prince. p dance. name Charles name:  12/5/2018 © 2002-09 Hal Perkins & UW CSE

Changing Parent Pointers frog jump … prince dance … eatFly … eatCake … p parent* parent*:  p jump. p eatFly. p parent: prince. p dance name Charles name:  12/5/2018 © 2002-09 Hal Perkins & UW CSE

Disadvantages of classes? Classes require programmers to understand a more complex model. To make a new kind of object, we have to create a new class first. To change an object, we have to change the class. Infinite meta-class regression. But: Does Self require programmer to reinvent structure? Common to structure Self programs with traits: objects that simply collect behavior for sharing. 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Contrast with C++ C++ Self Restricts expressiveness to ensure efficient implementation. Self Provides unbreakable high-level model of underlying machine. Compiler does fancy optimizations to obtain acceptable performance. 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Implementation Challenges I Many, many slow function calls: Function calls generally somewhat expensive. Dynamic dispatch makes message invocation even slower than typical procedure calls. OO programs tend to have lots of small methods. Everything is a message: even variable access! “The resulting call density of pure object-oriented programs is staggering, and brings naïve implementations to their knees” [Chambers & Ungar, PLDI 89] 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Implementation Challenges II No static type system Each reference could point to any object, making it hard to find methods statically. No class structure to enforce sharing Each object having a copy of its methods leads to space overheads. Optimized Smalltalk-80 roughly 10 times slower than optimized C. 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Optimization Strategies Avoid per object space requirements. Compile, don’t interpret. Avoid method lookup. Inline methods wherever possible. Saves method call overhead. Enables further optimizations. 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Clone Families Avoid per object data Implementation prototype Mutable map Fixed Info Fixed Model clone family Clone family: objects created from the same prototype. All such objects are identical except for their assignable slots. Factor the common information into a “map.” Representation of an object is the contents of the assignable slots plus a pointer to the shared map object. For each slot, the map contains: . the name of the slot . whether it is a parent . the offset of the associated data in the object if the slot is assignable . the value if it is not assignable. If the user changes a constant slot, then a new map is created for the resulting object. Under the hood, the maps play the role of classes/types from class-based object-oriented languages, but the user does not have to know. Compiler treates the map of an object as its internal type, used intensively during optimization. Mutable Mutable Mutable Mutable Mutable Mutable Mutable Mutable Map Fixed Map Fixed Map Fixed Map Fixed

Dynamic Compilation Avoid interpreting Source Byte Code Machine Code LOAD R0 MOV R1 2 ADD R1 R2 … 01001010010011000100101101000110 First method execution Method is entered Method is converted to byte codes when entered. Compiled to machine code when first executed. Code stored in cache if cache fills, previously compiled method flushed. Requires entire source (byte) code to be available. 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Lookup Cache Avoid method lookup Cache of recently used methods, indexed by (receiver type, message name) pairs. When a message is sent, compiler first consults cache if found: invokes associated code. if absent: performs general lookup and potentially updates cache. Berkeley Smalltalk would have been 37% slower without this optimization. 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Static Type Prediction Avoid method lookup Compiler predicts types that are unknown but likely: Arithmetic operations (+, -, <, etc.) have small integers as their receivers 95% of time in Smalltalk-80. ifTrue had Boolean receiver 100% of the time. Compiler inlines code (and test to confirm guess): if type = smallInt jump to method_smallInt call general_lookup 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Inline Caches First message send from a call site: Avoid method lookup First message send from a call site: general lookup routine invoked call site back-patched is previous method still correct? yes: invoke code directly no: proceed with general lookup & backpatch Successful about 95% of the time All compiled implementations of Smalltalk and Self use inline caches. 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Polymorphic Inline Caches Avoid method lookup Typical call site has <10 distinct receiver types. So often can cache all receivers. At each call site, for each new receiver, extend patch code: After some threshold, revert to simple inline cache (megamorphic site). Order clauses by frequency. Inline short methods into PIC code. if type = rectangle jump to method_rect if type = circle jump to method_circle call general_lookup 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Customized Compilation Inline methods Compile several copies of each method, one for each receiver type. Within each copy: Compiler knows the type of self Calls through self can be statically selected and inlined. Enables downstream optimizations. Increases code size. 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Type Analysis Constructed by compiler by flow analysis. Inline methods Constructed by compiler by flow analysis. Type: set of possible maps for object. Singleton: know map statically Union/Merge: know expression has one of a fixed collection of maps. Unknown: know nothing about expression. If singleton, we can inline method. If type is small, we can insert type test and create branch for each possible receiver (type casing). 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Message Splitting Inline methods Type information above a merge point is often better. Move message send “before” merge point: duplicates code improves type information allows more inlining 12/5/2018 © 2002-09 Hal Perkins & UW CSE

PICS as Type Source Inline methods Polymorphic inline caches build a call-site specific type database as the program runs. Compiler can use this runtime information rather than the result of a static flow analysis to build type cases. Must wait until PIC has collected information. When to recompile? What should be recompiled? Initial fast compile yielding slow code; then dynamically recompile hotspots. 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Performance Improvements Initial version of Self was 4-5 times slower than optimized C. Adding type analysis and message splitting got within a factor of 2 of optimized C. Replacing type analysis with PICS improved performance by further 37%. Current Self compiler is within a factor of 2 of optimized C. 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Impact on Java Sun cancels Self Self with PICs Animorphics Smalltalk Java becomes popular Animorphics Java Java Hotspot Sun buys A.J. 12/5/2018 © 2002-09 Hal Perkins & UW CSE

Summary of Self “Power of simplicity” Everything is an object: no classes, no variables. Provides high-level model that can’t be violated (even during debugging). Fancy optimizations recover reasonable performance. Many techniques now used in Java compilers. Papers describing various optimization techniques available from Self web site. http://research.sun.com/self/ 12/5/2018 © 2002-09 Hal Perkins & UW CSE

JavaScript Self-like language with Java syntax Originated in Netscape Dynamic OO language Prototypes instead of classes Nothing to do with Java beyond syntax Originated in Netscape “Standard” on today’s browsers 12/5/2018 © 2002-09 Hal Perkins & UW CSE

V8 (Google Chrome) Three primary features Fast property access Hidden classes Dynamic compiler Compile on first invocation Inline caching with back patching Generational garbage collection Segmented by types See http://code.google.com/apis/v8/design.html 12/5/2018 © 2002-09 Hal Perkins & UW CSE

High-performance JavaScript Self approach: V8 (Google Chrome) SquirrelFish Extreme (Safari / WebKit) Trace compilation: TraceMonkey (Firefox) Tamarin (Adobe Flash/Flex) No time to cover today; see Tracing for web 3.0, Chang et al, Virtual Execution Env 2009, etc. 12/5/2018 © 2002-09 Hal Perkins & UW CSE