-
Notifications
You must be signed in to change notification settings - Fork 27
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
Introduce ForbidComparableTEnum
cop
#225
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Introduce a mixin containing logic that will be shared between all cops that operate on `T::Enum`s.
Morriar
reviewed
Apr 19, 2024
egiurleo
force-pushed
the
emily/t-enum-comparable-cop
branch
2 times, most recently
from
April 19, 2024 17:12
435d3f2
to
c63e10b
Compare
egiurleo
commented
Apr 19, 2024
|
||
def on_send(node) | ||
return unless in_t_enum_class? | ||
return unless include_comparable?(node) || prepend_comparable?(node) |
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.
@Morriar Here's the prepend check
egiurleo
commented
Apr 19, 2024
Morriar
reviewed
Apr 19, 2024
This cop adds an offense whenever `Comparable` is included in a `T::Enum` class because it isn't performant (default implementation is about 8x as slow as comparing between constants).
egiurleo
force-pushed
the
emily/t-enum-comparable-cop
branch
from
April 19, 2024 20:10
c63e10b
to
85fcce0
Compare
Morriar
approved these changes
Apr 19, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related to #219. Best read commit-by-commit.
Thanks @sambostock for working on this with me!
Comparing
T::Enum
values is very slow. This cop adds an offense on anyT::Enum
class that includes theComparable
module and warns about performance impact.Performance evaluation
To assess the performance of
T::Enum#<=>
, I performed a benchmark comparing a few different implementations of comparableT::Enum
s:T::Enum
with no methods redefinedT::Enum
with arank
method that returns an integer and a reimplemented<=>
method that compares ranksT::Enum
with the<=>
method reimplemented to compare serialized valuesHere are the benchmark results:
Even the most performant implementation of comparable
T::Enum
(with the redefined<=>
method) is 3-4x slower than just comparing constants. If a developer didn't know this, it would be really easy for them to add significant performance overhead to their application.Click here for the full benchmark code comparing various implementations of comparable T::Enum
Why is the default implementation so slow?
To figure out why
T::Enum#<=>
is so slow, I usedruby-prof
to profile two of the implementations above -- the default Sorbet implementation and the implementation that redefines#<=>
. (I calledT::Utils.run_all_sig_blocks
before generating the profiles to cut down on sig-related noise in the profile.)Default implementation of
T::Enum#<=>
Implementation with redefined
<=>
methodAs you can see in the call tree generated by ruby-prof, the default Sorbet implementation spends a significant amount of time in
UnboundMethod#bind_call
, which is Sorbet wrapping the typed spaceship operator method. This adds significant performance overhead.Even without Sorbet's wrapping, the need to call
serialize
on theT::Enum
values when comparing them adds overhead compared to, say, comparing Ruby constants.Click here to see the code that generates the profiles above