Slide 1

Slide 1 text

The Fast Lane: Async Rails

Slide 2

Slide 2 text

Matheus Richard

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

I II III

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

What is Async?

Slide 7

Slide 7 text

Stuff running at the same time “ - You, possibly

Slide 8

Slide 8 text

Concurrency vs Parallelism

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

󰗺 󰘁

Slide 13

Slide 13 text

󰗺 󰘁 👾

Slide 14

Slide 14 text

󰗺 󰘁 🎮 👾

Slide 15

Slide 15 text

Concurrency 󰗺 󰘁 🎮 👾 I'LL PLAY THIS LEVEL

Slide 16

Slide 16 text

Concurrency 󰗺 󰘁 🎮 👾 I'LL PLAY THIS ONE

Slide 17

Slide 17 text

😇 🤝 😇

Slide 18

Slide 18 text

Cooperative Execution 😇 🤝 😇

Slide 19

Slide 19 text

😈 😈

Slide 20

Slide 20 text

😭 😈 🎮 👾 I'LL PLAY THEM ALL!

Slide 21

Slide 21 text

󰗺 󰘁 🎮 👾 5 MINUTES EACH 󰥺

Slide 22

Slide 22 text

󰗺 󰘁 🎮 👾 Preemptive Execution 5 MINUTES EACH 󰥺

Slide 23

Slide 23 text

They don't wanna play the same game

Slide 24

Slide 24 text

Concurrency 󰗺 󰘁 🎮 👾 󰗺 󰘁 🎮 ⚽ CONTEXT SWITCH

Slide 25

Slide 25 text

We don't like that! We want to play at the same time “ - Kids

Slide 26

Slide 26 text

We want independence! “ - Kids

Slide 27

Slide 27 text

Parallelism 󰗺 󰘁 🎮 🎮 🏈 ⚽

Slide 28

Slide 28 text

Parallelism 󰗺 󰘁 🎮 🎮 🏈 ⚽

Slide 29

Slide 29 text

Parallelism 󰗺 󰘁 🎮 🎮 👾 ⚽

Slide 30

Slide 30 text

Review time

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

CRuby Specific

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Fibers 🪡 Threads 🧵

Slide 36

Slide 36 text

🏠 Ractors 🏭 Processes

Slide 37

Slide 37 text

🏠 Ractors 🏭 Processes Fibers 🪡 Threads 🧵

Slide 38

Slide 38 text

🪡 Fibers

Slide 39

Slide 39 text

fiber = Fiber.new do "Hey, I'm a fiber!" end

Slide 40

Slide 40 text

fiber = Fiber.new do "Hey, I'm a fiber!" end puts fiber.resume # Hey, I'm a fiber!

Slide 41

Slide 41 text

boy = Fiber.new do puts "I'm playing stage 1" Fiber.yield puts "I'm playing stage 2" end end

Slide 42

Slide 42 text

boy = Fiber.new do puts "I'm playing stage 1" Fiber.yield puts "I'm playing stage 2" end end

Slide 43

Slide 43 text

boy = Fiber.new do puts "I'm playing stage 1" Fiber.yield puts "I'm playing stage 2" end end

Slide 44

Slide 44 text

boy = Fiber.new do puts "I'm playing stage 1" Fiber.yield puts "I'm playing stage 2" end end boy.resume # 󰗺 I'm playing stage 1

Slide 45

Slide 45 text

boy = Fiber.new do puts "I'm playing stage 1" Fiber.yield puts "I'm playing stage 2" end end boy.resume # 󰗺 I'm playing stage 1 boy.resume # 󰗺 I'm playing stage 2

Slide 46

Slide 46 text

󰗺 󰘁

Slide 47

Slide 47 text

current_stage = 1 boy = Fiber.new do loop do puts "󰗺 I'm playing stage :{current_stage}" current_stage += 1 Fiber.yield end end

Slide 48

Slide 48 text

current_stage = 1 boy = Fiber.new do loop do puts "󰗺 I'm playing stage :{current_stage}" current_stage += 1 Fiber.yield end end

