Slide 1

Slide 1 text

Hello!!!

Slide 2

Slide 2 text

Welcome!

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Thanks!!!

Slide 5

Slide 5 text

YOU PARTY ME!

Slide 6

Slide 6 text

As Loooonnnngg As Possible

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Aaron Patterson @tenderlove

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Ruby Core Rails Core

Slide 12

Slide 12 text

This is my first time to give a talk.

Slide 13

Slide 13 text

Twitter: tenderlove GitHub: tenderlove Instagram: tenderlove Yo: tenderlove

Slide 14

Slide 14 text

ಠ_ಠ ಠ_ಠ Separation Of Concerns

Slide 15

Slide 15 text

OMG! INTERNET! POINTS

Slide 16

Slide 16 text

Revert Commits Count Too!

Slide 17

Slide 17 text

More mistakes == more points!!!!

Slide 18

Slide 18 text

Short Stack Engineer

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Dad-Joke Programmer

Slide 22

Slide 22 text

Gorbachev Puff Puff Thunderhorse

Slide 23

Slide 23 text

SEA-TAC Airport YouTube

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Last Year

Slide 27

Slide 27 text

Hawker Centers

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

I love food!

Slide 31

Slide 31 text

Earth (top)

Slide 32

Slide 32 text

Radius at Latitude R = radius of earth! a = equatorial radius! b = polar radius! l = geodetic latitude

Slide 33

Slide 33 text

Seattle Singapore Latitude 47.6097° N 1.3667° N Radius 6,373 km 6,357 km

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

WE MISS YOU,! JIM!

Slide 37

Slide 37 text

Durian Fruit!!!

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Node.JS

Slide 43

Slide 43 text

Closer to the METAL

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

Speed up Rails,! Speed up your Code

Slide 46

Slide 46 text

Rails.inspect

Slide 47

Slide 47 text

THIS IS NOT MAGENTA

Slide 48

Slide 48 text

Performance! Tradeoffs

Slide 49

Slide 49 text

Speed vs! Memory

Slide 50

Slide 50 text

Time vs! Space

Slide 51

Slide 51 text

VS

Slide 52

Slide 52 text

Space! isn’t free.

Slide 53

Slide 53 text

Time! isn’t free.

Slide 54

Slide 54 text

Nothing! is free.

Slide 55

Slide 55 text

VS

Slide 56

Slide 56 text

Find a better algorithm.

Slide 57

Slide 57 text

Mystical Unicorn

Slide 58

Slide 58 text

Making Tradeoffs

Slide 59

Slide 59 text

RAM is cheap. (for web developers) SORRY HEROKU!

Slide 60

Slide 60 text

SPEED at the! cost of RAM.

Slide 61

Slide 61 text

Time and space are related.

Slide 62

Slide 62 text

Performance Tools

Slide 63

Slide 63 text

Raw Performance

Slide 64

Slide 64 text

benchmark/ips

Slide 65

Slide 65 text

benchmark Benchmark.bm do |x| x.report('some test') { N.times { some_test } } end How big? STD LIB

Slide 66

Slide 66 text

Output user system total real some test 0.000000 0.000000 0.000000 ( 0.000098)

Slide 67

Slide 67 text

benchmark/ips require 'benchmark/ips' require 'set' ! list = ('a'..'zzzz').to_a set = Set.new list ! Benchmark.ips do |x| x.report("set access") { set.include? "foo" } ! x.report("ary access") { list.include? "foo" } end G EM

Slide 68

Slide 68 text

Output Calculating ------------------------------------- set access 68622 i/100ms ary access 395 i/100ms ------------------------------------------------- set access 3047175.3 (±12.7%) i/s - 14959596 in 5.018692s ary access 3899.2 (±7.1%) i/s - 19750 in 5.096118s IPS

Slide 69

Slide 69 text

Set Include: 3047175.3 / sec

Slide 70

Slide 70 text

Array Include: 3899.2 / sec

Slide 71

Slide 71 text

IPS: Higher Is Better

Slide 72

Slide 72 text

Output Calculating ------------------------------------- set access 68622 i/100ms ary access 395 i/100ms ------------------------------------------------- set access 3047175.3 (±12.7%) i/s - 14959596 in 5.018692s ary access 3899.2 (±7.1%) i/s - 19750 in 5.096118s STDDEV

Slide 73

Slide 73 text

benchmark N = 100000 ! list = ('a'..'zzz').to_a hash = list.each_with_object({}) { |x,h| h[x] = true } set = Set.new list ! Benchmark.bm do |x| x.report("set access") { N.times { set.include? "foo" } } ! x.report("hash access") { N.times { hash.include? "foo" } } end STD LIB

Slide 74

Slide 74 text

Output user system total real set access 0.030000 0.000000 0.030000 ( 0.030044) hash access 0.030000 0.000000 0.030000 ( 0.032125) Set Is Faster? benchm ark

Slide 75

Slide 75 text

