Top Banner
Clojure and Android Daniel Solano Gómez Sattvik Software & Technology Resources, Ltd. Co. Clojure/conj 2011
88

Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Jun 29, 2018

Download

Documents

nguyennhi
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure and Android

Daniel Solano GómezSattvik Software & Technology Resources, Ltd. Co.

Clojure/conj 2011

Page 2: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure in Small Places

Daniel Solano GómezSattvik Software & Technology Resources, Ltd. Co.

Clojure/conj 2011

Page 3: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure to go

Daniel Solano GómezSattvik Software & Technology Resources, Ltd. Co.

Clojure/conj 2011

Page 4: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Overview

Android and the Dalvik VM

Dynamic compilation

Performance

Improving the Clojure/Android experience

Closing thoughts

Page 5: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Android and the Dalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to a running appWhat it all means

Dynamic compilation

Performance

Improving the Clojure/Android experience

Closing thoughts

Page 6: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

What is Android?

Page 7: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Android architecture

Page 8: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Android architecture

Page 9: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Android architecture

Page 10: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Android architecture

Page 11: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Android architecture

Page 12: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

The good news

Page 13: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

The bad news

(= JavaVM DalvikVM)

; => false

Page 14: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

The bad news

(= JavaVM DalvikVM); => false

Page 15: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Java VM vs. Dalvik VM

Page 16: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Java VM vs. Dalvik VM

Java VM Dalvik VM

VM type stack-based register-basedJIT type method traceParallel GC? Yes Only Android 2.3+

Processing power Up to 2×1.2GHz Up to lotsMax heap size 256MiB+ 16-48 MiB

APIs Java SE Not quite Java SEExecutable format class Dalvik EXecutable (DEX)

Page 17: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Java VM vs. Dalvik VM

Java VM Dalvik VM

VM type stack-based register-basedJIT type method traceParallel GC? Yes Only Android 2.3+

Processing power Up to 2×1.2GHz Up to lotsMax heap size 256MiB+ 16-48 MiB

APIs Java SE Not quite Java SEExecutable format class Dalvik EXecutable (DEX)

Page 18: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Java VM vs. Dalvik VM

Java VM Dalvik VM

VM type stack-based register-basedJIT type method traceParallel GC? Yes Only Android 2.3+

Processing power Up to 2×1.2GHz Up to lotsMax heap size 256MiB+ 16-48 MiB

APIs Java SE Not quite Java SEExecutable format class Dalvik EXecutable (DEX)

Page 19: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

A class file

Class file

Page 20: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

A class file

Class file

Header

Page 21: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

A class file

Class file

Header

Constant pool

Page 22: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

A class file

Class file

Header

Constant pool

Other data/code

Page 23: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

An application

C.classA.class B.classB.class

Page 24: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

An application

App.jar

C.classA.class B.classB.class

Page 25: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

A problem

App.jar

C.classA.class B.classB.class

Page 26: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Creating a DEX

App.jar

C.classA.class B.classB.class

classes.dex

Page 27: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Creating a DEX

App.jar

C.classA.class B.classB.class

classes.dex

Page 28: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Creating a DEX

App.jar

C.classA.class B.classB.class

classes.dex

Page 29: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Creating a DEX

App.jar

C.classA.class B.classB.class

classes.dex

Page 30: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Creating a DEX

App.jar

C.classA.class B.classB.class

classes.dex

Page 31: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Creating a DEX

App.jar

C.classA.class B.classB.class

classes.dex

Page 32: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

An Android application

App.apk

classes.dex

Page 33: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Installing the application

App.apk

classes.dex

Page 34: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Installing the application

App.apk

classes.dex

DEX

Page 35: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Installing the application

App.apk

classes.dex

DEX ODEX

Page 36: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Installing the application

ODEX

Page 37: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Consequences

▶ Loading bytecode in Android is aheavy-weight process.

▶ Dynamically loading bytecode isnot currently supported.

Page 38: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Android programminglanguagesStaticly (AOT) compiled

Page 39: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VMWhat is Android?Java VM vs. Dalvik VMFrome source to arunning appWhat it all means

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Android programminglanguagesInterpreted

Page 40: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

Android and the Dalvik VM

Dynamic compilationWhy?How?Implementation details

Performance

Improving the Clojure/Android experience

Closing thoughts

Page 41: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

Dynamic compilation onAndroid

Why?

Page 42: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

Benefits of dynamiccompilation

▶ Make the Clojure REPL possible▶ Make Android development fasterand easier

