SlideShare una empresa de Scribd logo
1 de 22
Descargar para leer sin conexión
MANUAL SPECIALIZATION
Szymon Matejczyk
@szymonmatejczyk
ACT 1:
WORLD RUINED
HIPSTERVS. OLDSCHOOL
def hipsterMin(a: Array[Int]): Int = {

a.foldLeft(Int.MaxValue){ _.min(_)}

}
def oldschoolMin(a: Array[Int]): Int = {

var min = Int.MaxValue

var i = 0

while (i < a.length) {

min = if (a(i) < min) a(i) else min

i += 1

}

min

}
HIPSTERVS. OLDSCHOOL
def hipsterMin(a: Array[Int]): Int = {

a.foldLeft(Int.MaxValue){ _.min(_)}

}
def oldschoolMin(a: Array[Int]): Int = {

var min = Int.MaxValue

var i = 0

while (i < a.length) {

min = if (a(i) < min) a(i) else min

i += 1

}

min

}
Running time
967722
123424
ACT 1I:
DENIAL
OLDSCHOOL
public int oldschoolMin(int[]);

Code:

0: ldc #72 // int 2147483647

2: istore_2

3: iconst_0

4: istore_3

5: iload_3

6: aload_1

7: arraylength

8: if_icmpge 33

11: aload_1

12: iload_3

13: iaload

14: iload_2

15: if_icmpge 24

18: aload_1

19: iload_3

20: iaload

21: goto 25

24: iload_2

25: istore_2

26: iload_3

27: iconst_1

28: iadd

29: istore_3

30: goto 5

33: iload_2

34: ireturn
HIPSTER
public int hipsterMin(int[]);

Code:

0: getstatic #67 // Field scala/Predef$.MODULE
$:Lscala/Predef$;

3: aload_1

4: invokevirtual #71 // Method scala/Predef
$.intArrayOps:([I)Lscala/collection/mutable/ArrayOps;

7: ldc #72 // int 2147483647

9: invokestatic #78 // Method scala/runtime/
BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;

12: new #80 // class szymon/Test$$anonfun
$hipsterMin$1

15: dup

16: aload_0

17: invokespecial #83 // Method szymon/Test$$anonfun
$hipsterMin$1."<init>":(Lszymon/Test;)V

20: invokeinterface #89, 3 // InterfaceMethod scala/
collection/mutable/ArrayOps.foldLeft:(Ljava/lang/Object;Lscala/
Function2;)Ljava/lang/Object;

25: invokestatic #93 // Method scala/runtime/
BoxesRunTime.unboxToInt:(Ljava/lang/Object;)I

28: ireturn
HIPSTER
public final class szymon.Test$$anonfun$hipsterMin$1 extends scala.runtime.AbstractFunction2$mcIII$sp
implements scala.Serializable {

public static final long serialVersionUID;



public final int apply(int, int);

Code:

0: aload_0

1: iload_1

2: iload_2

3: invokevirtual #21 // Method apply$mcIII$sp:(II)I

6: ireturn



public int apply$mcIII$sp(int, int);

Code:

0: getstatic #32 // Field scala/runtime/RichInt$.MODULE$:Lscala/runtime/RichInt$;

3: getstatic #37 // Field scala/Predef$.MODULE$:Lscala/Predef$;

6: iload_1

7: invokevirtual #41 // Method scala/Predef$.intWrapper:(I)I

10: iload_2

11: invokevirtual #44 // Method scala/runtime/RichInt$.min$extension:(II)I

14: ireturn



public final java.lang.Object apply(java.lang.Object, java.lang.Object);

Code:

0: aload_0

1: aload_1

2: invokestatic #51 // Method scala/runtime/BoxesRunTime.unboxToInt:(Ljava/lang/
Object;)I

5: aload_2

6: invokestatic #51 // Method scala/runtime/BoxesRunTime.unboxToInt:(Ljava/lang/
Object;)I

9: invokevirtual #53 // Method apply:(II)I

12: invokestatic #57 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/
Integer;

15: areturn



public szymon.Test$$anonfun$hipsterMin$1(szymon.Test);

Code:

0: aload_0

1: invokespecial #65 // Method scala/runtime/AbstractFunction2$mcIII$sp."<init>":()V