Slide 49

Slide 49 text

current_stage = 1 boy = Fiber.new do loop do puts "󰗺 I'm playing stage :{current_stage}" current_stage += 1 Fiber.yield end end

Slide 50

Slide 50 text

current_stage = 1 boy = Fiber.new do loop do puts "󰗺 I'm playing stage :{current_stage}" current_stage += 1 Fiber.yield end end

Slide 51

Slide 51 text

# ::. girl = Fiber.new do loop do puts "󰘁 I'm playing stage :{current_stage}" current_stage += 1 Fiber.yield end end

Slide 52

Slide 52 text

5.times do boy.resume girl.resume end

Slide 53

Slide 53 text

5.times do boy.resume girl.resume end # 󰗺 I'm playing stage 1 # 󰘁 I'm playing stage 2 # 󰗺 I'm playing stage 3 # 󰘁 I'm playing stage 4 # 󰗺 I'm playing stage 5 # 󰘁 I'm playing stage 6 # 󰗺 I'm playing stage 7 # 󰘁 I'm playing stage 8 # 󰗺 I'm playing stage 9 # 󰘁 I'm playing stage 10

Slide 54

Slide 54 text

2.times do boy.resume girl.resume girl.resume girl.resume girl.resume end

Slide 55

Slide 55 text

2.times do boy.resume girl.resume girl.resume girl.resume girl.resume end # 󰗺 I'm playing stage 1 # 󰘁 I'm playing stage 2 # 󰘁 I'm playing stage 3 # 󰘁 I'm playing stage 4 # 󰘁 I'm playing stage 5 # 󰗺 I'm playing stage 6 # 󰘁 I'm playing stage 7 # 󰘁 I'm playing stage 8 # 󰘁 I'm playing stage 9 # 󰘁 I'm playing stage 10

Slide 56

Slide 56 text

󰥺 YOU are the scheduler

Slide 57

Slide 57 text

🧵 Threads

Slide 58

Slide 58 text

thread = Thread.new do puts "Hey, I'm a thread!" end

Slide 59

Slide 59 text

thread = Thread.new do puts "Hey, I'm a thread!" end # threads start running # automatically

Slide 60

Slide 60 text

thread = Thread.new do puts "Hey, I'm a thread!" end thread.join # Hey, I'm a thread!

Slide 61

Slide 61 text

So what?

Slide 62

Slide 62 text

fibonacci(35) fibonacci(35)

Slide 63

Slide 63 text

fibonacci(35) # Runs in ~1s fibonacci(35) # Runs in ~1s

Slide 64

Slide 64 text

t1 = Thread.new do fibonacci(35) end t2 = Thread.new do fibonacci(35) end

Slide 65

Slide 65 text

t1 = Thread.new do fibonacci(35) # Runs in ~1s end t2 = Thread.new do fibonacci(35) # Runs in ~1s end

Slide 66

Slide 66 text

[t1, t2].each { |t| t.join } # Takes ~2 seconds to run

Slide 67

Slide 67 text

t1 Executing t2

Slide 68

Slide 68 text

t1 Executing t2

Slide 69

Slide 69 text

Threads don't run in parallel!

Slide 70

Slide 70 text

Threads switch automatically on I/O

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

t1 = Thread.new do fibonacci(35) end

Slide 73

Slide 73 text

t1 = Thread.new do sleep 1 end

Slide 74

Slide 74 text

[t1, t2].each { |t| t.join } # Takes ~1 second to run

Slide 75

Slide 75 text

t1 Executing t2 Time

Slide 76

Slide 76 text

t1 Executing t2 Time I/O

Slide 77

Slide 77 text

t1 = Thread.new do sleep 1 end

Slide 78

Slide 78 text

t1 = Thread.new do File.read("README.md") end

Slide 79

Slide 79 text

t1 = Thread.new do HTTP.get("example.com") end

Slide 80

Slide 80 text

t1 = Thread.new do DB.query("SELECT 1;") end

Slide 81