benchmark/ips list = ('a'..'zzz').to_a hash = list.each_with_object({}) { |x,h| h[x] = true } set = Set.new list ! Benchmark.ips do |x| x.report("set access") { set.include? "foo" } x.report("hash access") { hash.include? "foo" } end G EM

Slide 76

Slide 76 text

Output Calculating ------------------------------------- set access 73910 i/100ms hash access 73845 i/100ms ------------------------------------------------- set access 3081455.6 (±7.6%) i/s - 15299370 in 4.999343s hash access 3772358.3 (±7.2%) i/s - 18756630 in 5.004747s benchm ark/ips

Slide 77

Slide 77 text

IPS Graph 0 1000000 2000000 3000000 4000000 Iterations / Sec Set access Hash Access

Slide 78

Slide 78 text

Blackbox Testing

Slide 79

Slide 79 text

Cache Impls cache1 = Cache1.new cache2 = Cache2.new ! cache1["x"] = Object.new cache2["x"] = Object.new ! Benchmark.ips do |x| x.report("cache1") { cache1["x"] } x.report("cache2") { cache2["x"] } end

Slide 80

Slide 80 text

Collect Reports reports = [10, 100, 1000, 100_000].map do |i| cache1 = Cache1.new cache2 = Cache2.new ! (i - 1).times { |z| cache2[z.to_s] = cache1[z.to_s] = Object.new } ! cache1["x"] = Object.new cache2["x"] = Object.new ! report = Benchmark.ips do |x| x.report("cache1") { cache1["x"] } x.report("cache2") { cache2["x"] } end [i, report] end Report

Slide 81

Slide 81 text

Compile Data header = nil rows = reports.map { |i,report| header ||= [nil] + report.map(&:label) ["#{i} elements"] + report.map { |r| (1 / r.ips) * 10_000 } } puts header.join ',' rows.each { |r| puts r.join ',' } Seconds Per Iteration Seconds for 10k iters

Slide 82

Slide 82 text

Runtime Graph Time for 10,000 iterations (seconds) 0.01 0.1 1 10 100 Cache Size 10 elements 100 elements 1000 elements 100000 elements Cache 1 Cache 2

Slide 83

Slide 83 text

Cache Implementation class Cache1 def initialize @cache = {} end def [] k; @cache[k]; end def []= k,v; @cache[k] = v; end end ! class Cache2 def initialize @cache = [] end def [] k; x, = @cache.assoc(k); x; end def []= k,v; @cache << [k, v]; end end Constant Linear

Slide 84

Slide 84 text

Real World Example: Routes

Slide 85

Slide 85 text

Number Of Routes class MyTest routes = ActionDispatch::Routing::RouteSet.new routes.draw { resources(:articles) } end ! article = Article.new.tap(&:save!) ! Benchmark.ips do |x| x.report("link_to") { test.link_to "zomg", article } end

Slide 86

Slide 86 text

Add 10 routes class MyTest routes = ActionDispatch::Routing::RouteSet.new routes.draw { resources(:articles) 10.times do |num| resources num.to_s.to_sym end } end

Slide 87

Slide 87 text

Add 100 routes class MyTest routes = ActionDispatch::Routing::RouteSet.new routes.draw { resources(:articles) 100.times do |num| resources num.to_s.to_sym end } end

Slide 88

Slide 88 text

Add 1000 routes class MyTest routes = ActionDispatch::Routing::RouteSet.new routes.draw { resources(:articles) 1000.times do |num| resources num.to_s.to_sym end } end

Slide 89

Slide 89 text

Sec / 100k calls 9.5 9.7 9.9 10.1 10.3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 link_to

Slide 90

Slide 90 text

Size of URL class MyTest routes = ActionDispatch::Routing::RouteSet.new link = 'a' ! routes.draw { get "/#{link}/:id", :as => :article, :controller => :articles, :action => :show } end ! test = MyTest.new article = Article.new.tap(&:save!) ! puts "Model Instance" Benchmark.ips do |x| x.report("link_to") { test.link_to "zomg", article } end

Slide 91

Slide 91 text

Length of 1 class MyTest routes = ActionDispatch::Routing::RouteSet.new link = 1.times.map(&:to_s).join '/' ! routes.draw { get "/#{link}/:id", :as => :article, :controller => :articles, :action => :show } end

Slide 92

Slide 92 text

Length of 10 class MyTest routes = ActionDispatch::Routing::RouteSet.new link = 10.times.map(&:to_s).join '/' ! routes.draw { get "/#{link}/:id", :as => :article, :controller => :articles, :action => :show } end

Slide 93

Slide 93 text

Length of 100 class MyTest routes = ActionDispatch::Routing::RouteSet.new link = 100.times.map(&:to_s).join '/' ! routes.draw { get "/#{link}/:id", :as => :article, :controller => :articles, :action => :show } end

Slide 94

Slide 94 text

Sec / 100k calls 9 10.5 12 13.5 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 link_to

Slide 95

Slide 95 text

Where is time spent?

Slide 96

Slide 96 text

stackprof https://github.com/tmm1/stackprof

Slide 97

Slide 97 text

Profiling url_for require 'stackprof' StackProf.run(mode: :cpu, out: 'url_for.dump') do 5000.times { test.url_for article } end

Slide 98

Slide 98 text

View Results $ stackprof url_for.dump --text

Slide 99

Slide 99 text

================================== Mode: cpu(1000) Samples: 218 (0.00% miss rate) GC: 27 (12.39%) ================================== TOTAL (pct) SAMPLES (pct) FRAME 149 (68.3%) 57 (26.1%) ActionDispatch::Routing::RouteSet#url_for 183 (83.9%) 19 (8.7%) ActionDispatch::::UrlHelper#call 18 (8.3%) 18 (8.3%) #.build_host_url 14 (6.4%) 13 (6.0%) ActionDispatch::UrlHelper#handle_positional_args 68 (31.2%) 12 (5.5%) ActionDispatch::Journey::Formatter#generate 66 (30.3%) 12 (5.5%) ActionDispatch::Formatter#visit_CAT 10 (4.6%) 10 (4.6%) ActionDispatch::Utils::UriEncoder#escape 56 (25.7%) 7 (3.2%) block in ActionDispatch::Formatter#generate 13 (6.0%) 5 (2.3%) ActionDispatch#extract_parameterized_parts

Slide 100

Slide 100 text

GC.stat()

Slide 101

Slide 101 text

Total allocations GC.stat(:total_allocated_object)

Slide 102

Slide 102 text

Measure Allocations Person.find id before = GC.stat(:total_allocated_object) N.times { Person.find id } after = GC.stat(:total_allocated_object) puts (after - before) / N W arm up Benchmark Objects / Call Count O bjs C ount O bjs

Slide 103

Slide 103 text

Real World Example: Views

Slide 104

Slide 104 text

Request Benchmark task :allocated_objects do app = Ko1TestApp::Application.instance app.app do_test_task(app) env = rackenv "/books/new" do_test_task(app, env.dup) before = GC.stat :total_allocated_object TEST_CNT.times { do_test_task(app, env.dup) } after = GC.stat :total_allocated_object puts (after - before) / TEST_CNT end "/books/new"

Slide 105

Slide 105 text

Request Benchmark task :allocated_objects do app = Ko1TestApp::Application.instance app.app do_test_task(app) env = rackenv "/books/new" do_test_task(app, env.dup) before = GC.stat :total_allocated_object TEST_CNT.times { do_test_task(app, env.dup) } after = GC.stat :total_allocated_object puts (after - before) / TEST_CNT end

Slide 106

Slide 106 text

Request Benchmark task :allocated_objects do app = Ko1TestApp::Application.instance app.app do_test_task(app) env = rackenv "/books/new" do_test_task(app, env.dup) before = GC.stat :total_allocated_object TEST_CNT.times { do_test_task(app, env.dup) } after = GC.stat :total_allocated_object puts (after - before) / TEST_CNT end

Slide 107

Slide 107 text

Test Results Object Allocations Per Request 2000 2150 2300 2450 2600 4-0-stable 4-1-stable master 2000

Slide 108

Slide 108 text

HOW TO LIE WITH GRAPHS

Slide 109

Slide 109 text

Test Results Object Allocations Per Request 0 650 1300 1950 2600 4-0-stable 4-1-stable master

Slide 110

Slide 110 text

~19% reduction since 4-0-stable

Slide 111

Slide 111 text

~14% reduction since 4-1-stable

Slide 112

Slide 112 text

allocation_tracer https://github.com/ko1/allocation_tracer

Slide 113

Slide 113 text

Example ObjectSpace::AllocationTracer.trace do 1000.times { ["foo", {}] } end ! ObjectSpace::AllocationTracer.allocated_count_table

Slide 114

Slide 114 text

Output {:T_NONE=>0, :T_OBJECT=>0, :T_CLASS=>0, :T_MODULE=>0, :T_FLOAT=>0, :T_STRING=>1000, :T_REGEXP=>0, :T_ARRAY=>1000, :T_HASH=>1000, :T_ZOMBIE=>0}

Slide 115

Slide 115 text

Speeding up ActiveRecord

Slide 116

Slide 116 text

~3 Years of work

Slide 117

Slide 117 text

How! ActiveRecord! works.

Slide 118

Slide 118 text

ActiveRecord::Relation Post.find(10) ARel::SQL::Node Database ActiveRecord::Base SELECT * FROM …

Slide 119

Slide 119 text

Part 1

Slide 120

Slide 120 text

Bind Parameter Introduction

Slide 121

Slide 121 text

Post.find(1) Post.find(3) Post.find(5) SELECT * FROM posts WHERE id = 1 SELECT * FROM posts WHERE id = 3 SELECT * FROM posts WHERE id = 5

Slide 122

Slide 122 text

Post.find(1) Post.find(3) Post.find(5) SELECT * FROM posts WHERE id = ? [id, 1] SELECT * FROM posts WHERE id = ? [id, 3] SELECT * FROM posts WHERE id = ? [id, 5]

Slide 123

Slide 123 text

Separate static and dynamic content

