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

What's New in Ruby 2.0

What's New in Ruby 2.0

Detailed descriptions about new features and improvements of Ruby 2.0.0

Urabe Shyouhei

February 22, 2013
Tweet

More Decks by Urabe Shyouhei

Other Decks in Technology

Transcript

  1. 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.
  2. 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.
  3. 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
  4. 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!
  5. Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. Ruby and

    “Good Parts” “Ruby is designed to be fun.” – Yukihiro Matsumoto 出典 : 「オブジェクト指向スクリプト言語 Ruby 」
  6. 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
  7. 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/
  8. 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
  9. 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 での発言
  10. 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.”
  11. 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.
  12. 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.
  13. Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. That being

    said, • New things are added • Also lots of internal improvements
  14. 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.
  15. 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"
  16. 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
  17. 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
  18. 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]
  19. 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.
  20. 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.
  21. 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).
  22. 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).
  23. 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?
  24. 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
  25. 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
  26. 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.
  27. 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
  28. 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).
  29. 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.
  30. 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
  31. 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"
  32. 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.
  33. 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 #=> #<TracePoint:0x007f786a452448> trace.enable #=> #<TracePoint:0x007f786a452448> 0 / 0 #=> [5, :raise, #<ZeroDivisionError: divided by 0>]
  34. 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, }
  35. 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.
  36. 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!
  37. 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
  38. 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.
  39. 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?
  40. 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
  41. 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.
  42. 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.
  43. 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
  44. 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
  45. 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
  46. 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.
  47. 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.
  48. Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. net/http •

    Now supports SNI (Server Name Indication)
  49. 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.
  50. 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, …)
  51. 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!
  52. 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.