Method Shelters: Avoiding Conflicts among Class Extensions Caused by Local Rebinding Shumpei Akai, Shigeru Chiba Tokyo Institute of Technology 1.

Slides:



Advertisements
Similar presentations
Homework Answers 4. 2t – 8 = (m + n) = 7n 6. A = 1/2bh
Advertisements

1 VBScript Session What we learn last session?
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Advanced Piloting Cruise Plot.
Kapitel S3 Astronomie Autor: Bennett et al. Raumzeit und Gravitation Kapitel S3 Raumzeit und Gravitation © Pearson Studium 2010 Folie: 1.
Final and Abstract Classes
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Chapter 1 The Study of Body Function Image PowerPoint
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
UNITED NATIONS Shipment Details Report – January 2006.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
Arithmetic and Geometric Means
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
0 - 0.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Year 6 mental test 10 second questions
SYMMETRIC CRYPTOSYSTEMS Symmetric Cryptosystems 6/05/2014 | pag. 2.
Richmond House, Liverpool (1) 26 th January 2004.
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
Data Structures Using C++
ABC Technology Project
Hash Tables.
1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
Symbol Table.
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
VOORBLAD.
15. Oktober Oktober Oktober 2012.
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
“Start-to-End” Simulations Imaging of Single Molecules at the European XFEL Igor Zagorodnov S2E Meeting DESY 10. February 2014.
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
Copyright © 2013, 2009, 2006 Pearson Education, Inc.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
1..
1 Advanced C Programming from Expert C Programming: Deep C Secrets by Peter van der Linden CIS*2450 Advanced Programming Concepts.
© 2012 National Heart Foundation of Australia. Slide 2.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Chapter 5 Test Review Sections 5-1 through 5-4.
1 of 32 Images from Africa. 2 of 32 My little Haitian friend Antoine (1985)
25 seconds left…...
U1A L1 Examples FACTORING REVIEW EXAMPLES.
Take out the homework from last night then do, Warm up #1
Januar MDMDFSSMDMDFSSS
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
PSSA Preparation.
Immunobiology: The Immune System in Health & Disease Sixth Edition
Chapter 11 Component-Level Design
CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Immunobiology: The Immune System in Health & Disease Sixth Edition
CpSc 3220 Designing a Database
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
Data Structures Using C++ 2E
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Presentation transcript:

Method Shelters: Avoiding Conflicts among Class Extensions Caused by Local Rebinding Shumpei Akai, Shigeru Chiba Tokyo Institute of Technology 1

Class Extensions Destructively change method definitions in the existing classes ◦ Write new definitions in a separate file Available in ◦ Smalltalk, Objective-C, AspectJ … ◦ and Ruby  (called Open Class) 2

Class Extensions are popular in Ruby Ruby on Rails aggressively adds methods to built-in classes ◦ Class extensions are used in real applications ◦ e.g. 10.kilobytes # => NoMethodError: undefined method require “active_record” #load rails’ lib 10.kilobytes # => kilobytes # => NoMethodError: undefined method require “active_record” #load rails’ lib 10.kilobytes # =>

Conflicts among Class Extensions Class extensions are dangerous ◦ Cause conflicts, of course Ruby allows class extensions ◦ Ruby on Rails aggressively use ◦ Serious issue in Ruby ◦ Scope of class extensions are needed 4

What is the appropriate scope of class extensions? Global? ◦ Same as Ruby Lexical? ◦ Redefinitions are available in a lexical scope Local rebinding property (of Classbox *)? ◦ Redefine methods in another module by importing ◦ Redefinition is limited in imported module 5 * [‘05 Bergel et al.]

Global Scopes 6 List avg(): average of elems using div Integer div(): return rational Integer div(): return integer plus(): … minus(): … 1.div(2) #=> (1/2) Redefine destructively Module 1 Module 2 Module 3 No one can use original div()

Lexical Scopes 7 List avg(): average of elems using div Integer div(): return rational Integer div(): return integer plus(): … minus(): … Redefinition in Lexical scope Module 1 Module 2 Module 3 Cannot reuse redefined div() [1, 2].avg() #=> (3/2) 1.div(2) #=> 0 [1, 2].avg() #=> (3/2) 1.div(2) #=> 0 [1, 2].avg() #=> (3/2) 1.div(2) #=> 0 [1, 2].avg() #=> (3/2) 1.div(2) #=> 0

Local rebinding (Classbox) 8 List avg(): average of elems using div Integer div(): return rational Integer div(): return integer plus(): … minus(): … 1.div(2) #=> 0 Module 1 Module 2 Module 3 import [1, 2].avg() #=> (3/2) 1.div(2) #=> (1/2) [1, 2].avg() #=> (3/2) 1.div(2) #=> (1/2) Module 4 Redefinition in importing chain Original div() Redefined div()

Local rebinding (Classbox) 9 List avg(): average of elems using div Integer div(): return rational Integer div(): return integer plus(): … minus(): … [1, 2].avg() #=> (3/2) 1.div(2) #=> Conflicts [1, 2].avg() #=> (3/2) 1.div(2) #=> Conflicts Module 1 Module 2 Module 3 1.div(2) #=> (1/2) [1, 2].avg() #=> (3/2) 1.div(2) #=> (1/2) [1, 2].avg() #=> (3/2) Module 4