4: return
BOXING


public int apply$mcIII$sp(int, int);

Code:

0: getstatic #32 // Field scala/runtime/RichInt
$.MODULE$:Lscala/runtime/RichInt$;

3: getstatic #37 // Field scala/Predef$.MODULE
$:Lscala/Predef$;

6: iload_1

7: invokevirtual #41 // Method scala/Predef
$.intWrapper:(I)I

10: iload_2

11: invokevirtual #44 // Method scala/runtime/RichInt
$.min$extension:(II)I

14: ireturn



public final java.lang.Object apply(java.lang.Object, java.lang.Object);

Code:

0: aload_0

1: aload_1

2: invokestatic #51 // Method scala/runtime/
BoxesRunTime.unboxToInt:(Ljava/lang/Object;)I

5: aload_2

6: invokestatic #51 // Method scala/runtime/
BoxesRunTime.unboxToInt:(Ljava/lang/Object;)I

9: invokevirtual #53 // Method apply:(II)I

12: invokestatic #57 // Method scala/runtime/
BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;

15: areturn
BOXING: HOWTO AVOID
• don’t use generics!
• use libraries for high performance code
• specialization?!
WHY SPECIALIZATION FAILS?
STEP BACKTO JAVA
• Trove
• fastutil
• HPPC
import scala.collection.JavaConversions._
def newInt2IntOpenHashMap(): mutable.Map[Int, Int] =

new Int2IntOpenHashMap().asInstanceOf[jutil.Map[Int, Int]]
STEP BACKTO JAVA
• Trove
• fastutil
• HPPC
import scala.collection.JavaConversions._
def newInt2IntOpenHashMap(): mutable.Map[Int, Int] =

new Int2IntOpenHashMap().asInstanceOf[jutil.Map[Int, Int]]
ACT 1II:
SURVIVE IN BROKEN WORLD
SPECIALIZED QUEUE
trait CQueue[@specialized(Int, Long) T] {

/**

* @return Next element to be dequeued.

*/

def first(): T



/**

*

* @return Next element that is removed from the `CQueue`.

*/

def deque(): T



/**

* Adds element to the `CQueue`.

*/

def +=(elem: T)



def isEmpty: Boolean

}
IMPLEMENTATION
class CQueueAny[T, QueueType <: PriorityQueue[T]](protected val
underlying: QueueType)

extends CQueue[T] {

def +=(elem: T): Unit = underlying.enqueue(elem)

def deque(): T = underlying.dequeue()

override def first(): T = underlying.first()

override def isEmpty: Boolean = underlying.isEmpty

}



class CQueueInt[QueueType <: IntPriorityQueue](protected val
underlying: QueueType)

extends CQueue[Int] {

override def +=(elem: Int): Unit = underlying.enqueue(elem)

override def deque(): Int = underlying.dequeueInt()

override def first(): Int = underlying.firstInt()

override def isEmpty: Boolean = underlying.isEmpty

}
FACTORY
abstract class CQueueFactory[T] {

def fifo(): FIFO[T]

def priority(): CQueue[T]

def priority(order: Order[T]): CQueue[T]

}
private[collections] class CQueueFactoryAny[T] extends CQueueFactory[T] {

def fifo(): FIFO[T] = new CQueueAny[T, ObjectArrayFIFOQueue[T]](

new ObjectArrayFIFOQueue[T]()) with FIFO[T] {

override def enqueueFirst(elem: T): Unit =
underlying.enqueueFirst(elem)

}

}



