Top Banner
PERFORMANCE OPTIMIZATION IN RUBY PRATHMESH RANAUT © Prathmesh Ranaut, 2017
42

Performance Optimization in Ruby

Apr 16, 2017

Download

Engineering

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Performance Optimization in Ruby

PERFORMANCE OPTIMIZATION IN RUBY

PRATHMESH RANAUT

© Prathmesh Ranaut, 2017

Page 2: Performance Optimization in Ruby

© Prathmesh Ranaut, 2017

Page 3: Performance Optimization in Ruby

☃ -> "

© Prathmesh Ranaut, 2017

Page 4: Performance Optimization in Ruby

WHO AM I

> Computer Science Undergrad

> Helped ! of ruby apps improve performance

© Prathmesh Ranaut, 2017

Page 5: Performance Optimization in Ruby

> Celluloid - Concurrent ruby framework> Improved performance by 300%

© Prathmesh Ranaut, 2017

Page 6: Performance Optimization in Ruby

WHAT IS PERFORMANCE OPTIMIZATION? !

© Prathmesh Ranaut, 2017

Page 7: Performance Optimization in Ruby

DOING

MOREWITH

LESS

© Prathmesh Ranaut, 2017

Page 8: Performance Optimization in Ruby

HOW?

© Prathmesh Ranaut, 2017

Page 9: Performance Optimization in Ruby

TIP #1© Prathmesh Ranaut, 2017

Page 10: Performance Optimization in Ruby

ALWAYS HAVE A BENCHMARKING SUITE

© Prathmesh Ranaut, 2017

Page 11: Performance Optimization in Ruby

BECHMARK-IPS

© Prathmesh Ranaut, 2017

Page 12: Performance Optimization in Ruby

require "benchmark/ips"

Benchmark.ips do |ips| ips.report("addition") { 1 + 2 }end

© Prathmesh Ranaut, 2017

Page 13: Performance Optimization in Ruby

require "benchmark/ips"

Benchmark.ips do |ips| ips.report("addition") { 1 + 2 }end

Warming up -------------------------------------- addition 325.015k i/100msCalculating ------------------------------------- addition 12.544M (± 8.5%) i/s - 62.403M in 5.012765s

© Prathmesh Ranaut, 2017

Page 14: Performance Optimization in Ruby

class Worker include Celluloid

def hashed(hash, key) hash[key] endend

# Worker pool of different sizespool_10 = Worker.pool(size: 10)pool_100 = Worker.pool(size: 100)

hash = {}

ENTRIES = 10_000TESTS = 100_000# Using a constant key, to derive consistent benchmarking resultsKEY = 500

# Populate hash arrayENTRIES.times do |i| hash[i] = iend

Benchmark.bmbm do |ips| puts "Finding the key : #{KEY}"

ips.report("pool - 10") do TESTS.times do pool_10.async.hashed(hash, KEY) end end

ips.report("pool - 100") do TESTS.times do pool_100.async.hashed(hash, KEY) end endend

© Prathmesh Ranaut, 2017

Page 15: Performance Optimization in Ruby

TIP #2© Prathmesh Ranaut, 2017

Page 16: Performance Optimization in Ruby

UPGRADE RUBY

© Prathmesh Ranaut, 2017

Page 17: Performance Optimization in Ruby

RUBY 1.9

> YARV

© Prathmesh Ranaut, 2017

Page 18: Performance Optimization in Ruby

RUBY 1.9

> YARV

RUBY 2.0> Copy-on-Write

© Prathmesh Ranaut, 2017

Page 19: Performance Optimization in Ruby

RUBY 2.1

> Generation GC

RUBY 2.4> Improves the speed to access an instance variable

© Prathmesh Ranaut, 2017

Page 20: Performance Optimization in Ruby

TIP #3© Prathmesh Ranaut, 2017

Page 21: Performance Optimization in Ruby

TRY ANOTHER IMPLEMENTATION OF RUBY

© Prathmesh Ranaut, 2017

Page 22: Performance Optimization in Ruby

RUBY 2.3 VS JRUBY

RUBY 2.3

Calculating ------------------------------------- addition 12.544M (± 8.5%) i/s - 62.403M

JRUBY

Calculating ------------------------------------- addition 22.929M (± 9.3%) i/s - 113.127M

© Prathmesh Ranaut, 2017

Page 23: Performance Optimization in Ruby

TIP #4© Prathmesh Ranaut, 2017

Page 24: Performance Optimization in Ruby

PROFILE YOUR METHODS

© Prathmesh Ranaut, 2017

Page 25: Performance Optimization in Ruby

TIP #5© Prathmesh Ranaut, 2017

Page 26: Performance Optimization in Ruby

FIGURE OUT THE BOTTLENECKI/O OR CPU OR MEMORY

© Prathmesh Ranaut, 2017

Page 27: Performance Optimization in Ruby

TIP #6© Prathmesh Ranaut, 2017

Page 28: Performance Optimization in Ruby

SWITCH TO ELIXIR

© Prathmesh Ranaut, 2017

Page 29: Performance Optimization in Ruby

TRY MULTITHREADING

© Prathmesh Ranaut, 2017

Page 30: Performance Optimization in Ruby

TOOLSTHAT MAKE MY LIFE EASIER

© Prathmesh Ranaut, 2017

Page 31: Performance Optimization in Ruby

METHOD PROFILERHTTPS://GITHUB.COM/CHANGE/METHOD_PROFILER

© Prathmesh Ranaut, 2017

Page 32: Performance Optimization in Ruby

profiler = MethodProfiler.observe(BazingaClass)# Do stuff with the BazingaClassputs profiler.report

© Prathmesh Ranaut, 2017

Page 33: Performance Optimization in Ruby

© Prathmesh Ranaut, 2017

Page 34: Performance Optimization in Ruby

KCACHEGRINDHTTPS://KCACHEGRIND.GITHUB.IO/

© Prathmesh Ranaut, 2017

Page 35: Performance Optimization in Ruby

© Prathmesh Ranaut, 2017

Page 36: Performance Optimization in Ruby

RUBYPROFHTTPS://GITHUB.COM/RUBY-PROF/RUBY-PROF

© Prathmesh Ranaut, 2017

Page 37: Performance Optimization in Ruby

require 'ruby-prof'

# profile the coderesult = RubyProf.profile do # ... code to profile ...end

# print a graph profile to textprinter = RubyProf::GraphPrinter.new(result)printer.print(STDOUT, {})

© Prathmesh Ranaut, 2017

Page 38: Performance Optimization in Ruby

© Prathmesh Ranaut, 2017

Page 39: Performance Optimization in Ruby

ObjectSpace.count_objects

© Prathmesh Ranaut, 2017

Page 40: Performance Optimization in Ruby

{ :TOTAL=>30165, :FREE=>2216, :T_OBJECT=>928, :T_CLASS=>574, :T_MODULE=>38, :T_FLOAT=>4, :T_STRING=>13182, :T_REGEXP=>77, :T_ARRAY=>2715, :T_HASH=>88, :T_STRUCT=>3, :T_BIGNUM=>2, :T_FILE=>30, :T_DATA=>363, :T_MATCH=>233, :T_COMPLEX=>1, :T_SYMBOL=>6, :T_IMEMO=>4530, :T_NODE=>5138, :T_ICLASS=>37}

© Prathmesh Ranaut, 2017

Page 41: Performance Optimization in Ruby

DTRACE

© Prathmesh Ranaut, 2017

Page 42: Performance Optimization in Ruby

THANK YOU&

[email protected]

HTTPS://TWITTER.COM/PRATHMESHRANAUT

© Prathmesh Ranaut, 2017