Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. What's New in Ruby 2.0 February 22nd, 2013. URABE, Shyouhei. PFSYS Dept., GPS Div., DeNA Co., Ltd.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Ruby 2.0.0 is coming ● Ruby, version 2.0.0, is appearing in Feb. 24th, 2013. What's expected? – More syntax – More speed – More feature – … and maybe more!
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Ruby and “Good Parts” “Ruby is designed to be fun.” – Yukihiro Matsumoto 出典 : 「オブジェクト指向スクリプト言語 Ruby 」
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Ruby and “Good Parts” “I was socked. Ruby was the frst language emphasized on our feeling.” – Dr. Shinichiro Hara http://www.nicovideo.jp/watch/sm6516554
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Ruby and “Good Parts” “The real paradigm shift is in the fact that Ruby was designed to make programming fast, enjoyable and easy instead of being optimized for machines running it.” – Matt Aimonetti http://merbist.com/2009/11/16/the-ruby-revolution-take-ii/
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. What drives us “I was delighted to refect on the thought that they were right there in the values of Ward, Kent, and all the other people who've been advocating clean code, well-factored object-oriented design, and testability. These ideas have made a great impact on many other technological communi- ties, but in Ruby-land they are the orthodoxy.” – Martin Fowler http://www.martinfowler.com/bliki/RailsConf2007.html
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. What drives us “Ruby doesn't change your company. It is you who are changed. You are to change the world.” – Shunichi Arai 広島 Ruby 会議 01 での発言
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Why Ruby 2.0 ● Because we change things. ● We are eager to make ourself more and more happy, healthy, productive. ● Don't be afraid. “Embrace the change.”
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. What's New in Ruby 2.0 February 22nd, 2013. URABE, Shyouhei. PFSYS Dept., GPS Div., DeNA Co., Ltd.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. What's NOT new in Ruby 2 ● Almost everything. ● “100% backward compatible”, said matz. – In reality … ● (for instance) Rails runs as-is.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. New Syntax in Ruby 2.0 February 22nd, 2013. URABE, Shyouhei. PFSYS Dept., GPS Div., DeNA Co., Ltd.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Keyword arguments ● Following code was already OK since 1.x. ● So, what's the problem? obj.method "with", :lots => "of", :args => "in", :hash => "form"
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Keyword arguments ● The problem is on method definition: def obj.method(arg, hash) lots = Hash[:lots] || "default" args = Hash[:args] || "another" hand = Hash[:by hand] || "annoying" … end Hand-written code == root cause of bugs
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Keyword arguments ● Introducing keyword args (since 2.0): ● This way, local variables “a”, “b”, “c”, and “d” are all assigned. ● Caller side doesn't change. def obj.method(a, b = 1, c: 1, d: 2) Positional Optional Keyworded Keyworded
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Other minor syntactic updates ● Symbol array literal %i and %I introduced. ● Ruby now thinks its inputs to be UTF-8 encoded by default. You can of course specify other encodings explicitly. %i(foo bar baz) # => [:foo, :bar, :baz]
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Ruby 2.0 improvements of Core Performance February 22nd, 2013. URABE, Shyouhei. PFSYS Dept., GPS Div., DeNA Co., Ltd.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. require improvements ● Background: today as we have gems, booting ruby up tends to require 128+ libraries at once, resulting poor experience. ● Solution: require got faster (in sense of com- putational complexity). – Several techniques to reduce computations.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Backtrace lazy generation ● Since the beginning, backtraces has been arrays of strings. ● Each time an exception was raised, these strings were generated from up to down, even when that was not actually used. – Can get ultra slow when you have 1024+ stack frames (typical Rails situation).
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Backtrace lazy generation ● Starting ruby 2.x, Thread::Backtrace are used instead of strings. – Which are very lightweight. ● When you need to look at the backtrace, just convert them to strings (call #to_s).
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Flonum introduced ● On a 64bit machine (ubiquitous these days), pointers, integers and foating-point numbers are all 64bit width. ● In ruby, pointers and integers are C level register variables. But double was stored on-memory. What if we can handle them like pointers?
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Flonum introduced fags fags klass klass double double (unused) (unused) (unused) (unused) double/ptr double/ptr struct union
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Flonum introduced ● Problem: how to union a pointer and a double? MSB LSB offset PT PD PDP PML4 (unused) MSB LSB fraction exponent sign
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Flonum introduced ● Solution: some tricky bit-shifts – Sasada, K., “A Lightweight Representation of Floating-Point Numbers on Ruby Interpreter”, in PPL2008.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. GC things ● Bitmap marking: GC mark bits were in every objects, but moved into a dedicated memory page to reduce cache misshits (also much more CoW friendly). ● Nonrecursive marking: mark function now avoids the risk of machine stack overfow. ● Lazy sweep (since 1.9.3): sweeper collects only what's necessary (reduces stop time).
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Ruby 2.0 New Core Features #1 Instrumentations February 22nd, 2013. URABE, Shyouhei. PFSYS Dept., GPS Div., DeNA Co., Ltd.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. DTrace support! ● You can probe live production Ruby behaviour in non-stop, non-overhead fashion. ● Supports Solaris (dtrace), Mac OS X (dtrace), and Linux (SystemTrap). ● http://bugs.ruby-lang.org/projects/ruby/wiki/DTraceProbes
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. DTrace support! ● Mac example MAC % sudo dtrace -q -n 'ruby$target:::find-require-entry { printf("%s\n", copyinstr(arg0)) }' -c "ruby /var/apps/www/script/rails server"
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. TracePoint ● DTrace is great, but not ruby-way. I want to programmatically dynamically trace some execution! ● Then use TracePoint.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Ruby 2.0 New Core Features #2 Core Libraries February 22nd, 2013. URABE, Shyouhei. PFSYS Dept., GPS Div., DeNA Co., Ltd.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Fine-Grained Asynchronous Interrupt Handling ● Background: ruby execution sometimes gets interrupted for various reasons, e.g. timeout. timeout(rand()) do setup handle teardown end Complete Russian Roulette on where it gets interrupted!
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Fine-Grained Asynchronous Interrupt Handling Thread.async_interrupt_timing Timeout::Error => :defer do timeout(rand()) do begin Thread.async_interrupt_timing Timeout::Error => :immediate do setup handle … end ensure teardown end end end
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Fine-Grained Asynchronous Interrupt Handling Thread.async_interrupt_timing Timeout::Error => :defer do timeout(rand()) do begin Thread.async_interrupt_timing Timeout::Error => :immediate do setup handle … end ensure teardown end end end Blue area gets interrupted Red area does not.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Module Prepending ● Sometimes you want to add setup / teardown of a method defined elswhere. module ActiveRecordHelper def save ??? end end Wanna setup/teardown but how do we do that?
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Module Prepending module AR::Helper def save foo super baz end end Foo.new.save class Foo < AR::Base prepend AR::Helper def save bar end end
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Module Prepending ● This is how we solved Rails' “alias method chain” situation. AMC is no longer needed.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Lazy Enumerator ● Ruby's foo.bar.baz. … style (so-called being “fuent”) sometimes requires unnecessary temporary object to pass around, which can theoretically be avoided by lazy evaluations.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Lazy Enumerator File.open(path) {|fp| fp.each_line.lazy \ select {|line| /regexp/ =~ line }. \ each_with_index.map {|line, no| sprintf("%d: %s\n", no, line) }. \ first(10).each {|str| puts(str) } } No temporary array No temporary array No temporary array Doesn't even read until EOF
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Other new methods ● Kernel.__dir__: directory name of __FILE__ ● Kernel#to_h: ubiquitous Hash conversion ● Random class(1.9+): repeatable PRNG ● IO#wait_writable: wait until it gets writable ● Refinements: experimental.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Standard Library updates for Ruby 2.0 February 22nd, 2013. URABE, Shyouhei. PFSYS Dept., GPS Div., DeNA Co., Ltd.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Zlib binding ● Zlib now runs out of the interpreter lock. This means zlib runs faster than before on multi- threaded situations.
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. What's NOT new in Ruby 2 ● Almost everything. ● “100% backward compatible”, said matz. ● (for instance) Rails runs as-is. ● Don't be afraid! Just start using 2.0.0!
Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. That being said, ● New things are added ● Also lots of internal improvements ● Even if you are already confident about your current environment, 2.0.0 is worth looking at.