From c89b42c488a0f09f64dc205726ea3448295fd0e0 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 15 Feb 2018 13:11:22 +1100 Subject: [PATCH] PERF: only require the rss library if used Before: Total allocated: 257909321 bytes (2514134 objects) Total retained: 39681579 bytes (343387 objects) allocated memory by gem ----------------------------------- 42875979 rss retained memory by gem ----------------------------------- 2080188 rss retained objects by gem ----------------------------------- 13052 rss After: Total allocated: 210562047 bytes (2252030 objects) Total retained: 37433816 bytes (328635 objects) ---- So, 2 less megabytes on boot and 13000 objects stuck in ruby heaps forever. --- Gemfile.lock | 2 +- app/jobs/scheduled/poll_feed.rb | 14 +++++++++++--- script/boot_mem.rb | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 script/boot_mem.rb diff --git a/Gemfile.lock b/Gemfile.lock index 332ac6f5af2..7029f5d7cee 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -165,7 +165,7 @@ GEM lru_redux (1.1.0) mail (2.6.6) mime-types (>= 1.16, < 4) - memory_profiler (0.9.8) + memory_profiler (0.9.10) message_bus (2.1.2) rack (>= 1.1.3) metaclass (0.0.4) diff --git a/app/jobs/scheduled/poll_feed.rb b/app/jobs/scheduled/poll_feed.rb index 2f885c69a28..8c5b6c45312 100644 --- a/app/jobs/scheduled/poll_feed.rb +++ b/app/jobs/scheduled/poll_feed.rb @@ -3,9 +3,6 @@ # require 'digest/sha1' require 'excon' -require 'rss' -require_dependency 'feed_item_accessor' -require_dependency 'feed_element_installer' require_dependency 'final_destination' require_dependency 'post_creator' require_dependency 'post_revisor' @@ -27,12 +24,23 @@ module Jobs end def poll_feed + ensure_rss_loaded + # defer loading rss feed = Feed.new import_topics(feed.topics) end private + # rss lib is very expensive memory wise, no need to load it till it is needed + def ensure_rss_loaded + return if @@rss_loaded + require 'rss' + require_dependency 'feed_item_accessor' + require_dependency 'feed_element_installer' + @@rss_loaded + end + def not_polled_recently? $redis.set( 'feed-polled-recently', diff --git a/script/boot_mem.rb b/script/boot_mem.rb new file mode 100644 index 00000000000..61da1fa6c6b --- /dev/null +++ b/script/boot_mem.rb @@ -0,0 +1,21 @@ +# simple script to measure memory at boot + +if ENV['RAILS_ENV'] != "production" + exec "RAILS_ENV=production ruby #{__FILE__}" +end + +require 'memory_profiler' + +MemoryProfiler.report do + require File.expand_path("../../config/environment", __FILE__) + + Rails.application.routes.recognize_path('abc') rescue nil + + # load up the yaml for the localization bits, in master process + I18n.t(:posts) + + # load up all models and schema + (ActiveRecord::Base.connection.tables - %w[schema_migrations versions]).each do |table| + table.classify.constantize.first rescue nil + end +end.pretty_print