Slide 124

Slide 124 text

Theory

Slide 125

Slide 125 text

ActiveRecord::Relation Post.find(10) ARel::SQL::Node Database ActiveRecord::Base SELECT * FROM … Cacheable

Slide 126

Slide 126 text

Part 2

Slide 127

Slide 127 text

Code Decoupling

Slide 128

Slide 128 text

def add_constraints(scope) tables = construct_tables ! chain.each_with_index do |reflection, i| table, foreign_table = tables.shift, tables.first ! if reflection.source_macro == :has_and_belongs_to_many join_table = tables.shift ! scope = scope.joins(join( join_table, table[reflection.association_primary_key]. eq(join_table[reflection.association_foreign_key]) )) ! table, foreign_table = join_table, tables.first end ! if reflection.source_macro == :belongs_to if reflection.options[:polymorphic] key = reflection.association_primary_key(klass) else key = reflection.association_primary_key end ! foreign_key = reflection.foreign_key else key = reflection.foreign_key foreign_key = reflection.active_record_primary_key end ! if reflection == chain.last bind_val = bind scope, table.table_name, key.to_s, owner[foreign_key] scope = scope.where(table[key].eq(bind_val)) ! if reflection.type value = owner.class.base_class.name bind_val = bind scope, table.table_name, reflection.type.to_s, value scope = scope.where(table[reflection.type].eq(bind_val)) end else constraint = table[key].eq(foreign_table[foreign_key]) ! if reflection.type type = chain[i + 1].klass.base_class.name constraint = constraint.and(table[reflection.type].eq(type)) end ! scope = scope.joins(join(foreign_table, constraint)) end ! # Exclude the scope of the association itself, because that # was already merged in the #scope method. (scope_chain[i] - [self.reflection.scope]).each do |scope_chain_item| item = eval_scope(reflection.klass, scope_chain_item) ! scope.includes! item.includes_values scope.where_values += item.where_values end end ! scope end

Slide 129

Slide 129 text

One Method to Rule Them All, And In Legacy Code Bind Them.

Slide 130

Slide 130 text

rm habtm R ails 4.1

Slide 131

Slide 131 text

habtm.is_a?(hm:t) #=> true

Slide 132

Slide 132 text

commit 88c009377851912c60fd16ec4bfab3001ac2cf9f Author: Aaron Patterson Date: Wed Oct 2 15:53:56 2013 -0700 ! remove HABTM special cases from associations classes ! diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/ active_record/associations/association_scope.rb index 8027acf..d862a5f 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -44,18 +44,6 @@ module ActiveRecord chain.each_with_index do |reflection, i| table, foreign_table = tables.shift, tables.first - if reflection.source_macro == :has_and_belongs_to_many - join_table = tables.shift - - scope = scope.joins(join( - join_table, - table[reflection.association_primary_key]. - eq(join_table[reflection.association_foreign_key]) - )) - - table, foreign_table = join_table, tables.first - end - if reflection.source_macro == :belongs_to if reflection.options[:polymorphic] key = reflection.association_primary_key(self.klass) diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/ active_record/associations/join_dependency.rb index 5aa17e5..fa212f3 100644 --- a/activerecord/lib/active_record/associations/join_dependency.rb +++ b/activerecord/lib/active_record/associations/join_dependency.rb @@ -216,7 +216,7 @@ module ActiveRecord else association = join_part.instantiate(row) unless row[join_part.aliased_primary_key].nil? case macro - when :has_many, :has_and_belongs_to_many + when :has_many other = record.association(join_part.reflection.name) other.loaded! other.target.push(association) if association diff --git a/activerecord/lib/active_record/associations/join_dependency/join_association.rb b/ activerecord/lib/active_record/associations/join_dependency/join_association.rb

Slide 133

Slide 133 text

Part 3

Slide 134

Slide 134 text

Introduce a cache

Slide 135

Slide 135 text

Cache Code Example cache = ActiveRecord::StatementCache.new do |params| Person.where(name: params.bind).limit(1) end ! cache.execute ["Aaron"] cache.execute ["Ebi"] AR::Relation Execute

Slide 136

Slide 136 text

Binds Cache Object Internals #, #]], @indexes=[0]>, @query_builder= #> C om piled SQ L

Slide 137

Slide 137 text

Update Internals, Cache Relation Objects

Slide 138

Slide 138 text

Things That Use Relations ❤ Post.find() ❤ Post.find_by_* ❤ has_many ❤ has_many :through ❤ has_and_belongs_to_many ❤ belongs_to All Cacheable

Slide 139

Slide 139 text

Example Implementation

Slide 140

Slide 140 text

Person.find(id) s = find_by_statement_cache.synchronize { find_by_statement_cache[key] ||= StatementCache.create(connection) { |params| where(primary_key => params.bind).limit(1) } } record = s.execute([id], self, connection).first R elation! (static) Parameter! (dynamic)

Slide 141

Slide 141 text

