Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Fast Code for Ruby
Search
秒速284km
March 10, 2018
Programming
2
860
Fast Code for Ruby
沖縄Ruby会議02 #okrk02
http://ruby.okinawa/okrk02/
秒速284km
March 10, 2018
Tweet
Share
More Decks by 秒速284km
See All by 秒速284km
fukuoka_ruby_2019
284km
0
160
Rubyアソシエーション開発助成成果報告会
284km
0
2k
Charty - Visualize Real-world Data with Ruby
284km
1
2.3k
Charty - Visualizing your data in Ruby
284km
0
2.2k
.so にして色々な言語から便利にのっかろう
284km
0
53
Pragmatic Charty
284km
0
2.2k
Charty with Rails
284km
1
49
Charty (RubyGrant 2018)
284km
0
2.2k
Better CSV processing with Ruby 2.6
284km
0
70
Other Decks in Programming
See All in Programming
CI改善もDatadogとともに
taumu
0
200
Jakarta EE meets AI
ivargrimstad
0
420
PRレビューのお供にDanger
stoticdev
1
230
Go 1.24でジェネリックになった型エイリアスの紹介
syumai
2
290
Rubyで始める関数型ドメインモデリング
shogo_tksk
0
140
Boos Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
350
Bedrock Agentsレスポンス解析によるAgentのOps
licux
3
920
クリーンアーキテクチャから見る依存の向きの大切さ
shimabox
5
1.1k
密集、ドキュメントのコロケーション with AWS Lambda
satoshi256kbyte
1
210
苦しいTiDBへの移行を乗り越えて快適な運用を目指す
leveragestech
0
1k
Honoとフロントエンドの 型安全性について
yodaka
7
1.5k
From the Wild into the Clouds - Laravel Meetup Talk
neverything
0
150
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
1k
The World Runs on Bad Software
bkeepers
PRO
67
11k
Into the Great Unknown - MozCon
thekraken
35
1.6k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
40
2k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
10
510
Building Applications with DynamoDB
mza
93
6.2k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.1k
VelocityConf: Rendering Performance Case Studies
addyosmani
328
24k
Build The Right Thing And Hit Your Dates
maggiecrowley
34
2.5k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
30
4.6k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Transcript
Fast Code for Ruby @284km
͍͍͞ʂ
ࠓ͢͜ͱ Ruby Ͱ͍ίʔυΛॻ͘ benchmark_driver ͕͘͢͝ྑ͍ Ruby ͷָ͠ΈํΛͻͱͭ
͍ίʔυΛॻ͘
͍ίʔυΛॻ͘ ଌఆ ϕϯνϚʔΧʔ
ϕϯνϚʔΧʔ benchmark.rb (ඪ४ఴϥΠϒϥϦ) evanphx/benchmark-ips k0kubun/benchmark_driver
͍ίʔυ ಉ݁͡Ռ͕ಘΒΕΔෳͷॻ͖ํ ܭଌͯ͠ɺ͍΄͏͕͍ લఏ: ࠓճͷϕϯνϚʔΫͷ MBP Ͱ࣮ߦͨ͠ͷͰ͢
benchmark_driver e.g. ෳͷॲཧܥ ग़ྗܗࣜ (.md Մ)
Hash#keys.each Hash#each_key
Hash#keys.each Hash#each_key
͍ίʔυ ݅ʹґଘ͢Δ߹͕͋Δ ඞͣͦͷॻ͖ํ͕ྑ͍ͱݴ͑ͳ͍ έʔε͕͋Δ (e.g. gsub / tr)
gsub tr
gsub tr
gsub tr
gsub tr
gsub tr
gsub tr
͍ίʔυ Ͳ͏͍͏࣌ʹ༗ޮ͔ΛΔͱΑ͞ ͦ͏ ࣮Λ͘ͱ͔Δ͜ͱ͕͋Δ
Bang method ഁյత / ඇഁյత ͳϝιουͷྫ ͜ͷ࣮ݟ͍͢ͱࢥ͍·͢
Hash#merge Hash#merge! ͜͜Ͱαϯϓϧίʔυͦ Ε΄ͲॏཁͰͳ͘ɺ
Hash#merge Hash#merge! ͜͜Ͱαϯϓϧίʔυͦ Ε΄ͲॏཁͰͳ͘ɺ ࠩͷཧ༝ΛͬͯΈΔ
Hash#merge Hash#merge! dup ͨ͠ͷΛͯ݁͠ ՌΛฦ͍ͯ͠ΔͷͰͦͷ ͍
͞ ͱɺಡΈ͢͞
each_with_index while
each_with_index while
ಡΈ͢͞େࣄ ಡΈ͢͞ͱ…(ޙͰʹ͠·͢ ͕ yield / Proc#call ͦ͏Ͱ͢ΑͶ) ࣄͰॻ͘ίʔυͰɺνʔϜͷࣄ ͳͲʹΑͬͯԿ͕ಡΈ͍͔͢ม ΘΓͦ͏Ͱ͋Δʢͷײ
Rubocop::Cop::Performance ύϑΥʔϚϯε͕վળ͞Εͦ͏ͳͱ ͜ΖΛࢦఠͯ͘͠ΕΔ ੩తղੳπʔϧʹཔΔ͜ͱ༗ޮ ͜ͷؒͻͱͭࢦఠ͞Ε·ͨ͠
String#match String#match?
String#match String#match?
String#match String#match?
String#match String#match? MatchData ΦϒδΣΫτ Λฦ͢ʢϚονͨ͠߹ʣ
String#match String#match? MatchData ΦϒδΣΫτ Λฦ͢ʢϚονͨ͠߹ʣ rb_reg_match_p() ɺ Ϛονͨ͠Β true ͠ͳ͚
Ε false Λฦ͢ɻ
Ruby 2.5 ͍ͬͯ·͔͢ʁ όʔδϣϯΞοϓͱڞʹվળ͞ΕΔ ෦͓ͬͯ͘ͱศར
unpack1 unpack.first String#unpack1 2.4 Ͱೖ ͬͨ
unpack1 unpack.first String#unpack1 2.4 Ͱೖ ͬͨ
yield Proc#call 2.5 Ͱ Proc#call ͕͘ ͳͬͨ
yield Proc#call
yield Proc#call
࠷৽ͷ Ruby ͬͯΔͱࣄָ͕ʹͳͬͨΓ͢Δ Δͱɺ͏ͷָ͕͘͠ͳΔϧʔϓ JIT Λ༗ޮʹ͢Δͱɺ݁ՌͷมΘΔ ϕϯνϚʔΫ͋Γͦ͏ (2.6.0)
·ͱΊ ʹ;͐ʔͰʔͼΔ benchmark_driver ͕͘͢͝ྑ͍ Fast Code ͱ͍͏ Ruby ͷָ͠Έํ Ruby
ͷ࣮Λ͍ͯΈΔ ೖΓޱͱͯ͠ͷϕϯνϚʔΫྑ͍