-
-
Notifications
You must be signed in to change notification settings - Fork 225
Introduce RuboCop Performance #316
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
Merged
crmne
merged 6 commits into
crmne:main
from
tagliala:feature/introduce-rubocop-performance
Aug 13, 2025
Merged
Introduce RuboCop Performance #316
crmne
merged 6 commits into
crmne:main
from
tagliala:feature/introduce-rubocop-performance
Aug 13, 2025
Conversation
This file contains hidden or 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
tagliala
commented
Aug 1, 2025
fb0c645
to
f9c9af8
Compare
Disable `CollectionLiteralInLoop` cop in specs Automatically fix safe offenses: - Performance/BindCall - Performance/DoubleStartEndWith - Performance/StringReplacement ``` rubocop --only Performance/BindCall,Performance/DoubleStartEndWith,Performance/StringReplacement -a ```
`String#include?` is considerably more efficient than `String#match?`. However, for uniformity and future-proofing with minimal modifications, it is preferable to permit that comparison in capabilities detection.
Implementing a comprehensive fix for this issue may be regarded as a disruptive change due to its impact on the API. Specifically, a partial solution will introduce `Lint/UnusedMethodArgument` violations, which can be disregarded. However, benchmarking indicates that removing the `&block` argument from the method signature will result a performance improvement: ``` ruby 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [arm64-darwin24] Comparison (IPS): yield (no args): 10747202.2 i/s yield (&block arg): 9308969.9 i/s - 1.15x (± 0.00) slower block.call: 8607805.1 i/s - 1.25x (± 0.00) slower ``` Ref: - https://github.com/fastruby/fast-ruby?tab=readme-ov-file#proccall-and-block-arguments-vs-yield-code
``` rubocop --only Performance/MapCompact -A ``` These changes are safe because both `providers` and `capabilities` are a `Hash`, so they both include `Enumerable` and respond to `filter_map` Ref: - https://ruby-doc.org/core-3.1.0/Enumerable.html#method-i-filter_map
While `String#dup` is as fast as `+''` in Ruby >= 3.3, this is not the case for `String.new`, so this cop has been applied only in production code. RuboCop considers this change unsafe because `+''` returns an UTF-8 string, while `String.new` returns an ASCII string. In this context, this should not be a breaking change. ``` rubocop --only Performance/UnfreezeString -A ``` A manual fix has been applied in ActsAs concern to use empty string literal for ActiveRecord `content` assignment instead of `String.new` or `+''`. Passing `''` directly to ActiveRecord is sufficient since the string isn't mutated. Bench: ``` ruby 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [arm64-darwin24] Comparison (IPS): +'': 15938643.5 i/s String.new: 11016752.0 i/s - 1.45x (± 0.00) slower ``` Ref: - https://docs.rubocop.org/rubocop-performance/cops_performance.html#performanceunfreezestring - https://github.com/fastruby/fast-ruby?tab=readme-ov-file#stringdup-vs-string-code
There is no auto-correction for this offense, so it has been fixed manually Ref: https://github.com/fastruby/fast-ruby#normal-way-to-apply-method-vs-method-code
f9c9af8
to
4a33aa5
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #316 +/- ##
=======================================
Coverage 87.75% 87.75%
=======================================
Files 88 88
Lines 3480 3480
Branches 724 724
=======================================
Hits 3054 3054
Misses 426 426 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
crmne
approved these changes
Aug 13, 2025
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.
LGTM!
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.
What this does
Hello, thanks for your work on this library
This is my first contribution here. I think that adding RuboCop Performance plugin this can benefit on the long term
Each offense has been fixed in an individual commit, with explanation and benchmarks, where possible.
Some cops have been temporarily disabled because they can break API
Type of change
Scope check
Quality check
overcommit --install
and all hooks passI updated documentation if neededmodels.json
,aliases.json
)API changes
Related issues