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

Solve your problem, speed up your life, and paint it black with Ruby!

Solve your problem, speed up your life, and paint it black with Ruby!

Slides for Rakuten Technology Conference 2013 talk "Solve your problem, speed up your life, and paint it black with Ruby!" http://tech.rakuten.co.jp/

Akira Matsuda

October 26, 2013
Tweet

More Decks by Akira Matsuda

Other Decks in Programming

Transcript

  1. Solve your problem,
    speed up your life,
    and paint it black
    with Ruby!
    Akira Matsuda

    View full-size slide

  2. Please allow me
    to introduce myself

    View full-size slide

  3. I'm a man of
    Ruby and Rails

    View full-size slide

  4. Pleased to meet you

    View full-size slide

  5. Hope you guess my name

    View full-size slide

  6. My name
    •GitHub: amatsuda
    •Twitter: @a_matsuda

    View full-size slide

  7. We meetup on every
    Ruby Tuesday

    View full-size slide

  8. Members
    and ...

    View full-size slide

  9. From all over Asakusa

    View full-size slide

  10. From all over Japan

    View full-size slide

  11. From all over the World!

    View full-size slide

  12. I'm a Rubyist.

    View full-size slide

  13. Ruby is different today.
    I hear every
    programmer say
    •Ruby is a "web language"
    •Ruby is a "slow language"

    View full-size slide

  14. Ruby.is_a? web language

    View full-size slide

  15. Ruby.is_a? web language
    •Obviously it is!

    View full-size slide

  16. I use Ruby because...

    View full-size slide

  17. I use Ruby because...
    •I'm a web programmer.

    View full-size slide

  18. I use Ruby because...
    •I'm a web programmer.
    •Every web programmer uses
    Rails.

    View full-size slide

  19. I use Ruby because...
    •I'm a web programmer.
    •Every web programmer uses
    Rails.
    •Ruby is the language "under
    Rails".

    View full-size slide

  20. Why Ruby?
    •Rails is my business... and
    business is good!
    •I'm not playing on Ruby,
    I'm just working on Rails.

    View full-size slide

  21. I hear every
    programmer say
    •Ruby is a "web language"
    •Ruby is a "slow language"

    View full-size slide

  22. Ruby.is_a? slow language

    View full-size slide

  23. I heard people say Ruby is slow,
    so what shall I do?

    View full-size slide

  24. New trolls!
    •Submit a CFP titled "7 reasons
    why Ruby is slow" for a Ruby
    conference. Immediately.
    •Be quick, or be dead you'll miss a
    chance to get up on stage.

    View full-size slide

  25. 7 reasons?
    •They say GC is slow.
    •They say Ruby is slow because of
    the "GIL" thing.

    View full-size slide

  26. "GIL" (or GVL)
    • What exactly is it?
    • Don't ask me. I don't know.
    Ask @_ko1 or @kosaki55tea
    • No, actually you need not to know about it.
    • I hear every programmer say JRuby or
    Rubinius are better and faster than MRI
    because they are GIL free.
    So let's just throw MRI away.

    View full-size slide

  27. GIL free!
    •Thank Matz Almighty, we are
    free at last!
    •Roll over @yotii23's husband,
    tell Patch Monster the news.

    View full-size slide

  28. How is it free?
    •Let's take a look.

    View full-size slide

  29. Concurrency is such a lonely word.
    Every Thread is so untrue.
    array = []
    5.times.map do
    Thread.new do
    1000.times do
    array << nil
    end
    end
    end.each(&:join)
    puts array.size
    http://www.jstorimer.com/blogs/workingwithcode/8085491-nobody-understands-the-gil

    View full-size slide

  30. Concurrency is hardly ever heard.
    And mostly what we need from Ruby.
    •% ruby -v thread_tes.rb
    ruby 2.1.0dev (2013-10-14 trunk
    43282) [x86_64-darwin12.5.0]
    5000
    •% ruby -v thread_tes.rb
    rubinius 2.1.1 (2.1.0 be67ed17
    2013-10-18 JI) [x86_64-darwin12.5.0]
    3504

    View full-size slide

  31. But ... but I still feel like Ruby is kind
    of slow for me, so what can I do?

    View full-size slide

  32. No trolls!
    •Submit a CFP titled "7 reasons
    why Ruby is slow" for a Ruby
    conference. Immediately.

    View full-size slide

  33. Don't troll. Code.
    • Unless you're a VM programmer, shut your
    mouth.
    • That's @_Ko1's business.
    • That's why Heroku pays @_ko1's salary.
    • Instead, nd a problem that YOU can deal
    with.
    • And don't let it be. Don't whisper words
    of wisdom. Solve it.

    View full-size slide

  34. I know a person who actually
    have been doing this
    •He thought Rails is slow because
    assets are slow.
    •=> The asset pipeline
    •He thought Rails is slow because we
    are reloading assets for every
    request.
    •=> Turbolinks

    View full-size slide

  35. He's the speed king

    View full-size slide

  36. Your Rails app is slow
    because...
    •Kernel#require is slow
    •RubyGems & Bundler is slow
    •ActiveRecord is slow
    •RSpec is slow
    •Rack is slow

    View full-size slide

  37. Kernel#require
    •@_ko1 told me that
    require_relative would be way
    faster than require
    •So we tried

    View full-size slide

  38. https://github.com/amatsuda/
    rails/tree/require_relative
    •Rewriting 544 `require` calls to
    `require_relative`

    View full-size slide

  39. 'Cause I try and I try
    and I try and I try
    • Before:
    % time rails r 1
    4.07s user 1.32s system 95% cpu 5.650 total
    % time rails r 1
    3.91s user 1.15s system 99% cpu 5.087 total
    % time rails r 1
    3.91s user 1.17s system 99% cpu 5.107 total
    • After:
    % time rails r 1
    4.20s user 1.24s system 99% cpu 5.467 total
    % time rails r 1
    3.98s user 1.17s system 99% cpu 5.167 total
    % time rails r 1
    3.99s user 1.15s system 99% cpu 5.169 total

    View full-size slide

  40. I can't get no!
    I can't get no!
    •NO performance improvement!

    View full-size slide

  41. Hail, hail to Bundler
    •It turned out that RubyGems
    rewrites Kernel#require and
    makes it slow, but Bundler re-
    rewrites it and makes it fast
    nally.

    View full-size slide

  42. The problems I recently
    found

    View full-size slide

  43. database_cleaner is slow

    View full-size slide

  44. 10% of `time rake spec`
    was spent for DB cleaning
    •time rake spec: 14m01s
    •time spent on cleaning DB:
    77m35s

    View full-size slide

  45. So I rewrote
    database_cleaner
    •Come cleaning faster!
    •Just call `delete` if I hear `insert`
    •FIX ME!

    View full-size slide

  46. amatsuda/
    database_rewinder

    View full-size slide

  47. Speedy's coming
    •time rake spec: 13m15s
    •time spent on cleaning DB: 0m8s

    View full-size slide

  48. Loading a huge HTML is
    slow

    View full-size slide

  49. lazy-load partials
    •Separate a huge webpage into
    several partials. Then load the
    partials via separate requests.
    •Rails views already have "partials".
    •What if partials are lazy-load-
    able?

    View full-size slide

  50. amatsuda/
    ljax_rails

    View full-size slide

  51. ljax_rails
    <%= render 'users' %>
    =>
    •<%= render 'users', remote: true
    %>

    View full-size slide

  52. bundler is slow
    •$LOAD_PATH

    View full-size slide

  53. In our mid-sized app's case
    % rails r 'puts $:' | grep gems | wc -l
    200

    View full-size slide

  54. Can't they be squashed
    into one lib/ directory?
    •Because each gem are basically
    namespaced.
    •The only thing that looks good
    on me, at runtime, is lib/
    directory in most cases.

    View full-size slide

  55. Current directory structure
    ᵓᴷᴷ gem1
    ᴹ ᵋᴷᴷ lib >> LOAD_PATH
    ᴹ ᵓᴷᴷ gem1
    ᴹ ᴹ ᵓᴷᴷ bar.rb
    ᴹ ᴹ ᵋᴷᴷ foo.rb
    ᴹ ᵋᴷᴷ gem1.rb
    ᵋᴷᴷ gem2
    ᵋᴷᴷ lib >> $LOAD_PATH
    ᵋᴷᴷ gem2
    ᵓᴷᴷ fuga.rb
    ᵋᴷᴷ hoge.rb

    View full-size slide

  56. Squash all gems into one
    directory
    ᵋᴷᴷ all_bundled_gems_in_one
    ᵋᴷᴷ lib >> LOAD_PATH
    ᵓᴷᴷ gem1
    ᴹ ᵓᴷᴷ bar.rb
    ᴹ ᵋᴷᴷ foo.rb
    ᵓᴷᴷ gem1.rb
    ᵋᴷᴷ gem2
    ᵓᴷᴷ fuga.rb
    ᵋᴷᴷ hoge.rb

    View full-size slide

  57. (bundle_squash)
    •(It should be possible)
    •(But not yet on the Web...)

    View full-size slide

  58. Problems I'm willing to
    challenge
    •Make Haml faster
    •Or create a faster fork of Haml
    •Make ActiveRecord faster
    •Or create a faster subset of AR

    View full-size slide

  59. Ruby.is_a? web language
    •(reprise)

    View full-size slide

  60. Ruby.is_a? web language
    and
    !Ruby.is_a? web_only language
    •Ruby off Rails!

    View full-size slide

  61. One thing I hate on my
    computer

    View full-size slide

  62. One thing I hate on my
    computer

    View full-size slide

  63. Who here are happy with
    the Finder.app?

    View full-size slide

  64. Finder.app
    •Mr. Finder.app, what went down
    in your head
    •Your UI to me seems so tragic,
    with the thrill of it all

    View full-size slide

  65. At Railsษڧձ@౦ژ last month
    •We talked about how Finder.app
    sucks
    •We agreed that we need a better
    " ler"
    •And I started creating it last
    month.

    View full-size slide

  66. Why Finder.app sucks

    View full-size slide

  67. Why Finder.app sucks
    •Productivity
    •Design

    View full-size slide

  68. Productivity
    • It's so hard to do simple things such as
    `mkdir`, `rm`, or `cd` to a hidden
    directory.
    • How can I `touch`? How can I `ln -s`?
    How can I `chmod`?
    • Telling me more and more about some
    useless features. Supposed to : re: my
    productivity.

    View full-size slide

  69. Design
    •It's white!

    View full-size slide

  70. Paint it black!
    •I see a white window and I want
    it painted black.

    View full-size slide

  71. Paint it black!

    View full-size slide

  72. Paint it black!
    •No colors anymore I want them
    to turn black.

    View full-size slide

  73. Paint it black!

    View full-size slide

  74. So, I made my own nder
    •And saw that it was good.

    View full-size slide

  75. How can Finder.app be
    better?
    •Needs to be more productive.
    •Needs to be painted black.

    View full-size slide

  76. FD
    •It's only a DOS application
    but I like it.

    View full-size slide

  77. About FD
    •http://news.mynavi.jp/articles/
    2012/11/20/FD/
    •A legendary " ler" for MS-DOS
    •Firstly released in 1989

    View full-size slide

  78. Do ya think I'm sexy?
    •This tool is not very glamorous
    but still very sexy.

    View full-size slide

  79. Get back to where you
    once belonged

    View full-size slide

  80. What a drag it is
    getting old
    •Am I sticking to an old tool that
    I'm used to because I'm too old to
    learn new tools?
    •No. I'm creating this not just for
    nostalgy but for productivity.

    View full-size slide

  81. Technologies
    •Xnix
    •Terminal
    •Ruby
    •Curses

    View full-size slide

  82. amatsuda/rfd
    % gem i rfd

    View full-size slide

  83. Concept, design
    •FD + Vim
    •Simple MVC
    •M: les & directories
    •V: Curses
    •C: logic
    •Customizable, plugin-able architecture

    View full-size slide

  84. Implementation
    •Basically commands are one-
    char
    •Each command is de ned as a
    method in commands.rb
    •Pressing a key simply runs that
    method (send)

    View full-size slide

  85. rfd commands
    •Moving the cursor
    •Manipulating les
    •Viewing, Editing
    •Manipulating archives

    View full-size slide

  86. TODO
    •tree view
    •command customization
    •color customization
    •undo
    •watchr

    View full-size slide

  87. Summary (1)
    •I use Ruby to solve "my" problem
    •OSS != volunteering.
    No need to "contribute" to
    someone else.
    Start with "your" problem.
    It's a very good place to start.

    View full-size slide

  88. Summary (2)
    •Ruby is slow, and I'm trying to
    make it faster.
    •You can do it even if you're not a
    VM hacker or a GC hacker.
    •There're many unoptimized parts
    left in the whole Rails app stack.

    View full-size slide

  89. Summary (3)
    •I'm painting it black with Ruby.
    •It works ne, and I'm happy with
    that.

    View full-size slide

  90. Conclusion
    •Play with Ruby for fun rather
    than just work with Ruby for
    money!
    •Let's spend the night with Ruby!

    View full-size slide