Adopting the JVM Ola Bini computational metalinguist - - PowerPoint PPT Presentation

adopting the jvm
SMART_READER_LITE
LIVE PREVIEW

Adopting the JVM Ola Bini computational metalinguist - - PowerPoint PPT Presentation

Adopting the JVM Ola Bini computational metalinguist ola.bini@gmail.com http://olabini.com/blog onsdag, 2010 november 03 Your host From Sweden to Chicago through ThoughtWorks Language geek at ThoughtWorks JRuby core developer, Ioke and Seph


slide-1
SLIDE 1

Ola Bini

computational metalinguist

  • la.bini@gmail.com

http://olabini.com/blog

Adopting the JVM

  • nsdag, 2010 november 03
slide-2
SLIDE 2

From Sweden to Chicago through ThoughtWorks Language geek at ThoughtWorks JRuby core developer, Ioke and Seph creator Member of the JSR 292 EG

Your host

  • nsdag, 2010 november 03
slide-3
SLIDE 3

Demographics

  • nsdag, 2010 november 03
slide-4
SLIDE 4

Smorgasbord

Whys and wherefores How to get started Polyglot patterns Interoperability JVM tweaks and tools Bytecode JSR 292 Politics

  • nsdag, 2010 november 03
slide-5
SLIDE 5

Why the JVM?

Fantastic platform

JIT Memory model and GC Threading Libraries Tools

Runs mostly anywhere Interoperability between languages Many interesting languages already available

  • nsdag, 2010 november 03
slide-6
SLIDE 6

Why not Java?

More ceremony than essence

Verbosity

Slow turnaround

Compile time cycle

Abstraction level Expressivity Rate of change Useful libraries in other languages

  • nsdag, 2010 november 03
slide-7
SLIDE 7

Sapir-Whorf

  • nsdag, 2010 november 03
slide-8
SLIDE 8
  • nsdag, 2010 november 03
slide-9
SLIDE 9

Programming languages

Iverson - “Notation as a tool of thought” Paul Graham - The Blub paradox Ruby - Matz says one inspiration was novel Babel-17 Steve Yegge - The difference between recursion and iteration

  • nsdag, 2010 november 03
slide-10
SLIDE 10

Where do you start?

  • nsdag, 2010 november 03
slide-11
SLIDE 11

Adopting a new language

Choose with care

Be strategic about language use

Productivity bump

New syntax and new libraries But also new ways of thinking

Leverage should increase quickly Not that different from learning a new framework

  • nsdag, 2010 november 03
slide-12
SLIDE 12

Polyglot Patterns

  • nsdag, 2010 november 03
slide-13
SLIDE 13

Architecture

  • nsdag, 2010 november 03
slide-14
SLIDE 14
  • nsdag, 2010 november 03
slide-15
SLIDE 15
  • nsdag, 2010 november 03
slide-16
SLIDE 16
  • nsdag, 2010 november 03
slide-17
SLIDE 17

Polyglot Testing

Scala, Clojure, Ruby, Groovy, etc

All of these can test Java code very well

Quicker turnaround More manageable tests Use libraries from test language (RSpec, Erjang QuickCheck) Approaches

Exploratory testing - REPL Unit and functional testing Acceptance testing - Cucumber

  • nsdag, 2010 november 03
slide-18
SLIDE 18

import org.thoughtworks.PrimeFinder describe PrimeFinder do it "finds the next prime number" do pf = PrimeFinder.from(10) pf.next.should == 11 pf = PrimeFinder.from(25) pf.next.should == 29 end end

  • nsdag, 2010 november 03
slide-19
SLIDE 19

import(org:thoughtworks:PrimeFinder) forAll(natural n, pf = PrimeFinder from(n) pf next should be odd )

  • nsdag, 2010 november 03
slide-20
SLIDE 20

Build Scripting

Is Maven or Ant the best way to build your system? BuildR GAnt Polyglot Maven General build scripts

  • nsdag, 2010 november 03
slide-21
SLIDE 21

includeTargets << gant.targets.Clean cleanPattern << [ '**/*~' , '**/*.bak' ] cleanDirectory << 'build' target ( stuff : 'A target to do some stuff.' ) { println ( 'Stuff' ) depends ( clean ) echo ( message : 'A default message from Ant.' )

  • therStuff ( )

} target ( otherStuff : 'A target to do some other stuff' ) { println ( 'OtherStuff' ) echo ( message : 'Another message from Ant.' ) clean ( ) } setDefaultTarget ( stuff )

  • nsdag, 2010 november 03
