$30 off During Our Annual Pro Sale. View Details »

Fast Code for Ruby

Fast Code for Ruby

沖縄Ruby会議02 #okrk02
http://ruby.okinawa/okrk02/

秒速284km

March 10, 2018
Tweet

More Decks by 秒速284km

Other Decks in Programming

Transcript

  1. Fast Code for Ruby @284km

  2. ͸͍͍͞ʂ

  3. ࠓ೔࿩͢͜ͱ Ruby Ͱ଎͍ίʔυΛॻ͘ benchmark_driver ͕͘͢͝ྑ͍ Ruby ͷָ͠ΈํΛͻͱͭ

  4. ଎͍ίʔυΛॻ͘

  5. ଎͍ίʔυΛॻ͘ ଌఆ ϕϯνϚʔΧʔ

  6. ϕϯνϚʔΧʔ benchmark.rb (ඪ४ఴ෇ϥΠϒϥϦ) evanphx/benchmark-ips k0kubun/benchmark_driver

  7. ଎͍ίʔυ ಉ݁͡Ռ͕ಘΒΕΔෳ਺ͷॻ͖ํ ܭଌͯ͠ɺ଎͍΄͏͕଎͍ લఏ: ࠓճͷϕϯνϚʔΫ͸๻ͷ MBP Ͱ࣮ߦͨ͠΋ͷͰ͢

  8. benchmark_driver e.g. ෳ਺ͷॲཧܥ ग़ྗܗࣜ (.md Մ)

  9. Hash#keys.each Hash#each_key

  10. Hash#keys.each Hash#each_key

  11. ଎͍ίʔυ ৚݅ʹґଘ͢Δ৔߹͕͋Δ ඞͣͦͷॻ͖ํ͕ྑ͍ͱ͸ݴ͑ͳ͍ έʔε͕͋Δ (e.g. gsub / tr)

  12. gsub tr

  13. gsub tr

  14. gsub tr

  15. gsub tr

  16. gsub tr

  17. gsub tr

  18. ଎͍ίʔυ Ͳ͏͍͏࣌ʹ༗ޮ͔Λ஌ΔͱΑ͞ ͦ͏ ࣮૷Λ೷͘ͱ෼͔Δ͜ͱ͕͋Δ

  19. Bang method ഁյత / ඇഁյత ͳϝιουͷྫ ͜ͷ࣮૷͸ݟ΍͍͢ͱࢥ͍·͢

  20. Hash#merge Hash#merge! ͜͜Ͱ͸αϯϓϧίʔυ͸ͦ Ε΄ͲॏཁͰ͸ͳ͘ɺ

  21. Hash#merge Hash#merge! ͜͜Ͱ͸αϯϓϧίʔυ͸ͦ Ε΄ͲॏཁͰ͸ͳ͘ɺ ଎౓ࠩͷཧ༝Λ௥ͬͯΈΔ

  22. Hash#merge Hash#merge! dup ͨ͠΋ͷΛ౉ͯ݁͠ ՌΛฦ͍ͯ͠ΔͷͰͦͷ෼ ஗͍

  23. ଎͞ ͱɺಡΈ΍͢͞

  24. each_with_index while

  25. each_with_index while

  26. ಡΈ΍͢͞େࣄ ಡΈ΍͢͞ͱ͸…(ޙͰ࿩୊ʹ͠·͢ ͕ yield / Proc#call ΋ͦ͏Ͱ͢ΑͶ) ࢓ࣄͰॻ͘ίʔυͰ͸ɺνʔϜͷࣄ ৘ͳͲʹΑͬͯԿ͕ಡΈ΍͍͔͢͸ม ΘΓͦ͏Ͱ͸͋Δʢ๻ͷײ૝

  27. Rubocop::Cop::Performance ύϑΥʔϚϯε͕վળ͞Εͦ͏ͳͱ ͜ΖΛࢦఠͯ͘͠ΕΔ ੩తղੳπʔϧʹཔΔ͜ͱ΋༗ޮ ͜ͷؒͻͱͭࢦఠ͞Ε·ͨ͠

  28. String#match String#match?

  29. String#match String#match?

  30. String#match String#match?

  31. String#match String#match? MatchData ΦϒδΣΫτ Λฦ͢ʢϚονͨ͠৔߹ʣ

  32. String#match String#match? MatchData ΦϒδΣΫτ Λฦ͢ʢϚονͨ͠৔߹ʣ rb_reg_match_p() ͸ɺ Ϛονͨ͠Β true ͠ͳ͚

    Ε͹ false Λฦ͢ɻ
  33. Ruby 2.5 ࢖͍ͬͯ·͔͢ʁ όʔδϣϯΞοϓͱڞʹվળ͞ΕΔ ෦෼͸஌͓ͬͯ͘ͱศར

  34. unpack1 unpack.first String#unpack1 ͸ 2.4 Ͱೖ ͬͨ

  35. unpack1 unpack.first String#unpack1 ͸ 2.4 Ͱೖ ͬͨ

  36. yield Proc#call 2.5 Ͱ Proc#call ͕଎͘ ͳͬͨ

  37. yield Proc#call

  38. yield Proc#call

  39. ࠷৽ͷ Ruby ஌ͬͯΔͱ࢓ࣄָ͕ʹͳͬͨΓ͢Δ ஌Δͱɺ௥͏ͷָ͕͘͠ͳΔϧʔϓ JIT Λ༗ޮʹ͢Δͱɺ݁ՌͷมΘΔ ϕϯνϚʔΫ΋͋Γͦ͏ (2.6.0)

  40. ·ͱΊ ʹ;͐ʔͰʔͼΔ benchmark_driver ͕͘͢͝ྑ͍ Fast Code ͱ͍͏ Ruby ͷָ͠Έํ Ruby

    ͷ࣮૷Λ೷͍ͯΈΔ ೖΓޱͱͯ͠ͷϕϯνϚʔΫ΋ྑ͍