Slide 81 text

󰵏 You have a babysitter

Slide 82

Slide 82 text

󰗺 󰘁

Slide 83

Slide 83 text

󰥺 YOU PLAY A STAGE… …THEN YOU RUN AROUND THE BLOCK

Slide 84

Slide 84 text

Thread.new do puts "󰗺 I'm playing stage :{current_stage} now!" current_stage += 1 end

Slide 85

Slide 85 text

Thread.new do puts "󰗺 I'm playing stage :{current_stage} now!" current_stage += 1 sleep 1 end

Slide 86

Slide 86 text

boy Playing Running girl Time

Slide 87

Slide 87 text

boy girl Time Playing Running

Slide 88

Slide 88 text

current_stage = 1 5.times do [ Thread.new do ... end, Thread.new do ... end ].each(&:join) end

Slide 89

Slide 89 text

current_stage = 1 5.times do [ Thread.new do ... end, Thread.new do ... end ].each(&:join) end

Slide 90

Slide 90 text

5.018222999991849 seconds to run

Slide 91

Slide 91 text

1 second Executing I/O

Slide 92

Slide 92 text

girl Time 1 second Executing I/O 1 second . j o i n boy 1 second 1 second . j o i n ...

Slide 93

Slide 93 text

Threads are great!

Slide 94

Slide 94 text

Threads are great! Right…?

Slide 95

Slide 95 text

󰗺 I'm playing stage 1 now! 󰘁 I'm playing stage 1 now! 󰗺 I'm playing stage 3 now! 󰘁 I'm playing stage 3 now! 󰗺 I'm playing stage 5 now! 󰘁 I'm playing stage 5 now! 󰗺 I'm playing stage 7 now! 󰘁 I'm playing stage 7 now! 󰗺 I'm playing stage 9 now! 󰘁 I'm playing stage 9 now!

Slide 96

Slide 96 text

Thread.new do puts "󰗺 I'm playing stage :{current_stage} now!" current_stage += 1 sleep 1 end

Slide 97

Slide 97 text

Thread.new do puts "󰗺 I'm playing stage :{current_stage} now!" current_stage += 1 sleep 1 end

Slide 98

Slide 98 text

current_stage = 1 mutex = Thread::Mutex.new

Slide 99

Slide 99 text

Thread.new do mutex.synchronize do puts "󰘁 I'm playing stage :{current_stage} now!" current_stage += 1 end sleep 1 end

Slide 100

Slide 100 text

Thread.new do mutex.synchronize do puts "󰘁 I'm playing stage :{current_stage} now!" current_stage += 1 end sleep 1 end

Slide 101

Slide 101 text

Thread.new do mutex.synchronize do puts "󰘁 I'm playing stage :{current_stage} now!" current_stage += 1 end sleep 1 end

Slide 102

Slide 102 text

Thread.new do mutex.synchronize do puts "󰘁 I'm playing stage :{current_stage} now!" current_stage += 1 end sleep 1 end

Slide 103

Slide 103 text

󰗺 I'm playing stage 1 now! 󰘁 I'm playing stage 2 now! 󰗺 I'm playing stage 3 now! 󰘁 I'm playing stage 4 now! 󰗺 I'm playing stage 5 now! 󰘁 I'm playing stage 6 now! 󰗺 I'm playing stage 7 now! 󰘁 I'm playing stage 8 now! 󰗺 I'm playing stage 9 now! 󰘁 I'm playing stage 10 now! 5.014558000024408 seconds to run

Slide 104

Slide 104 text

Threads are hard

Slide 105

Slide 105 text

Shared mutable state is bad

Slide 106

Slide 106 text

🏠 Ractors

Slide 107

Slide 107 text

current_stage = 1 Ractor.new do puts "󰗺 I'm playing stage :{current_stage}" end.take

Slide 108

Slide 108 text

current_stage = 1 Ractor.new do puts "󰗺 I'm playing stage :{current_stage}" end.take # raises ArgumentError

Slide 109

Slide 109 text

