SlideShare a Scribd company logo
Method Shelters :
Another Way to Resolve Class
Extension Conflicts
Classboxes でも Refinements でもない別のやり方




                    Shumpei Akai / 赤井駿平
                                 @flexfrank
What I talk about today
Open Class causes conflicts
 of methods
Method Shelters resolve it!
% whoami
 Shumpei Akai / 赤井駿平
 @flexfrank
 Ph.D. Student / 学生(博士課程)
    ◦ 東工大の千葉研
    ◦ Programming Languages and Moduralization
    ◦ プログラミング言語とモジュール化

   Today’s topic is my current research
Open Class
 Ruby’s one of the important features
 You can (re)define methods in existing
  classes
    ◦ including Object, Integer, Array, …


   Frequently used in Ruby world
in open-uri
   open-uri redefines “open” method
    ◦ It accepts URI
       open("http://penguindrum.jp/"){|f|f.read}
       # => Errno::ENOENT: No such file or directory

       require "open-uri”
       open("http://penguindrum.jp/"){|f|f.read}
       # => "<!DOCTYPE html PUBLIC "-//W3C//DTD
       XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
       transitional.dtd”…”
in Ruby on Rails
   ActiveSupport adds various convenient
    methods to core classes
    ◦ e.g.

    10.megabytes # => 10485760
    1.day.ago # => Sat Jul 16 16:15:00 +0900 2011
    "survival strategy".pluralize #=> "survival strategies"
Or Monkey   Patching
Problem of Open Class
 Methods may conflict
 When One library adds a method
    ◦ and another library adds a method with the
      same name in the same class
   The method defined first is vanished!
    ◦ depends on the order of “require”
    ◦ Confusing!
Method conflicts
   I encountered about five years ago
    ◦ Ruby on Rails and flvtool added a method to
      String
    ◦ they used String as binary/array of int
   Difficult to collaborate these two libraries
    ◦ I separated processes
    ◦ communicating via dRuby
Another example of conflicts
   “mathn” redefines the “Integer#/”
    ◦ returns a Rational object
    ◦ Ordinary code expects “/” returns a integer
      Programs get broken
Existing Solutions
   Several module systems are proposed to
    resolve conflicts
    ◦   Selector namespaces (for Smalltalk)
    ◦   Classboxes (for Smalltalk and Java)
    ◦   Refinements (for Ruby)
    ◦   …
Refinements
   Proposed by Shugo Maeda [ruby-core:33322]


                           class Foo
       module MathN          using MathN
        refine Fixnum do     def foo
         def /(other)         p1/2
          quo(other)         end
          end               end
        end
       end                 f = Foo.new
                           f.foo #=> (1/2)
                           p1/2
Refinements (cont.)
   Refinements changes behavior of
    methods in a lexical scope
    ◦ methods defined by Refinements are not
      enabled in indirectly called methods
    ◦ No local rebinding

   I need local rebinding
    ◦ e.g. scoped monkey patching
Classboxes
   You might have known via matz’s diary
Classboxes
 A classbox restrict the scope of methods
 A classbox can import a class in another
  classbox
    ◦ You can use an imported class
    ◦ You can add/redefine methods to the
      imported class
    ◦ A redefined method can be called from the
      imported class
      Local rebinding property
Classboxes




 Cited from
 “Classbox/J: Controlling the Scope of Change in Java”
The Problem in Classboxes
   Importing overwrites internally used
    classes
    ◦ Importing causes another conflict
The Problem in
     Classboxes

    Redefines                            Original
   Integer#div                           Integer




Uses redefined                       Use original
  Integer#div                          Integer
returns rational




                       Oops!
                   returns integer
I need another module system
   A new module should:
    ◦ have local rebinding
    ◦ provide a way to resolve conflicts cause by
      importing
    ◦ not depends on the order of load
Method Shelters
Key concept

