Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby Performance - The Last Mile - RubyConf India 2016

headius
March 20, 2016

Ruby Performance - The Last Mile - RubyConf India 2016

Talk on how JRuby is bringing performance and concurrency to Ruby, delivered at RubyConf India 2016 in Kochi, Kerala, India.

headius

March 20, 2016
Tweet

More Decks by headius

Other Decks in Technology

Transcript

  1. What you love from Ruby • Latest Ruby language features

    • Mostly-same Ruby standard library • Pure-Ruby gems work great • Native gems with JRuby support • It walks like Ruby, talks like Ruby • It is Ruby!
  2. With the power of the JVM • Fast JIT to

    native code • Fully parallel threading • Leading-edge garbage collectors • Access to Java, Scala, Clojure, ... • But it's still Ruby!
  3. What do you optimize for? • Easy to develop with:

    short time until first deploy • Fast startup: good response cycle at command line • Straight-line performance, many operations per second • Parallelism: utilize many cores to get more done
  4. Production Quality? • Support for 99%+ of Ruby language features

    • Important parts of standard library • Runs typical Ruby applications and libraries • Healthy extension ecosystem • CRuby, JRuby are the only real options right now
  5. CRuby (MRI) • Up until 1.9, AST interpreter • YARV

    bytecode VM introduced for 1.9.0 • GC and performance improvements through 2.x series • Ruby 2.3 is latest, released in December • Future work on JIT, GC, happening now
  6. JRuby • Many redesigns since creation in 2001 • AST

    interpreter until 2007 • Simple AST-to-bytecode JIT until JRuby 9000 • Optimizing compiler with JIT for 9k • JRuby 9.0.5 is current, 9.1 in a couple weeks • Next-gen Truffle runtime in the works
  7. Block Pass-through loop {
 puts Benchmark.measure {
 i = 0


    while i < 1_000_000
 i+=1
 foo { }; foo { }; foo { }; foo { }; foo { }
 end
 }
 }
  8. csv.rb Converters Converters = {
 integer: lambda { |f|
 Integer(f.encode(ConverterEncoding))

    rescue f
 },
 float: lambda { |f|
 Float(f.encode(ConverterEncoding)) rescue f
 },
  9. Concurrency? Parallelism? • Parallelism happens on the harder, e.g. multi-core

    • Concurrency happens in the software, e.g. Thread API • You can have concurrency without parallelism • You can have both with JRuby
  10. Parallelism in Ruby • On CRuby, usually process-level • Ruby

    threads prevented from running in parallel • Extensions, IO can opt to release lock • On JRuby, usually thread-level • Ruby thread == JVM thread == OS thread • Single-process, shared memory
  11. A Mailing Queue • A simple example of concurrency •

    For each job, construct an email to send • Some computation added to make processing heavier • "Ruby Concurrency and Parallelism: A Practical Tutorial"
 https://www.toptal.com/ruby/ruby-concurrency-and-parallelism-a- practical-primer
  12. require "./lib/mailer"
 require "benchmark"
 
 puts Benchmark.measure{
 (ARGV[0] || 10_000).times

    do |i|
 Mailer.deliver do
 from "eki_#{i}@eqbalq.com"
 to "jill_#{i}@example.com"
 subject "Threading and Forking (#{i})"
 body "Some content"
 end
 end
 }
  13. POOL_SIZE = (ARGV[0] || 10).to_i
 
 jobs = Queue.new
 


    (ARGV[1] || 10_000).to_i.times{|i| jobs.push i}
 
 workers = (POOL_SIZE).times.map do
 Thread.new do
 begin
 while x = jobs.pop(true)
 Mailer.deliver do
 ...
 end
 end
 rescue ThreadError
 end
 end
 end
 
 workers.map(&:join)
  14. CRuby: mailer * 1000 Time in Seconds 0 37.5 75

    112.5 150 synchronous 4 threads 4 forks
  15. JRuby vs MRI Times Improvement 0 0.85 1.7 2.55 3.4

    CRuby Forks JRuby Threads 3.37x 3.09x
  16. Great Features, Hidden Costs • Blocks are expensive to create,

    slower than method calls • case/when is an O(n) cascade of calls • Singleton classes/methods are costly and hurt method cache • Literal arrays, hashes, strings have to be constructed, GCed • Flow-control exceptions can be very expensive and hard to find
  17. CRuby Tooling • Basic GC stats built in • Simple

    profilers in standard library • Some third-party tools • stackprof, ruby-prof, perftools.rb, ...
  18. JVM Tooling • Wide range of GCs: parallel, concurrent, realtime,

    pauseless • Built-in tools for analyzing GC, JIT, thread, IO, heap • Built-in remote monitoring via JMX • Dozens of tools out there for profiling, management, and more
  19. VisualVM • Graphical console into your application • Monitor GC,

    threads, CPU usage • Sampled or full profiling with GUI browser • Live memory dumping, heap inspection • Ships with every OpenJDK install
  20. Java Mission Control • Extremely low-overhead application recording • Analyze

    results offline in JMC • GC, CPU, heap events, IO operation all browsable • Commercial feature, free for development use
  21. More on GC • JVM GCs are incredibly tunable with

    sensible defaults • Tools like http://gceasy.io and JClarity give you a deeper view • These are the best GCs and the best tools in the world
  22. Profiling Tools • Command line options: --profile, --sample • JVM

    command-line profilers like prof • Many graphical sampling/complete profiling options • Flame graphs, stack profilers, you name it!