WHAT DO THESE EVEN MEAN??? Ways to call `Post.find` ❤ Post.find(1) ❤ Post.find([1]) ❤ Post.find([[1]]) ❤ Post.find({1 => 1}) ❤ Post.find(post) ❤ Post.find(1) { … } ❤ scoping { Post.find(1) } Cachea wtf

Slide 142

Slide 142 text

Rejection Code def find(*ids) # We don't have cache keys for this stuff yet return super unless ids.length == 1 return super if block_given? || primary_key.nil? || default_scopes.any? || columns_hash.include?(inheritance_column) || ids.first.kind_of?(Array)

Slide 143

Slide 143 text

Performance of ActiveRecord

Slide 144

Slide 144 text

Post.find() Post.find_by_name()

Slide 145

Slide 145 text

`Post.find` Calls Per Second over Time Calls Per Second (higher is better) 0 4000 8000 12000 16000 2-3-stable 3-0-stable 3-1-stable 3-2-stable 4-0-stable 4-1-stable master adequaterecord SQLite3 MySQL2 MySQL PostgreSQL

Slide 146

Slide 146 text

`Post.find_by_name` Calls Per Second over Time Calls Per Second (higher is better) 0 3500 7000 10500 14000 2-3-stable 3-0-stable 3-1-stable 3-2-stable 4-0-stable 4-1-stable master adequaterecord SQLite3 MySQL2 MySQL PostgreSQL

Slide 147

Slide 147 text

Calls Per Second (MySQL) Calls Per Second (higher is better) 0 2000 4000 6000 8000 2-3-stable 3-0-stable 3-1-stable 3-2-stable 4-0-stable 4-1-stable master adequaterecord find by id find by name

Slide 148

Slide 148 text

% Faster than 4-1-stable 0.00% 45.00% 90.00% 135.00% 180.00% SQLite3 MySQL2 MySQL PostgreSQL find by id find by name

Slide 149

Slide 149 text

% Faster than 2-3-stable 0.00% 35.00% 70.00% 105.00% 140.00% SQLite3 MySQL PostgreSQL find by id find by name

Slide 150

Slide 150 text

Objects Allocated Per Call (find by id) Objects Allocated (lower is better) 0 75 150 225 300 2-3-stable 3-0-stable 3-1-stable 3-2-stable 4-0-stable 4-1-stable master adequate SQLite3 MySQL2 MySQL PostgreSQL

Slide 151

Slide 151 text

Object Allocated Per Call (find by name) Objects Allocated (lower is better) 0 75 150 225 300 2-3-stable 3-0-stable 3-1-stable 3-2-stable 4-0-stable 4-1-stable master adequate SQLite3 MySQL2 MySQL PostgreSQL

Slide 152

Slide 152 text

70% Fewer Objects Compared to 4-1-stable

Slide 153

Slide 153 text

55% Fewer Objects Compared to 2-3-stable

Slide 154

Slide 154 text

belongs_to

Slide 155

Slide 155 text

belongs_to calls per second Calls Per Second (higher is better) 0 3000 6000 9000 12000 2-3-stable 3-0-stable 3-1-stable 3-2-stable 4-0-stable 4-1-stable master adequaterecord SQLite3 MySQL MySQL2 PostgreSQL

Slide 156

Slide 156 text

belongs_to Percent Faster 0.00% 45.00% 90.00% 135.00% 180.00% SQLite3 MySQL MySQL2 PostgreSQL 2-3-stable 4-1-stable

Slide 157

Slide 157 text

has_many has_many :through

Slide 158

Slide 158 text

`has_many` Call Speed Over Time Calls Per Second (higher is better) 0 2750 5500 8250 11000 2-3-stable 3-0-stable 4-1-stable master adequaterecord SQlite3 MySQL MySQL2 PostgreSQL

Slide 159

Slide 159 text

`hm:t` Call Speed Over Time Calls Per Second (higher is better) 0 3000 6000 9000 12000 2-3-stable 3-0-stable 4-1-stable master adequaterecord SQLite3 MySQL MySQL2 PostgreSQL

Slide 160

Slide 160 text

Percent Faster than 2-3-stable 0% 25% 50% 75% 100% SQLite3 MySQL PostgreSQL has_many hm:t

Slide 161

Slide 161 text

Percent Faster than 4-1-stable 0% 65% 130% 195% 260% SQLite3 MySQL MySQL2 PostgreSQL has_many hm:t

Slide 162

Slide 162 text

has_many :through growth

Slide 163

Slide 163 text

Growth Test class A < ActiveRecord::Base has_many :bs has_many :cs, :through => :bs end ! class B < ActiveRecord::Base belongs_to :a has_many :cs end ! class C < ActiveRecord::Base belongs_to :b end

Slide 164

Slide 164 text

Growth Test class A < ActiveRecord::Base has_many :bs has_many :cs, :through => :bs has_many :ds, :through => :cs end ! class B < ActiveRecord::Base belongs_to :a has_many :cs end ! class C < ActiveRecord::Base belongs_to :b has_many :ds end ! class D < ActiveRecord::Base belongs_to :b end as we add more,! what’s the speed?

Slide 165