private[collections] class CQueueFactoryInt extends CQueueFactory[Int] {

override def fifo() = new CQueueInt[IntArrayFIFOQueue](
new IntArrayFIFOQueue()) with FIFO[Int] {

def enqueueFirst(elem: Int): Unit = underlying.enqueueFirst(elem)

}

}
GLUE
object CQueue {

import Implicits._



def fifo[@specialized(Int, Long) T](): FIFO[T]= {

implicitly[CQueueFactory[T]].fifo()

}



trait LowerPriorityImplicit {

private val factoryAny = new CQueueFactoryAny[AnyRef]()

implicit def factoryAny[T]: CQueueFactory[T] =
factoryAny.asInstanceOf[CQueueFactory[T]]

}



trait LowPriorityImplicit extends LowerPriorityImplicit {

implicit val factoryInt: CQueueFactory[Int] = new CQueueFactoryInt()

}



object Implicits extends LowPriorityImplicit

}
USAGEclass CQueueSpec extends WordSpec with Matchers {

def verifyQueue(q: CQueue[Int], dequeSeq: Seq[Int]): Unit = {

q.isEmpty should be (true)



q += 1

q += 2

q += 1

q += 3



q.isEmpty should be (false)



dequeSeq.foreach {

e => q.deque() should equal (e)

}

q.isEmpty should be (true)



intercept[NoSuchElementException](q.deque())

}



"CQueue" should {

"enqueue/deque elements" when {

"using fifo" in {

val q = CQueue.fifo[Int]()

verifyQueue(q, Seq(1, 2, 1, 3))

}

}

}

}
ACT IV:
MACROS!!
REKLAMY

Más contenido relacionado

La actualidad más candente

Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)hhliu
 
프알못의 Keras 사용기
프알못의 Keras 사용기프알못의 Keras 사용기
프알못의 Keras 사용기Mijeong Jeon
 
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf MilanFrom Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf MilanFabio Collini
 
iOS와 케라스의 만남
iOS와 케라스의 만남iOS와 케라스의 만남
iOS와 케라스의 만남Mijeong Jeon
 
Functions, Types, Programs and Effects
Functions, Types, Programs and EffectsFunctions, Types, Programs and Effects
Functions, Types, Programs and EffectsRaymond Roestenburg
 
Java simple programs
Java simple programsJava simple programs
Java simple programsVEERA RAGAVAN
 
One Monad to Rule Them All
One Monad to Rule Them AllOne Monad to Rule Them All
One Monad to Rule Them AllJohn De Goes
 
Humble introduction to category theory in haskell
Humble introduction to category theory in haskellHumble introduction to category theory in haskell
Humble introduction to category theory in haskellJongsoo Lee
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocamlpramode_ce
 
Pydiomatic
PydiomaticPydiomatic
Pydiomaticrik0
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptMiao Siyu
 
响应式编程及框架
响应式编程及框架响应式编程及框架
响应式编程及框架jeffz
 
Collection v3
Collection v3Collection v3
Collection v3Sunil OS
 
Use Applicative where applicable!
Use Applicative where applicable!Use Applicative where applicable!
Use Applicative where applicable!Hermann Hueck
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple ProgramsUpender Upr
 
Swift internals
Swift internalsSwift internals
Swift internalsJung Kim
 
The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)jeffz
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonTendayi Mawushe
 

La actualidad más candente (20)

Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
 
프알못의 Keras 사용기
프알못의 Keras 사용기프알못의 Keras 사용기
프알못의 Keras 사용기
 
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf MilanFrom Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
 
iOS와 케라스의 만남
iOS와 케라스의 만남iOS와 케라스의 만남
iOS와 케라스의 만남
 
Functions, Types, Programs and Effects
Functions, Types, Programs and EffectsFunctions, Types, Programs and Effects
Functions, Types, Programs and Effects
 
Java simple programs
Java simple programsJava simple programs
Java simple programs
 
Initial Java Core Concept
Initial Java Core ConceptInitial Java Core Concept
Initial Java Core Concept
 
One Monad to Rule Them All
One Monad to Rule Them AllOne Monad to Rule Them All
One Monad to Rule Them All
 
Humble introduction to category theory in haskell
Humble introduction to category theory in haskellHumble introduction to category theory in haskell
Humble introduction to category theory in haskell
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocaml
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
响应式编程及框架
响应式编程及框架响应式编程及框架
响应式编程及框架
 
Collection v3
Collection v3Collection v3
Collection v3
 
Use Applicative where applicable!
Use Applicative where applicable!Use Applicative where applicable!
Use Applicative where applicable!
 
Akka tips
Akka tipsAkka tips
Akka tips
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
Swift internals
Swift internalsSwift internals
Swift internals
 
The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in Python
 

Destacado

Como acreditar los derechos del autor
Como acreditar los derechos del autorComo acreditar los derechos del autor
Como acreditar los derechos del autor201577102547572
 