STAGE = "Foo" Ractor.new do puts "󰗺 I'm playing stage :{STAGE}" end.take # raises Ractor.:IsolationError

Slide 110

Slide 110 text

STAGE = "Foo".freeze Ractor.new do puts "󰗺 I'm playing stage :{STAGE}" end.take # I'm playing stage Foo

Slide 111

Slide 111 text

current_stage = 1 Ractor.new(current_stage) do |stage| puts "󰗺 I'm playing stage :{stage}" end.take # I'm playing stage 1

Slide 112

Slide 112 text

current_stage = 1 Ractor.new(current_stage) do |stage| puts "󰗺 I'm playing stage :{stage}" end.take # I'm playing stage 1

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

󰗺 🎮 👾 󰘁 🎮 👾

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

🏭 Processes

Slide 118

Slide 118 text

stage = Stage.new kid_1 = Process.fork do 5.times do puts "󰗺 I'm playing a stage :{stage}" end end

Slide 119

Slide 119 text

stage = Stage.new kid_1 = Process.fork do 5.times do puts "󰗺 I'm playing a stage :{stage}" end end

Slide 120

Slide 120 text

󰗺 󰘁

Slide 121

Slide 121 text

🏠 🏠 󰗺 󰘁

Slide 122

Slide 122 text

🏠 󰥺󰸩 🏠 󰥺󰸩 󰗺 󰘁

Slide 123

Slide 123 text

🏠 󰥺󰸩 󰗺 🏠 󰥺󰸩 󰘁

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

No content

Slide 127

Slide 127 text

Concurrent Parallel

Slide 128

Slide 128 text

Concurrent Parallel Light Heavy Easy Hard

Slide 129

Slide 129 text

Concurrent Parallel Light Heavy Fibers 🪡 Easy Hard

Slide 130

Slide 130 text

Fibers Threads Ractors Processes Concurrent Parallel Light Heavy 🪡 🧵 Easy Hard

Slide 131

Slide 131 text

Concurrent Parallel Light Heavy Fibers Threads Ractors 🪡 🧵 🏠 Easy Hard

Slide 132

Slide 132 text

Concurrent Parallel Light Heavy Fibers Threads Ractors Processes 🪡 🧵 🏠 🏭 Easy Hard

Slide 133

Slide 133 text

Fiber::Scheduler

Slide 134

Slide 134 text

Async gem

Slide 135

Slide 135 text

Concurrent Parallel Light Heavy Fibers Threads Ractors Processes 🪡 🧵 🏠 🏭 Easy Hard

Slide 136

Slide 136 text

Concurrent Parallel Easy Hard 🪡 Fibers Threads Ractors Processes 🏠 🏭 🧵

Slide 137

Slide 137 text

No content

Slide 138

Slide 138 text

Code

Slide 139

Slide 139 text

Principles

Slide 140

Slide 140 text

Don't do tomorrow what you can do today

Slide 141

Slide 141 text

Don't do tomorrow what you can do today Don't do tomorrow what you can do today 🚫

Slide 142

Slide 142 text

Don't do now what you can do later

Slide 143

Slide 143 text

class RegistrationsController def create @registration = Registration.new(params) if @registration.save RegistrationMailer .welcome_email(@registration) .deliver_now redirect_to @registration end # ... end

Slide 144

Slide 144 text

class RegistrationsController def create @registration = Registration.new(params) if @registration.save RegistrationMailer .welcome_email(@registration) .deliver_now redirect_to @registration end # ... end

Slide 145

Slide 145 text

class RegistrationsController def create @registration = Registration.new(params) if @registration.save RegistrationMailer .welcome_email(@registration) .deliver_now redirect_to @registration end # ... end

Slide 146

Slide 146 text

class RegistrationsController def create @registration = Registration.new(params) if @registration.save RegistrationMailer .welcome_email(@registration) .deliver_later redirect_to @registration end # ... end

Slide 147

Slide 147 text

class RegistrationsController def create @registration = Registration.new(params) if @registration.save RegistrationMailer .welcome_email(@registration) .deliver_later redirect_to @registration end # ... end