slide-22
SLIDE 22

Alien Libraries

X library consumed by Java

Depends on how the language exposes Java functionality

Java library consumed by X

Using Xs Java Integration features

Sometimes complicates build process

  • nsdag, 2010 november 03
slide-23
SLIDE 23

(.. System (getProperties) (get "os.name"))

  • nsdag, 2010 november 03
slide-24
SLIDE 24

Service Injection

Using DI to compose languages Spring has ScriptFactory

Allows implementation of any interface in most languages

  • nsdag, 2010 november 03
slide-25
SLIDE 25

package com.thoughtworks; public interface Animal { void run(); } class Fox def run puts "Running away!" end end Fox.new

  • nsdag, 2010 november 03
slide-26
SLIDE 26

<?xml version="1.0" encoding="utf-8"?> <beans> <bean class="org.springframework.scripting.support.ScriptFactoryPostProcessor" /> <bean id="fox" class="org.springframework.scripting.jruby.JRubyScriptFactory"> <constructor-arg value="file:fox.rb" /> <constructor-arg value="com.thoughtworks.Animal" /> </bean> </beans>

  • nsdag, 2010 november 03
slide-27
SLIDE 27

End User Scripting

Make it possible to customize behaviour Save/Load scripts Expose a model to the scripts How much of the application can be implemented like this? Versioning

  • nsdag, 2010 november 03
slide-28
SLIDE 28

Java as the C of the JVM

Use another language as the main language Drop down to Java

Performance Features that top level doesn’t have To bind things together

  • nsdag, 2010 november 03
slide-29
SLIDE 29

MOPping

  • nsdag, 2010 november 03
slide-30
SLIDE 30

BSF

  • nsdag, 2010 november 03
slide-31
SLIDE 31

JLabel mylabel = new JLabel(); BSFManager.registerScriptingEngine("ruby", "org.jruby.javasupport.bsf.JRubyEngine", new String[] { "rb" }); BSFManager manager = new BSFManager(); manager.declareBean("label", mylabel, JFrame.class); manager.exec("ruby", "(java)", 1, 1, "$label.setText(\"This is a test.\")");

  • nsdag, 2010 november 03
slide-32
SLIDE 32

JSR 223

  • nsdag, 2010 november 03
slide-33
SLIDE 33

ScriptEngineManager m = new ScriptEngineManager(); ScriptEngine rubyEngine = m.getEngineByName("jruby"); ScriptContext context = rubyEngine.getContext(); context.setAttribute("label", new Integer(4), ScriptContext.ENGINE_SCOPE); try { rubyEngine.eval("puts 2 + $label", context); } catch(ScriptException e) { e.printStackTrace(); }

  • nsdag, 2010 november 03
slide-34
SLIDE 34

Dynamic MOP

  • nsdag, 2010 november 03
slide-35
SLIDE 35

JVM Tweaking

  • nsdag, 2010 november 03
slide-36
SLIDE 36

The basic flags

  • server, -client
  • Xmx, -Xms, -Xss
  • Xverify:none
  • XX:PermSize=512m
  • XX:MaxPermSize=512m
  • nsdag, 2010 november 03
slide-37
SLIDE 37

What’s happening with GC?

  • XX:+PrintGC
  • XX:+PrintGCTimeStamps
  • XX:+PrintGCDetails
  • nsdag, 2010 november 03
slide-38
SLIDE 38

What’s happening with JITting?

  • XX:+PrintCompilation
  • XX:+PrintInlining (debug build only)
  • XX:+PrintOptoAssembly (debug build only)
  • XX:+PrintAssembly (debug build only)
  • XX:+UnlockDiagnosticVMOptions
  • XX:+PrintNMethods
  • XX:+PrintSignatureHandlers
  • nsdag, 2010 november 03
slide-39
SLIDE 39

Other diagnostics

  • XX:+HeapDumpOnOutOfMemoryError
  • XX:+TraceClassLoading
  • XX:+TraceClassUnloading
  • XX:+TraceClassLoadingPreorder
  • XX:+TraceClassResolution
  • nsdag, 2010 november 03
slide-40
SLIDE 40

Tuning

  • XX:+AggressiveOpts
  • XX:+UseCompressedOops (for x64 versions)
  • XX:+DoEscapeAnalysis
  • XX:CompileThreshold=n (default 10000)
  • XX:NewRatio=5
  • XX:ParallelGCThreads=2
  • nsdag, 2010 november 03
