Slide 1

Slide 1 text

Hello!!!

Slide 2

Slide 2 text

Welcome!

Slide 3

Slide 3 text

Thanks!!!

Slide 4

Slide 4 text

You Beer Me

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Samuel L. Jackson @tenderlove

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

America

Slide 9

Slide 9 text

'MURICA

Slide 10

Slide 10 text

FREEDOM

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Ruby Core Rails Core

Slide 14

Slide 14 text

Rack-Core Racc-Core

Slide 15

Slide 15 text

Psych-Core Fiddle-Core

Slide 16

Slide 16 text

Nokogiri-Core rails_autolink-Core

Slide 17

Slide 17 text

Core-Core IHaveNoIdea-Core

Slide 18

Slide 18 text

☑Adequate Everything. Adequately.

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

ಠ_ಠ ಠ_ಠ Separation Of Concerns

Slide 21

Slide 21 text

OMG! INTERNET! POINTS

Slide 22

Slide 22 text

Revert Commits Count Too!

Slide 23

Slide 23 text

More mistakes == more points!!!!

Slide 24

Slide 24 text

Short Stack Engineer

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Dad-Joke Programmer

Slide 28

Slide 28 text

Gorbachev Puff Puff Thunderhorse

Slide 29

Slide 29 text

SEA-TAC Airport YouTube

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Node.JS

Slide 34

Slide 34 text

Closer to the METAL

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

ENGLAND!!

Slide 37

Slide 37 text

No content

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

I realize

Slide 42

Slide 42 text

There is no "d" in "z".

Slide 43

Slide 43 text

"z" == "zee"

Slide 44

Slide 44 text

$$$$$$$$ $$$$$$$$ $$$ 'MURICA!

Slide 45

Slide 45 text

I feel very bad…

Slide 46

Slide 46 text

@fishermanchips

Slide 47

Slide 47 text

Brighton Ruby! Recap!

Slide 48

Slide 48 text

Ruby Order SORTED

Slide 49

Slide 49 text

Hi, I’m! Samuel L. Jackson!

Slide 50

Slide 50 text

Better Security

Slide 51

Slide 51 text

255.255.255.254 Important!

Slide 52

Slide 52 text

Cucumber

Slide 53

Slide 53 text

Steak Holder

Slide 54

Slide 54 text

How to pick JS libraries

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

Quadrant System

Slide 58

Slide 58 text

Cats Code Beer Adequate System

Slide 59

Slide 59 text

1 Quadrant Short.

Slide 60

Slide 60 text

It’s Adequate.

Slide 61

Slide 61 text

RubyMotion! is a bridge. (JUST KIDDING!)

Slide 62

Slide 62 text

My Favorite Type System

Slide 63

Slide 63 text

Word Perfect 5.1

Slide 64

Slide 64 text

Yo Dawg, I heard you like Sandi

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

Fizz Buzz?

Slide 67

Slide 67 text

I am not good enough to be a developer.

Slide 68

Slide 68 text

If I can developer, you can too!

Slide 69

Slide 69 text

BANANA!! ANANA!B

Slide 70

Slide 70 text