Hide your methods
Hide your imports
What is a method shelter
   A method shelter is a module which
    provides a scope of methods
    ◦ define methods in a method shelter
    ◦ import other method shelters
      You can call methods in the imported shelter
      You can call methods in the shelter which is
       importing the current shelter
        for local rebinding
                                          call          importee


                                                 call
                               importer
A Code with Method Shelters
shelter :MathN do
 class Fixnum # fixed size integer in Ruby
  def /(x)
    Rational(self,x)
  end
 end
end
shelter :Average do
 class Array
  def avg
    sum = self.inject(0){|r,i|r+i}
    sum / self.size
  end
 end
 hide
 import :MathN
end
What conforms a method shelter
   A method shelter is separated into tow
    parts
    ◦ An exposed chamber and a hidden chamber
    ◦ in order to protect internally used methods
    ◦ Each chamber can define methods and import


                 - Exposed   - Hidden
Exposed Chambers                   S0
                                        - Obj#m0
 for public API
 Exposed methods
    ◦ Visible from importer
    ◦ Importer can call or
      redefine exposed             S1
      methods
   Exposedly import
    ◦ Transitive importing
    ◦ Imported methods are
                                   S2
      also visible from importer
Hidden chamber                   S0
                                      - Obj#m1   - Obj#m0
 for internally used
  methods
 Hidden method                  S1
    ◦ invisible in importing
      shelter
   Hiddenly import
                                 S2
    ◦ Imported methods are not
      exposed
Global Methods
   Ordinal methods not in shelters
    ◦ Callable from any shelter
    ◦ Global methods can call methods in a shelter
      if the caller is in the shelter
                                      Global

                                    - Obj#g0




                               S0
                                     obj.g0()
No Ambiguity
   If 2+ methods are found in imported
    shelters                 S3
                                  - C#m0
    ◦ Error!


        S1                   S2
               - C#m0



                        S0
             Error!
Syntax
 I don’t want to edit parse.y
 define methods in a block
    shelter :ShelterName do
     class Foo
      def hoge # <- defined in the method shelter
      end
     end
    end
Syntax: Import
shelter :ShelterName do
  import :AnotherShelterName
end
Syntax: hide
   “hide” method switches a chamber
    ◦ do def or import below “hide”


shelter :ShelterName do
  # exposed chamber
 hide
  # hidden chamber
end
Evaluate
   Evaluate within a shelter

       shelter_eval :ShelterName do
         #shelter is enabled
       end
Method Lookup Algorithm
 1. look up hidden-chamber
 2. look up exposed-chamber
 3. look up global methods


   If not found, go to superclass
8                                   3




             7               6           2




                                 5   1
                     start

Global           9

                                 4
Found!
                          1

Start




        Global
Implementation
 Based on Ruby 1.9.2
 Add one implicit argument to method:
    ◦ A node of method shelter tree
Optimization: Method Cache
   Shelter node cache
    ◦ Caches method entry in a node of shelter
   Extend inline cache
    ◦ Size of an inline cache : 3 word -> 4word
                                   (per method call)
    ◦ Stores the found shelter node
Performance: empty methods
   Call a empty method 10,000,000 times
    ◦ Less than 5% overhead when shelters are
      used
Performance: Fibonacci
   fib(33) in a method shelter
    ◦ Up to 20% overhead
Performance: Ruby on Rails
   Enabled in an action method
    ◦ Numeric#/.*bytes?/ methods are in a shelter
   In the action
    ◦ 1. Call one method in shelter
    ◦ 2. One access to SQLite via ActiveRecord
 on WEBRick
 Rails3
Performance:
             Ruby on Rails (result)
              4% overhead on production env.
              50% on development
                 ◦ Method caches are invalidated per req.
production




development
Cache hit ratio on rails
   Count shelter’s cache hit
Performance: tDiary 3.0.1
   defined
    String#to_a, String#each, String#method_
    missing in a shelter
    ◦ These are used for compatibility of 1.8 & 1.9
 Ran on CGI with apache
 Method shelter improved performance !!
    ◦ Why?
