Implementing Method Type Specialisation In Dotty T Outline What - - PowerPoint PPT Presentation

implementing
SMART_READER_LITE
LIVE PREVIEW

Implementing Method Type Specialisation In Dotty T Outline What - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Method Type Specialisation in Dotty

T

Implementing Method Type Specialisation In Dotty

Author Alexandre Sikiaridis Masters Student - EPFL Supervised by Dmitry Petrashko Martin Odersky

slide-2
SLIDE 2

Outline

  • What is specialisation?
  • Method Specialisation - Implementation in Dotty
  • Future Developments

1/15

Method Type Specialisation in Dotty

slide-3
SLIDE 3

What is Specialisation?

2/15

  • Scala compiles parametric polymorphism away through

boxing

  • This impacts runtime performance negatively

Method Type Specialisation in Dotty

slide-4
SLIDE 4

What is Specialisation?

2/15

  • Scala compiles parametric polymorphism away through

boxing

  • This impacts runtime performance negatively

Method Type Specialisation in Dotty

slide-5
SLIDE 5

What is Specialisation?

Method Type Specialisation in Dotty

2/15

Compiled with scalac

slide-6
SLIDE 6

What is Specialisation?

3/15

  • Specialisation is an optimisation option
  • It avoids boxing by generating ’’specialised’’ copies of methods

and classes

Method Type Specialisation in Dotty

slide-7
SLIDE 7

What is Specialisation?

3/15

  • Specialisation is an optimisation option
  • It avoids boxing by generating ’’specialised’’ copies of methods

and classes

Method Type Specialisation in Dotty

@specialised(Int)

slide-8
SLIDE 8

What is Specialisation?

Method Type Specialisation in Dotty

3/15

Compiled with scalac

slide-9
SLIDE 9

What is Specialisation?

4/15

  • 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

understand the exponential function.” ~ Albert A. Bartlett

Method Type Specialisation in Dotty

slide-10
SLIDE 10

What is Specialisation?

4/15

  • 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

understand the exponential function.” ~ Albert A. Bartlett

Method Type Specialisation in Dotty

slide-11
SLIDE 11

Implementation in Dotty

5/15

Method Type Specialisation in Dotty

slide-12
SLIDE 12

Implementation in Dotty

  • Can be divided into four steps
  • Annotations retrieval
  • Symbols generation
  • Trees translation
  • Method calls dispatching

6/15

Method Type Specialisation in Dotty

slide-13
SLIDE 13

Implementation in Dotty

  • Can be divided into four steps
  • Annotations retrieval
  • Symbols generation
  • Trees translation
  • Method calls dispatching

6/15

Method Type Specialisation in Dotty

PreSpecializer TypeSpecializer DenotTransformer

slide-14
SLIDE 14

Annotations Retrieval

7/15

Method Type Specialisation in Dotty

  • Specialisation is done on-

demand

  • Either user-provided,

requested by an earlier phase (e.g. Linker) or triggered with ‘’-Yspecialize:all’’

slide-15
SLIDE 15

Annotations Retrieval

7/15

Method Type Specialisation in Dotty

  • Specialisation is done on-

demand

  • Either user-provided,

requested by an earlier phase (e.g. Linker) or triggered with ‘’-Yspecialize:all’’

  • @specialized annotations

trigger the optimisation

  • Parameters to the annotation

can be Types or

Specializable Groups

slide-16
SLIDE 16

Annotations Retrieval

7/15

Method Type Specialisation in Dotty

  • Specialisation is done on-

demand

  • Either user-provided,

requested by an earlier phase (e.g. Linker) or triggered with ‘’-Yspecialize:all’’

  • @specialized annotations

trigger the optimisation

  • Parameters to the annotation

can be Types or

Specializable Groups

  • Those have to be retrieved

upfront

slide-17
SLIDE 17

Annotations Retrieval

8/15

Method Type Specialisation in Dotty

  • Specialisation types are passed along to TypeSpecializer with the

concerned method’s symbol, through a PhaseCache

  • Requests are later stored by TypeSpecializer in a map from symbol to

types list

slide-18
SLIDE 18

Symbols Generation

9/15

Method Type Specialisation in Dotty

  • TypeSpecializer triggers the generation of specialised

symbols when appropriate

  • Produces the combinatations of primitive types indicated by

PreSpecializer

  • Calls DenotTransformer for concrete generation of Symbols
slide-19
SLIDE 19

Symbols Generation

10/15

Method Type Specialisation in Dotty

  • Dotty performs caching for subtypes
  • It holds ’’Frozen’’ Flags to ensure transformations are transferred

to subtypes

  • Symbols must be generated during transformation of the

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

slide-20
SLIDE 20

Trees Translation

11/15

Method Type Specialisation in Dotty

  • TypeSpecializer goes

through all DefDef’s

  • If it finds one that has been

requested for specialisation:

  • It creates a PolyDefDef

remembering TypeBounds

  • It instantiates a

TreeTypeMap handling

recursive translation of the RHS

slide-21
SLIDE 21

Incomplete Type Information

12/15

Method Type Specialisation in Dotty

  • In some cases, relying on type inference fails and creates

TypeMismatch errors

  • 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

(Refers to issue #592 | www.github.com/lampepfl/dotty/issues/592)

slide-22
SLIDE 22

Incomplete Type Information

12/15

Method Type Specialisation in Dotty

slide-23
SLIDE 23

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 specialised variant

  • If not all types fit, method call remains generic

13/15

Method Type Specialisation in Dotty

slide-24
SLIDE 24

Future Developments

14/15

Method Type Specialisation in Dotty

  • Partial Specialisation
  • ’’What if I only want to specialise T?’’
  • A prototype exists, but needs some debugging still
  • Class Specialisation
  • More testing
slide-25
SLIDE 25

A ’’Special’’ Thank You

  • I. Dragos

Type Specialization in Scala 2.8

[http://www.scala-lang.org/old/sites/default/files/sids/dragos/Thu,%202010-05- 06,%2017:56/sid-spec.pdf]

15/15

Method Type Specialisation in Dotty