Brunner_Andrea_DOS_2015
Brunner_Andrea_DOS_2015Brunner_Andrea_DOS_2015
Brunner_Andrea_DOS_2015Andrea Brunner
 
Progress Monitoring Interpretation
Progress Monitoring InterpretationProgress Monitoring Interpretation
Progress Monitoring InterpretationMadison Hopkins
 
Herramientas de google
Herramientas de googleHerramientas de google
Herramientas de googleJenifer Procel
 
Certificate of completion for Project+ 2015
Certificate of completion for Project+ 2015Certificate of completion for Project+ 2015
Certificate of completion for Project+ 2015barnesjohn
 
Killeen Parks Master Plan
Killeen Parks Master PlanKilleen Parks Master Plan
Killeen Parks Master PlanCityofKilleen
 
Curiositats animals
Curiositats animalsCuriositats animals
Curiositats animalsmertxita
 
Past Continuous: free time activities
Past Continuous: free time activitiesPast Continuous: free time activities
Past Continuous: free time activitiespeggym26
 
Instructional Design Presentation
Instructional Design PresentationInstructional Design Presentation
Instructional Design PresentationCandace Ward
 
Tips for Successful Residential Property Tax Appeals
Tips for Successful Residential Property Tax AppealsTips for Successful Residential Property Tax Appeals
Tips for Successful Residential Property Tax AppealsCurley & Rothman, LLC
 
TJ-SP 2017 - Matéria- CONCURSO ASSISTENTE SOCIAL JUDICIÁRIO-
TJ-SP 2017 - Matéria- CONCURSO ASSISTENTE SOCIAL JUDICIÁRIO- TJ-SP 2017 - Matéria- CONCURSO ASSISTENTE SOCIAL JUDICIÁRIO-
TJ-SP 2017 - Matéria- CONCURSO ASSISTENTE SOCIAL JUDICIÁRIO- Rosane Domingues
 

Destacado (15)

Como acreditar los derechos del autor
Como acreditar los derechos del autorComo acreditar los derechos del autor
Como acreditar los derechos del autor
 
Brunner_Andrea_DOS_2015
Brunner_Andrea_DOS_2015Brunner_Andrea_DOS_2015
Brunner_Andrea_DOS_2015
 
MY NEW EXHIBITION
MY NEW EXHIBITIONMY NEW EXHIBITION
MY NEW EXHIBITION
 
Progress Monitoring Interpretation
Progress Monitoring InterpretationProgress Monitoring Interpretation
Progress Monitoring Interpretation
 
Herramientas de google
Herramientas de googleHerramientas de google
Herramientas de google
 
Certificate of completion for Project+ 2015
Certificate of completion for Project+ 2015Certificate of completion for Project+ 2015
Certificate of completion for Project+ 2015
 
Zodiac theme park1
Zodiac theme park1Zodiac theme park1
Zodiac theme park1
 
Killeen Parks Master Plan
Killeen Parks Master PlanKilleen Parks Master Plan
Killeen Parks Master Plan
 
Curiositats animals
Curiositats animalsCuriositats animals
Curiositats animals
 
Past Continuous: free time activities
Past Continuous: free time activitiesPast Continuous: free time activities
Past Continuous: free time activities
 
MENU2016PDF copy
MENU2016PDF copyMENU2016PDF copy
MENU2016PDF copy
 
Instructional Design Presentation
Instructional Design PresentationInstructional Design Presentation
Instructional Design Presentation
 
Tips for Successful Residential Property Tax Appeals
Tips for Successful Residential Property Tax AppealsTips for Successful Residential Property Tax Appeals
Tips for Successful Residential Property Tax Appeals
 
Світіння моря
Світіння моряСвітіння моря
Світіння моря
 
TJ-SP 2017 - Matéria- CONCURSO ASSISTENTE SOCIAL JUDICIÁRIO-
TJ-SP 2017 - Matéria- CONCURSO ASSISTENTE SOCIAL JUDICIÁRIO- TJ-SP 2017 - Matéria- CONCURSO ASSISTENTE SOCIAL JUDICIÁRIO-
TJ-SP 2017 - Matéria- CONCURSO ASSISTENTE SOCIAL JUDICIÁRIO-
 

Similar a Manual specialization

DjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New RelicDjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New RelicGraham Dumpleton
 
Djangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New RelicDjangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New RelicNew Relic
 