Slide 148

Slide 148 text

class RegistrationsController def create @registration = Registration.new(params) if @registration.save CollectRegistrationStatsJob .perform_later(@registration) redirect_to @registration end # ... end

Slide 149

Slide 149 text

class RegistrationsController def create @registration = Registration.new(params) if @registration.save ProcessProfilePictureJob .perform_later(@registration) redirect_to @registration end # ... end

Slide 150

Slide 150 text

class User < ApplicationRecord has_one_attached :avatar def delete_avatar avatar.purge end end

Slide 151

Slide 151 text

class User < ApplicationRecord has_one_attached :avatar def delete_avatar avatar.purge end end

Slide 152

Slide 152 text

class User < ApplicationRecord has_one_attached :avatar def delete_avatar_later avatar.purge_later end end

Slide 153

Slide 153 text

class Team < ApplicationRecord has_many :players, dependent: :destroy end class Player < ApplicationRecord belongs_to :team end

Slide 154

Slide 154 text

class Team < ApplicationRecord has_many :players, dependent: :destroy end class Player < ApplicationRecord belongs_to :team end

Slide 155

Slide 155 text

Team.destroy_by(name: "Flamengo")

Slide 156

Slide 156 text

Team.destroy_by(name: "Flamengo") # DELETE FROM "players" WHERE "players"."id" = 1 # DELETE FROM "players" WHERE "players"."id" = 2 # ... # DELETE FROM "players" WHERE "players"."id" = 11 # DELETE FROM "teams" WHERE "teams"."id" = 1

Slide 157

Slide 157 text

class Team < ApplicationRecord has_many :players, dependent: :destroy end class Player < ApplicationRecord belongs_to :team end

Slide 158

Slide 158 text

class Team < ApplicationRecord has_many :players, dependent: :destroy_async end class Player < ApplicationRecord belongs_to :team end

Slide 159

Slide 159 text

Team.destroy_by(name: "Flamengo")

Slide 160

Slide 160 text

Team.destroy_by(name: "Flamengo") # Enqueued ActiveRecord::DestroyAssociationAsyncJob

Slide 161

Slide 161 text

Don't do now what you can do later

Slide 162

Slide 162 text

But…

Slide 163

Slide 163 text

Don't stand still

Slide 164

Slide 164 text

Time tasks Executing I/O

Slide 165

Slide 165 text

Time Executing I/O tasks

Slide 166

Slide 166 text

HTTP Requests

Slide 167

Slide 167 text

class TwitterNewsletter def initialize(tweets) @tweets = tweets end def to_s # ... end end

Slide 168

Slide 168 text

def to_s @tweets .map { |tweet| summary_for(tweet) } .join("\n") end

Slide 169

Slide 169 text

def summary_for(tweet) AI.ask("Write a summary for this tweet: :{tweet}") end

Slide 170

Slide 170 text

module AI def self.ask(question) HTTP.post(URL, question) end end

Slide 171

Slide 171 text

module AI def self.ask(question) sleep(1) end end

Slide 172

Slide 172 text

tweets = [ "I'm learning Ruby", "I'm learning Rails" ] time = Benchmark.measure do puts TwitterNewsletter.new(tweets) end puts ":{time.real} seconds to run" # 2.011646999977529 seconds to run

Slide 173

Slide 173 text

tweets = [ "I'm learning Ruby", "I'm learning Rails" ] time = Benchmark.measure do puts TwitterNewsletter.new(tweets) end puts ":{time.real} seconds to run" # 2.011646999977529 seconds to run

Slide 174

Slide 174 text

tweets = [ "I'm learning Ruby", "I'm learning Rails" ] time = Benchmark.measure do puts TwitterNewsletter.new(tweets) end puts ":{time.real} seconds to run" # 2.011646999977529 seconds to run

Slide 175

Slide 175 text

Time requests Executing I/O

Slide 176

Slide 176 text

