From 88249932a006fbe7b244d27844538d20f35a02c7 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Thu, 9 May 2019 17:44:06 +1000 Subject: [PATCH] PERF: remove SQL notifiers for every SQL execution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Benchmarking: ``` Benchmark.ips do |b| b.report("simple") do User.first end end ActiveSupport::Notifications.notifier.listeners_for("sql.active_record").clear Benchmark.ips do |b| b.report("simple") do User.first end end ``` ``` sam@arch discourse % RAILS_ENV=production ruby script/micro_bench.rb Before Calculating ------------------------------------- simple 3.289k (± 4.4%) i/s - 16.575k in 5.049771s After Calculating ------------------------------------- simple 3.491k (± 3.6%) i/s - 17.442k in 5.002226s ```` --- config/initializers/300-perf.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 config/initializers/300-perf.rb diff --git a/config/initializers/300-perf.rb b/config/initializers/300-perf.rb new file mode 100644 index 00000000000..3186698a8c5 --- /dev/null +++ b/config/initializers/300-perf.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +if Rails.env == "production" + # This event happens quite a lot and fans out to ExplainSubscriber + # and Logger, this cuts out 2 method calls that every time we run SQL + # + # In production we do not care about Explain or Logging SQL statements + # at this level + # + # Micro bench shows for `User.first` this takes us from 3.3k/s to 3.5k/s + ActiveSupport::Notifications.notifier.unsubscribe("sql.active_record") +end