-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Make ActiveRecord::Base.logger a class_attribute #42237
Conversation
👍 Probably deserves a mention in the changelog. I suspect most will just be using |
c4eea8b
to
88bfa86
Compare
Good point, I always forget -_-. Done. |
Seems fine to me but I'd need @tenderlove to clarify this from the bug discussion:
|
This quote is in opposition to "class instance variables": class AR::Base
class << self
attr_accessor :logger
end
end Because indeed we expect |
`cattr_accessor` rely on class variables which has terrible performance on long ancestor chains. See https://bugs.ruby-lang.org/issues/17763 for a detailed description of the problem. In comparison `class_attribute` on `ActiveRecord::Base` is almost 7x faster: ``` Calculating ------------------------------------- logger 1.700M (± 0.9%) i/s - 8.667M in 5.097595s clogger 11.556M (± 0.9%) i/s - 58.806M in 5.089282s Comparison: clogger: 11555754.2 i/s logger: 1700280.4 i/s - 6.80x (± 0.00) slower ``` This is because `ActiveRecord::Base.ancestors.size == 62`.
88bfa86
to
dcc2530
Compare
Nice! I thought that change made sense too when I wrote about it in https://bugs.ruby-lang.org/issues/17763#note-7 |
Ref: rails#42237 Ref: https://bugs.ruby-lang.org/issues/17763 Ruby class variables are slow, and get slower the more ancestors the class has. And it doesn't seem likely to get faster any time soon. Overall I'm under the impression that ruby-core consider them more or less deprecated. But in many cases where `cattr_accessor` is used, a class instance variable could work just fine, and would be much faster.
Ref: rails#42237 Ref: https://bugs.ruby-lang.org/issues/17763 Ruby class variables are slow, and get slower the more ancestors the class has. And it doesn't seem likely to get faster any time soon. Overall I'm under the impression that ruby-core consider them more or less deprecated. But in many cases where `cattr_accessor` is used, a class instance variable could work just fine, and would be much faster. For Active Record this means most of the `cattr_accessor` on `ActiveRecord::Base` could actually be `attr_accessor` on `ActiveRecord`. I started with `legacy_connection_handling` as a proof of concept.
cattr_accessor
rely on class variables which has terribleperformance on long ancestor chains. See https://bugs.ruby-lang.org/issues/17763
for a detailed description of the problem.
In comparison
class_attribute
onActiveRecord::Base
is almost 7xfaster:
This is because
ActiveRecord::Base.ancestors.size == 62
.Other
cattr
usages onActiveRecord::Base
should be considered too, butlogger
is by far the biggest hostspot, so probably the best one to start the discussion.@eileencodes @jhawthorn @tenderlove @rafaelfranca