FS2 for Fun and Profit
FS2 for Fun and ProfitFS2 for Fun and Profit
FS2 for Fun and ProfitAdil Akhter
 
Smalltalk blocks and closures origin and evolution by Juan Escalada
Smalltalk blocks and closures origin and evolution by Juan EscaladaSmalltalk blocks and closures origin and evolution by Juan Escalada
Smalltalk blocks and closures origin and evolution by Juan EscaladaFAST
 
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Ontico
 
What's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritageWhat's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritageDroidConTLV
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfRahul04August
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a ElixirSvet Ivantchev
 
Swift Sequences & Collections
Swift Sequences & CollectionsSwift Sequences & Collections
Swift Sequences & CollectionsCocoaHeads France
 
Python profiling
Python profilingPython profiling
Python profilingdreampuf
 
Is java8a truefunctionallanguage
Is java8a truefunctionallanguageIs java8a truefunctionallanguage
Is java8a truefunctionallanguageSamir Chekkal
 
Is java8 a true functional programming language
Is java8 a true functional programming languageIs java8 a true functional programming language
Is java8 a true functional programming languageSQLI
 
Blending Culture in Twitter Client
Blending Culture in Twitter ClientBlending Culture in Twitter Client
Blending Culture in Twitter ClientKenji Tanaka
 
Gevent what's the point
Gevent what's the pointGevent what's the point
Gevent what's the pointseanmcq
 

Similar a Manual specialization (20)

DjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New RelicDjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New Relic
 
Djangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New RelicDjangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New Relic
 
FS2 for Fun and Profit
FS2 for Fun and ProfitFS2 for Fun and Profit
FS2 for Fun and Profit
 
Smalltalk blocks and closures origin and evolution by Juan Escalada
Smalltalk blocks and closures origin and evolution by Juan EscaladaSmalltalk blocks and closures origin and evolution by Juan Escalada
Smalltalk blocks and closures origin and evolution by Juan Escalada
 
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
 
What's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritageWhat's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritage
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
Ruby tricks2
Ruby tricks2Ruby tricks2
Ruby tricks2
 
Scala coated JVM
Scala coated JVMScala coated JVM
Scala coated JVM
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
 
Swift Sequences & Collections
Swift Sequences & CollectionsSwift Sequences & Collections
Swift Sequences & Collections
 
Python profiling
Python profilingPython profiling
Python profiling
 
Is java8a truefunctionallanguage
Is java8a truefunctionallanguageIs java8a truefunctionallanguage
Is java8a truefunctionallanguage
 
Is java8 a true functional programming language
Is java8 a true functional programming languageIs java8 a true functional programming language
Is java8 a true functional programming language
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
 
Play image
Play imagePlay image
Play image
 
Blending Culture in Twitter Client
Blending Culture in Twitter ClientBlending Culture in Twitter Client
Blending Culture in Twitter Client
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
Gevent what's the point
Gevent what's the pointGevent what's the point
Gevent what's the point
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 

Último

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 

Último (20)

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 