Page 43: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

A review(defn hello [name] (str "Hello, " name \!))

Page 44: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

A review

name

defn hello

str "Hello, " name \!

(defn hello [name] (str "Hello, " name \!))

Page 45: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

A review

name

defn hello

str "Hello, " name \!

(defn hello [name] (str "Hello, " name \!))

Class

Page 46: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

What about Android?

name

defn hello

str "Hello, " name \!

(defn hello [name] (str "Hello, " name \!))

Class

Page 47: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

What about Android?

name

defn hello

str "Hello, " name \!

(defn hello [name] (str "Hello, " name \!))

ODEX

Page 48: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

What about Android?

name

defn hello

str "Hello, " name \!

(defn hello [name] (str "Hello, " name \!))

ODEX

Page 49: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

A new class loader

URLClassLoader

defineClass()

DynamicClassLoader

Page 50: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

A new class loader

URLClassLoader

defineClass()

DynamicClassLoader

defineMissingClass()

JvmDynamicClassLoader DalvikDynamicClassLoader

Page 51: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

The Dalvik process

(defn hello [name] (str "Hello, " name \!))

Page 52: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

The Dalvik process

name

defn hello

str "Hello, " name \!

(defn hello [name] (str "Hello, " name \!))

Page 53: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

The Dalvik process

name

defn hello

str "Hello, " name \!

(defn hello [name] (str "Hello, " name \!))

Class

Page 54: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

The Dalvik process

name

defn hello

str "Hello, " name \!

(defn hello [name] (str "Hello, " name \!))

ClassDEX

Page 55: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

The Dalvik process

name

defn hello

str "Hello, " name \!

(defn hello [name] (str "Hello, " name \!))

ClassAPK DEX

Page 56: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

The Dalvik process

name

defn hello

str "Hello, " name \!

(defn hello [name] (str "Hello, " name \!))

ODEX ClassAPK DEX

Page 57: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

Choosing the rightDynamicClassLoader