I’ve never played Mario Kart. :’(

Slide 71

Slide 71 text

Strong Ducks?

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Gave me a run for my money.

Slide 74

Slide 74 text

Ground Breaking Technology

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

Speed up Rails,! Speed up your Code

Slide 77

Slide 77 text

Stuff on my mind

Slide 78

Slide 78 text

Pro Tip:

Slide 79

Slide 79 text

ruby -d

Slide 80

Slide 80 text

ruby -d -Ilib:test test/cases/ base_test.rb

Slide 81

Slide 81 text

Exception `LoadError' at rubygems.rb:1194 - cannot load such file -- rubygems/defaults/ operating_system ! Exception `LoadError' at kernel_require.rb:55 - cannot load such file -- bundler

Slide 82

Slide 82 text

Find Swallowed Exceptions!

Slide 83

Slide 83 text

Rack

Slide 84

Slide 84 text

def call(env) end LOL Global Data

Slide 85

Slide 85 text

There will be no Rack 2.0* * There will be a Rack 2.0

Slide 86

Slide 86 text

Ruby >= 2.0?

Slide 87

Slide 87 text

Next Generation

Slide 88

Slide 88 text

def call(env, input, output) end IO IO

Slide 89

Slide 89 text

Just Thoughts.

Slide 90

Slide 90 text

Performance Tools

Slide 91

Slide 91 text

Raw Performance

Slide 92

Slide 92 text

benchmark/ips

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

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

Slide 95

Slide 95 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 96

Slide 96 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 97

Slide 97 text

Set Include: 3047175.3 / sec

Slide 98

Slide 98 text

Array Include: 3899.2 / sec

Slide 99

Slide 99 text

IPS: Higher Is Better

Slide 100

Slide 100 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 101

Slide 101 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 102

Slide 102 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 103

Slide 103 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 104

Slide 104 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 105

Slide 105 text

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

Slide 106

Slide 106 text

Blackbox Testing

Slide 107

Slide 107 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 108

Slide 108 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 109

Slide 109 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 110

Slide 110 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 111

Slide 111 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 112

Slide 112 text

Real World Example: Routes

Slide 113

Slide 113 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 114

Slide 114 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 115

Slide 115 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 116

Slide 116 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 117

Slide 117 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 118

Slide 118 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 119

Slide 119 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 120

Slide 120 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 121

Slide 121 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 122

Slide 122 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 123

Slide 123 text

Where is time spent?

Slide 124

Slide 124 text

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

Slide 125

Slide 125 text

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

Slide 126

Slide 126 text

View Results $ stackprof url_for.dump --text

Slide 127

Slide 127 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 128

Slide 128 text

GC.stat()

Slide 129

Slide 129 text

Total allocations GC.stat(:total_allocated_object)

Slide 130

Slide 130 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 131

Slide 131 text

Real World Example: Views

Slide 132

Slide 132 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 133

Slide 133 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 134

Slide 134 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 135

Slide 135 text

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

Slide 136

Slide 136 text

HOW TO LIE WITH GRAPHS

Slide 137

Slide 137 text

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

Slide 138

Slide 138 text

~19% reduction since 4-0-stable

Slide 139

Slide 139 text

~14% reduction since 4-1-stable

Slide 140

Slide 140 text

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

Slide 141

Slide 141 text

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

Slide 142

Slide 142 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 143

Slide 143 text

Speeding up Helpers

Slide 144

Slide 144 text

Object Allocation Reduction

Slide 145

Slide 145 text

Profiling Request / Response

Slide 146

Slide 146 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 147

Slide 147 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 148

Slide 148 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 149

Slide 149 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 150

Slide 150 text

ActiveSupport::SafeBuffer #initialize

Slide 151

Slide 151 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 152

Slide 152 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 153

Slide 153 text

In Rails

Slide 154

Slide 154 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 155

Slide 155 text

HTML Sanitization in Rails

Slide 156

Slide 156 text

ActiveSupport:: SafeBuffer

Slide 157

Slide 157 text

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

Slide 158

Slide 158 text

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

Slide 159

Slide 159 text

`html_safe` just tags the string.

Slide 160

Slide 160 text

ERB::Utils.h

Slide 161

Slide 161 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 162

Slide 162 text

Creates 2 Strings.

Slide 163

Slide 163 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 164

Slide 164 text

String -> String -> SafeBuffer -> String

Slide 165

Slide 165 text

String -> String -> String

Slide 166

Slide 166 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 167

Slide 167 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 168

Slide 168 text

String -> String -> String

Slide 169

Slide 169 text

~200 Allocations Per Request for /books/new

Slide 170

Slide 170 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 171

Slide 171 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 172

Slide 172 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 173

Slide 173 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 174

Slide 174 text

~19% reduction since 4-0-stable

Slide 175

Slide 175 text

~14% reduction since 4-1-stable

Slide 176

Slide 176 text

YMMV (Your Mileage (kilometers?) May Vary)

Slide 177

Slide 177 text

String Object Reduction

Slide 178

Slide 178 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 179

Slide 179 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 180

Slide 180 text

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

Slide 181

Slide 181 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 182

Slide 182 text

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

Slide 183

Slide 183 text

Template Literals Can’t Change

Slide 184

Slide 184 text

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

Slide 185

Slide 185 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 186

Slide 186 text

Speeding up Output

Slide 187

Slide 187 text

WARNING:! Work in Progress

Slide 188

Slide 188 text

Law of Demeter

Slide 189

Slide 189 text

Suggestion of Demeter

Slide 190

Slide 190 text

No content

Slide 191

Slide 191 text

Arrested Developer?

Slide 192

Slide 192 text

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

Slide 193

Slide 193 text

Fewer Types == Faster / Easier code

Slide 194

Slide 194 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 195

Slide 195 text

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

Slide 196

Slide 196 text

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

Slide 197

Slide 197 text

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

Slide 198

Slide 198 text

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

Slide 199

Slide 199 text

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

Slide 200

Slide 200 text

safe_append= class OutputBuffer end

Slide 201

Slide 201 text

Law of Demeter

Slide 202

Slide 202 text

Defensive Programming

Slide 203

Slide 203 text

Limiting Types

Slide 204

Slide 204 text

"I only handle strings!"

Slide 205

Slide 205 text

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

Slide 206

Slide 206 text

How can you get the Output Buffer?

Slide 207

Slide 207 text

Who Mutates the OutputBuffer?

Slide 208

Slide 208 text

I think no one.

Slide 209

Slide 209 text

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

Slide 210

Slide 210 text

Cache Invariants

Slide 211

Slide 211 text

Eliminate Objects

Slide 212

Slide 212 text

"No code is faster than no code"

Slide 213

Slide 213 text

Limit Types

Slide 214

Slide 214 text

Fewer Types = Less Code

Slide 215

Slide 215 text

Less Code = Faster Code

Slide 216

Slide 216 text

Report Performance Issues

Slide 217

Slide 217 text

Rails 4.2 will be the fastest ever!

Slide 218

Slide 218 text

THANKS!