jruby: you’ve got java in my ruby › sf2009 › dl › qcon-sanfran-2009 › ... · foreign...
TRANSCRIPT
![Page 2: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/2.jpg)
Differing Goals
Two Audiences?
Ruby - “Why JVM is good for Ruby impl”
Java - “Appreciate how the JVM complements another language and learn a little Ruby along the way”
![Page 3: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/3.jpg)
Who am I?
JRuby co-lead
Java guy (since the beginning?)
Ruby guy 7-8 years
Employed by Engine Yard to work on JRuby!
“It’s my day job”
![Page 4: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/4.jpg)
What is JRuby?
Ruby on the JVM (Java 5+)
Open-Source: GPL/CPL/LGPL
1.8.7 compatible
Has 1.9-mode (--1.9)
![Page 5: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/5.jpg)
JRuby Boasts...
Great Compatibility
Fast!
Native Threads
All the JVM Buzzwords
More on this a little later
![Page 6: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/6.jpg)
Note on Compatibility
~37,000 passing rubyspecs
~22,000 passing assertions
CI Runs
Java versions, platforms, common libraries
![Page 7: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/7.jpg)
Incompatibilities
Missing some POSIX behavior (e.g. no fork())
No continuations (callcc)
Slower Startup
Cannot run native C extensions <-- Biggest :(
Java Native Extensions (ar-jdbc, yaml, ...)
Foreign Function Interface (FFI)
![Page 8: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/8.jpg)
Foreign Function Interface (FFI)
require 'ffi'
module POSIX extend FFI::Library # not usually necessary since libc is always linked ffi_lib 'c'
attach_function :getuid, :getuid, [], :uint attach_function :getpid, :getpid, [], :uintendputs "Process #{POSIX.getpid}, user #{POSIX.getuid}"
![Page 9: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/9.jpg)
JRuby Status Update
JRuby 1.4.0 released (November 2, 2009)
New Embedding framework: RedBridge
Improved windows support + installer
New bug-for-bug YAML parser: Yecht
400+ issues resolved since 1.3.1
JRuby 1.5 coming around new years
![Page 10: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/10.jpg)
JVM Appreciation
![Page 11: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/11.jpg)
JVM is Mature...
“silver-back” implementations
Decades of debugging and optimizations
Capable of incredibly long uptimes
Keeps improving over time...
![Page 12: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/12.jpg)
JVM keeps improving...
![Page 13: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/13.jpg)
JVM is Pervasive...
Every OS you know runs JVM including a few you don’t
Most machines already have JVM installed
![Page 14: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/14.jpg)
JVM Hotspot
Dynamic profiling your application to optimize the chunks of code which matter
Can runtime profiling be smarter than a static compiler?
Are you smarter than your runtime?
![Page 15: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/15.jpg)
Hotspot Session
Disclaimer: All optimizations shown can happen, but this is merely representative
![Page 16: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/16.jpg)
Hotspot Session:Initial Code
Vector v = new Vector(3); // Thread-safe list....reset(v); // Called many times....void reset(Vector v) { for (int i = 0; i < v.size(); i++) { v.set(i) = 0; } }
![Page 17: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/17.jpg)
Hotspot Session:Inlining
void reset(Vector v) { fast_guard(v) { for (int i = 0; i < lock { arr.length }; i++) { lock { arr[i] = 0; } } } }
![Page 18: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/18.jpg)
Hotspot Session:Simple Optz (loop unroll)
void reset(Vector v) { fast_guard(v) { lock { arr[0] = 0; } lock { arr[1] = 0; } lock { arr[2] = 0; } }}
![Page 19: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/19.jpg)
Hotspot Session:Lock Coarsening
void reset(Vector v) { fast_guard(v) { lock { arr[0] = 0; arr[1] = 0; arr[2] = 0; } } }
![Page 20: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/20.jpg)
Hotspot Session:Array Copy Stubsvoid reset(Vector v) { fast_guard(v) { lock { arrayCopyStub(v, [0,0,0]) } }}
![Page 21: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/21.jpg)
Hotspot: Fractal
![Page 22: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/22.jpg)
JVM: Multiple performance profiles
![Page 23: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/23.jpg)
JVM and Garbage!
Many Garbage Collectors to fit your workload
Army of engineers working on them
Incremental, Compacting, Generational, Concurrent, Parallel
Tons of tunables
![Page 24: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/24.jpg)
JVM GCs: Incremental
Faster partial GC
Smaller discrete phases to reduce GC pauses
Sometimes concurrent phases for no pause
C Ruby is stop-the-world
![Page 25: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/25.jpg)
JVM GCs: Compacting
No fragmentation
Runtime does not gobble all your memory over time
No Fragmentation == Long runtimes
C Ruby is not compacting
![Page 26: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/26.jpg)
JVM GCs: Generational
Short-lived objects collect EXTREMELY fast via incremental collections
Long-lived object get promoted to different object pool(s)
Ruby creates tons of short-lived garbage
C Ruby is not generational
![Page 27: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/27.jpg)
Garbage Collection:Parallel, Concurrent
Splitting GC across multiple cores
GC’ing while execution is still happening
Dark magic
Becoming more and more relevant in Multi-core world
![Page 28: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/28.jpg)
JVM GCs: Tunables!-J-verbose:gc to give live GC information...
![Page 29: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/29.jpg)
JVM Tools
Profilers
Debuggers
Verbose runtime information from VM
jconsole + JMX
![Page 30: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/30.jpg)
JProfile Demo
![Page 31: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/31.jpg)
Java IRB Demo
Midi and Swing in cut time!
![Page 32: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/32.jpg)
Java Library Demo
JMonkeyEngine + JMEPhysics
3D-accelerated Scene-graph library
Is Ruby fast enough?
MADNESS!
![Page 33: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/33.jpg)
Cleaning Up Java APIs
Ruby Language
Has less ceremony
Has features which Java doesn’t
Blocks
DSLs (aka Syntactic Gymnastics)
![Page 34: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/34.jpg)
Less Ceremony == Easier to consume
No type declarations
No checked exceptions
Much richer core libraries
Common tasks simplified
![Page 35: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/35.jpg)
Blocks Remove Boilerplate
def read(filename) open_file = Reader.new(JFile.new(filename)) yield open_fileensure open_file.closeend
read(“my_data_file”) do |fd| fd.read(30) # ... more stuff ...end
![Page 36: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/36.jpg)
Blocks & Reusability
![Page 37: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/37.jpg)
Blocks & Reusability
![Page 38: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/38.jpg)
Blocks & Reusability
![Page 39: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/39.jpg)
Blocks & Reusability
![Page 40: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/40.jpg)
DSLs (Syntactic Gym.)
Use Ruby syntax features to dress up Java APIs
![Page 41: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/41.jpg)
/* Missing try/catches.... */DynamicPhysicsNode iceQube = getPhysicsSpace().createDynamicNode();iceQube.attachChild(new Box("Icecube", Vector3f.new, CUBE_SIZE, CUBE_SIZE, CUBE_SIZE));iceQube.generatePhysicsGeometry();iceQube.setMaterial(Material.ICE);TextureState textureState = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();URL url = System.getResource("data/images/Monkey.jpg");Texture texture = TextureManager.loadTexture(url, Texture::MinificationFilter:Trilinear, Texture::MagnificationFilter::Bilinear, true);texture.setWrap(Texture::WrapMode::Repeat);textureState.setTexture(texture);setRenderState(textureState);iceQube.computeMass();iceQube.getLocalTranslation().set(START_X, START_Y, START_Z);
@icecube = physics_space.create_dynamic do geometry Cube("Icecube", CUBE_SIZE) made_of Material::ICE texture "data/images/Monkey.jpg" at *START end
![Page 42: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/42.jpg)
Conclusions
JVM is a great base for languages
Java libraries are easy to Rubify
![Page 43: JRuby: You’ve got Java in my Ruby › sf2009 › dl › qcon-sanfran-2009 › ... · Foreign Function Interface (FFI) require 'ffi' module POSIX extend FFI::Library # not usually](https://reader034.vdocument.in/reader034/viewer/2022042401/5f0fa1637e708231d4451dfa/html5/thumbnails/43.jpg)
Thanks
jruby: http://www.jruby.org/
email: [email protected]
twitter: tom_enebo
blog: http://blog.enebo.com/
jrme: http://www.kenai.com/projects/jrme