-
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 legacy_connection_handling
a module instance variable
#42442
Make legacy_connection_handling
a module instance variable
#42442
Conversation
b5e019c
to
ca3d38b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine since the documented public API is called via the Railtie. Technically an application could be using the getter to check the value but that seems rare and unlikely to me. 👍🏼
Yeah I made sure it wasn't documented. For the followups, if some are documented we can add a delegator with a deprecation warning. Ok, I'll try to get a green CI and start working on the other attributes. |
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.
ca3d38b
to
8459c1c
Compare
Ref: #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 variablecould work just fine, and would be much faster.
For Active Record this means most of the
cattr_accessor
onActiveRecord::Base
could actually be
attr_accessor
onActiveRecord
.I started with
legacy_connection_handling
as a proof of concept.Performance
I've ran quick benchmark
Each case is benched twice, once in a class without ancestors, once in a class with 62 ancestors (like ActiveRecord::Base).
In short it's 5 to 6 times faster than
cattr_accessor
, and the added benefit is that it doesn't define an instance accessor by default, which in most case is not needed, so it avoid leaking useless methods.Followup
I just converted a single attribute, but if other agree that this is the way to go, I'd like to mass migrate similar
cattr
in Active Record, and possibly in other gems as well for consistency.cc @eileencodes @jhawthorn @tenderlove @rafaelfranca