▶ New var:clojure.core/*vm-type*

▶ May be either :dalvik-vm or:java-vm

▶ Setting depends on java.vm.namesystem property

▶ Decision takes place inRT.makeClassLoader()

Page 58: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

New dependency

▶ Relies on code from Android SDKto perform class → DEXtransformation

▶ Current process extracts codefrom dx.jar

▶ A source-based dependencywould be better

▶ Source available under ApacheLicense 2.0

Page 59: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

Android compatibility

▶ Doesn’t work on versions earlierthan 2.1

▶ Contains workaround for bug 5697(fixed in Android 2.2)

Page 60: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

DynamiccompilationWhy?How?Implementation details

Performance

Improving theClojure/Androidexperience

Closing thoughts

Clojure compatibility

▶ All Dalvik-related code is loadedreflectively

▶ Some complex macros may notcompile due to size limitations

▶ clojure.core/bean not availabledue lack of java.beans API

Page 61: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

PerformanceBenchmarksAnalysis

Improving theClojure/Androidexperience

Closing thoughts

Android and the Dalvik VM

Dynamic compilation

PerformanceBenchmarksAnalysis

Improving the Clojure/Android experience

Closing thoughts

Page 62: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

PerformanceBenchmarksAnalysis

Improving theClojure/Androidexperience

Closing thoughts

Benchmarking Clojure andthe competition

▶ Clojure 1.2.1 (Android version)▶ Clojure 1.3 (unmodified)▶ Java▶ Scala (Scala 2.9.1)▶ Ruby (JRuby 1.6.5,ruboto-core-0.5.1)

Page 63: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

PerformanceBenchmarksAnalysis

Improving theClojure/Androidexperience

Closing thoughts

Package size (MB)

Page 64: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

PerformanceBenchmarksAnalysis

Improving theClojure/Androidexperience

Closing thoughts

Installed size (MB)

Page 65: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

PerformanceBenchmarksAnalysis

Improving theClojure/Androidexperience

Closing thoughts

Startup time (s)

Page 66: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

PerformanceBenchmarksAnalysis

Improving theClojure/Androidexperience

Closing thoughts

Heap size (MB)

Page 67: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

PerformanceBenchmarksAnalysis

Improving theClojure/Androidexperience

Closing thoughts

Question

Where did all thetime and spacego?

Page 68: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

PerformanceBenchmarksAnalysis

Improving theClojure/Androidexperience

Closing thoughts

Answer

clojure.core

Page 69: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

PerformanceBenchmarksAnalysis

Improving theClojure/Androidexperience

Closing thoughts

Top 10 space hogsBy retained heap in bytes

clojure.core/*out* 8,608Class clojure.java.io.__init 12,784Class clojure.lang.Compiler 13,152Class clojure.deftype__init 14,024clojure.core/*in* 25,056clojure.core.protocols namespace 25,584Class clojure.lang.Namespace 26,480clojure.java.io namespace 31,488clojure.core namespace 34,992Class clojure.core__init 203,616

TOTAL 395,784

Page 70: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

PerformanceBenchmarksAnalysis

Improving theClojure/Androidexperience

Closing thoughts

Measuring time, a test

// equivalent to (+ 1 2 3)RT.var(”clojure.core”,”+”)

.invoke(1,2,3);

Page 71: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

PerformanceBenchmarksAnalysis

Improving theClojure/Androidexperience

Closing thoughts

Test resultsTimes are in milliseconds

Iteration RT.var() Var.invoke() Total

1 0.240 0.397 7,856.7732 0.299 0.345 0.686

Page 72: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

PerformanceBenchmarksAnalysis

Improving theClojure/Androidexperience

Closing thoughts

Where did 7.8 seconds go?CPU time

clojure.lang.RT.doInit()

95.8%

Page 73: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

PerformanceBenchmarksAnalysis

Improving theClojure/Androidexperience

Closing thoughts

Where did 7.8 seconds go?CPU time

RT.load("clojure/core")

83.2%

12.5%

(in-ns 'user)

(refer 'clojure.core)

Page 74: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

PerformanceBenchmarksAnalysis

Improving theClojure/Androidexperience

Closing thoughts

Where did 7.8 seconds go?Object churn

Count Size (MiB)Allocated 128,280 4.65Freed 91,259 3.63Net 37,021 1.02

Page 75: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

PerformanceBenchmarksAnalysis

Improving theClojure/Androidexperience

Closing thoughts

Improving performance

▶ Remove user namespace▶ Remove metadata▶ Transients during initialization▶ Serializable clojure.core▶ Source-level tree shaker

Page 76: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Android and the Dalvik VM

Dynamic compilation

Performance

Improving the Clojure/Android experience

Closing thoughts

Page 77: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Making Clojure a first-classlanguage for Androiddevelopment

▶ Improve performance▶ Standard library▶ Development tools

Page 78: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Neko: The Clojure/AndroidToolkit

▶ Available fromgithub.com/sattvik/neko

▶ Ease object-oriented/functionalimpedance mismatch

▶ Reduce boilerplate▶ Integrate into Clojure-contrib?

Page 79: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Development tools

▶ Android development version ofClojure

▶ Build tools▶ REPL/dynamic development tools

Page 80: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Android developmentversion of Clojure

▶ Available fromgithub.com/sattvik/clojure

▶ Needs:▶ Upgrade to Clojure 1.3▶ Better handling of dx tool integration

▶ Merge upstream?

Page 81: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Build tools

▶ Neko provides some helper codefor Ant/Android SDK

▶ Would be nice to see support frommainstream Clojure tools

Page 82: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

REPL/dynamicdevelopment tools

▶ Requires Android developmentversion of Clojure

▶ VimClojure works with some minormodifications

▶ Would be good to see toolscompatible with nrepl, Emacs, etc.

Page 83: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Android and the Dalvik VM

Dynamic compilation

Performance

Improving the Clojure/Android experience

Closing thoughts

Page 84: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Android and Clojure

Page 85: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Android and Clojure

▶ Clojure has the potential to be afirst-class language for Androiddevelopment.

▶ Dynamic development on Androidis a killer feature.

▶ It needs better tool andcommunity support.

Page 86: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

What about ClojureScript?

▶ Should work for web apps▶ Might work with mobiledevelopment frameworks

▶ For native development,constrained by availability ofJavaScript engines

Page 87: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Decaffeinated AndroidDeveloping on Android without Java

Daniel Solano Gómez

Decaffeinated Android

Page 88: Clojure and Android - raw.githubusercontent.com · Clojureand Android DanielSolano Gómez Androidandthe DalvikVM Dynamic compilation Performance Benchmarks Analysis Improvingthe Clojure…

Clojure andAndroid

Daniel SolanoGómez

Android and theDalvik VM

Dynamiccompilation

Performance

Improving theClojure/Androidexperience

Closing thoughts

Questions?Blog www.deepbluelambda.org

Twitter @deepbluelambdaE-mail [email protected] groups.google.com/group/

clojure-androidClojure REPL for Android: