Boston Ruby Meetup presentation by Joe Ferris, CTO of thoughtbot, and Simeon Simeonov, CTO of Swoop, on ways to optimize the memory footprint of data intensive Ruby on Rails applications.
8. Display Advertising
Makes the Web Suck
User-focused optimization
Tens of millions of users
1000+% better than average
200+% better than Google
Swoop Fixes That
30. Memory Churn
•Allocating and deallocating tons of
objects slows down processing
•Mutation limits allocations, but
makes it easier to introduce bugs
31. 1 hashes.each do |hash|
2 hash['domain'].downcase!
3 hash['domain'].strip!
4 end
Spot the Bug!
32. # In shared state:
@domains[id] ||= Domain.name_for(id) || ''
!
# Much later:
hash['domain'].downcase!
hash['domain'].strip!
33. Good News!
•Allocating and freeing objects is
fairly fast in Ruby
•Keeping your stack frame light
will limit the effects of memory
churn
41. def duplicate(number, count)
if count > 0
[number] + repeat(number, count - 1)
else
[]
end
end
!
def sum(list)
list.inject(0) do |result, number|
result + number
end
end
43. duplicate :: Int -> Int -> [Int]
duplicate number count
| count <= 0 =
[]
| otherwise =
number:duplicate number (count - 1)
!
sum :: [Int] -> Int
sum [x] = x
sum (x:remaining) = x + sum remaining