class TwitterNewsletter def to_s @tweets .map { |tweet| summary_for(tweet) } .join("\n") end end

Slide 177

Slide 177 text

def to_s Sync do @tweets .map { |tweet| Async { summary_for(tweet) } } .map(&:wait) .join("\n") end end

Slide 178

Slide 178 text

def to_s Sync do @tweets .map { |tweet| Async { summary_for(tweet) } } .map(&:wait) .join("\n") end end

Slide 179

Slide 179 text

def to_s Sync do @tweets .map { |tweet| Async { summary_for(tweet) } } .map(&:wait) .join("\n") end end

Slide 180

Slide 180 text

def to_s Sync do @tweets .map { |tweet| Async { summary_for(tweet) } } .map(&:wait) .join("\n") end end

Slide 181

Slide 181 text

def to_s Sync do @tweets .map { |tweet| Async { summary_for(tweet) } } .map(&:wait) .join("\n") end end

Slide 182

Slide 182 text

tweets = [ "I'm learning Ruby", "I'm learning Rails" ] time = Benchmark.measure do puts TwitterNewsletter.new(tweets) end puts ":{time.real} seconds to run" # 1.011646999977529 seconds to run

Slide 183

Slide 183 text

tweets = [ "I'm learning Ruby", "I'm learning Rails" ] time = Benchmark.measure do puts TwitterNewsletter.new(tweets) end puts ":{time.real} seconds to run" # 1.011646999977529 seconds to run

Slide 184

Slide 184 text

Time Executing I/O requests

Slide 185

Slide 185 text

tweets = [ "I'm learning Ruby", "I'm learning Rails" ] time = Benchmark.measure do puts TwitterNewsletter.new(tweets) end puts ":{time.real} seconds to run" # 1.011646999977529 seconds to run

Slide 186

Slide 186 text

tweets = Array.new(1_000) do "Tweet :{_1}" end time = Benchmark.measure do puts TwitterNewsletter.new(tweets) end puts ":{time.real} seconds to run" # 1.011646999977529 seconds to run

Slide 187

Slide 187 text

tweets = Array.new(1_000) do "Tweet :{_1}" end time = Benchmark.measure do puts TwitterNewsletter.new(tweets) end puts ":{time.real} seconds to run" # 1.011646999977529 seconds to run

Slide 188

Slide 188 text

No content

Slide 189

Slide 189 text

DB Queries

Slide 190

Slide 190 text

class ReportsController def create @new_authors = Author.recent @new_books = Book.recent @new_reviews = BookReview.recent end end

Slide 191

Slide 191 text

class Book < ActiveRecord::Base belongs_to :author scope :recent, :> { where("select true from pg_sleep(1)").limit(1) } end

Slide 192

Slide 192 text

Author Load (1002.0ms) Book Load (1001.5ms) BookReview Load (1001.7ms)

Slide 193

Slide 193 text

3 seconds

Slide 194

Slide 194 text

ActiveRecord::Relation#load_async

Slide 195

Slide 195 text

Time Executing I/O queries

Slide 196

Slide 196 text

class ReportsController def create @new_authors = Author.recent @new_books = Book.recent @new_reviews = BookReview.recent end end

Slide 197

Slide 197 text

class ReportsController def create @new_authors = Author.recent.load_async @new_books = Book.recent.load_async @new_reviews = BookReview.recent.load_async end end

Slide 198

Slide 198 text

ASYNC Author Load (1010.2ms) (db time 1011.4ms) ASYNC Book Load (2.2ms) (db time 1013.8ms) ASYNC BookReview Load (0.2ms) (db time 1014.7ms)

Slide 199

Slide 199 text

ASYNC Author Load (1010.2ms) (db time 1011.4ms) ASYNC Book Load (2.2ms) (db time 1013.8ms) ASYNC BookReview Load (0.2ms) (db time 1014.7ms)

Slide 200

Slide 200 text

ASYNC Author Load (1010.2ms) (db time 1011.4ms) ASYNC Book Load (2.2ms) (db time 1013.8ms) ASYNC BookReview Load (0.2ms) (db time 1014.7ms)