Manual specialization

  • 3. HIPSTERVS. OLDSCHOOL def hipsterMin(a: Array[Int]): Int = {
 a.foldLeft(Int.MaxValue){ _.min(_)}
 } def oldschoolMin(a: Array[Int]): Int = {
 var min = Int.MaxValue
 var i = 0
 while (i < a.length) {
 min = if (a(i) < min) a(i) else min
 i += 1
 }
 min
 }
  • 4. HIPSTERVS. OLDSCHOOL def hipsterMin(a: Array[Int]): Int = {
 a.foldLeft(Int.MaxValue){ _.min(_)}
 } def oldschoolMin(a: Array[Int]): Int = {
 var min = Int.MaxValue
 var i = 0
 while (i < a.length) {
 min = if (a(i) < min) a(i) else min
 i += 1
 }
 min
 } Running time 967722 123424
  • 5.
  • 7. OLDSCHOOL public int oldschoolMin(int[]);
 Code:
 0: ldc #72 // int 2147483647
 2: istore_2
 3: iconst_0
 4: istore_3
 5: iload_3
 6: aload_1
 7: arraylength
 8: if_icmpge 33
 11: aload_1
 12: iload_3
 13: iaload
 14: iload_2
 15: if_icmpge 24
 18: aload_1
 19: iload_3
 20: iaload
 21: goto 25
 24: iload_2
 25: istore_2
 26: iload_3
 27: iconst_1
 28: iadd
 29: istore_3
 30: goto 5
 33: iload_2
 34: ireturn
  • 8. HIPSTER public int hipsterMin(int[]);
 Code:
 0: getstatic #67 // Field scala/Predef$.MODULE $:Lscala/Predef$;
 3: aload_1
 4: invokevirtual #71 // Method scala/Predef $.intArrayOps:([I)Lscala/collection/mutable/ArrayOps;
 7: ldc #72 // int 2147483647
 9: invokestatic #78 // Method scala/runtime/ BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
 12: new #80 // class szymon/Test$$anonfun $hipsterMin$1
 15: dup
 16: aload_0
 17: invokespecial #83 // Method szymon/Test$$anonfun $hipsterMin$1."<init>":(Lszymon/Test;)V
 20: invokeinterface #89, 3 // InterfaceMethod scala/ collection/mutable/ArrayOps.foldLeft:(Ljava/lang/Object;Lscala/ Function2;)Ljava/lang/Object;
 25: invokestatic #93 // Method scala/runtime/ BoxesRunTime.unboxToInt:(Ljava/lang/Object;)I
 28: ireturn
  • 9. HIPSTER public final class szymon.Test$$anonfun$hipsterMin$1 extends scala.runtime.AbstractFunction2$mcIII$sp implements scala.Serializable {
 public static final long serialVersionUID;
 
 public final int apply(int, int);
 Code:
 0: aload_0
 1: iload_1
 2: iload_2
 3: invokevirtual #21 // Method apply$mcIII$sp:(II)I
 6: ireturn
 
 public int apply$mcIII$sp(int, int);
 Code:
 0: getstatic #32 // Field scala/runtime/RichInt$.MODULE$:Lscala/runtime/RichInt$;
 3: getstatic #37 // Field scala/Predef$.MODULE$:Lscala/Predef$;
 6: iload_1
 7: invokevirtual #41 // Method scala/Predef$.intWrapper:(I)I
 10: iload_2
 11: invokevirtual #44 // Method scala/runtime/RichInt$.min$extension:(II)I
 14: ireturn
 
 public final java.lang.Object apply(java.lang.Object, java.lang.Object);
 Code:
 0: aload_0
 1: aload_1
 2: invokestatic #51 // Method scala/runtime/BoxesRunTime.unboxToInt:(Ljava/lang/ Object;)I
 5: aload_2
 6: invokestatic #51 // Method scala/runtime/BoxesRunTime.unboxToInt:(Ljava/lang/ Object;)I
 9: invokevirtual #53 // Method apply:(II)I
 12: invokestatic #57 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/ Integer;
 15: areturn
 
 public szymon.Test$$anonfun$hipsterMin$1(szymon.Test);
 Code:
 0: aload_0
 1: invokespecial #65 // Method scala/runtime/AbstractFunction2$mcIII$sp."<init>":()V
 4: return
  • 10. BOXING 
 public int apply$mcIII$sp(int, int);
 Code:
 0: getstatic #32 // Field scala/runtime/RichInt $.MODULE$:Lscala/runtime/RichInt$;
 3: getstatic #37 // Field scala/Predef$.MODULE $:Lscala/Predef$;
 6: iload_1
 7: invokevirtual #41 // Method scala/Predef $.intWrapper:(I)I
 10: iload_2
 11: invokevirtual #44 // Method scala/runtime/RichInt $.min$extension:(II)I
 14: ireturn
 
 public final java.lang.Object apply(java.lang.Object, java.lang.Object);
 Code:
 0: aload_0
 1: aload_1
 2: invokestatic #51 // Method scala/runtime/ BoxesRunTime.unboxToInt:(Ljava/lang/Object;)I
 5: aload_2
 6: invokestatic #51 // Method scala/runtime/ BoxesRunTime.unboxToInt:(Ljava/lang/Object;)I
 9: invokevirtual #53 // Method apply:(II)I
 12: invokestatic #57 // Method scala/runtime/ BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
 15: areturn
  • 11. BOXING: HOWTO AVOID • don’t use generics! • use libraries for high performance code • specialization?!
  • 13. STEP BACKTO JAVA • Trove • fastutil • HPPC import scala.collection.JavaConversions._ def newInt2IntOpenHashMap(): mutable.Map[Int, Int] =
 new Int2IntOpenHashMap().asInstanceOf[jutil.Map[Int, Int]]
  • 14. STEP BACKTO JAVA • Trove • fastutil • HPPC import scala.collection.JavaConversions._ def newInt2IntOpenHashMap(): mutable.Map[Int, Int] =
 new Int2IntOpenHashMap().asInstanceOf[jutil.Map[Int, Int]]
  • 15. ACT 1II: SURVIVE IN BROKEN WORLD
  • 16. SPECIALIZED QUEUE trait CQueue[@specialized(Int, Long) T] {
 /**
 * @return Next element to be dequeued.
 */
 def first(): T
 
 /**
 *
 * @return Next element that is removed from the `CQueue`.
 */
 def deque(): T
 
 /**
 * Adds element to the `CQueue`.
 */
 def +=(elem: T)
 
 def isEmpty: Boolean
 }
  • 17. IMPLEMENTATION class CQueueAny[T, QueueType <: PriorityQueue[T]](protected val underlying: QueueType)
 extends CQueue[T] {
 def +=(elem: T): Unit = underlying.enqueue(elem)
 def deque(): T = underlying.dequeue()
 override def first(): T = underlying.first()
 override def isEmpty: Boolean = underlying.isEmpty
 }
 
 class CQueueInt[QueueType <: IntPriorityQueue](protected val underlying: QueueType)
 extends CQueue[Int] {
 override def +=(elem: Int): Unit = underlying.enqueue(elem)
 override def deque(): Int = underlying.dequeueInt()
 override def first(): Int = underlying.firstInt()
 override def isEmpty: Boolean = underlying.isEmpty
 }
  • 18. FACTORY abstract class CQueueFactory[T] {
 def fifo(): FIFO[T]
 def priority(): CQueue[T]
 def priority(order: Order[T]): CQueue[T]
 } private[collections] class CQueueFactoryAny[T] extends CQueueFactory[T] {
 def fifo(): FIFO[T] = new CQueueAny[T, ObjectArrayFIFOQueue[T]](
 new ObjectArrayFIFOQueue[T]()) with FIFO[T] {
 override def enqueueFirst(elem: T): Unit = underlying.enqueueFirst(elem)
 }
 }
 
 private[collections] class CQueueFactoryInt extends CQueueFactory[Int] {
 override def fifo() = new CQueueInt[IntArrayFIFOQueue]( new IntArrayFIFOQueue()) with FIFO[Int] {
 def enqueueFirst(elem: Int): Unit = underlying.enqueueFirst(elem)
 }
 }
  • 19. GLUE object CQueue {
 import Implicits._
 
 def fifo[@specialized(Int, Long) T](): FIFO[T]= {
 implicitly[CQueueFactory[T]].fifo()
 }
 
 trait LowerPriorityImplicit {
 private val factoryAny = new CQueueFactoryAny[AnyRef]()
 implicit def factoryAny[T]: CQueueFactory[T] = factoryAny.asInstanceOf[CQueueFactory[T]]
 }
 
 trait LowPriorityImplicit extends LowerPriorityImplicit {
 implicit val factoryInt: CQueueFactory[Int] = new CQueueFactoryInt()
 }
 
 object Implicits extends LowPriorityImplicit
 }
  • 20. USAGEclass CQueueSpec extends WordSpec with Matchers {
 def verifyQueue(q: CQueue[Int], dequeSeq: Seq[Int]): Unit = {
 q.isEmpty should be (true)
 
 q += 1
 q += 2
 q += 1
 q += 3
 
 q.isEmpty should be (false)
 
 dequeSeq.foreach {
 e => q.deque() should equal (e)
 }
 q.isEmpty should be (true)
 
 intercept[NoSuchElementException](q.deque())
 }
 
 "CQueue" should {
 "enqueue/deque elements" when {
 "using fifo" in {
 val q = CQueue.fifo[Int]()
 verifyQueue(q, Seq(1, 2, 1, 3))
 }
 }
 }
 }