Slide 165 text

Time Taken to Call hm:t 100k times Seconds Taken (lower is better) 0 40 80 120 160 Nuber of hm:t associations 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 y = 0.5783x + 5.9815 R² = 0.9971 y = 7.448x + 16.506 R² = 0.9974 4-1-stable adequate AWWWWW YEAH, CONSTANT TIME!

Slide 166

Slide 166 text

TL;DR: ~100% faster

Slide 167

Slide 167 text

TL;DR: 9001% better

Slide 168

Slide 168 text

Challenges

Slide 169

Slide 169 text

Trade Memory For Speed

Slide 170

Slide 170 text

Total Cache Size = count(find_by_*) * Size

Slide 171

Slide 171 text

Raw Relations

Slide 172

Slide 172 text

Example Controller class PeopleController def index @people = Person.where(name: params[:name]).to_a end end

Slide 173

Slide 173 text

Can we cache it?

Slide 174

Slide 174 text

Experimental Branch Person.where(name: params[:name]).to_a Iterations Per Second (higher is better) 0 2000 4000 6000 8000 where(:name) Master Experimental Branch

Slide 175

Slide 175 text

~30% faster

Slide 176

Slide 176 text

Key Calculation def cached_query if [joins_values, having_values, group_values, order_values, select_values, ].any?(&:any?) return yield end ! if limit_value || offset_value || distinct_value || from_value || lock_value return yield end ! if where_values.length != bind_values.length return yield end ! key = if bind_values.any? "bv_#{bind_values.map { |bv| bv.first.name }.join}" else :find_all_stuff end ! connection = @klass.connection cache = @klass.find_by_statement_cache ! s = cache[key] || cache.synchronize { cache[key] ||= begin bvs = bind_values.map { |bv| bv = bv.dup bv[1] = StatementCache::Substitute.new bv } StatementCache.create_with_binds(connection, arel, bvs) end } s.execute bind_values.map(&:last), @klass, connection end Key Calculation Query Execution

Slide 177

Slide 177 text

Over 11 variables impact the key

Slide 178

Slide 178 text

This code only handles two types of queries

Slide 179

Slide 179 text

One more experiment

Slide 180

Slide 180 text

Finder Comparison Benchmark.ips do |x| x.report('Person.where') { Person.where(name: 'Aaron').first } x.report('Person.find_by') { Person.find_by_name('Aaron') } end

Slide 181

Slide 181 text

Finder Comparison Calls Per Second 0 3500 7000 10500 14000 Finder .where() .find_by_name

Slide 182

Slide 182 text

find_by_name is 3x faster

Slide 183

Slide 183 text

Finder Comparison Benchmark.ips do |x| x.report('Person.where') { Person.where(name: 'Aaron').first } x.report('Person.find_by') { Person.find_by_name('Aaron') } end New AR::Relation Allocation

Slide 184

Slide 184 text

Should we cache all relations?

Slide 185

Slide 185 text

I’m not sure.

Slide 186

Slide 186 text

I want a new API.

Slide 187

Slide 187 text

New API class Person < ActiveRecord::Base cached_query(:find_all_by_name) do |name| where(name: name) end end ! Person.find_all_by_name("foo") Person.find_all_by_name("bar")

Slide 188

Slide 188 text

Cache Key is Easy

Slide 189

Slide 189 text

Relation API maintained

Slide 190

Slide 190 text

Speeding up Helpers

Slide 191

Slide 191 text

Object Allocation Reduction

Slide 192

Slide 192 text

Profiling Request / Response

Slide 193

Slide 193 text

Test Code task :view_stack do app = Ko1TestApp::Application.instance app.app ! env = rackenv "/books/new" do_test_task(app, env.dup) require 'stackprof' puts "#" * 90 StackProf.run(mode: :cpu, out: 'req-res.dump') do 1800.times { do_test_task(app, env.dup) } end end

Slide 194

Slide 194 text

Test Code task :view_stack do app = Ko1TestApp::Application.instance app.app ! env = rackenv "/books/new" do_test_task(app, env.dup) require 'stackprof' puts "#" * 90 StackProf.run(mode: :cpu, out: 'req-res.dump') do 1800.times { do_test_task(app, env.dup) } end end

Slide 195

Slide 195 text

Test Code task :view_stack do app = Ko1TestApp::Application.instance app.app ! env = rackenv "/books/new" do_test_task(app, env.dup) require 'stackprof' puts "#" * 90 StackProf.run(mode: :cpu, out: 'req-res.dump') do 1800.times { do_test_task(app, env.dup) } end end

Slide 196

Slide 196 text