Our proposal: Method Shelters A method shelter is a module which provides a way to control scopes of class extensions ◦ 2 mechanisms  Preserves local rebinding  Or redefine methods in limited scope Based on Ruby 10

A Code with Method Shelters shelter :MathN do class Fixnum # fixed size integer in Ruby def /(x) Rational(self,x) end shelter :Average do class Array def avg sum = self.inject(0){|r,i|r+i} sum / self.size end hide import :MathN end shelter :MathN do class Fixnum # fixed size integer in Ruby def /(x) Rational(self,x) end shelter :Average do class Array def avg sum = self.inject(0){|r,i|r+i} sum / self.size end hide import :MathN end 11

Chambers in a method shelter A method shelter has two parts ◦ An exposed chamber and a hidden chamber ◦ Each chamber contains methods and “import”s ◦ Exposed : for public APIs (similar to public) ◦ Hidden : for internal use (similar to protected) - Obj#m0 S0 Exposed Hidden 12 Import

Exposed Chambers for public API ◦ Local rebinding Methods ◦ Visible from importer Import ◦ Imported methods are also visible from importer - Obj#m0 S0 S1 S2 import 13 Call/redefine

Hidden chamber for internally used methods ◦ Not called/redefined from importer Method ◦ Visible only from the same shelter Import ◦ Imported methods are not visible from importer - Obj#m1 - Obj#m0 S0 S1 S2 14 Call import

Method Lookup Algorithm Contexts : ◦ (class, method name, current shelter) Search for methods as follows: ◦ 1. look up the current shelter’s hidden- chamber and its importing shelters ◦ 2. look up the current shelter’s exposed- chamber and its importing shelters ◦ 3.If not found, go to the superclass 15

- m1 - m0 current First, look up this group Second, 16 *Look up from importer

Detect Ambiguity If you use exposed chambers, it may cause conflicts ◦ Detects and raises an error S0 - C#m0 S1 S2 Error! - C#m0 S3 17

Syntax We have not modified syntax ◦ Ruby has powerful syntax ◦ Use ordinal methods with a block shelter :ShelterName do class Foo def hoge # <- defined in the method shelter end shelter :ShelterName do class Foo def hoge # <- defined in the method shelter end 18

Syntax: Import shelter :ShelterName do import :AnotherShelterName end shelter :ShelterName do import :AnotherShelterName end 19

Syntax: hide “hide” method switches a chamber ◦ Methods and imports below “hide” are in the hidden chamber shelter :ShelterName do # exposed chamber hide # hidden chamber end shelter :ShelterName do # exposed chamber hide # hidden chamber end 20

Example of method shelters 21 List avg(): average of elems using div Integer div(): return rational Integer div(): return integer plus(): … minus(): … Shelter1 Sheleter 2 Sheleter 3 [1, 2].avg() #=> (3/2) 1.div(2) #=> 0 [1, 2].avg() #=> (3/2) 1.div(2) #=> 0 Shelter 4

Example of method shelters 22 List avg(): average of elems using div Integer div(): return rational Integer div(): return integer plus(): … minus(): … Shelter1 Sheleter 2 Sheleter 3 [1, 2].avg() #=> (3/2) 1.div(2) #=> (1/2) [1, 2].avg() #=> (3/2) 1.div(2) #=> (1/2) Shelter 4

Implementation Based on Ruby Add one implicit argument to method: ◦ Current method shelter Optimize method-lookup caches ◦ Shelter node cache  Caches method body in a shelter ◦ Extend inline cache  Stores the found shelter  Size of an inline cache : 3 word -> 4word (per method call) 23

Micro benchmark : empty methods Micro benchmark : empty methods Call an empty method in shelter 10,000,000 times ◦ Less than 5% overhead when shelters are used 24

Micro benchmark : Ruby on Rails Enabled shelters in an action method ◦ Numeric#kilobytes method in a shelter In the action method ◦ 1. Call kilobytes method in shelter ◦ 2. One access to SQLite 4% overhead 25

Related Work Refinements (for Ruby) ◦ Provide a scope of methods ◦ Redefined methods are available in lexical scope  No local rebinding Classboxes [‘05 Bergel et al.] ◦ A classbox provides the scope of class extensions ◦ Introduce Local rebinding property 26

Conclusion Method shelters provide 2 mechanisms for implementing scope of class extensions ◦ Exposed and Hidden ◦ You can control scopes of class extensions by combining them ◦ Avoid conflicts Implementation on Ruby ◦ reasonable overhead by caches 27

Evaluate Should specify the initial method shelter ◦ Evaluate within a shelter shelter_eval :ShelterName do #shelter is enabled end shelter_eval :ShelterName do #shelter is enabled end 28

Global Methods Methods not in shelters ◦ Callable from any shelter ◦ Global methods can call methods in a shelter if the caller is in the shelter  To preserve local rebinding obj.g0() S0 - Obj#g0 Global 29