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

Merged

Conversation

jdelStrother
Copy link
Contributor

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?

jdelStrother and others added 2 commits August 23, 2024 10:32
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
```
Copy link
Member

@HarlemSquirrel HarlemSquirrel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done! Thank you!

@HarlemSquirrel HarlemSquirrel added this pull request to the merge queue Dec 19, 2024
Merged via the queue into amazing-print:master with commit b37cfd5 Dec 19, 2024
7 checks passed
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.

2 participants