Slide 1

Slide 1 text

Micro Performance Improvements RailsConf, April 2017

Slide 2

Slide 2 text

Ernesto Tagwerker @etagwerker Founder & Software Developer at Ombu Labs

Slide 3

Slide 3 text

Best Practices 3

Slide 4

Slide 4 text

https://github.com/bbatsov/ruby-style-guide

Slide 5

Slide 5 text

https://speakerdeck.com/sferik/writing-fast-ruby

Slide 6

Slide 6 text

Bad Code vs. Good Code 6

Slide 7

Slide 7 text

# Bad HOUR_6 = Time.parse("2000-01-01 06:00:00 UTC") # Good HOUR_6 = Time.at(946685160).utc

Slide 8

Slide 8 text

$ bundle exec ruby benchmarks/time/parse_vs_at.rb Ruby version: 2.3.3 Warming up -------------------------------------- Time.parse 3.287k i/100ms Time.at 66.086k i/100ms Calculating ------------------------------------- Time.parse 34.052k (± 3.2%) i/s - 170.924k in 5.024737s Time.at 842.576k (± 2.2%) i/s - 4.230M in 5.022207s Comparison: Time.at: 842576.2 i/s Time.parse: 34051.9 i/s - 24.74x slower

Slide 9

Slide 9 text

# OK phone = Hash.new(number: number) phone[:number] # Better Phone = Struct.new(:number) phone = Phone.new(number) phone.number

Slide 10

Slide 10 text

$ bundle exec ruby benchmarks/struct_vs_hash.rb Ruby version: 2.3.3 Warming up -------------------------------------- struct 22.148k i/100ms hash 4.688k i/100ms Calculating ------------------------------------- struct 240.460k (± 2.7%) i/s - 1.218M in 5.069592s hash 47.926k (± 3.9%) i/s - 243.776k in 5.094413s Comparison: struct: 240459.9 i/s hash: 47926.1 i/s - 5.02x slower

Slide 11

Slide 11 text

# Bad Post.select(:id).map(&:id) # Good Post.pluck(:id)

Slide 12

Slide 12 text

$ bundle exec rake benches:pluck_vs_map Ruby version: 2.3.3 Warming up -------------------------------------- map(&:id) 11.000 i/100ms pluck(:id) 80.000 i/100ms Calculating ------------------------------------- map(&:id) 124.205 (±11.3%) i/s - 616.000 in 5.026999s pluck(:id) 807.127 (± 2.0%) i/s - 4.080k in 5.057062s Comparison: pluck(:id): 807.1 i/s map(&:id): 124.2 i/s - 6.50x slower

Slide 13

Slide 13 text

https://github.com/ombulabs/benches/ 13

Slide 14

Slide 14 text

Resources 14

Slide 15

Slide 15 text

15 https://speakerdeck.com/etagwerker/micro- performance-improvements https://github.com/evanphx/benchmark-ips https://github.com/evanphx/benchmark.fyi

Slide 16

Slide 16 text

Thank you! @etagwerker 16