TOTAL (pct) SAMPLES (pct) FRAME 813 (9.5%) 813 (9.5%) ActiveSupport::SafeBuffer#initialize 699 (8.1%) 350 (4.1%) block in ActiveRecord::Read#read_attribute 486 (5.7%) 298 (3.5%) ActionController::UrlFor#url_options 670 (7.8%) 274 (3.2%) ActionDispatch::Journey::Format#evaluate 773 (9.0%) 253 (2.9%) ActionDispatch#parameterize_args 1172 (13.6%) 220 (2.6%) ActiveRecord::Persistence#instantiate 213 (2.5%) 213 (2.5%) block in SQLite3::Statement#each 208 (2.4%) 208 (2.4%) ActiveSupport::SafeBuffer#html_safe? 204 (2.4%) 204 (2.4%) ActionDispatch::UrlFor#routes_generation? 245 (2.9%) 191 (2.2%) block (2 levels) in Class#class_attribute

Slide 197

Slide 197 text

ActiveSupport::SafeBuffer #initialize

Slide 198

Slide 198 text

Finding Calls require 'active_support/all' ! trace = TracePoint.new(:c_call, :call) { |tp| if tp.defined_class == ActiveSupport::SafeBuffer && tp.method_id == :initialize puts "#" * 90 puts tp.binding.eval "caller" end } ! trace.enable "asdfadsf".html_safe ActiveSupport::SafeBuffer.new "omgee" Callstack

Slide 199

Slide 199 text

Output ##################################################################### t1.rb:7:in `eval' t1.rb:7:in `block in ' lib/active_support/core_ext/string/output_safety.rb:166:in `initialize' lib/active_support/core_ext/string/output_safety.rb:251:in `new' lib/active_support/core_ext/string/output_safety.rb:251:in `html_safe' t1.rb:12:in `' ##################################################################### t1.rb:7:in `eval' t1.rb:7:in `block in ' lib/active_support/core_ext/string/output_safety.rb:166:in `initialize' t1.rb:13:in `new' t1.rb:13:in `'

Slide 200

Slide 200 text

In Rails

Slide 201

Slide 201 text

