JRuby!
Me

• Charles Oliver Nutter
 • @headius
• Java developer since 1996
• JRuby developer since 2006
• Starting at Red Hat on Monday!
Red Hat FAQ

• Primary job still JRuby
• Expanding to help other languages
• Working with OpenJDK team
• Continuing to support EY JRuby efforts
What is JRuby?
Ruby on JVM
• Core classes and runtime in Java
 • Moving parts to Ruby over time
• Standard command line
• 1.8 and 1.9 compatible
 • 1.9 default in JRuby 1.7
• Drop in replacement for MRI*
*caveats
• Weak low-level UNIX stuff
 • Improving over time
• Poor C extension support
 • Not maintained...off by default in 1.7
• Some features differ or unavailable
 • ObjectSpace, trace funcs, callcc...
JRuby 1.7 (preview)

• Ruby 1.9.3 mode by default
 • Many 1.9 compat fixes
• Numerous perf improvements
 • Java 7 invokedynamic support
• Beginning of new optimizing compiler
Getting Started

• Need a JVM...
• rvm install jruby
 • rvm install jruby-1.7.0-preview1
• Download manually
 • Unpack, edit PATH, done
Try it Out
JRuby is the best*
  Ruby runtime
The Team
JRuby Team
JRuby Team


    Charlie
JRuby Team

               Charlie                  Tom




Nick   Hiro   Marcin     Nahi   Wayne   Subbu   Douglas   Douglas
                                                           Douglas
                                                           Contribs
JRuby Team

                Charlie                          Tom




Nick   Hiro    Marcin        Nahi     Wayne      Subbu    Douglas   Douglas
                                                                     Douglas
                                                                     Contribs




          Douglas
           Douglas        Douglas
                           Douglas    Douglas
                                       Douglas       Douglas
                                                       Other
                                                      Douglas
           OpenJDK          Android       J9
                                                         JVMs
JRuby Structure
                 JRuby
          Bytecode    Core       Java
Parser
             JIT     Classes    Integ

                 JVM
                       Native
Threads     GC                  C API
                        JIT
JRuby Structure
                   JRuby
             Bytecode    Core      Java
    Parser
                JIT     Classes   Integ


JRuby team can focus on implementing JRuby.
JRuby Team Doesn’t
     Have to Work On*
•   Memory allocation     •   Native JIT

•   Garbage collectors    •   Tooling

•   Native threading      •   Server environments

•   Cross-platform        •   Native extensions

•   Mobile/Embedded VMs
We could stop working
on JRuby and it would
continue to get faster.
Google Summer
        of Code
• JRuby accepted as an organization
• Eight students!
 • Shoes, Krypt, evented IO, fibers, Ruboto,
    IR dumping, IR to Dalvik, benchmarking
• Three more from C42
Platforms
Systems

• Best Ruby on Windows?
• Exotic platforms: zLinux, OpenVMS, AS/400
• Android’s Dalvik
• Embedded JVMs
Ruboto
Servers

• Any Java server can host Ruby
• Trinidad
 • Ruby-style CLI server atop Tomcat
• Torquebox
 • Full Ruby stack atop JBossAS
Torquebox
•   Rack-compatible         •   Server management

•   Database connectivity   •   Clustering

•   Background daemons      •   In and out-process cache

•   Scheduled jobs          •   Web sockets

•   Messaging               •   Authentication

•   Asynchronous tasks      •   XA Transactions
Torquebox
•   Rack-compatible         •   Server management

•   Database connectivity   •   Clustering

•   All R
    Background daemons      •   In and out-process cache

•   Scheduled jobs
                      uby   •   Web sockets

•   Messaging
                          APIs
                            •   Authentication

•   Asynchronous tasks      • ! XA Transactions
Libraries
Libraries

• 340k versioned jars in Maven central
 • Compare to 35k gems in RubyGems.org
• Vast majority have no native code
• All usable from JRuby
Midi Example
GC
JVM GC
• Wide array of options
 • Generation size, worker threads,
    concurrency, tenuring...