Slide 201

Slide 201 text

ASYNC Author Load (1010.2ms) (db time 1011.4ms) ASYNC Book Load (2.2ms) (db time 1013.8ms) ASYNC BookReview Load (0.2ms) (db time 1014.7ms)

Slide 202

Slide 202 text

1 second

Slide 203

Slide 203 text

3x Faster

Slide 204

Slide 204 text

No content

Slide 205

Slide 205 text

No content

Slide 206

Slide 206 text

Thread Pool

Slide 207

Slide 207 text

default: &default adapter: postgresql pool: 5

Slide 208

Slide 208 text

🎮 🎮 🎮

Slide 209

Slide 209 text

󰗺 󰘁 󰪌 🎮 🎮 🎮

Slide 210

Slide 210 text

󰘁 🎮🎮🎮

Slide 211

Slide 211 text

󰘁 🎮🎮🎮 󰗺 󰪌 WE WANT TO PLAY TOO

Slide 212

Slide 212 text

󰘁 🎮🎮🎮 😡 🤬 WAIT LOL

Slide 213

Slide 213 text

Be careful with busy routes

Slide 214

Slide 214 text

class BooksController def show @new_books = Book.recent.load_async @external_books = HTTP.get( "https::/external.com/books" ) end end

Slide 215

Slide 215 text

Lazy Loading Lazy Loading

Slide 216

Slide 216 text

Async Views Async Views

Slide 217

Slide 217 text

No content

Slide 218

Slide 218 text

class DashboardController def show @best_sellers = Book.best_sellers @top_reviewed_books = Book.top_reviewed # ... end end

Slide 219

Slide 219 text

class DashboardController def show @best_sellers = Book.best_sellers # takes 10 seconds @top_reviewed_books = Book.top_reviewed # ... end end

Slide 220

Slide 220 text

class DashboardController def show @best_sellers = Book.best_sellers.load_async @top_reviewed_books = Book.top_reviewed.load_async # ... end end

Slide 221

Slide 221 text

Lazy-loaded Turbo Frames

Slide 222

Slide 222 text

Dashboard

Slide 223

Slide 223 text

Dashboard

Slide 224

Slide 224 text

Dashboard BestSellers

Slide 225

Slide 225 text

Dashboard BestSellers

Slide 226

Slide 226 text

Dashboard
BestSellers

Slide 227

Slide 227 text

Dashboard

Slide 228

Slide 228 text

:::- ... → :/div>
:::- ... ::>:/div> :/div>

Slide 229

Slide 229 text

:/turbo-frame>
:::- ... ::>:/div> :/div>

Slide 230

Slide 230 text

:/turbo-frame>
:::- ... ::>:/div> :/div>

Slide 231

Slide 231 text

:/turbo-frame>
:::- ... ::>:/div> :/div>

Slide 232

Slide 232 text

:/turbo-frame>
:::- ... ::>:/div> :/div>

Slide 233

Slide 233 text

class Books::BestSellersController def index @best_sellers = Book.best_sellers # takes 10 seconds end end

Slide 234

Slide 234 text

Best Sellers:/h1> <% @best_sellers.each do |book| %> <%# ... %> <% end %> :/turbo-frame>

Slide 235

Slide 235 text

Dashboard

Slide 236

Slide 236 text

Dashboard

Slide 237

Slide 237 text

Dashboard BestSellers

Slide 238

Slide 238 text

Dashboard BestSellers

Slide 239

Slide 239 text

Dashboard BestSellers

Slide 240

Slide 240 text

Dashboard
BestSellers

Slide 241

Slide 241 text

Dashboard

Slide 242

Slide 242 text

Slide 243

Slide 243 text

just some random text here. This is not import nore itant so i

Slide 244

Slide 244 text

just some random text here. This is not import nore itant so i just some random text here. This is not important so ignore it ⏳

Slide 245

Slide 245 text

Principles

Slide 246

Slide 246 text

Slide 247

Slide 247 text