Why shelter made tDiary fast
 String#method_missing issue
 “require” calls its arg’s to_path method if
  defined
    ◦ If arg’s method_missing is defined, try to call
      it
    ◦ String#method_missing slows “require”
   Method shelter restrict its negative effect
Other Usage:
protect optimized methods
   In ruby, + - * / … are optimized
    ◦ only if they are not redefined
    ◦ Redefinition slows programs
 Method shelters can confine effect of
  redefinition
 Method shelter can improve performance
Other Usage:
shelter-private accessor
 Ruby has no private instance variables
 A method shelter can mimic private ivars
    ◦ by generating unique name
    ◦ Accessible within the defined shelter
     and visible shelters
class Module
 def shelter_accessor(name)
  define_method name do
    ivname=get_unique_name(name)
    self.instance_variable_get(ivname)
  end
  define_method( (name.to_s+"=").to_sym) do|val|
    ivname= get_unique_name(name)
    self.instance_variable_set(ivname,val)
  end
 end
end
Conclusion
 Open class is dangerous
 Method shelters resolving conflicts
    ◦ With hidden methods, hiddenly importing
   I implemented in Ruby
    ◦ Not so slow (個人的な感覚)

   For more details or the source code,
    ◦ wait for the acceptance of my paper
      Deadline: 2.days.since
Questions?
時間が余ったら
Global
   In my lookup algorithm,
    ◦ Shelter must have up to one parent

 For simpler semantics
 For efficient implementation
before
         C




     B




         A
after
        C’       C’’




        B




             A

More Related Content

What's hot

Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
Abhijeet Dubey
 
Java basics variables
 Java basics   variables Java basics   variables
Java basics variables
JoeReddieMedia
 
Aspect oriented programming_with_spring
Aspect oriented programming_with_springAspect oriented programming_with_spring
Aspect oriented programming_with_spring
Guo Albert
 
Core java concepts
Core java concepts Core java concepts
Core java concepts
javeed_mhd
 
Core Java
Core JavaCore Java
Core Java
Bharat17485
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
arvind pandey
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
Michael Stal
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in java
Garik Kalashyan
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
Michael Stal
 
Oop2011 actor presentation_stal
Oop2011 actor presentation_stalOop2011 actor presentation_stal
Oop2011 actor presentation_stal
Michael Stal
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
DaisyWatson5
 
201005 accelerometer and core Location
201005 accelerometer and core Location201005 accelerometer and core Location
201005 accelerometer and core Location
Javier Gonzalez-Sanchez
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
Gurpreet singh
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
Ramrao Desai
 
Lecture04 polymorphism
Lecture04 polymorphismLecture04 polymorphism
Lecture04 polymorphism
Hariz Mustafa
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
Anil Bapat
 
A (too) Short Introduction to Scala
A (too) Short Introduction to ScalaA (too) Short Introduction to Scala
A (too) Short Introduction to Scala
Riccardo Cardin
 
First fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionFirst fare 2011 frc-java-introduction
First fare 2011 frc-java-introduction
Oregon FIRST Robotics
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
Ram132
 

What's hot (20)

Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
Java basics variables
 Java basics   variables Java basics   variables
Java basics variables
 
Aspect oriented programming_with_spring
Aspect oriented programming_with_springAspect oriented programming_with_spring
Aspect oriented programming_with_spring
 
Core java concepts
Core java concepts Core java concepts
Core java concepts
 
Core Java
Core JavaCore Java
Core Java
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in java
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
 
Oop2011 actor presentation_stal
Oop2011 actor presentation_stalOop2011 actor presentation_stal
Oop2011 actor presentation_stal
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
 
201005 accelerometer and core Location
201005 accelerometer and core Location201005 accelerometer and core Location
201005 accelerometer and core Location
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Lecture04 polymorphism
Lecture04 polymorphismLecture04 polymorphism
Lecture04 polymorphism
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
 
A (too) Short Introduction to Scala
A (too) Short Introduction to ScalaA (too) Short Introduction to Scala
A (too) Short Introduction to Scala
 
First fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionFirst fare 2011 frc-java-introduction
First fare 2011 frc-java-introduction
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 

Similar to Method Shelters : Another Way to Resolve Class Extension Conflicts

Andy On Closures
Andy On ClosuresAndy On Closures
Andy On Closures
melbournepatterns
 
core_java.ppt
core_java.pptcore_java.ppt
core_java.ppt
YashikaDave
 
The programming philosophy of jrql
The programming philosophy of jrqlThe programming philosophy of jrql
The programming philosophy of jrql
msg systems ag
 
Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012
skinandbones
 
The Naked Bundle - Symfony Usergroup Belgium
The Naked Bundle - Symfony Usergroup BelgiumThe Naked Bundle - Symfony Usergroup Belgium
The Naked Bundle - Symfony Usergroup Belgium
Matthias Noback
 
Legacy Coderetreat @Budapest 2013 02 16
Legacy Coderetreat @Budapest 2013 02 16Legacy Coderetreat @Budapest 2013 02 16
Legacy Coderetreat @Budapest 2013 02 16
Adi Bolboaca
 
Java
JavaJava
Groovy 2 and beyond
Groovy 2 and beyondGroovy 2 and beyond
Groovy 2 and beyond
Guillaume Laforge
 
Java Programming - Inheritance
Java Programming - InheritanceJava Programming - Inheritance
Java Programming - Inheritance
Oum Saokosal
 
inheritance
inheritanceinheritance
inheritance
Mohit Patodia
 
Java inheritance concept, interface, objects, extends
Java inheritance concept, interface, objects, extendsJava inheritance concept, interface, objects, extends
Java inheritance concept, interface, objects, extends
HelariaXavier
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
Sumit Srivastava
 
The Ring programming language version 1.8 book - Part 81 of 202
The Ring programming language version 1.8 book - Part 81 of 202The Ring programming language version 1.8 book - Part 81 of 202
The Ring programming language version 1.8 book - Part 81 of 202
Mahmoud Samir Fayed
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
ParikhitGhosh1
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
Connex
 
The Naked Bundle - Symfony Barcelona
The Naked Bundle - Symfony BarcelonaThe Naked Bundle - Symfony Barcelona
The Naked Bundle - Symfony Barcelona
Matthias Noback
 
The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014
Matthias Noback
 
chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)
It Academy
 
Implementation
ImplementationImplementation
Implementation
adil raja
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 

Similar to Method Shelters : Another Way to Resolve Class Extension Conflicts (20)

Andy On Closures
Andy On ClosuresAndy On Closures
Andy On Closures
 
core_java.ppt
core_java.pptcore_java.ppt
core_java.ppt
 
The programming philosophy of jrql
The programming philosophy of jrqlThe programming philosophy of jrql
The programming philosophy of jrql
 
Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012
 
The Naked Bundle - Symfony Usergroup Belgium
The Naked Bundle - Symfony Usergroup BelgiumThe Naked Bundle - Symfony Usergroup Belgium
The Naked Bundle - Symfony Usergroup Belgium
 
Legacy Coderetreat @Budapest 2013 02 16
Legacy Coderetreat @Budapest 2013 02 16Legacy Coderetreat @Budapest 2013 02 16
Legacy Coderetreat @Budapest 2013 02 16
 
Java
JavaJava
Java
 
Groovy 2 and beyond
Groovy 2 and beyondGroovy 2 and beyond
Groovy 2 and beyond
 
Java Programming - Inheritance
Java Programming - InheritanceJava Programming - Inheritance
Java Programming - Inheritance
 
inheritance
inheritanceinheritance
inheritance
 
Java inheritance concept, interface, objects, extends
Java inheritance concept, interface, objects, extendsJava inheritance concept, interface, objects, extends
Java inheritance concept, interface, objects, extends
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
 
The Ring programming language version 1.8 book - Part 81 of 202
The Ring programming language version 1.8 book - Part 81 of 202The Ring programming language version 1.8 book - Part 81 of 202
The Ring programming language version 1.8 book - Part 81 of 202
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
The Naked Bundle - Symfony Barcelona
The Naked Bundle - Symfony BarcelonaThe Naked Bundle - Symfony Barcelona
The Naked Bundle - Symfony Barcelona
 
The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014
 
chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)
 
Implementation
ImplementationImplementation
Implementation
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
 

Recently uploaded

WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdfWhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
ArgaBisma
 
How RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptxHow RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptx
SynapseIndia
 
find out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challengesfind out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challenges
huseindihon
 
Using LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and MilvusUsing LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and Milvus
Zilliz
 
Data Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining DataData Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining Data
Safe Software
 
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
sunilverma7884
 
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-InTrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc
 
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Torry Harris
 
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
bhumivarma35300
 
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdfBT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
Neo4j
 
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSECHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
kumarjarun2010
 
Recent Advancements in the NIST-JARVIS Infrastructure
Recent Advancements in the NIST-JARVIS InfrastructureRecent Advancements in the NIST-JARVIS Infrastructure
Recent Advancements in the NIST-JARVIS Infrastructure
KAMAL CHOUDHARY
 
WhatsApp Spy Online Trackers and Monitoring Apps
WhatsApp Spy Online Trackers and Monitoring AppsWhatsApp Spy Online Trackers and Monitoring Apps
WhatsApp Spy Online Trackers and Monitoring Apps
HackersList
 
EuroPython 2024 - Streamlining Testing in a Large Python Codebase
EuroPython 2024 - Streamlining Testing in a Large Python CodebaseEuroPython 2024 - Streamlining Testing in a Large Python Codebase
EuroPython 2024 - Streamlining Testing in a Large Python Codebase
Jimmy Lai
 
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes..."Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
Anant Gupta
 
IPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite SolutionIPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite Solution
IPLOOK Networks
 
The Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF GuideThe Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF Guide
Shiv Technolabs
 
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdfAcumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
BrainSell Technologies
 
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyyActive Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
RaminGhanbari2
 
The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...
The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...
The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...
digitalxplive
 

Recently uploaded (20)

WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdfWhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
 
How RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptxHow RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptx
 
find out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challengesfind out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challenges
 
Using LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and MilvusUsing LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and Milvus
 
Data Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining DataData Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining Data
 
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
 
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-InTrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
 
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
 
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
 
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdfBT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
 
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSECHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
 
Recent Advancements in the NIST-JARVIS Infrastructure
Recent Advancements in the NIST-JARVIS InfrastructureRecent Advancements in the NIST-JARVIS Infrastructure
Recent Advancements in the NIST-JARVIS Infrastructure
 
WhatsApp Spy Online Trackers and Monitoring Apps
WhatsApp Spy Online Trackers and Monitoring AppsWhatsApp Spy Online Trackers and Monitoring Apps
WhatsApp Spy Online Trackers and Monitoring Apps
 
EuroPython 2024 - Streamlining Testing in a Large Python Codebase
EuroPython 2024 - Streamlining Testing in a Large Python CodebaseEuroPython 2024 - Streamlining Testing in a Large Python Codebase
EuroPython 2024 - Streamlining Testing in a Large Python Codebase
 
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes..."Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
 
IPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite SolutionIPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite Solution
 
The Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF GuideThe Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF Guide
 
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdfAcumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
 
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyyActive Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
 
The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...
The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...
The Rise of AI in Cybersecurity How Machine Learning Will Shape Threat Detect...
 

Method Shelters : Another Way to Resolve Class Extension Conflicts

  • 1. Method Shelters : Another Way to Resolve Class Extension Conflicts Classboxes でも Refinements でもない別のやり方 Shumpei Akai / 赤井駿平 @flexfrank
  • 2. What I talk about today Open Class causes conflicts of methods Method Shelters resolve it!
  • 3. % whoami  Shumpei Akai / 赤井駿平  @flexfrank  Ph.D. Student / 学生(博士課程) ◦ 東工大の千葉研 ◦ Programming Languages and Moduralization ◦ プログラミング言語とモジュール化  Today’s topic is my current research
  • 4. Open Class  Ruby’s one of the important features  You can (re)define methods in existing classes ◦ including Object, Integer, Array, …  Frequently used in Ruby world
  • 5. in open-uri  open-uri redefines “open” method ◦ It accepts URI open("http://penguindrum.jp/"){|f|f.read} # => Errno::ENOENT: No such file or directory require "open-uri” open("http://penguindrum.jp/"){|f|f.read} # => "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd”…”
  • 6. in Ruby on Rails  ActiveSupport adds various convenient methods to core classes ◦ e.g. 10.megabytes # => 10485760 1.day.ago # => Sat Jul 16 16:15:00 +0900 2011 "survival strategy".pluralize #=> "survival strategies"
  • 7. Or Monkey Patching
  • 8. Problem of Open Class  Methods may conflict  When One library adds a method ◦ and another library adds a method with the same name in the same class  The method defined first is vanished! ◦ depends on the order of “require” ◦ Confusing!
  • 9. Method conflicts  I encountered about five years ago ◦ Ruby on Rails and flvtool added a method to String ◦ they used String as binary/array of int  Difficult to collaborate these two libraries ◦ I separated processes ◦ communicating via dRuby
  • 10. Another example of conflicts  “mathn” redefines the “Integer#/” ◦ returns a Rational object ◦ Ordinary code expects “/” returns a integer  Programs get broken
  • 11. Existing Solutions  Several module systems are proposed to resolve conflicts ◦ Selector namespaces (for Smalltalk) ◦ Classboxes (for Smalltalk and Java) ◦ Refinements (for Ruby) ◦ …
  • 12. Refinements  Proposed by Shugo Maeda [ruby-core:33322] class Foo module MathN using MathN refine Fixnum do def foo def /(other) p1/2 quo(other) end end end end end f = Foo.new f.foo #=> (1/2) p1/2
  • 13. Refinements (cont.)  Refinements changes behavior of methods in a lexical scope ◦ methods defined by Refinements are not enabled in indirectly called methods ◦ No local rebinding  I need local rebinding ◦ e.g. scoped monkey patching
  • 14. Classboxes  You might have known via matz’s diary
  • 15. Classboxes  A classbox restrict the scope of methods  A classbox can import a class in another classbox ◦ You can use an imported class ◦ You can add/redefine methods to the imported class ◦ A redefined method can be called from the imported class  Local rebinding property
  • 16. Classboxes Cited from “Classbox/J: Controlling the Scope of Change in Java”
  • 17. The Problem in Classboxes  Importing overwrites internally used classes ◦ Importing causes another conflict
  • 18. The Problem in Classboxes Redefines Original Integer#div Integer Uses redefined Use original Integer#div Integer returns rational Oops! returns integer
  • 19. I need another module system  A new module should: ◦ have local rebinding ◦ provide a way to resolve conflicts cause by importing ◦ not depends on the order of load
  • 21. Key concept Hide your methods Hide your imports
  • 22. What is a method shelter  A method shelter is a module which provides a scope of methods ◦ define methods in a method shelter ◦ import other method shelters  You can call methods in the imported shelter  You can call methods in the shelter which is importing the current shelter  for local rebinding call importee call importer
  • 23. A Code with Method Shelters shelter :MathN do class Fixnum # fixed size integer in Ruby def /(x) Rational(self,x) end end end shelter :Average do class Array def avg sum = self.inject(0){|r,i|r+i} sum / self.size end end hide import :MathN end
  • 24. What conforms a method shelter  A method shelter is separated into tow parts ◦ An exposed chamber and a hidden chamber ◦ in order to protect internally used methods ◦ Each chamber can define methods and import - Exposed - Hidden
  • 25. Exposed Chambers S0 - Obj#m0  for public API  Exposed methods ◦ Visible from importer ◦ Importer can call or redefine exposed S1 methods  Exposedly import ◦ Transitive importing ◦ Imported methods are S2 also visible from importer
  • 26. Hidden chamber S0 - Obj#m1 - Obj#m0  for internally used methods  Hidden method S1 ◦ invisible in importing shelter  Hiddenly import S2 ◦ Imported methods are not exposed
  • 27. Global Methods  Ordinal methods not in shelters ◦ Callable from any shelter ◦ Global methods can call methods in a shelter if the caller is in the shelter Global - Obj#g0 S0 obj.g0()
  • 28. No Ambiguity  If 2+ methods are found in imported shelters S3 - C#m0 ◦ Error! S1 S2 - C#m0 S0 Error!
  • 29. Syntax  I don’t want to edit parse.y  define methods in a block shelter :ShelterName do class Foo def hoge # <- defined in the method shelter end end end
  • 30. Syntax: Import shelter :ShelterName do import :AnotherShelterName end
  • 31. Syntax: hide  “hide” method switches a chamber ◦ do def or import below “hide” shelter :ShelterName do # exposed chamber hide # hidden chamber end
  • 32. Evaluate  Evaluate within a shelter shelter_eval :ShelterName do #shelter is enabled end
  • 33. Method Lookup Algorithm  1. look up hidden-chamber  2. look up exposed-chamber  3. look up global methods  If not found, go to superclass
  • 34. 8 3 7 6 2 5 1 start Global 9 4
  • 35. Found! 1 Start Global
  • 36. Implementation  Based on Ruby 1.9.2  Add one implicit argument to method: ◦ A node of method shelter tree
  • 37. Optimization: Method Cache  Shelter node cache ◦ Caches method entry in a node of shelter  Extend inline cache ◦ Size of an inline cache : 3 word -> 4word (per method call) ◦ Stores the found shelter node
  • 38. Performance: empty methods  Call a empty method 10,000,000 times ◦ Less than 5% overhead when shelters are used
  • 39. Performance: Fibonacci  fib(33) in a method shelter ◦ Up to 20% overhead
  • 40. Performance: Ruby on Rails  Enabled in an action method ◦ Numeric#/.*bytes?/ methods are in a shelter  In the action ◦ 1. Call one method in shelter ◦ 2. One access to SQLite via ActiveRecord  on WEBRick  Rails3
  • 41. Performance: Ruby on Rails (result)  4% overhead on production env.  50% on development ◦ Method caches are invalidated per req. production development
  • 42. Cache hit ratio on rails  Count shelter’s cache hit
  • 43. Performance: tDiary 3.0.1  defined String#to_a, String#each, String#method_ missing in a shelter ◦ These are used for compatibility of 1.8 & 1.9  Ran on CGI with apache  Method shelter improved performance !! ◦ Why?
  • 44. Why shelter made tDiary fast  String#method_missing issue  “require” calls its arg’s to_path method if defined ◦ If arg’s method_missing is defined, try to call it ◦ String#method_missing slows “require”  Method shelter restrict its negative effect
  • 45. Other Usage: protect optimized methods  In ruby, + - * / … are optimized ◦ only if they are not redefined ◦ Redefinition slows programs  Method shelters can confine effect of redefinition  Method shelter can improve performance
  • 46. Other Usage: shelter-private accessor  Ruby has no private instance variables  A method shelter can mimic private ivars ◦ by generating unique name ◦ Accessible within the defined shelter and visible shelters
  • 47. class Module def shelter_accessor(name) define_method name do ivname=get_unique_name(name) self.instance_variable_get(ivname) end define_method( (name.to_s+"=").to_sym) do|val| ivname= get_unique_name(name) self.instance_variable_set(ivname,val) end end end
  • 48. Conclusion  Open class is dangerous  Method shelters resolving conflicts ◦ With hidden methods, hiddenly importing  I implemented in Ruby ◦ Not so slow (個人的な感覚)  For more details or the source code, ◦ wait for the acceptance of my paper  Deadline: 2.days.since
  • 52. In my lookup algorithm, ◦ Shelter must have up to one parent  For simpler semantics  For efficient implementation
  • 53. before C B A
  • 54. after C’ C’’ B A