Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Why JRuby? - RubyConf 2012

2,415 views

Published on

Talk on why you'll need JRuby someday, delivered by Charles Oliver Nutter and Thomas Enebo at RubyConf 2012.

Published in: Technology
  • Be the first to comment

Why JRuby? - RubyConf 2012

  1. 1. Why JRuby?
  2. 2. Who? Thomas Charlie@tom_enebo @headius
  3. 3. What is JRuby?
  4. 4. JRuby is Ruby! * 1.8.7 & 1.9.3 compatibility drop-in replacement * see next slide
  5. 5. *caveats• Weak but improving low-level UNIX stuff• No C extension support • Not maintained...off by default in 1.7• Some features differ or unavailable • ObjectSpace, trace funcs, callcc, fork...
  6. 6. JRuby is Ruby ...and then some?
  7. 7. Announcing JRuby 1.7.0• Ruby 1.9.3 compat (--1.8 for 1.8.7)• Java 7 invokedynamic support• Much awesomeness over 1.6.x
  8. 8. Getting Started1. Install a JVM2. rvm install jruby3. Profit! % jruby -e ‘puts “wowee”’
  9. 9. The JRuby Difference
  10. 10. JRuby Team Charlie TomNick Hiro Marcin Nahi Wayne Subbu Douglas Douglas Douglas Contribs Douglas Douglas Douglas Douglas Douglas Other Douglas OpenJDK Android JVMs
  11. 11. JRuby JRubyYoko JVMJ. Rose OS (libc, ...)
  12. 12. MRInobu MRI OS (libc, ...)
  13. 13. JRuby JRubyYoko JVMJ. Rose OS (libc, ...)
  14. 14. We get it for Free! JVMJ. Rose Garbage Profiled Native JIT Optimizations Collection Native Tooling Cross Platform Threading
  15. 15. Profile: John Rose • JVM Engineer since 1997 • Invented C* Language • JSR 292 lead
  16. 16. JVM over time(what if we were lazy?)
  17. 17. Go Java Go! 300% for free JRuby 1.0.3 (bm_red_black_tree.rb) 3022.5 15 7.5 0 Java 1.4 Java 5 Java 6 Java 7
  18. 18. Go Java Go! 300% for free JRuby 1.0.3 (bm_red_black_tree.rb) MRI 1.8 3022.5 15 7.5 0 Java 1.4 Java 5 Java 6 Java 7
  19. 19. JVM Benefits
  20. 20. Cross-Platform
  21. 21. JVM is everywhere• Unix++, Windows• Exotic platforms: zLinux, OpenVMS, AS/400• Mobile: Android’s Dalvik, Embedded JVMs
  22. 22. Java bytecode == portability
  23. 23. actionmailer-javamail, active_documentum, activerecord-jdbc-adapter, activerecord-jdbcdbf-adapter, activerecord-jdbcderby-adapter, activerecord-jdbch2-adapter, activerecord-jdbchsqldb-adapter, activerecord-jdbcmssql-adapter, activerecord-jdbcmysql-adapter, activerecord-jdbcpostgresql-adapter, activerecord- jdbcsqlite3-adapter, activerecord-netezza-adapter, activerecord-vertica-adapter, akephalos, akephalos-nerian, akephalos2, akka-actor-jars, akka-remote-jars, akubra_llstore_migrate, Antwrap, async-http-client-jars, atomic, atoulme-Antwrap, autotest-java, bbrowning-deltacloud-client, bbrowning-deltacloud-core, bcrypt-ruby, bee_java, berkeley-db-java-jars, bert, bio-maf, blockenspiel, boc, bond, bosdk, bouncy-castle-java, boxed-geminabox, brute-fuzzy, bryanl-gherkin, bson, buby, buildr, buildr-resolver, buildrizpack, butternut, capistrano-java, capybara-java_script_lint, carrierwave-neo4j, carrierwave_imagevoodoo, cassandra- jars, chrest, cloby, commons-io-jars, concurrently, contextual, coupler, cucumber-java, cucumber-jvm, cuke4duke, cuuid, dm-ldap-adapter, dm-lucene-adapter, do-jdbc_sqlserver, do_derby, do_h2, do_hsqldb, do_jdbc, do_mysql, do_openedge, do_oracle, do_postgres, do_sqlite3, do_sqlserver, doubleshot, dripdrop, dubious, duby, engineyard-visualvm, epall-limelight, errbit_zmq_handler, eurydice, euston, euston-daemons, euston-eventstore, euston-projections, euston- rabbitmq, euston-websites, eventmachine, excemel, faye-websocket, ffi, fig, file-find, fishwife, foreman, forkit, forkjoin, gamelan, gemshit, geoip-jars, get_back, gherkin, glassfish, gravitext-util, gravitext-xmlprod, grizzly, guava-jars, hadoop-find, hashdot-test-daemon, hiredis, hitimes, hope, hostor, hot_bunnies, hourglass, hpricot, http_parser.rb, inline_javascript, iudex, iudex-async-httpclient, iudex-barc, iudex-brutefuzzy-protobuf, iudex-brutefuzzy-service, iudex-char-detector, iudex-core, iudex-da, iudex-filter, iudex-html, iudex-http, iudex-http-test, iudex-httpclient-3, iudex-jetty-httpclient, iudex-rome, iudex-simhash, iudex-worker, ivy-jars, iyyov, jactive_support, java-autotest, java-inline, java2ruby-xmldsig, java_bin, java_inline, java_override, java_properties, java_streamify,java_testing_guff, javabean_xml, javaclass, javaeye4r, javagems, javajake, javaobj, javaobjs, javaobs, javaparse, javasand, javascript-securehash-rails, javascript-state- machine-rails, javascript_auto_include, javascript_eraser, javascript_features, javascript_i18n, javascript_localize, javascript_safe_logger, javascript_util_asset_pack, javascripto, javascripto-rails, jdbc-derby, jdbc-hsqldb, jdbc-jtds, jdbc-openedge, jdbc-openedge-internal, jdbc-postgres, jdbc-sqlite3, jedis-jars, jena-jruby, jessica, jettr, jetty, jetty-jsp, jgeoip, jms4r, jpdfer, jrack_handlers, jrtm, jruby-activemq, jruby-akka_jars, jruby-elasticsearch_jars, jruby- httpclient, jruby-launcher, jruby-management, jruby-metrics, jruby-pageant, jruby-vijava, jruby_gc_stats, jruby_sandbox, jruby_threach, jrubyconf-button, jsmetric4java, json, json-jruby, jsound, kb-activerecord-jdbc-adapter, kirk, kyotocabinet-java, ladle, latex-decode, launchy, libnotify, limelight, linecache, logback, logback-jars, looksee, lumix, mack-javascript, markdownj, maven_irb_plugin, metrics-core-jars, metrics-java, mguymon-buildr, mikka, mini_aether, mirah,mirah_model, miso-java, mixology, mm_mq, mongrel, msgpack-idl-java, msgpack-jruby, multimeter, naether, nanoc-javascript-concatenator, neo4j, neo4j-admin, neo4j-advanced, neo4j-community, neo4j-core, neo4j-enterprise, neo4j-spatial, neo4j-will_paginate, neo4j-wrapper, netty-jars, ning-compress-jars, nio4r,nokogiri, nokogiri-fitzsimmons, nokogiri-maven, nosqoop4u, ontomde-demo-java5, ontomde-java, ontomde-java-frontend, ontomde-uml2-java, ontomde-uml2- kbjava, open_nlp, pacer, pacer-dex, pacer-neo4j, pacer-orient, pelops-jars, persvr, pg_array_parser, protobuf-jars, pry, puma, qtjruby-core, qwirk_active_mq_adapter, qwirk_jms_adapter, rabbitmqadmin-cli, ragweed, rails_javascript_helpers, rakejava, rave, rcov, realityforge-jekyll, realityforge- jekylltask, redcar-bundles, redcar-clojure, redcar-filter-through-command, redcar-groovy, redcar-icons, redcar-javamateview, redcar-javascript, redcar-mirah, redcar-svnkit, redcar-xulrunner-win, RedCloth, refinerycms-javascripts, reigns, revo-nokogiri, rika, rjack-async-httpclient, rjack-commons-codec, rjack-commons-dbcp, rjack-commons-dbutils, rjack-commons-pool, rjack-httpclient-3, rjack-httpclient-4, rjack-icu, rjack-jackson, rjack-jdom, rjack-jets3t, rjack-jetty, rjack-jetty-jsp, rjack-jms, rjack-jms-spec, rjack-logback, rjack-lucene, rjack-maven, rjack-mina, rjack-nekohtml, rjack-protobuf, rjack-qpid-client, rjack-rome, rjack-slf4j, rjack-solr, rjack-tarpit, rjack-xerces, rmagick4j, rmodbus, rtm-javatmapi, rtm-majortom, rtm-ontopia, rtm-tinytim, rtm-tmql, rubeus, rubinius-core- api, ruby-blockcache, ruby-debug-base, ruby-maven, ruby2java, rubydoop, rubyjedi-nokogiri_java, scala-inline, scala-library-jars, scriptty, slf4j, slf4j-jars, slyphon- zookeeper, slyphon-zookeeper_jar, smackr, smartimage, SNMP4JR, solr_sail, spiegela-jruby-httpclient, sproutcore, spymemcached, sqldroid, steamcannon- deltacloud-client, steamcannon-deltacloud-core, stilts-stomp-client, supermarket, svm_toolkit, swt, theduke, thick, to-javascript, torquebox-base, torquebox- cache, torquebox-configure, torquebox-container-foundation, torquebox-core, torquebox-messaging, torquebox-messaging-container, torquebox-naming, torquebox-naming-container, torquebox-security, torquebox-server, torquebox-vfs, torquebox-web, twitter4j4r, UDJrb, unageanu-javaclass, unimidi, universe- javascript, url_escape, weakling, webbit-jars, wildnet-jackson, wildnet-netty, wildnet-server, wildsonet-hazelcast, wildsonet-netty, wildsonet-server, wildsonet- streamer, wrest, wrong, xdojava, xqruby, zookeeper
  24. 24. actionmailer-javamail, active_documentum, activerecord-jdbc-adapter, activerecord-jdbcdbf-adapter, activerecord-jdbcderby-adapter, activerecord-jdbch2-adapter, activerecord-jdbchsqldb-adapter, activerecord-jdbcmssql-adapter, activerecord-jdbcmysql-adapter, activerecord-jdbcpostgresql-adapter, activerecord- jdbcsqlite3-adapter, activerecord-netezza-adapter, activerecord-vertica-adapter, akephalos, akephalos-nerian, akephalos2, akka-actor-jars, akka-remote-jars, akubra_llstore_migrate, Antwrap, async-http-client-jars, atomic, atoulme-Antwrap, autotest-java, bbrowning-deltacloud-client, bbrowning-deltacloud-core, bcrypt-ruby, bee_java, berkeley-db-java-jars, bert, bio-maf, blockenspiel, boc, bond, bosdk, bouncy-castle-java, boxed-geminabox, brute-fuzzy, bryanl-gherkin, bson, buby, buildr, buildr-resolver, buildrizpack, butternut, capistrano-java, capybara-java_script_lint, carrierwave-neo4j, carrierwave_imagevoodoo, cassandra- jars, chrest, cloby, commons-io-jars, concurrently, contextual, coupler, cucumber-java, cucumber-jvm, cuke4duke, cuuid, dm-ldap-adapter, dm-lucene-adapter, do-jdbc_sqlserver, do_derby, do_h2, do_hsqldb, do_jdbc, do_mysql, do_openedge, do_oracle, do_postgres, do_sqlite3, do_sqlserver, doubleshot, dripdrop, dubious, duby, engineyard-visualvm, epall-limelight, errbit_zmq_handler, eurydice, euston, euston-daemons, euston-eventstore, euston-projections, euston- rabbitmq, euston-websites, eventmachine, excemel, faye-websocket, ffi, fig, file-find, fishwife, foreman, forkit, forkjoin, gamelan, gemshit, geoip-jars, get_back, gherkin, glassfish, gravitext-util, gravitext-xmlprod, grizzly, guava-jars, hadoop-find, hashdot-test-daemon, hiredis, hitimes, hope, hostor, hot_bunnies, hourglass, hpricot, http_parser.rb, inline_javascript, iudex, iudex-async-httpclient, iudex-barc, iudex-brutefuzzy-protobuf, iudex-brutefuzzy-service, iudex-char-detector, iudex-core, iudex-da, iudex-filter, iudex-html, iudex-http, iudex-http-test, iudex-httpclient-3, iudex-jetty-httpclient, iudex-rome, iudex-simhash, iudex-worker, ivy-jars, iyyov, jactive_support, java-autotest, java-inline, java2ruby-xmldsig, java_bin, java_inline, java_override, java_properties, java_streamify,java_testing_guff, javabean_xml, javaclass, javaeye4r, javagems, javajake, javaobj, javaobjs, javaobs, javaparse, javasand, javascript-securehash-rails, javascript-state- machine-rails, javascript_auto_include, javascript_eraser, javascript_features, javascript_i18n, javascript_localize, javascript_safe_logger, javascript_util_asset_pack, javascripto, javascripto-rails, jdbc-derby, jdbc-hsqldb, jdbc-jtds, jdbc-openedge, jdbc-openedge-internal, jdbc-postgres, jdbc-sqlite3, No build tools! jedis-jars, jena-jruby, jessica, jettr, jetty, jetty-jsp, jgeoip, jms4r, jpdfer, jrack_handlers, jrtm, jruby-activemq, jruby-akka_jars, jruby-elasticsearch_jars, jruby- httpclient, jruby-launcher, jruby-management, jruby-metrics, jruby-pageant, jruby-vijava, jruby_gc_stats, jruby_sandbox, jruby_threach, jrubyconf-button, jsmetric4java, json, json-jruby, jsound, kb-activerecord-jdbc-adapter, kirk, kyotocabinet-java, ladle, latex-decode, launchy, libnotify, limelight, linecache, logback, logback-jars, looksee, lumix, mack-javascript, markdownj, maven_irb_plugin, metrics-core-jars, metrics-java, mguymon-buildr, mikka, mini_aether, mirah,mirah_model, miso-java, mixology, mm_mq, mongrel, msgpack-idl-java, msgpack-jruby, multimeter, naether, nanoc-javascript-concatenator, neo4j, neo4j-admin, neo4j-advanced, neo4j-community, neo4j-core, neo4j-enterprise, neo4j-spatial, neo4j-will_paginate, neo4j-wrapper, netty-jars, ning-compress-jars, nio4r,nokogiri, nokogiri-fitzsimmons, nokogiri-maven, nosqoop4u, ontomde-demo-java5, ontomde-java, ontomde-java-frontend, ontomde-uml2-java, ontomde-uml2- kbjava, open_nlp, pacer, pacer-dex, pacer-neo4j, pacer-orient, pelops-jars, persvr, pg_array_parser, protobuf-jars, pry, puma, qtjruby-core, qwirk_active_mq_adapter, qwirk_jms_adapter, rabbitmqadmin-cli, ragweed, rails_javascript_helpers, rakejava, rave, rcov, realityforge-jekyll, realityforge- jekylltask, redcar-bundles, redcar-clojure, redcar-filter-through-command, redcar-groovy, redcar-icons, redcar-javamateview, redcar-javascript, redcar-mirah, redcar-svnkit, redcar-xulrunner-win, RedCloth, refinerycms-javascripts, reigns, revo-nokogiri, rika, rjack-async-httpclient, rjack-commons-codec, rjack-commons-dbcp, rjack-commons-dbutils, rjack-commons-pool, rjack-httpclient-3, rjack-httpclient-4, rjack-icu, rjack-jackson, rjack-jdom, rjack-jets3t, rjack-jetty, rjack-jetty-jsp, rjack-jms, rjack-jms-spec, rjack-logback, rjack-lucene, rjack-maven, rjack-mina, rjack-nekohtml, rjack-protobuf, rjack-qpid-client, rjack-rome, rjack-slf4j, rjack-solr, rjack-tarpit, rjack-xerces, rmagick4j, rmodbus, rtm-javatmapi, rtm-majortom, rtm-ontopia, rtm-tinytim, rtm-tmql, rubeus, rubinius-core- api, ruby-blockcache, ruby-debug-base, ruby-maven, ruby2java, rubydoop, rubyjedi-nokogiri_java, scala-inline, scala-library-jars, scriptty, slf4j, slf4j-jars, slyphon- zookeeper, slyphon-zookeeper_jar, smackr, smartimage, SNMP4JR, solr_sail, spiegela-jruby-httpclient, sproutcore, spymemcached, sqldroid, steamcannon- deltacloud-client, steamcannon-deltacloud-core, stilts-stomp-client, supermarket, svm_toolkit, swt, theduke, thick, to-javascript, torquebox-base, torquebox- cache, torquebox-configure, torquebox-container-foundation, torquebox-core, torquebox-messaging, torquebox-messaging-container, torquebox-naming, torquebox-naming-container, torquebox-security, torquebox-server, torquebox-vfs, torquebox-web, twitter4j4r, UDJrb, unageanu-javaclass, unimidi, universe- javascript, url_escape, weakling, webbit-jars, wildnet-jackson, wildnet-netty, wildnet-server, wildsonet-hazelcast, wildsonet-netty, wildsonet-server, wildsonet- streamer, wrest, wrong, xdojava, xqruby, zookeeper
  25. 25. Libraries Lucene Neo4j Selenium47k libraries in MavenHadoop EHCache JMonkeyEngine Sitemesh
  26. 26. Languages/Polyglot Clojure Scala JythonRhino/Nashorn/ GroovyDynJS (JavaScript) Micro Focus JVM Visual COBOL
  27. 27. Polyglot• Use right tool for the right job• Running in the same VM• Another dimension of libraries
  28. 28. GC
  29. 29. GC Matters• Applications grow over time• Ruby is very object-heavy• Multiprocess multiplies the problem• You will eventually have issues
  30. 30. JVM GC• Wide array of options• Many GCs to choose from• Scales up to massive heaps• Best GCs in the world!
  31. 31. OpenJDK GCs• Parallel: multi-core stop-the-world• Concurrent: STW young, concurrent old• G1: concurrent young and old• Serial: single-thread STW
  32. 32. Object Homogeneity• Everything expressed as JVM objects• Everything shares the same infrastructure • Including extensions, libraries, etc• One GC to rule them all • And one standard memory model
  33. 33. gc_demo.rb• Heavy GC, mix of old and young• Steadily growing heap use
  34. 34. class Simple  attr_accessor :nextendtop = Simple.newputs 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. 35. Ruby 1.9.3 JRuby8000600040002000 0 GC count
  36. 36. Ruby 1.9.3 JRuby10000 1000 100 10 1 GC count
  37. 37. 15 Ruby 1.9.3 JRuby11.25 7.5 3.75 0 GC time %
  38. 38. Ruby 2.0.0 JRuby Time per GC versus heap usage 300ms 225msTime per GC 150ms 75ms 0ms 188KB/29MB 27MB/127MB 199MB/238MB Heap usage (MRI/JRuby)
  39. 39. Ruby 2.0.0 JRuby Time per GC versus heap usage :-( :-(User Unhappiness :-/ :-| :-) 188KB/29MB 27MB/127MB 199MB/238MB Heap usage (MRI/JRuby)
  40. 40. Ruby 2.0.0 4,000 3,000Time Per GC (ms) 2,000 1,000 0 0M 1,250M 2,500M 3,750M 5,000M Heap Size
  41. 41. Findings• Lower, more uniform GC times• Reduced or eliminated pauses• Very large heaps are no problem• Predictable, consistent experience
  42. 42. Threads
  43. 43. Real Parallellism• Ruby thread = JVM thread = native thread• One process can use all cores• One server can handle all requests
  44. 44. require benchmarkary = (1..1000000).to_aloop {  puts Benchmark.measure {    10.times {      ary.each {|i|}    }  }}
  45. 45. require benchmarkary = (1..1000000).to_aloop {  puts Benchmark.measure {    (1..10).map {      Thread.new {        ary.each {|i|}      }    }.map(&:join)  }}
  46. 46. Ruby 1.9unthreaded
  47. 47. Ruby 1.9 Ruby 1.9unthreaded threaded
  48. 48. Ruby 1.9 Ruby 1.9unthreaded threaded JRubyunthreaded
  49. 49. Ruby 1.9 Ruby 1.9unthreaded threaded JRuby JRubyunthreaded threaded
  50. 50. threaded_reverse Per-iteration time versus thread count 0.8s0.65s 0.5s0.35s 0.2s one thread two threads three threads four threads
  51. 51. Nonlinear?• More work means more objects• Allocation/GC needs memory bandwidth• No different from multi-process
  52. 52. Tools
  53. 53. Profiling• Java profilers • VisualVM,YourKit, NetBeans, JXInsight• jruby [--profile | --profile.graph]• jruby -Xreify.classes=true• JVM command-line profilers
  54. 54. 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
  55. 55. VisualVM• CPU, memory, thread monitoring• CPU and memory profiling• VisualGC• Heap analysis
  56. 56. JRuby Goodies
  57. 57. Scripting Java
  58. 58. JavaFX straight port Simple Clock
  59. 59. @minute_hand = Path.new.tap do |mh| Draw minute hand mh.setFill(Color::BLACK) mh.getElements.add MoveTo.new(4, -4) mh.getElements.add ArcTo.new(-1, -1, 0, -4, -4, false, false) mh.getElements.add LineTo.new(0, -radius) mh.getTransforms.add Rotate.newend Implement Java Eventclass Refresher include EventHandler handler def handle(event); event.source.refresh; endend refresh timeline every 1sTimeline.new.tap do |time| time.setCycleCount Timeline::INDEFINITE time.getKeyFrames.add(Keyframe.new(Duration.millis(1000)), Refresher.new) time.playend
  60. 60. getFooBar -> fooBar@minute_hand = Path.new.tap do |mh| mh.setFill(Color::BLACK) was getElements mh.elements.add MoveTo.new(4, -4) mh.elements.add ArcTo.new(-1, -1, 0, -4, -4, false, false) mh.elements.add LineTo.new(0, -radius) mh.transforms.add Rotate.newend was getTransformsclass Refresher include EventHandler def handle(event); event.source.refresh; endendTimeline.new.tap do |time| time.setCycleCount Timeline::INDEFINITE time.keyFrames.add(Keyframe.new(Duration.millis(1000)), Refresher.new) time.playend was getKeyFrames
  61. 61. setFoo(a) -> foo = a@minute_hand = Path.new.tap do |mh| mh.fill = Color::BLACK was setFill mh.elements.add MoveTo.new(4, -4) mh.elements.add ArcTo.new(-1, -1, 0, -4, -4, false, false) mh.elements.add LineTo.new(0, -radius) mh.transforms.add Rotate.newendclass Refresher include EventHandler def handle(event); event.source.refresh; endendTimeline.new.tap do |time| was setCycleCount time.cycleCount = Timeline::INDEFINITE time.keyFrames.add(Keyframe.new(Duration.millis(1000)), Refresher.new) time.playend
  62. 62. camelCase -> camel_case@minute_hand = Path.new.tap do |mh| mh.fill = Color::BLACK mh.elements.add MoveTo.new(4, -4) mh.elements.add ArcTo.new(-1, -1, 0, -4, -4, false, false) mh.elements.add LineTo.new(0, -radius) mh.transforms.add Rotate.newendclass Refresher include EventHandler def handle(event); event.source.refresh; endendTimeline.new.tap do |time| was cycleCount time.cycle_count = Timeline::INDEFINITE time.key_frames.add(Keyframe.new(Duration.millis(1000)), Refresher.new) time.playend was keyFrames
  63. 63. Procs as interfaces@minute_hand = Path.new.tap do |mh| mh.fill = Color::BLACK mh.elements.add MoveTo.new(4, -4) mh.elements.add ArcTo.new(-1, -1, 0, -4, -4, false, false) mh.elements.add LineTo.new(0, -radius) mh.transforms.add Rotate.newend No more extra classTimeline.new.tap do |time| time.cycle_count = Timeline::INDEFINITE time.key_frames.add Keyframe.new(Duration.millis(1000)) {refresh} time.playend
  64. 64. Common Adornments ‘add’ becomes ‘<<‘@minute_hand = Path.new.tap do |mh| mh.fill = Color::BLACK mh.elements << MoveTo.new(4, -4) mh.elements << ArcTo.new(-1, -1, 0, -4, -4, false, false) mh.elements << LineTo.new(0, -radius) mh.transforms << Rotate.newendTimeline.new.tap do |time| time.cycle_count = Timeline::INDEFINITE time.key_frames << Keyframe.new(Duration.millis(1000)) {refresh} time.playend
  65. 65. Purugin• Minecraft scripting in JRuby• Clean and simple DSL• Purogo: LOGO for Minecraft
  66. 66. turtle("four-sided triangle") do |*args|  dim = (args[0] || 5).to_i  block_type = (args[1] || :stone).to_sym  layer do    4.times do |i|      forward dim      turnleft 90    end  end    pivot do    block :none    forward 1    turnleft 90    forward 1    turnup 90    forward 1    turndown 90    turnright 90    block block_type  end  block block_type  (1...dim).step(2).to_a.reverse.each do |i|    dim = i    layer    pivot  endend
  67. 67. Pimping a side-projecthttps://github.com/enebo/Purugin
  68. 68. Java Native Extensions
  69. 69. Native Extensions Suck“Look, nobody enjoysshooting penguins, but ifyou have to shoot penguins,well, you might as wellenjoy it.” - Free Waterfall Sr.
  70. 70. Flowchart of ShameCan I write it in Ruby? Script Java? no yes no Does it need Can I use FFI? to be n times faster? no no Native Extension
  71. 71. Java Native Extensions• Written in Java • Have same performance as Java • Use same GC as JRuby • First Class Citizens
  72. 72. Performance
  73. 73. Go Java Go! 300% for free JRuby 1.0.3 (bm_red_black_tree.rb) 3022.5 15 7.5 0 Java 1.4 Java 5 Java 6 Java 7
  74. 74. Go JRuby Go! OpenJDK 8 (bm_red_black_tree.rb)86420 1.0.3 1.1.6 1.4.0 1.5.6 1.6.8 1.7.0
  75. 75. Severe gains??? OpenJDK 8 (bm_red_black_tree.rb) 9 Severe perf bottleneck(s)6.75 4.52.25 0 1.0.3 1.1.6 1.4.0 1.5.6 1.6.8 1.7.0
  76. 76. 1.0.3 & red_black_tree -J-verbose:gc [GC (Allocation Failure) 223608K->96408K(330752K), 0.0159780 secs] [GC (Allocation Failure) 208920K->100792K(335168K), 0.0157550 secs] [GC (Allocation Failure) 213304K->105144K(332160K), 0.0181010 secs]8.1s [GC (Allocation Failure) 205112K->108920K(334400K), 0.0187580 secs] [GC (Allocation Failure) 208888K->112712K(329152K), 0.0154440 secs] +28 more [GC (Allocation Failure) 313780K->199892K(339072K), 0.0142010 secs] [GC (Allocation Failure) 318420K->204420K(331520K), 0.0175690 secs]8.4s [GC (Allocation Failure) 306948K->208316K(335680K), 0.0188120 secs] [Full GC (Ergonomics) 208316K->54991K(352256K), 0.2709750 secs] [GC (Allocation Failure) 157519K->58959K(349248K), 0.0120840 secs] +28 more Moar Objects!
  77. 77. 1.7.0 & red_black_tree-J-verbose:gc [GC (Allocation Failure) 155729K->39697K(207296K), 0.0072730 secs] 0.963s [GC (Allocation Failure) 160785K->40657K(208320K), 0.0108620 secs] 0.968s [GC (Allocation Failure) 161745K->41649K(210112K), 0.0083760 secs] 0.968s [GC (Allocation Failure) 166193K->39729K(210688K), 0.0070670 secs] 0.99s
  78. 78. OpenJDK 8 (bm_red_black_tree.rb) 32.25 1.50.75 0 1.1.6 1.4.0 1.5.6 1.6.8 1.7.0
  79. 79. OpenJDK 8 (bm_red_black_tree.rb) 3 JVM Feature!2.25 1.50.75 0 1.1.6 1.4.0 1.5.6 1.6.8 1.7.0
  80. 80. Performance• JRuby compiles Ruby to JVM bytecode• JVM compiles bytecode to native• Best JIT technology in the world• Getting even better with invokedynamic
  81. 81. How to Optimize Ruby• Do less work • Reduce dispatch and call overhead • Reduce memory overhead• Find static patterns • Profile running code • Emit optimized version with guards
  82. 82. JVM Optimizations• Profiling • Watch code, optimize hottest paths• Inlining • Combine code, optimize as a whole• Escape analysis • Eliminate transient objects
  83. 83. def foo; Object.new; enddef invoker; foo; endi = 0while i < 10000  invoker  i+=1end
  84. 84. Inline foo into invoker def invoker; Object.new; end i = 0 while i < 10000   invoker   i+=1 end
  85. 85. Inline invoker into loop i = 0 while i < 10000   Object.new   i+=1 end
  86. 86. Object is transienti = 0while i < 10000  i+=1end
  87. 87. Loop does nothingi = 10000
  88. 88. Variable i is never read
  89. 89. InvokeDynamic• Java 7+ feature• Allows us to teach JVM about Ruby • Dynamic calls can inline • Constants become truly constant • JVM’s best optimizations can work
  90. 90. Does It Work?
  91. 91. def foo; 1; enddef invoker; foo; endN.times do i = 0 while i < 10000   invoker   i+=1 endend
  92. 92. $ jruby -J-XX:+UnlockDiagnosticVMOptions -J-XX:+PrintInlining -J-XX:+PrintCompilation script.rb 79 1 b java.lang.String::hashCode (55 bytes) 109 2 b java.util.Properties$LineReader::readLine (452 bytes) @ 48 java.io.Reader::read (9 bytes) never executed @ 62 java.io.FilterInputStream::read (9 bytes) @ 303 java.lang.System::arraycopy (0 bytes) (intrinsic) @ 388 java.io.Reader::read (9 bytes) never executed....
  93. 93. def foo; 1; end def invoker; foo; end N.times do i = 0 while i < 10000    invoker    i+=1 end end3321 190 b ruby.__dash_e__::block_0$RUBY$__file__ (83 bytes) @ 6 org.jruby.RubyFixnum::op_lt (22 bytes) inline (hot) @ 66 org.jruby.RubyBasicObject::isTrue (15 bytes) inline (hot) @ 6 ruby.__dash_e__::method__1$RUBY$invoker (9 bytes) inline (hot) @ 6 ruby.__dash_e__::method__0$RUBY$foo (7 bytes) inline (hot) @ 5 org.jruby.RubyFixnum::op_plus_one (31 bytes) inline (hot)
  94. 94. def foo; 1; end def invoker; foo; end N.times do i = 0 while i < 10000    invoker    i+=1 end end3321 190 b ruby.__dash_e__::block_0$RUBY$__file__ (83 bytes) @ 6 org.jruby.RubyFixnum::op_lt (22 bytes) inline (hot) @ 66 org.jruby.RubyBasicObject::isTrue (15 bytes) inline (hot) @ 6 ruby.__dash_e__::method__1$RUBY$invoker (9 bytes) inline (hot) @ 6 ruby.__dash_e__::method__0$RUBY$foo (7 bytes) inline (hot) @ 5 org.jruby.RubyFixnum::op_plus_one (31 bytes) inline (hot)
  95. 95. def foo; 1; end def invoker; foo; end N.times do i = 0 while i < 10000    invoker    i+=1 end end3321 190 b ruby.__dash_e__::block_0$RUBY$__file__ (83 bytes) @ 6 org.jruby.RubyFixnum::op_lt (22 bytes) inline (hot) @ 66 org.jruby.RubyBasicObject::isTrue (15 bytes) inline (hot) @ 6 ruby.__dash_e__::method__1$RUBY$invoker (9 bytes) inline (hot) @ 6 ruby.__dash_e__::method__0$RUBY$foo (7 bytes) inline (hot) @ 5 org.jruby.RubyFixnum::op_plus_one (31 bytes) inline (hot)
  96. 96. def foo; 1; end def invoker; foo; end N.times do i = 0 while i < 10000    invoker    i+=1 end end3321 190 b ruby.__dash_e__::block_0$RUBY$__file__ (83 bytes) @ 6 org.jruby.RubyFixnum::op_lt (22 bytes) inline (hot) @ 66 org.jruby.RubyBasicObject::isTrue (15 bytes) inline (hot) @ 6 ruby.__dash_e__::method__1$RUBY$invoker (9 bytes) inline (hot) @ 6 ruby.__dash_e__::method__0$RUBY$foo (7 bytes) inline (hot) @ 5 org.jruby.RubyFixnum::op_plus_one (31 bytes) inline (hot)
  97. 97. def foo; 1; end def invoker; foo; end N.times do i = 0 while i < 10000    invoker    i+=1 end end3321 190 b ruby.__dash_e__::block_0$RUBY$__file__ (83 bytes) @ 6 org.jruby.RubyFixnum::op_lt (22 bytes) inline (hot) @ 66 org.jruby.RubyBasicObject::isTrue (15 bytes) inline (hot) @ 6 ruby.__dash_e__::method__1$RUBY$invoker (9 bytes) inline (hot) @ 6 ruby.__dash_e__::method__0$RUBY$foo (7 bytes) inline (hot) @ 5 org.jruby.RubyFixnum::op_plus_one (31 bytes) inline (hot)
  98. 98. def foo; 1; end def invoker; foo; end N.times do i = 0 while i < 10000    invoker    i+=1 end end3321 190 b ruby.__dash_e__::block_0$RUBY$__file__ (83 bytes) @ 6 org.jruby.RubyFixnum::op_lt (22 bytes) inline (hot) @ 66 org.jruby.RubyBasicObject::isTrue (15 bytes) inline (hot) @ 6 ruby.__dash_e__::method__1$RUBY$invoker (9 bytes) inline (hot) @ 6 ruby.__dash_e__::method__0$RUBY$foo (7 bytes) inline (hot) @ 5 org.jruby.RubyFixnum::op_plus_one (31 bytes) inline (hot)
  99. 99. def foo; 1; end def invoker; foo; end N.times do i = 0 while i < 10000    invoker    i+=1 end end3321 190 b ruby.__dash_e__::block_0$RUBY$__file__ (83 bytes) @ 6 org.jruby.RubyFixnum::op_lt (22 bytes) inline (hot) @ 66 org.jruby.RubyBasicObject::isTrue (15 bytes) inline (hot) @ 6 ruby.__dash_e__::method__1$RUBY$invoker (9 bytes) inline (hot) @ 6 ruby.__dash_e__::method__0$RUBY$foo (7 bytes) inline (hot) @ 5 org.jruby.RubyFixnum::op_plus_one (31 bytes) inline (hot)
  100. 100. def foo; 1; end def invoker; foo; end N.times do i = 0 while i < 10000    invoker    i+=1 end end3321 190 b ruby.__dash_e__::block_0$RUBY$__file__ (83 bytes) @ 6 org.jruby.RubyFixnum::op_lt (22 bytes) inline (hot) @ 66 org.jruby.RubyBasicObject::isTrue (15 bytes) inline (hot) @ 6 ruby.__dash_e__::method__1$RUBY$invoker (9 bytes) inline (hot) @ 6 ruby.__dash_e__::method__0$RUBY$foo (7 bytes) inline (hot) @ 5 org.jruby.RubyFixnum::op_plus_one (31 bytes) inline (hot)
  101. 101. Does It Help?
  102. 102. JRuby/Java 6 JRuby/Java 7
  103. 103. JRuby/Java 6 JRuby/Java 7 Times Faster than Ruby 1.9.3 53.75 2.51.25 0 base64 richards neural mandelbrot redblack
  104. 104. JRuby/Java 6 JRuby/Java 7 Times Faster than Ruby 1.9.3 53.75 2.5 1.914 1.806 1.538 1.5651.25 1.346 0 base64 richards neural mandelbrot redblack
  105. 105. JRuby/Java 6 JRuby/Java 7 Times Faster than Ruby 1.9.3 5 4.226 4.323.75 3.66 3.44 2.5 2.658 1.914 1.806 1.538 1.5651.25 1.346 0 base64 richards neural mandelbrot redblack
  106. 106. smooth_sort# Original Author: Keith Schwarz (htiek@cs.stanford.edu)## Translated to Ruby by Chuck Remes (chuckremes on github)## An implementation of Dijkstras Smoothsort algorithm, a modification of# heapsort that runs in O(n lg n) in the worst case, but O(n) if the data# are already sorted. For more information about how this algorithm works# and some of the details necessary for its proper operation, please see## http://www.keithschwarz.com/smoothsort/
  107. 107. Ruby 1.8.7 Ruby 1.9.3 Ruby 2.0.0 JRuby100000 75000 50000 25000 19590.7 11439 4750 0 Iterations per second
  108. 108. And Rails?
  109. 109. 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!• JRuby BOF in Room C at 6:30PM
  110. 110. JRUBY BOOK
  111. 111. Thank you!• @headius• jruby.org• torquebox.org

×