Slide 248

Slide 248 text

Slide 249

Slide 249 text

@font-face { font-family: "My Font"; font-style: normal; font-weight: 400; font-display: swap; src: font-url("my-font.ttf"); }

Slide 250

Slide 250 text

@font-face { font-family: "My Font"; font-style: normal; font-weight: 400; font-display: swap; src: font-url("my-font.ttf"); }

Slide 251

Slide 251 text

Rafael França rules! Lorem ipsum, dolor sit amet consectetur adipisicing elit. Rem sint nisi, dolorem accusantium fugiat, eligendi temporibus voluptatum est.

Slide 252

Slide 252 text

Rafael França rules! Lorem ipsum, dolor sit amet consectetur adipisicing elit. Rem sint nisi, dolorem accusantium fugiat, eligendi temporibus voluptatum est.

Slide 253

Slide 253 text

Slide 254

Slide 254 text

No content

Slide 255

Slide 255 text

No content

Slide 256

Slide 256 text

Rails .application .config .action_view .image_loading = "lazy"

Slide 257

Slide 257 text

just some random text here. This is not import nore itant so i

Slide 258

Slide 258 text

No content

Slide 259

Slide 259 text

Async Dev Tools

Slide 260

Slide 260 text

No content

Slide 261

Slide 261 text

Adapted from xkcd

Slide 262

Slide 262 text

class AddIndexToUserRoles < ActiveRecord::Migration disable_ddl_transaction! def change add_index :users, :role, algorithm: :concurrently end end

Slide 263

Slide 263 text

class AddIndexToUserRoles < ActiveRecord::Migration disable_ddl_transaction! def change add_index :users, :role, algorithm: :concurrently end end

Slide 264

Slide 264 text

Parallel Tests

Slide 265

Slide 265 text

class ActiveSupport::TestCase parallelize(workers: 2) end

Slide 266

Slide 266 text

class ActiveSupport::TestCase parallelize( workers: :number_of_processors ) end

Slide 267

Slide 267 text

| Workers | Test Suite Time | |---------|-----------------| | 1 | 40 min | | 2 | 20 min | | 4 | 10 min |

Slide 268

Slide 268 text

No content

Slide 269

Slide 269 text

parallel_tests gem

Slide 270

Slide 270 text

All Roses

Slide 271

Slide 271 text

Pitfalls

Slide 272

Slide 272 text

With great power…

Slide 273

Slide 273 text

Async

Slide 274

Slide 274 text

No content

Slide 275

Slide 275 text

No content

Slide 276

Slide 276 text

No content

Slide 277

Slide 277 text

No content

Slide 278

Slide 278 text

No content

Slide 279

Slide 279 text

class IndexUsersOnEmail < ActiveRecord::Migration def change add_index :users, :email, unique: true end end

Slide 280

Slide 280 text

class BooksController < ApplicationController def index @books = Book.published.includes(:authors) end end

Slide 281

Slide 281 text

<% books.each do |book| %> <% cache(book) do %> <%= render book %> <% end %> <% end %>

Slide 282

Slide 282 text

class RolesController < ApplicationController def index Rails.cache.fetch("roles", expires_in: 1.hour) do Auth0::Roles.fetch_all end end end

Slide 283

Slide 283 text

user_roles = users.map do |user| user.roles end.flatten

Slide 284

Slide 284 text

user_roles = users.flat_map do |user| user.roles end

Slide 285

Slide 285 text

rubocop-performance

Slide 286

Slide 286 text

def fibonacci(n) if n < 2 n else fibonacci(n - 1) + fibonacci(n - 2) end end

Slide 287

Slide 287 text

No content

Slide 288

Slide 288 text

No content

Slide 289

Slide 289 text

No content

Slide 290

Slide 290 text

@MatheusRich

Slide 291

Slide 291 text

Questions.ask_now @MatheusRich | www.matheusrich.com | thoughtbot

Slide 292

Slide 292 text

Questions.ask_later @MatheusRich | www.matheusrich.com | thoughtbot