Repository navigation
Optimize AS::LogSubscriber - #45796
Conversation
6766b38 to
7084e62
Compare
|
This seems to hurt the benchmark from #44469 a little bit... still faster than before that PR, but measurable. Maybe due to the extra filtering? Before: After: |
|
One option might be creating a new group_klass for these |
d247bc7 to
832feb7
Compare
|
Thanks for pointing out you benchmark. I managed to get back to only ~5% slower. Not sure we can do much better as ultimately not being able to cache the whole thing necessarily is a bit more work. However my assumption here is that some frequent events are only subscribed by various resultsbenchmark```ruby # frozen_string_literal: truerequire 'fileutils' BRANCH = class EventedSubscriber class SilencedSubscriber < EventedSubscriber 10.times do ActiveSupport::Notifications.monotonic_subscribe("timed_monotonic") do |name, started, ended, id, payload| ActiveSupport::Notifications.subscribe("event_object") do |event| ActiveSupport::Notifications.subscribe("evented", EventedSubscriber.new) ActiveSupport::Notifications.subscribe("silenced", SilencedSubscriber.new) def bench(name, &block) bench("timed") do bench("timed_monotonic") do bench("event_object") do bench("evented") do bench("unsubscribed") do bench("silenced") do |
832feb7 to
286b39c
Compare
jhawthorn
left a comment
There was a problem hiding this comment.
I think within 5% is great. Thank you for optimizing further ❤️
|
Thanks for the review John! One thing I realized I didn't handle is methods defined after |
The various LogSubscriber subclasses tend to subscribe to events but then end up doing nothing if the log level is high enough. But even if we end up not logging, we have to go through the entire notification path, record timing etc. By allowing subscribers to dynamically bail out early, we can save a lot of work if all subscribers are silenced.
286b39c to
bd19d1b
Compare
In rails#45796 I overlooked that `ActiveRecord::LogSubscriber#sql` wasn't only logging the SQL query but was also responsible for collecting the `:db_runtime` metric. Ultimately I think it is cleaner to move this concern to `RuntimeRegistry`.
Since, rails/rails#45796, `ActiveSupport::Notifications.notifier.listeners_for` doesn't return the all `listeners`. Need to use `all_listeners_for` to get all `listeners`.
* Disable `Gemspec/DependencyVersion` cop Currently, CI is broken because of `Gemspec/DevelopmentDependencies` cop. https://github.com/roidrage/lograge/actions/runs/5100966316/jobs/9169550600#step:4:36 Rubocop recommends to write development dependencies in Gemfile. But this gem has multiple Gemfiles and it might be redundant. So I disable it to pass Rubocop. * Bump `truffleruby` to the latest Because truffleruby-21.3 for Ubuntu 22.04 doesn't exist. https://github.com/ruby/ruby-builder/releases/tag/toolcache * Explicitly require a using library * Add more mock methods To avoid `NameError`. * Support Rails 7.1 Since, rails/rails#45796, `ActiveSupport::Notifications.notifier.listeners_for` doesn't return the all `listeners`. Need to use `all_listeners_for` to get all `listeners`. * Bump JRuby version JRuby 9.2.x is compatible with Ruby 2.5.x and we don't run tests against CRuby 2.5.x now. So I think we can remove it.
The various
LogSubscribersubclasses tend to subscribe to events but then end up doing nothing if the log level is high enough.But even if we end up not logging, we have to go through the entire notification path, record timing etc.
By allowing subscribers to dynamically bail out early, we can save a lot of work if all subscribers are silenced.