-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Case when splat #2143
Case when splat #2143
Conversation
bb9107d
to
98b992c
Compare
# case foo | ||
# when 3 | ||
# baz | ||
# when *[1, 2, 3, 4] |
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.
It's strange to use an example where rearranging the when
s has changed the semantics. So remove 3
from the array.
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.
Good catch. I didn't mean to have overlap in the example.
If the splat operator is being applied to an array literal you could also just change when *[1, 2, 3] to when 1, 2, 3 Perhaps that's a better way to auto-correct in that particular case, provided that it gives the same improvement regarding memory allocation. |
Good idea about auto-correcting splat on array literal to be a list without the array. I will see what this does to memory usage. Assuming that it does not have the same issue that using splat does, I will make that correction. |
98b992c
to
503cbf6
Compare
I was able to test
|
8a4dbe0
to
ad9171f
Compare
I have updated the cop to maintain relative condition order and correct splat array literal to remove the splat and square brackets. There is potential for increased performance by rearranging the splat when conditions, but it is impossible to RuboCop to automatically handle those conditions. |
f5a0a9a
to
a97c154
Compare
The code has been modified and rebased. Let me know if I need to tweak anything else. |
👍 LGTM |
a97c154
to
1585ae1
Compare
# the expansion. | ||
# | ||
# This is not a guaranteed performance improvement. If the data being | ||
# processed by the `case` condition is normalized in a manor that favors |
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.
manor -> manner
1585ae1
to
fcdcf6f
Compare
updated |
This cop was implemented[1] to optimise memory allocations in case statements with splatted conditions. In profiling over some of the most case-heavy parts of the rubocop codebase it showed a 2% improvement in allocations. However, as the original PR notes, the suggested change: * is only safe to make if the case conditions are disjoint * doesn't confer performance benefits in all conditions This commit therefore removes the cop and marks it as obsolete. [1] rubocop#2143
This is part 1 of memory performance improvements.
There is a slight risk in the auto-correct. If more than one
when
condition could apply to the same input, and the earlier condition is used as a short circuiting, then auto-correct could change the code to do the wrong thing. I do not think this is a large concern. If somebody thinks otherwise, I will gladly disable auto-correct by default. I also wasn't sure if I should document this risk inside of the description, class, or not at all.Example
While running memory_profiler on RuboCop. The sample for the data came from running RuboCop against
lib/rubocop/cop/performance
,lib/rubocop/cop/rails
, andlib/rubocop/cop/lint
. I could not sample running against the entire project because of the time and resources needed to analyze that much memory.Total memory consumption dropped by about 2%
The memory allocation for
variable_force.rb
dropped by 30%.There was little impact on
locatable.rb
, andsafe_assignment.rb
didn't fall into the top 50 most memory consuming files.