slide-41
SLIDE 41

More information here:

http://www.oracle.com/technetwork/java/javase/tech/ vmoptions-jsp-140102.html

  • nsdag, 2010 november 03
slide-42
SLIDE 42

JVM Tools

  • nsdag, 2010 november 03
slide-43
SLIDE 43

JMX

  • nsdag, 2010 november 03
slide-44
SLIDE 44

JConsole

  • nsdag, 2010 november 03
slide-45
SLIDE 45

VisualVM

  • nsdag, 2010 november 03
slide-46
SLIDE 46

Eclipse Memory Analyzer

  • nsdag, 2010 november 03
slide-47
SLIDE 47

JVM Bytecode

  • nsdag, 2010 november 03
slide-48
SLIDE 48

ASM

  • nsdag, 2010 november 03
slide-49
SLIDE 49

BiteScript

  • nsdag, 2010 november 03
slide-50
SLIDE 50

JSR 292

  • nsdag, 2010 november 03
slide-51
SLIDE 51

Method handles

A pointer to a Java method As fast to invoke as calling the method directly No introspection available - it’s an opaque stateless object Combinator based library around them Capability based

Security check at lookup, not at invocation If you have the object you can call it

  • nsdag, 2010 november 03
slide-52
SLIDE 52

MH catchException(MH target, Class exType, MH handler) MH collectArguments(MH target, MethodType newType) MH convertArguments(MH target, MethodType newType) MH dropArguments(MH target, int pos, Class... valueTypes) MH filterArguments(MH target, MH... filters) MH foldArguments(MH target, MH combiner) MH guardWithTest(MH test, MH target, MH fallback) MH insertArguments(MH target, int pos, Object... values) MH spreadArguments(MH target, MethodType newType)

Method handle combinators

  • nsdag, 2010 november 03
slide-53
SLIDE 53

MethodType type = MethodType.methodType(void.class, String.class); MethodHandle handle = MethodHandles.lookup().findVirtual(PrintStream.class, "print", type); handle.invokeExact(System.out, "Hello method handles\n");

  • nsdag, 2010 november 03
slide-54
SLIDE 54

Neat trick

public static void sayClassName() { String name = MethodHandles.lookup().lookupClass().getName(); System.err.println(name); }

  • nsdag, 2010 november 03
slide-55
SLIDE 55

Invoke Dynamic

New byte code For each call site - calls a bootstrap method Bootstrap method defined per class (in general)

Returns a CallSite or MethodHandle that should be used Usually a guardWithTest MethodHandle is expected

CallSites can be invalidated according to language specific conditions InvokeDynamic interface that can be used to call any method with

  • nsdag, 2010 november 03
slide-56
SLIDE 56

@BootstrapMethod(value=DynTest.class, name="bootstrap") public final class DynTest { private static CallSite bootstrap(Class<?> theClass, String theName, MethodType theType) { MethodHandle mh = MethodHandles.lookup(). findStatic(DynTest.class, "generic", theType); CallSite cs = new CallSite(mh); cs.setTarget(mh); return cs; } public static String generic(String theArgument) { return theArgument + ": brought to you by DynTest.generic()"; } public static void main(String[] theArgs) throws Exception { System.out.println((String)InvokeDynamic.bar("bar invoked dynamically")); } }

  • nsdag, 2010 november 03
slide-57
SLIDE 57

Java Politics

  • nsdag, 2010 november 03
slide-58
SLIDE 58

The JCP and JSRs

  • nsdag, 2010 november 03
slide-59
SLIDE 59

Apache Harmony

  • nsdag, 2010 november 03
slide-60
SLIDE 60

Java 7 delayed

  • nsdag, 2010 november 03
slide-61
SLIDE 61

Oracle sues Google

  • nsdag, 2010 november 03
slide-62
SLIDE 62

IBM joins OpenJDK

  • nsdag, 2010 november 03
slide-63
SLIDE 63

Doug Lea

  • nsdag, 2010 november 03
slide-64
SLIDE 64

Apple JDK deprecated

  • nsdag, 2010 november 03
slide-65
SLIDE 65

Java 7

  • nsdag, 2010 november 03
slide-66
SLIDE 66

Forking?

  • nsdag, 2010 november 03
slide-67
SLIDE 67

Oracle as Java steward

  • nsdag, 2010 november 03
slide-68
SLIDE 68

Questions?

OLA BINI

http://olabini.com

  • bini@thoughtworks.com

@olabini

  • nsdag, 2010 november 03