Author Alexandre Sikiaridis Masters Student - EPFL Supervised by Dmitry Petrashko Martin Odersky Method Type Specialisation in Dotty Implementing Method Type Specialisation In Dotty T
Outline • What is specialisation? • Method Specialisation - Implementation in Dotty • Future Developments Method Type Specialisation in Dotty 1/15
What is Specialisation? • Scala compiles parametric polymorphism away through boxing • This impacts runtime performance negatively Method Type Specialisation in Dotty 2/15
What is Specialisation? • Scala compiles parametric polymorphism away through boxing • This impacts runtime performance negatively Method Type Specialisation in Dotty 2/15
What is Specialisation? Method Type Specialisation in Dotty 2/15 Compiled with scalac
What is Specialisation? • Specialisation is an optimisation option • It avoids boxing by generating ’’ specialised ’’ copies of methods and classes Method Type Specialisation in Dotty 3/15
What is Specialisation? • Specialisation is an optimisation option • It avoids boxing by generating ’’ specialised ’’ copies of methods and classes Method Type Specialisation in Dotty @specialised(Int) 3/15
What is Specialisation? Method Type Specialisation in Dotty 3/15 Compiled with scalac
What is Specialisation? • Beware! Specialisation may result in significant code-size increase • Creates 10 copies per type parameter (9 primitive types) – so 1’000 clones of methods like Function3 • “The greatest shortcoming of the human race is our inability to Method Type Specialisation in Dotty understand the exponential function .” ~ Albert A. Bartlett 4/15
What is Specialisation? • Beware! Specialisation may result in significant code-size increase • Creates 10 copies per type parameter (9 primitive types) – so 1’000 clones of methods like Function3 • “The greatest shortcoming of the human race is our inability to Method Type Specialisation in Dotty understand the exponential function .” ~ Albert A. Bartlett 4/15
Implementation in Dotty Method Type 5/15 Specialisation in Dotty
Implementation in Dotty • Can be divided into four steps • Annotations retrieval Method Type Specialisation in Dotty • Symbols generation • Trees translation • Method calls dispatching 6/15
Implementation in Dotty • Can be divided into four steps • Annotations retrieval PreSpecializer Method Type Specialisation in Dotty • Symbols generation DenotTransformer • Trees translation TypeSpecializer • Method calls dispatching 6/15
Annotations Retrieval • Specialisation is done on- demand • Either user-provided, requested by an earlier phase Method Type (e.g. Linker) or triggered with Specialisation in Dotty ‘’ -Yspecialize:all ’’ 7/15
Annotations Retrieval • Specialisation is done on- demand • Either user-provided, requested by an earlier phase Method Type (e.g. Linker) or triggered with Specialisation in Dotty ‘’ -Yspecialize:all ’’ • @specialized annotations trigger the optimisation • Parameters to the annotation can be Types or Specializable Groups 7/15
Annotations Retrieval • Specialisation is done on- demand • Either user-provided, requested by an earlier phase Method Type (e.g. Linker) or triggered with Specialisation in Dotty ‘’ -Yspecialize:all ’’ • @specialized annotations trigger the optimisation • Parameters to the annotation can be Types or Specializable Groups • Those have to be retrieved 7/15 upfront
Annotations Retrieval • Specialisation types are passed along to TypeSpecializer with the concerned method’s symbol, through a PhaseCache Method Type Specialisation in Dotty • Requests are later stored by TypeSpecializer in a map from symbol to types list 8/15
Symbols Generation • TypeSpecializer triggers the generation of specialised symbols when appropriate • Produces the combinatations of primitive types indicated by PreSpecializer Method Type Specialisation in Dotty • Calls DenotTransformer for concrete generation of Symbols 9/15
Symbols Generation • Dotty performs caching for subtypes • It holds ’’ Frozen ’’ Flags to ensure transformations are transferred to subtypes • Symbols must be generated during transformation of the Method Type Specialisation in Dotty denotation of the owner • They can then be entered in their owner’s scope • Method declarations outside of classes can be specialised without those precautions 10/15
Trees Translation • TypeSpecializer goes through all DefDef ’s • If it finds one that has been requested for Method Type Specialisation in Dotty specialisation: • It creates a PolyDefDef remembering TypeBounds • It instantiates a TreeTypeMap handling recursive translation of the 11/15 RHS
Incomplete Type Information (Refers to issue #592 | www.github.com/lampepfl/dotty/issues/592) • In some cases, relying on type inference fails and creates TypeMismatch errors Method Type Specialisation in Dotty • From A<:B and B<:Int , typechecker does not infer A<:Int • Returning Foo[A] does not fit the expected Foo[Int] • Elected solution: type casts 12/15
Incomplete Type Information 12/15 Method Type Specialisation in Dotty
Method Calls Dispatching • Method calls are compared to generated specialised variants • If several variants are available, best fitting one is determined by comparing argument types at call site and those of the Method Type specialised variant Specialisation in Dotty • If not all types fit, method call remains generic 13/15
Future Developments • Partial Specialisation Method Type Specialisation in Dotty • ’’ What if I only want to specialise T ?’’ • A prototype exists, but needs some debugging still • Class Specialisation 14/15 • More testing
A ’’ Special ’’ Thank You Method Type Specialisation in Dotty I. Dragos Type Specialization in Scala 2.8 [http://www.scala-lang.org/old/sites/default/files/sids/dragos/Thu,%202010-05- 15/15 06,%2017:56/sid-spec.pdf]
Recommend
More recommend