The Future of JRuby - Baruco 2013
-
Upload
charles-nutter -
Category
Technology
-
view
2.502 -
download
1
description
Transcript of The Future of JRuby - Baruco 2013
![Page 1: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/1.jpg)
The FUTURE of
Tuesday, September 17, 13
![Page 2: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/2.jpg)
Me
• Charles Oliver Nutter
• @headius
• Java developer since 1996
• JRuby developer since 2006
• Red Hat / JBoss polyglot group
Tuesday, September 17, 13
![Page 3: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/3.jpg)
Have you heard ofJRuby?
Tuesday, September 17, 13
![Page 4: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/4.jpg)
Have you triedJRuby?
Tuesday, September 17, 13
![Page 5: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/5.jpg)
Are you usingJRuby?
Tuesday, September 17, 13
![Page 6: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/6.jpg)
What is JRuby?
Tuesday, September 17, 13
![Page 7: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/7.jpg)
Ruby on the JVM
Tuesday, September 17, 13
![Page 8: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/8.jpg)
Ruby on the JVM
I don't like Java so I don't like JRuby
Tuesday, September 17, 13
![Page 9: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/9.jpg)
Ruby on the JVM
I don't like Java so I don't like JRuby
LOL applet
s
Tuesday, September 17, 13
![Page 10: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/10.jpg)
Ruby on the JVM
JVM SUCKS R
OFL
I don't like Java so I don't like JRuby
LOL applet
s
Tuesday, September 17, 13
![Page 11: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/11.jpg)
Ruby on the JVM
JVM SUCKS R
OFLAbstractMetaRubyImplementationFactoryFactoryImpl
I don't like Java so I don't like JRuby
LOL applet
s
Tuesday, September 17, 13
![Page 12: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/12.jpg)
Welcome to Spain
Tuesday, September 17, 13
![Page 13: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/13.jpg)
Welcome to Spain
Tuesday, September 17, 13
![Page 14: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/14.jpg)
Welcome to Spain
Tuesday, September 17, 13
![Page 15: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/15.jpg)
Welcome to Spain
Tuesday, September 17, 13
![Page 16: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/16.jpg)
Welcome to Spain
Tuesday, September 17, 13
![Page 17: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/17.jpg)
Welcome to Spain
Tuesday, September 17, 13
![Page 18: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/18.jpg)
Welcome to Spain
Tuesday, September 17, 13
![Page 19: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/19.jpg)
JRuby is Ruby!!!on the JVM... shhh!
Tuesday, September 17, 13
![Page 20: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/20.jpg)
The Basics• Compatible with Ruby 1.8.7, 1.9.3
• Mostly written in (clean) Java
• More and more in Ruby going forward
• Entire world of JVM libraries available
JVM
JDK Classes Other Libraries
JRuby Core Classes JRuby Runtime
More Core Classes Standard Lib Extras
Your Application
FFITuesday, September 17, 13
![Page 21: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/21.jpg)
Roadmap
1.6.0 1.6.1
1.6.2 1.6.3 1.6.7.2
1.7.0.pre1
1.6.4 ... 1.6.8
...1.7.3 1.7.4 1.7.5
1.7.6
...9000!
Next week or two
Tuesday, September 17, 13
![Page 22: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/22.jpg)
JRuby 9000 really is the next version...
9k...Coming 2014
Tuesday, September 17, 13
![Page 23: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/23.jpg)
One point release later...
Tuesday, September 17, 13
![Page 24: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/24.jpg)
9K Questions
• Ruby 2.0 or 2.1-only?
• 1.8 support gone
• 1.9 support gone
• Java 7+ only?
• New compiler will be... ?
Tuesday, September 17, 13
![Page 25: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/25.jpg)
Why JRuby?
Tuesday, September 17, 13
![Page 26: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/26.jpg)
JRuby Team
Tuesday, September 17, 13
![Page 27: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/27.jpg)
JRuby Team
Charlie
Tuesday, September 17, 13
![Page 28: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/28.jpg)
JRuby Team
Charlie Tom
Nick Hiro Marcin Nahi Wayne Subbu DouglasDouglasContribsDouglas
Tuesday, September 17, 13
![Page 29: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/29.jpg)
JRuby Team
Charlie Tom
Nick Hiro Marcin Nahi Wayne Subbu DouglasDouglasContribs
DouglasDouglasOpenJDKDouglasDouglasAndroid
DouglasDouglasJ9DouglasDouglasOther
JVMs
Douglas
Tuesday, September 17, 13
![Page 30: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/30.jpg)
JVM Over Time
0
7.5
15
22.5
30
Java 1.4 Java 5 Java 6 Java 7
JRuby 1.0.3 (bm_red_black_tree.rb)
Tuesday, September 17, 13
![Page 31: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/31.jpg)
Versus MRI 1.8
0
7.5
15
22.5
30
Java 1.4 Java 5 Java 6 Java 7
JRuby 1.0.3 (bm_red_black_tree.rb) MRI 1.8
Tuesday, September 17, 13
![Page 32: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/32.jpg)
0ms
75ms
150ms
225ms
300ms
188KB/29MB 27MB/127MB 199MB/238MB
Time per GC versus heap usage
Tim
e pe
r G
C
Heap usage (MRI/JRuby)
Ruby 2.0.0 JRuby
Tuesday, September 17, 13
![Page 33: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/33.jpg)
Features Over Time
1.6 1.8.4 1.8.6 1.8.7 1.9.2 1.9.3 2.0 2.1
Ruby FeaturesJRuby Support
Tuesday, September 17, 13
![Page 34: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/34.jpg)
However...
• JVM development is not fast
• JRuby must move forward
• Constantly improving
Tuesday, September 17, 13
![Page 35: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/35.jpg)
What could be better?
Performance
Native Extensions
Concurrency
Startup Time
Tuesday, September 17, 13
![Page 36: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/36.jpg)
Performance
Tuesday, September 17, 13
![Page 37: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/37.jpg)
Hard to Optimize
• Dynamic calls with lots of overhead
• Dynamic object structure with indirection
• Lots and lots of objects
Tuesday, September 17, 13
![Page 38: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/38.jpg)
Solutions
Tuesday, September 17, 13
![Page 39: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/39.jpg)
Compile to Bytecode
• JVM likes JVM bytecode (surprise!)
• Simple compilation of Ruby
• Let JVM do the work
• Can we do better?
Tuesday, September 17, 13
![Page 40: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/40.jpg)
Invokedynamic
• New JVM feature for languages
• Bytecode + IR to describe calls
• JVM patches straight through
• Optimize any kind of call like Java
• Ruby as fast as Java...in theory
Tuesday, September 17, 13
![Page 41: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/41.jpg)
Tuesday, September 17, 13
![Page 42: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/42.jpg)
0 1 2 3 4
ruby-1.9.3 + Ruby
ruby-2.0.0 + Ruby
maglev + Ruby
macruby-0.12 + Ruby
rbx-2.0.0rc1 + Ruby
ruby-1.9.3 + C ext
ruby-2.0.0 + C ext
jruby + Ruby
jruby + Java ext
red/black tree, pure Ruby versus native
Runtime per iteration
Tuesday, September 17, 13
![Page 43: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/43.jpg)
0 1 2 3 4
ruby-1.9.3 + Ruby
ruby-2.0.0 + Ruby
maglev + Ruby
macruby-0.12 + Ruby
rbx-2.0.0rc1 + Ruby
ruby-1.9.3 + C ext
ruby-2.0.0 + C ext
jruby + Ruby
jruby + Java ext
3.96s
2.48s
red/black tree, pure Ruby versus native
Runtime per iteration
Tuesday, September 17, 13
![Page 44: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/44.jpg)
0 1 2 3 4
ruby-1.9.3 + Ruby
ruby-2.0.0 + Ruby
maglev + Ruby
macruby-0.12 + Ruby
rbx-2.0.0rc1 + Ruby
ruby-1.9.3 + C ext
ruby-2.0.0 + C ext
jruby + Ruby
jruby + Java ext
3.96s
2.48s
1.39s
1.19s
red/black tree, pure Ruby versus native
Runtime per iteration
Tuesday, September 17, 13
![Page 45: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/45.jpg)
0 1 2 3 4
ruby-1.9.3 + Ruby
ruby-2.0.0 + Ruby
maglev + Ruby
macruby-0.12 + Ruby
rbx-2.0.0rc1 + Ruby
ruby-1.9.3 + C ext
ruby-2.0.0 + C ext
jruby + Ruby
jruby + Java ext
3.96s
2.48s
1.39s
1.19s
0.51s
0.51s
0.51s
red/black tree, pure Ruby versus native
Runtime per iteration
Tuesday, September 17, 13
![Page 46: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/46.jpg)
0 1 2 3 4
ruby-1.9.3 + Ruby
ruby-2.0.0 + Ruby
maglev + Ruby
macruby-0.12 + Ruby
rbx-2.0.0rc1 + Ruby
ruby-1.9.3 + C ext
ruby-2.0.0 + C ext
jruby + Ruby
jruby + Java ext
3.96s
2.48s
1.39s
1.19s
0.51s
0.51s
0.51s
0.29s
0.1s
red/black tree, pure Ruby versus native
Runtime per iteration
Tuesday, September 17, 13
![Page 47: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/47.jpg)
But...
• Indy was really slow in first Java 7 release
• Got fast in 7u2...and turned out broken
• Rewritten for 7u40
• Slow to warm up
• Getting reports that there's still issues
• Java 8 due in March
Tuesday, September 17, 13
![Page 48: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/48.jpg)
Other Options
• New IR compiler/runtime in 9k
• Optimize Ruby code before JVM
• Specialize types, elide allocations
Tuesday, September 17, 13
![Page 49: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/49.jpg)
LexicalAnalysisParsing
SemanticAnalysis
Optimization
Bytecode Generation
Interpret
AST
IR Instructions
CFG DFG ...
Existing
New!
Dalvik Generation ...
Tuesday, September 17, 13
![Page 50: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/50.jpg)
1 check_arity(2, 0, -1)2 a(0:0) = recv_pre_reqd_arg(0)3 thread_poll4 line_num(2)5 %v_2 = call(+, a(0:0), [1:Fixnum])6 return(%v_2)
-Xir.passes=LocalOptimizationPass,DeadCodeElimination
def foo(a, b) c = 1 d = a + cend
0 check_arity(2, 0, -1)1 a(0:0) = recv_pre_reqd_arg(0)2 b(0:1) = recv_pre_reqd_arg(1)3 %block(0:2) = recv_closure4 thread_poll5 line_num(1)6 c(0:3) = 1:fixnum7 line_num(2)8 %v_0 = call(+, a(0:0), [c(0:3)])9 d(0:4) = copy(%v_0)10 return(%v_0)
Optimization
propagation
Tuesday, September 17, 13
![Page 51: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/51.jpg)
Other Options
• Truffle/Graal
• New compiler backends from Oracle
• Graal = direct API to native JIT
• Truffle = magic optimizing AST atop Graal
• Ruby on Truffle 5x-6x faster than JRuby
• But...
Tuesday, September 17, 13
![Page 52: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/52.jpg)
The Hard Part {
Tuesday, September 17, 13
![Page 53: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/53.jpg)
Sooo....
• Keep working with JVM guys on InDy
• Get our own optimizing compiler done
• Explore Graal/Truffle backend
• Compiler geeks wanted! :-)
Tuesday, September 17, 13
![Page 54: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/54.jpg)
Concurrency
Tuesday, September 17, 13
![Page 55: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/55.jpg)
True Parallellism
Ruby Threads
NativeThreads
Ruby 1.8.7 Ruby 2.0.0
Green Threading
CPU Coresin Use
JRuby
Global LockSingle Thread Real Threading
Tuesday, September 17, 13
![Page 56: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/56.jpg)
Multicore in MRI
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
Ten-way concurrency * 200MB = 2GB
Tuesday, September 17, 13
![Page 57: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/57.jpg)
Multicore in MRI
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
100-way concurrency * 200MB = 20GB
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
200MB MRI Instance
Tuesday, September 17, 13
![Page 58: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/58.jpg)
Multicore in JRuby
300MB JRuby Instance
One instance across 10 threads = 300MB
Tuesday, September 17, 13
![Page 59: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/59.jpg)
Multicore in JRuby
300MB JRuby Instance
One instance across 100 threads = 300MB
Tuesday, September 17, 13
![Page 60: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/60.jpg)
But...
• Ruby world is still growing up
• Concurrency tools being created
• Libraries being made threadsafe
• We need to do more to help
Tuesday, September 17, 13
![Page 61: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/61.jpg)
Unsafe Operations
• Concurrent read+write on core structures
• Non-atomic updates
•@count +=1
•@cache ||= MyCache.new
• Thread pooling
• Coordinating threads
Tuesday, September 17, 13
![Page 62: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/62.jpg)
thread_safe
• Concurrency-safe Hash
• Concurrency-safe Array
require 'thread_safe'
sa = ThreadSafe::Array.newsh = ThreadSafe::Hash.new
Tuesday, September 17, 13
![Page 63: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/63.jpg)
hamster• Persistent collections for Ruby
• A la Clojure and others
simon = Hamster.hash(:name => "Simon", :gender => :male) simon[:name] # => "Simon"simon.get(:gender) # => :male james = simon.put(:name, "James") # => {:name => "James", :gender => :male}simon # => {:name => "Simon", :gender => :male}james[:name] # => "James"simon[:name] # => "Simon" male = simon.delete(:name) # => {:gender => :male}simon # => {:name => "Simon", :gender => :male}male.has_key?(:name) # => falsesimon.has_key?(:name) # => true
Tuesday, September 17, 13
![Page 64: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/64.jpg)
atomic
• Atomic value holder
• Safely update current value
• Edit value only if unchanged
• Full CPU-level atomicity guarantees
Tuesday, September 17, 13
![Page 65: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/65.jpg)
require 'atomic'
my_atomic = Atomic.new(0)my_atomic.value # => 0my_atomic.value = 1my_atomic.swap(2) # => 1my_atomic.compare_and_swap(2, 3) # => true, updated to 3my_atomic.compare_and_swap(2, 3) # => false, current is not 2
my_atomic = Atomic.new(0)my_atomic.update {|v| v + 1}begin my_atomic.try_update {|v| v + 1}rescue Atomic::ConcurrentUpdateError => cue # deal with it (retry, propagate, etc)end
Tuesday, September 17, 13
![Page 66: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/66.jpg)
jo
• Threaded implementation of "goroutines"
• "channel" for communication
Tuesday, September 17, 13
![Page 67: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/67.jpg)
# pinger ponger printerdef pinger(c) 20.times { c << 'ping' }end
def ponger(c) 20.times { c << 'pong' }end
def printer(c) 40.times do puts c.take sleep 1 endend
c = chanjo {pinger(c)} # all on separate threadsjo {ponger(c)}jo {printer(c)}
Tuesday, September 17, 13
![Page 68: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/68.jpg)
Bottom Line
• Concurrency can work in Ruby
• Use the right tools and patterns
• Immutability FTW
• Test your apps and libs on JRuby!
Tuesday, September 17, 13
![Page 69: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/69.jpg)
Native Extensions
Tuesday, September 17, 13
![Page 70: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/70.jpg)
Why Not Ruby?
• Performance
• Fine grained (lots of calls down to C)
• Coarse grained (toss work over the wall)
• Library access
Tuesday, September 17, 13
![Page 71: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/71.jpg)
JRuby 1.6 C Exts
• Limited support (now disabled)
• Will be moved to external gem
• If you want it, support it
• Some stuff worked...most didn’t
Tuesday, September 17, 13
![Page 72: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/72.jpg)
Problems
• Performance
• Data copying to emulate raw structs
• Locking to keep C code thread-safe
• Multiple JRuby instances in one JVM
• No way from C to know which one
• Huge API to support
Tuesday, September 17, 13
![Page 73: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/73.jpg)
Alternatives
Tuesday, September 17, 13
![Page 74: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/74.jpg)
Java Integration
• Call Java (Scala, Clojure, ...) from Ruby
• Smart mapping of method names
• Type conversions as appropriate
• Super easy and fun
Tuesday, September 17, 13
![Page 75: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/75.jpg)
import javax.swing.JFrameimport javax.swing.JLable
frame = JFrame.new("Window")label = JLabel.new("Hello")
frame.add(label)frame.default_close_operation = JFrame::EXIT_ON_CLOSEframe.packframe.visible = true
Tuesday, September 17, 13
![Page 76: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/76.jpg)
Java Native Extensions
• Similar to C ext for MRI, but with Java
• Fast call protocol...basically free
• Same GC for all objects
• Have to keep in sync if C version too
Tuesday, September 17, 13
![Page 77: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/77.jpg)
FFI
• Ruby API/DSL for calling native code
• Runs on all Ruby impls
• Maintained by JRuby team!
• Solves "access" use case
• Works well for coarse-grained calls
Tuesday, September 17, 13
![Page 78: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/78.jpg)
Ruby FFI exampleclass Timeval < FFI::Struct layout :tv_sec => :ulong, :tv_usec => :ulongend
module LibC extend FFI::Library ffi_lib FFI::Library::LIBC attach_function :gettimeofday, [ :pointer, :pointer ], :intend
t = Timeval.newLibC.gettimeofday(t.pointer, nil)
Tuesday, September 17, 13
![Page 79: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/79.jpg)
But...
• Struct binding issues
• Across OSes (also 32+64 bit archs)
• Across library versions
• Library compile option mismatches
• Fine-grained perf sometimes suffers
Tuesday, September 17, 13
![Page 80: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/80.jpg)
Ruby FFI Generator
• https://github.com/neelance/ffi-gen
• Clang-based Ruby FFI generator
• Used to generate clang binding it uses
• It's meta!
Tuesday, September 17, 13
![Page 81: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/81.jpg)
require "ffi/gen"
FFI::Gen.generate( module_name: "Clang", ffi_lib: "clang", headers: ["clang-c/Index.h"], cflags: `llvm-config --cflags`.split(" "), prefixes: ["clang_", "CX"], output: "clang-c/index.rb")
Tuesday, September 17, 13
![Page 82: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/82.jpg)
# A single translation unit, which resides in an index. class TranslationUnitImpl < FFI::Struct layout :dummy, :char end
# Identifies a specific source location within a translation # unit. # # Use clang_getExpansionLocation() or clang_getSpellingLocation() # to map a source location to a particular file, line, and column. # # = Fields: # :ptr_data :: # (Array<FFI::Pointer(*Void)>) # :int_data :: # (Integer) class SourceLocation < FFI::Struct layout :ptr_data, [:pointer, 2], :int_data, :uint end
Tuesday, September 17, 13
![Page 83: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/83.jpg)
# Retrieves the source location associated with a given file/line/column # in a particular translation unit. # # @method get_location(tu, file, line, column) # @param [TranslationUnitImpl] tu # @param [FFI::Pointer(File)] file # @param [Integer] line # @param [Integer] column # @return [SourceLocation] # @scope class attach_function :get_location, :clang_getLocation, [TranslationUnitImpl, :pointer, :uint, :uint], SourceLocation.by_value
Tuesday, September 17, 13
![Page 84: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/84.jpg)
XNI
• Ruby + plain old C
• Covers access and perf cases
• Cross-implementation support
• Struct mapping in compile phase
• Experimental
https://github.com/wmeissner/xni
Tuesday, September 17, 13
![Page 85: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/85.jpg)
hitimes C Ext/** * call-seq: * interval.start -> boolean * * mark the start of the interval. Calling start on an already started * interval has no effect. An interval can only be started once. If the * interval is truely started +true+ is returned otherwise +false+. */VALUE hitimes_interval_start( VALUE self ){ hitimes_interval_t *i; VALUE rc = Qfalse;
Data_Get_Struct( self, hitimes_interval_t, i ); if ( 0L == i->start_instant ) { i->start_instant = hitimes_get_current_instant( ); i->stop_instant = 0L; i->duration = -1.0l;
rc = Qtrue; }
return rc;}
Tuesday, September 17, 13
![Page 86: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/86.jpg)
hitimes XNI/** * call-seq: * interval.start -> boolean * * mark the start of the interval. Calling start on an already started * interval has no effect. An interval can only be started once. If the * interval is truely started +true+ is returned otherwise +false+. */bool hitimes_interval_start( RubyEnv* env, hitimes_interval_t* i ){ if ( 0L == i->start_instant ) { i->start_instant = hitimes_get_current_instant( ); i->stop_instant = 0L; i->duration = -1.0l;
return true; }
return false;}
Tuesday, September 17, 13
![Page 87: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/87.jpg)
Startup Time
Tuesday, September 17, 13
![Page 88: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/88.jpg)
#1 Pain Point
Tuesday, September 17, 13
![Page 89: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/89.jpg)
Hard Problem
• MRI boot time is 95% native
• JRuby boot time is 0% native code
• Mostly Java, which needs to warm up
• Parser, interpreter, core classes, compiler
• Even if our code is better, we start slow
Tuesday, September 17, 13
![Page 90: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/90.jpg)
Child Processes
• Reduce need for sub-Ruby invokes
• rails -> clean rails env in child
• rails/rake -> bundler relaunch
• rake test -> 4+ processes in Rails
• rake -> rspec in subprocess
• Fix requires changing many libraries
Tuesday, September 17, 13
![Page 91: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/91.jpg)
LexicalAnalysisParsing
SemanticAnalysis
Optimization
Bytecode Generation
Interpret
AST
IR Instructions
CFG DFG ...
Existing
MORE
Dalvik Generation ...
Tuesday, September 17, 13
![Page 92: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/92.jpg)
Solutions
Tuesday, September 17, 13
![Page 93: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/93.jpg)
Nailgun/Drip
• Always running background JVM
• Not quite production quality
• signals, IO
• Small Ruby scripts very fast
• Rails not much faster
• Lots of requires, objects, boot logic
Tuesday, September 17, 13
![Page 94: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/94.jpg)
GSoC 2012IR Persistence
IR Instructions
CFG DFG ...
file.ir
file.rbcompile
file.ccompile
file.o
Tuesday, September 17, 13
![Page 95: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/95.jpg)
Reflection on GSoC
• Size matters
• # of bytes
• Intern()‘ing of identifiers matter
• Laziness can help a lot
Tuesday, September 17, 13
![Page 96: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/96.jpg)
Defined vs Used MethodsCMD DEFINED USED SAVINGS
-e ‘:foo’ 501 33 ~93%
gem install rails
1897 529 ~72%
rails scaffold 9411 1647 ~82%
rake db:migrate
9397 1662 ~82%
rake spec 4595 904 ~80%
Tuesday, September 17, 13
![Page 97: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/97.jpg)
New IR Persistence
• Binary format
• Constant pool to intern only once per id
• (currently once per occurrence)
• Incremental loading of method bodies
Tuesday, September 17, 13
![Page 98: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/98.jpg)
Ultimate Startup!
rails new fooJRuby Instance
IR Datarails generate
JRuby Instance
rake db:migrate
JRuby Instance
Compile
Use
Use
Background JVM
Tuesday, September 17, 13
![Page 99: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/99.jpg)
Ruby is Strong
• Still growing and improving
• MRI too!
• Concurrency can be done
• C extensions are holding us back
• Never surrender!
Tuesday, September 17, 13
![Page 100: The Future of JRuby - Baruco 2013](https://reader038.fdocuments.net/reader038/viewer/2022110113/5403aab88d7f72294d8b4858/html5/thumbnails/100.jpg)
Thank You!
• Charles Oliver Nutter
• @headius
• http://blog.headius.com
Tuesday, September 17, 13