Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace runtime defined? calls with const_defined? #222

Closed
wants to merge 1 commit into from
Closed

Replace runtime defined? calls with const_defined? #222

wants to merge 1 commit into from

Conversation

etehtsea
Copy link

@etehtsea etehtsea commented Oct 23, 2015

Using defined? in runtime is affecting app performance.
ActiveSupport::Dependencies which automatically hooks into const_missing
makes situation even worse.

# jruby-9.0.3.0
require 'benchmark/ips'
# Uncomment for the second bench
# require 'active_support/dependencies'

Benchmark.ips do |x|
  x.config(warmup: 30, time: 20)
  x.report("const_defined?") do
    Object.const_defined?(:ActionDispatch) &&
      ::ActionDispatch.const_defined?(:Http) &&
      ::ActionDispatch::Http.const_defined?(:ParameterFilter)
  end
  x.report("defined?") { defined?(ActionDispatch::Http::ParameterFilter) }
  x.compare!
end
Comparison:
      const_defined?:  5621473.2 i/s
            defined?:    13207.5 i/s - 425.63x slower

Comparison (with ActiveSupport::Dependencies):
    const_defined?:  6011560.2 i/s
            defined?:     4278.5 i/s - 1405.06x slower

Real app flamegraph (https://github.com/SamSaffron/flamegraph) profiling results:

… activesupport-4.2.4/lib/active_support/dependencies.rb:184:in `const_missing'   (40 samples - 51.28%)
… newrelic_rpm-3.14.0.305/lib/new_relic/agent/parameter_filtering.rb:17:in `filter_using_rails'   (12 samples - 15.38%)
… activesupport-4.2.4/lib/active_support/dependencies.rb:184:in `const_missing'   (40 samples - 51.28%)
… newrelic_rpm-3.14.0.305/lib/new_relic/agent/instrumentation/middleware_proxy.rb:37:in `is_sinatra_app?' (27 samples - 34.62%)

Using `defined?` in runtime is affecting app performance.
ActiveSupport::Dependencies which automatically hooks into const_missing
makes situation even worse.

require 'benchmark/ips'
# Uncomment for the second bench
# require 'active_support/dependencies'
SINATRA_BASE = '::Sinatra::Base'.freeze

Benchmark.ips do |x|
  x.config(warmup: 30, time: 20)
  x.report("const_defined?") { Object.const_defined?(SINATRA_BASE) }
  x.report("defined?") { defined?(::Sinatra::Base) }
  x.compare!
end

Comparison:
      const_defined?:  3569050.7 i/s
      defined?:        12576.2 i/s - 283.79x slower

Comparison (with ActiveSupport::Dependencies):
      const_defined?:  3400806.8 i/s
      defined?:        4106.9 i/s - 828.07x slower

Real app flamegraph profiling results:

… activesupport-4.2.4/lib/active_support/dependencies.rb:184:in `const_missing'	(40 samples - 51.28%)
… newrelic_rpm-3.14.0.305/lib/new_relic/agent/parameter_filtering.rb:17:in `filter_using_rails'	(12 samples - 15.38%)

… activesupport-4.2.4/lib/active_support/dependencies.rb:184:in `const_missing'	(40 samples - 51.28%)
… newrelic_rpm-3.14.0.305/lib/new_relic/agent/instrumentation/middleware_proxy.rb:37:in `is_sinatra_app?'	(27 samples - 34.62%)
@t3hk0d3
Copy link

t3hk0d3 commented Oct 25, 2015

👍

@kwugirl
Copy link
Contributor

kwugirl commented Feb 8, 2016

Hi @etehtsea @t3hk0d3 -- thanks for submitting those benchmarks, and apologies of the delay in a response! I tried it out and it looks like there is a big difference when run on jruby but not on MRI--has that been your experience as well?

Are these the only two changes that you would need, or were they just a demonstration of the kinds of changes to be made? The agent uses defined? quite a bit throughout the codebase, and as your code shows, using const_defined? requires stepping through the different levels of namespaces, which comes with its own cost.

@kwugirl
Copy link
Contributor

kwugirl commented Mar 25, 2016

Hi @etehtsea @t3hk0d3 -- wanted to check in again and see if this was still a concern for you? See my comments above.

@etehtsea etehtsea closed this Apr 19, 2016
@etehtsea
Copy link
Author

I think this issue should be addressed upstream and/or fixed somehow globally (for the whole rpm codebase).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants