13. 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!
14. 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!
23. 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
25. 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
26. 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
27. 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
53. 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
54.
55. 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
56. 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
57. 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
}
58. 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)
75. 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
82. 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
83. 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
84.
85.
86. 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
87.
88.
89.
90. 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