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

Micro-optimization to AmazingPrint::ActiveRecord#cast #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jdelStrother
Copy link

I kept seeing Module#ancestors show up in some profiling involving SemanticLogger.

Changing to x.is_a?(ActiveRecord::Relation) and y <= ActiveRecord::Base should be equivalent, but makes AmazingPrint::Inspector#awesome 7% faster:

require "rails"
require "active_record"
require "amazing_print"
require "benchmark/ips"

inspector = AmazingPrint::Inspector.new
log = { foo: { bar: ["baz"] } }
Benchmark.ips do |x|
  x.report("ancestors.include?") do |times|
    i = 0
    $ap_ancestors = true
    while i < times
      inspector.awesome(log)
      i += 1
    end
  end
  x.report("class <= ") do |times|
    i = 0
    $ap_ancestors = false
    while i < times
      inspector.awesome(log)
      i += 1
    end
  end

  x.compare!
end
ruby 3.2.4 (2024-04-23 revision af471c0e01) [arm64-darwin23]
Warming up --------------------------------------
  ancestors.include?     4.081k i/100ms
           class <=      4.289k i/100ms
Calculating -------------------------------------
  ancestors.include?     40.419k (± 1.5%) i/s -    204.050k in   5.049537s
           class <=      43.345k (± 1.5%) i/s -    218.739k in   5.047616s

Comparison:
           class <= :    43344.7 i/s
  ancestors.include?:    40418.6 i/s - 1.07x  slower

I might be missing something though - any idea why ancestors was used in the first place?

I kept seeing Module#ancestors show up in some profiling involving SemanticLogger.

Changing to `foo.is_a?(ActiveRecord::Relation)` and `foo <= ActiveRecord::Base` should be equivalent, but makes `AmazingPrint::Inspector#awesome` 7% faster:

```ruby
require "rails"
require "active_record"
require "amazing_print"
require "benchmark/ips"

inspector = AmazingPrint::Inspector.new
log = { foo: { bar: ["baz"] } }
Benchmark.ips do |x|
  x.report("ancestors.include?") do |times|
    i = 0
    $ap_ancestors = true
    while i < times
      inspector.awesome(log)
      i += 1
    end
  end
  x.report("class <= ") do |times|
    i = 0
    $ap_ancestors = false
    while i < times
      inspector.awesome(log)
      i += 1
    end
  end

  x.compare!
end
```

```
ruby 3.2.4 (2024-04-23 revision af471c0e01) [arm64-darwin23]
Warming up --------------------------------------
  ancestors.include?     4.081k i/100ms
           class <=      4.289k i/100ms
Calculating -------------------------------------
  ancestors.include?     40.419k (± 1.5%) i/s -    204.050k in   5.049537s
           class <=      43.345k (± 1.5%) i/s -    218.739k in   5.047616s

Comparison:
           class <= :    43344.7 i/s
  ancestors.include?:    40418.6 i/s - 1.07x  slower
```
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.

1 participant