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

9d2f78236e45a335301ba1195026105d?s=128

Urabe Shyouhei

February 22, 2013
Tweet

Transcript

  1. 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. 2.
  3. 3.

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

    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
  5. 6.
  6. 7.
  7. 8.
  8. 9.

    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!
  9. 12.

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

    “Good Parts” “Ruby is designed to be fun.” – Yukihiro Matsumoto 出典 : 「オブジェクト指向スクリプト言語 Ruby 」
  10. 13.

    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
  11. 14.

    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/
  12. 15.
  13. 16.
  14. 17.

    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
  15. 18.

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

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

    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.
  18. 22.

    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.
  19. 23.

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

    said, • New things are added • Also lots of internal improvements
  20. 24.

    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.
  21. 25.

    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"
  22. 26.

    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
  23. 27.

    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
  24. 28.

    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]
  25. 29.

    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.
  26. 30.

    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.
  27. 32.

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

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

    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?
  30. 36.

    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
  31. 37.

    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
  32. 38.

    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.
  33. 39.

    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
  34. 40.

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

    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.
  36. 42.

    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
  37. 43.

    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"
  38. 44.

    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.
  39. 45.

    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>]
  40. 46.

    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, }
  41. 47.

    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.
  42. 48.

    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!
  43. 49.

    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
  44. 50.

    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.
  45. 51.

    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?
  46. 52.

    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
  47. 53.

    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.
  48. 54.

    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.
  49. 55.

    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
  50. 56.

    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
  51. 57.

    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
  52. 58.

    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.
  53. 59.

    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.
  54. 61.

    Copyright (C) 2013 DeNA Co.,Ltd. All Rights Reserved. net/http •

    Now supports SNI (Server Name Indication)
  55. 62.

    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.
  56. 63.

    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, …)
  57. 65.

    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!
  58. 66.

    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.