Slide 1

Slide 1 text

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.

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Ruby 2 is CoMING! February 22nd, 2013. URABE, Shyouhei. PFSYS Dept., GPS Div., DeNA Co., Ltd.

Slide 4

Slide 4 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. /people/@me/@self ● @shyouhei ● Ruby Core Team ● Ruby 1.8.{5,6,7} release mgr. & branch mentor

Slide 5

Slide 5 text

Delight and Impact the World

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

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!

Slide 10

Slide 10 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Ruby is Growing

Slide 11

Slide 11 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. http://modulecounts.com

Slide 12

Slide 12 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Ruby and “Good Parts” “Ruby is designed to be fun.” – Yukihiro Matsumoto 出典 : 「オブジェクト指向スクリプト言語 Ruby 」

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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/

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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 での発言

Slide 19

Slide 19 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Change

Slide 20

Slide 20 text

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.”

Slide 21

Slide 21 text

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.

Slide 22

Slide 22 text

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.

Slide 23

Slide 23 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. That being said, ● New things are added ● Also lots of internal improvements

Slide 24

Slide 24 text

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.

Slide 25

Slide 25 text

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"

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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]

Slide 29

Slide 29 text

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.

Slide 30

Slide 30 text

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.

Slide 31

Slide 31 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. require improvements

Slide 32

Slide 32 text

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).

Slide 33

Slide 33 text

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).

Slide 34

Slide 34 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Backtrace lazy generation

Slide 35

Slide 35 text

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?

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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.

Slide 39

Slide 39 text

Benchmark results 0 0.5 1 1.5 2 2.5 3 app_answer app_factorial app_pentomino app_tak io_file_create io_select loop_for loop_whileloop so_array so_count_words so_fasta so_mandelbrot so_nbody so_nsieve_bits so_pidigits so_sieve vm1_block* vm1_float_calc* vm1_length* vm1_lvar_set* vm1_rescue* vm2_array* vm2_case* vm2_method* vm2_poly_method_ov* vm2_raise2* vm2_super* vm3_backtrace vm3_thread_mutex vm_thread_mutex1 vm_thread_pass clean/flonum clean/flonum.z © benchmarked by @_ko1

Slide 40

Slide 40 text

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).

Slide 41

Slide 41 text

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.

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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"

Slide 44

Slide 44 text

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.

Slide 45

Slide 45 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. TracePoint trace = TracePoint.new(:raise) do |tp| p [tp.lineno, tp.event, tp.raised_exception] end #=> # trace.enable #=> # 0 / 0 #=> [5, :raise, #]

Slide 46

Slide 46 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. GC stats ● GC.stat: live object counts ● GC::Profiler(1.9+): GC timing profiler GC.stat #=> { count: 17, heap_used: 79, heap_length: 79, heap_increment: 0, heap_live_num: 22798, heap_free_num: 18020, heap_final_num: 0, total_allocated_object: 82397, total_freed_object: 59599, }

Slide 47

Slide 47 text

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.

Slide 48

Slide 48 text

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!

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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.

Slide 51

Slide 51 text

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?

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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.

Slide 54

Slide 54 text

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.

Slide 55

Slide 55 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Lazy Enumerator File.open(path) {|fp| fp.each_line. \ select {|line| /regexp/ =~ line }. \ each_with_index.map {|line, no| sprintf("%d: %s\n", no, line) }. \ first(10).each {|str| puts(str) } } Generates temporary array Generates temporary array Generates temporary array

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Lazy Enumerator ● Interesting application: infinite enumerator # Leibniz formula for π (0..Float::INFINITY).lazy.map {|i| ((-1) ** i) / (2*i + 1).to_f }.take(65536).reduce(:+) * 4

Slide 58

Slide 58 text

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.

Slide 59

Slide 59 text

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.

Slide 60

Slide 60 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. CGI ● CGI is now HTML5 ready.

Slide 61

Slide 61 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. net/http ● Now supports SNI (Server Name Indication)

Slide 62

Slide 62 text

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.

Slide 63

Slide 63 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Updated stdlibs ● Rubygems 2.0.0 ● JSON 1.7.7 ● Rake 0.9.5 ● Rdoc 4.0 ● And others (REXML, yaml, openssl, …)

Slide 64

Slide 64 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Conclusion

Slide 65

Slide 65 text

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!

Slide 66

Slide 66 text

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.

Slide 67

Slide 67 text

Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Use Ruby Today!