• Many GCs to choose from
• Scales up to massive heaps
• Best GCs in the world!
class Simple
  attr_accessor :next
end

top = Simple.new

puts Benchmark.measure {
  outer = 10
  total = 100000
  per = 100

  outer.times do
    total.times do
      per.times { Simple.new }
      s = Simple.new
      top.next = s
      top = s
    end
  end
}
15
                GC time %
11.25


  7.5


 3.75


   0
        JRuby               Ruby 1.9.3
Threads
Real Parallellism

• Ruby thread = JVM thread = native thread
• One process can use all cores
• One server can handle all requests
require 'benchmark'

ary = (1..1000000).to_a

loop {
  puts Benchmark.measure {
    10.times {
      ary.each {|i|}
    }
  }
}
require 'benchmark'

ary = (1..1000000).to_a

loop {
  puts Benchmark.measure {
    (1..10).map {
      Thread.new {
        ary.each {|i|}
      }
    }.map(&:join)
  }
}
Ruby 1.9
unthreaded
Ruby 1.9    Ruby 1.9
unthreaded   threaded
Ruby 1.9    Ruby 1.9
unthreaded   threaded




  JRuby
unthreaded
Ruby 1.9    Ruby 1.9
unthreaded   threaded




  JRuby        JRuby
unthreaded   threaded
threaded_reverse

 0.8



0.65



 0.5



0.35



 0.2
       one thread   two threads     three threads   four threads
Nonlinear?

• More work means more objects
• More objects needs memory bandwidth
• No different from multi-process
Performance
Performance

• JRuby compiles Ruby to JVM bytecode
• JVM compiles bytecode to native
• Best JIT technology in the world
• Getting even better with invokedynamic
def foo
  bar
end

def bar
  baz     foo   bar   baz
end

def baz
  # ...
end
JRuby on Java 5/6
def foo
  bar
end

def bar            JRuby            JRuby
  baz     foo        call   bar       call     baz
end                 logic            logic
def baz
  # ...
end
                Kills many JVM optimizations
JRuby on Java 7
def foo
  bar




                   X X
end

def bar             JRuby             JRuby
  baz     foo         call    bar       call   baz
end                  logic             logic
def baz
  # ...
end
                Dynamic call logic built into JVM
JRuby on Java 7
def foo
  bar
end

def bar
  baz     foo               bar              baz
end

def baz
  # ...
end
                Straight through dispatch path
JRuby on Java 7
def foo
  bar
end

def bar
  baz           foo       bar       baz
end

def baz
  # ...
end
          Optimizations (like inlining) can happen!
JRuby/Java 6   JRuby/Java 7
JRuby/Java 6                   JRuby/Java 7
                        Times Faster than Ruby 1.9.3
  5



3.75



 2.5



1.25



  0
       base64      richards        neural      mandelbrot     redblack
JRuby/Java 6                     JRuby/Java 7
                            Times Faster than Ruby 1.9.3
  5



3.75



 2.5

                                    1.914          1.806
                    1.538                                         1.565
1.25   1.346




  0
         base64      richards          neural      mandelbrot      redblack
JRuby/Java 6                     JRuby/Java 7
                                 Times Faster than Ruby 1.9.3
  5


                                                                4.226           4.32
3.75
                                                 3.66
                                 3.44


 2.5           2.658


                                         1.914          1.806
                         1.538                                          1.565
1.25   1.346




  0
         base64           richards          neural      mandelbrot       redblack
Tools
Profiling

• Java profilers
 • VisualVM,YourKit, NetBeans, JXInsight
• jruby [--profile | --profile.graph]
• jruby -Xreify.classes=true
• JVM command-line profilers
Monitoring

• Java Management Extensions (JMX)
 • Gems available for clients and servers
• jconsole and VisualVM
• Most servers provide additional tools
• New Relic, etc have JVM support
VisualVM

• CPU, memory, thread monitoring
• CPU and memory profiling
• VisualGC
• Heap analysis
Your Turn