Tag Options def tag_option(key, value, escape) if value.is_a?(Array) value = escape ? safe_join(value, " ") : value.join(" ") else value = escape ? ERB::Util.h(value) : value end %(#{key}="#{value}") end

Slide 202

Slide 202 text

HTML Sanitization in Rails

Slide 203

Slide 203 text

ActiveSupport:: SafeBuffer

Slide 204

Slide 204 text

Ordinary String >> x = "foo" => "foo" >> x.class => String >> x.html_safe? => false

Slide 205

Slide 205 text

SafeBuffer >> x = "foo" => "foo" >> y = x.html_safe => "foo" >> y.class => ActiveSupport::SafeBuffer >> y.html_safe? => true

Slide 206

Slide 206 text

`html_safe` just tags the string.

Slide 207

Slide 207 text

ERB::Utils.h

Slide 208

Slide 208 text

ERB::Utils.h def html_escape(s) s = s.to_s if s.html_safe? s else s.gsub(HTML_ESCAPE_REGEXP, HTML_ESCAPE).html_safe end end

Slide 209

Slide 209 text

Creates 2 Strings.

Slide 210

Slide 210 text

Tag Options def tag_option(key, value, escape) if value.is_a?(Array) value = escape ? safe_join(value, " ") : value.join(" ") else value = escape ? ERB::Util.h(value) : value end %(#{key}="#{value}") end

Slide 211

Slide 211 text

String -> String -> SafeBuffer -> String

Slide 212

Slide 212 text

String -> String -> String

Slide 213

Slide 213 text

Extract Method def unwrapped_html_escape(s) # :nodoc: s = s.to_s if s.html_safe? s else s.gsub(HTML_ESCAPE_REGEXP, HTML_ESCAPE) end end ! def html_escape(s) unwrapped_html_escape(s).html_safe end

Slide 214

Slide 214 text

Update Callers def tag_option(key, value, escape) if value.is_a?(Array) value = escape ? safe_join(value, " ") : value.join(" ") else value = escape ? ERB::Util.unwrapped_html_escape(value) : value end %(#{key}="#{value}") end

Slide 215

Slide 215 text

String -> String -> String

Slide 216

Slide 216 text

~200 Allocations Per Request for /books/new

Slide 217

Slide 217 text

Request Benchmark task :allocation_tracer do app = Ko1TestApp::Application.instance app.app do_test_task(app) env = rackenv "/books/new" do_test_task(app, env.dup) ObjectSpace::AllocationTracer.trace do TEST_CNT.times { do_test_task(app, env.dup) } end p ObjectSpace::AllocationTracer.allocated_count_table end "/books/new"

Slide 218

Slide 218 text

Request Benchmark task :allocation_tracer do app = Ko1TestApp::Application.instance app.app do_test_task(app) env = rackenv "/books/new" do_test_task(app, env.dup) ObjectSpace::AllocationTracer.trace do TEST_CNT.times { do_test_task(app, env.dup) } end p ObjectSpace::AllocationTracer.allocated_count_table end

Slide 219

Slide 219 text

Request Benchmark task :allocation_tracer do app = Ko1TestApp::Application.instance app.app do_test_task(app) env = rackenv "/books/new" do_test_task(app, env.dup) ObjectSpace::AllocationTracer.trace do TEST_CNT.times { do_test_task(app, env.dup) } end p ObjectSpace::AllocationTracer.allocated_count_table end

Slide 220

Slide 220 text

Allocations Per Request 0 275 550 825 1100 T_STRING T_ARRAY T_HASH T_NODE T_DATA OTHER 4-0-stable 4-1-stable master

Slide 221

Slide 221 text

~19% reduction since 4-0-stable

Slide 222

Slide 222 text

~14% reduction since 4-1-stable

Slide 223

Slide 223 text

YMMV (Your Mileage May Vary)

Slide 224

Slide 224 text

String Object Reduction

Slide 225

Slide 225 text

Mutable Strings irb(main):007:0> 5.times { irb(main):008:1* p "foo".object_id irb(main):009:1> } 70344882872020 70344882871920 70344882871840 70344882871720 70344882871540 => 5 irb(main):010:0>

Slide 226

Slide 226 text

Frozen Strings irb(main):010:0> 5.times { irb(main):011:1* p "foo".freeze.object_id irb(main):012:1> } 70344870307760 70344870307760 70344870307760 70344870307760 70344870307760 => 5

Slide 227

Slide 227 text

ERB Template <% books.each do |book| %> <%= book.name %> <% end %>

Slide 228

Slide 228 text

Compiled Template @output_buffer = output_buffer || ActionView::OutputBuffer.new;@output_buffer.safe_append='

Listing books

! Name ! '.freeze; @books.each do |book| @output_buffer.safe_append=' '.freeze;@output_buffer.append=( book.name );@output_buffer.safe_append=' '.freeze;@output_buffer.append=( link_to 'Show', book );@output_buffer.safe_append=' td> '.freeze; end @output_buffer.safe_append=' !

Slide 229

Slide 229 text

Compiled Template @output_buffer = ActionView::OutputBuffer.new @output_buffer.safe_append=' ' @output_buffer.append=( book.name ) HTML Literal

Slide 230

Slide 230 text

Template Literals Can’t Change

Slide 231

Slide 231 text

Add `freeze` @output_buffer = ActionView::OutputBuffer.new @output_buffer.safe_append=' ’.freeze @output_buffer.append=( book.name ) HTML Literal

Slide 232

Slide 232 text

Allocations Per Request 0 275 550 825 1100 T_STRING T_ARRAY T_HASH T_NODE T_DATA OTHER 4-0-stable 4-1-stable master

Slide 233

Slide 233 text

Speeding up Output

Slide 234

Slide 234 text

WARNING:! Work in Progress

Slide 235

Slide 235 text

Law of Demeter

Slide 236

Slide 236 text

Suggestion of Demeter

Slide 237

Slide 237 text

No content

Slide 238

Slide 238 text

Arrested Developer?

Slide 239

Slide 239 text

It’s not about dots, it’s about types.

Slide 240

Slide 240 text

Fewer Types == Faster / Easier code

Slide 241

Slide 241 text

Compiled Template @output_buffer = output_buffer || ActionView::OutputBuffer.new;@output_buffer.safe_append='

Listing books

! Name ! '.freeze; @books.each do |book| @output_buffer.safe_append=' '.freeze;@output_buffer.append=( book.name );@output_buffer.safe_append=' '.freeze;@output_buffer.append=( link_to 'Show', book );@output_buffer.safe_append=' td> '.freeze; end @output_buffer.safe_append=' !

Slide 242

Slide 242 text

Compiled Template @output_buffer = ActionView::OutputBuffer.new @output_buffer.safe_append=' '.freeze @output_buffer.append=( book.name ) HTML Literal

Slide 243

Slide 243 text

safe_append= class OutputBuffer def safe_append=(value) return self if value.nil? super(value.to_s) end end W hy?

Slide 244

Slide 244 text

safe_append= class OutputBuffer def safe_append=(value) super(value.to_s) end end

Slide 245

Slide 245 text

Law of Demeter

Slide 246

Slide 246 text

Defensive Programming

Slide 247

Slide 247 text

superclass def safe_append=(value) (!html_safe? || arg.html_safe?) ? arg : ERB::Utils.h(arg) end Only on m utations

Slide 248

Slide 248 text

How can you get the Output Buffer?

Slide 249

Slide 249 text

Who Mutates the OutputBuffer?

Slide 250

Slide 250 text

I think no one.

Slide 251

Slide 251 text

superclass def safe_append=(value) arg.html_safe? ? arg : ERB::Utils.h(arg) end

Slide 252

Slide 252 text

Cache Invariants

Slide 253

Slide 253 text

Eliminate Objects

Slide 254

Slide 254 text

"No code is faster than no code"

Slide 255

Slide 255 text

Limit Types

Slide 256

Slide 256 text

Fewer Types = Less Code

Slide 257

Slide 257 text

Less Code = Faster Code

Slide 258

Slide 258 text

Report Performance Issues

Slide 259

Slide 259 text

Rails 4.2 will be the fastest ever!

Slide 260

Slide 260 text

THANKS!

Slide 261

Slide 261 text

append= def append=(value) (!html_safe? || arg.html_safe?) ? arg : escape(arg) end