• Try your apps on JRuby and tell us
• Turn on JRuby in @travisci
• Let us know what you think of JRuby
• Help us make JRuby even better!
Thank you!

• @headius
• jruby.org
• torquebox.org

Euruko 2012 - JRuby

  • 1.
  • 2.
    Me • Charles OliverNutter • @headius • Java developer since 1996 • JRuby developer since 2006 • Starting at Red Hat on Monday!
  • 3.
    Red Hat FAQ •Primary job still JRuby • Expanding to help other languages • Working with OpenJDK team • Continuing to support EY JRuby efforts
  • 4.
  • 5.
    Ruby on JVM •Core classes and runtime in Java • Moving parts to Ruby over time • Standard command line • 1.8 and 1.9 compatible • 1.9 default in JRuby 1.7 • Drop in replacement for MRI*
  • 6.
    *caveats • Weak low-levelUNIX stuff • Improving over time • Poor C extension support • Not maintained...off by default in 1.7 • Some features differ or unavailable • ObjectSpace, trace funcs, callcc...
  • 7.
    JRuby 1.7 (preview) •Ruby 1.9.3 mode by default • Many 1.9 compat fixes • Numerous perf improvements • Java 7 invokedynamic support • Beginning of new optimizing compiler
  • 8.
    Getting Started • Needa JVM... • rvm install jruby • rvm install jruby-1.7.0-preview1 • Download manually • Unpack, edit PATH, done
  • 9.
  • 10.
    JRuby is thebest* Ruby runtime
  • 11.
  • 12.
  • 13.
    JRuby Team Charlie
  • 14.
    JRuby Team Charlie Tom Nick Hiro Marcin Nahi Wayne Subbu Douglas Douglas Douglas Contribs
  • 15.
    JRuby Team Charlie Tom Nick Hiro Marcin Nahi Wayne Subbu Douglas Douglas Douglas Contribs Douglas Douglas Douglas Douglas Douglas Douglas Douglas Other Douglas OpenJDK Android J9 JVMs
  • 16.
    JRuby Structure JRuby Bytecode Core Java Parser JIT Classes Integ JVM Native Threads GC C API JIT
  • 17.
    JRuby Structure JRuby Bytecode Core Java Parser JIT Classes Integ JRuby team can focus on implementing JRuby.
  • 18.
    JRuby Team Doesn’t Have to Work On* • Memory allocation • Native JIT • Garbage collectors • Tooling • Native threading • Server environments • Cross-platform • Native extensions • Mobile/Embedded VMs
  • 19.
    We could stopworking on JRuby and it would continue to get faster.
  • 20.
    Google Summer of Code • JRuby accepted as an organization • Eight students! • Shoes, Krypt, evented IO, fibers, Ruboto, IR dumping, IR to Dalvik, benchmarking • Three more from C42
  • 21.
  • 22.
    Systems • Best Rubyon Windows? • Exotic platforms: zLinux, OpenVMS, AS/400 • Android’s Dalvik • Embedded JVMs
  • 23.
  • 24.
    Servers • Any Javaserver can host Ruby • Trinidad • Ruby-style CLI server atop Tomcat • Torquebox • Full Ruby stack atop JBossAS
  • 25.
    Torquebox • Rack-compatible • Server management • Database connectivity • Clustering • Background daemons • In and out-process cache • Scheduled jobs • Web sockets • Messaging • Authentication • Asynchronous tasks • XA Transactions
  • 26.
    Torquebox • Rack-compatible • Server management • Database connectivity • Clustering • All R Background daemons • In and out-process cache • Scheduled jobs uby • Web sockets • Messaging APIs • Authentication • Asynchronous tasks • ! XA Transactions
  • 27.
  • 28.
    Libraries • 340k versionedjars in Maven central • Compare to 35k gems in RubyGems.org • Vast majority have no native code • All usable from JRuby
  • 31.
  • 32.
  • 33.
    JVM GC • Widearray of options • Generation size, worker threads, concurrency, tenuring... • Many GCs to choose from • Scales up to massive heaps • Best GCs in the world!
  • 34.
    class Simple   attr_accessor :next end top= Simple.new puts Benchmark.measure {   outer = 10   total = 100000   per = 100   outer.times do     total.times do       per.times { Simple.new }       s = Simple.new       top.next = s       top = s     end   end }
  • 35.
    15 GC time % 11.25 7.5 3.75 0 JRuby Ruby 1.9.3
  • 36.
  • 37.
    Real Parallellism • Rubythread = JVM thread = native thread • One process can use all cores • One server can handle all requests
  • 38.
    require 'benchmark' ary =(1..1000000).to_a loop {   puts Benchmark.measure {     10.times {       ary.each {|i|}     }   } }
  • 39.
    require 'benchmark' ary =(1..1000000).to_a loop {   puts Benchmark.measure {     (1..10).map {       Thread.new {         ary.each {|i|}       }     }.map(&:join)   } }
  • 41.
  • 42.
    Ruby 1.9 Ruby 1.9 unthreaded threaded
  • 43.
    Ruby 1.9 Ruby 1.9 unthreaded threaded JRuby unthreaded
  • 44.
    Ruby 1.9 Ruby 1.9 unthreaded threaded JRuby JRuby unthreaded threaded
  • 45.
    threaded_reverse 0.8 0.65 0.5 0.35 0.2 one thread two threads three threads four threads
  • 46.
    Nonlinear? • More workmeans more objects • More objects needs memory bandwidth • No different from multi-process
  • 47.
  • 48.
    Performance • JRuby compilesRuby to JVM bytecode • JVM compiles bytecode to native • Best JIT technology in the world • Getting even better with invokedynamic
  • 49.
    def foo bar end def bar baz foo bar baz end def baz # ... end
  • 50.
    JRuby on Java5/6 def foo bar end def bar JRuby JRuby baz foo call bar call baz end logic logic def baz # ... end Kills many JVM optimizations
  • 51.
    JRuby on Java7 def foo bar X X end def bar JRuby JRuby baz foo call bar call baz end logic logic def baz # ... end Dynamic call logic built into JVM
  • 52.
    JRuby on Java7 def foo bar end def bar baz foo bar baz end def baz # ... end Straight through dispatch path
  • 53.
    JRuby on Java7 def foo bar end def bar baz foo bar baz end def baz # ... end Optimizations (like inlining) can happen!
  • 55.
    JRuby/Java 6 JRuby/Java 7
  • 56.
    JRuby/Java 6 JRuby/Java 7 Times Faster than Ruby 1.9.3 5 3.75 2.5 1.25 0 base64 richards neural mandelbrot redblack
  • 57.
    JRuby/Java 6 JRuby/Java 7 Times Faster than Ruby 1.9.3 5 3.75 2.5 1.914 1.806 1.538 1.565 1.25 1.346 0 base64 richards neural mandelbrot redblack
  • 58.
    JRuby/Java 6 JRuby/Java 7 Times Faster than Ruby 1.9.3 5 4.226 4.32 3.75 3.66 3.44 2.5 2.658 1.914 1.806 1.538 1.565 1.25 1.346 0 base64 richards neural mandelbrot redblack
  • 60.
  • 61.
    Profiling • Java profilers • VisualVM,YourKit, NetBeans, JXInsight • jruby [--profile | --profile.graph] • jruby -Xreify.classes=true • JVM command-line profilers
  • 62.
    Monitoring • Java ManagementExtensions (JMX) • Gems available for clients and servers • jconsole and VisualVM • Most servers provide additional tools • New Relic, etc have JVM support
  • 63.
    VisualVM • CPU, memory,thread monitoring • CPU and memory profiling • VisualGC • Heap analysis
  • 66.
    Your Turn • Tryyour apps on JRuby and tell us • Turn on JRuby in @travisci • Let us know what you think of JRuby • Help us make JRuby even better!
  • 67.
    Thank you! • @headius •jruby.org • torquebox.org