Lohith R Gowda - RVU - Performance cop
Description
This PR introduces multiple enhancements to Cookstyle focused on improving CPU and memory efficiency, as well as enhancing code maintainability through shared abstractions.
🚀 New Performance Cops Added
-
RepeatedResourcesInLoopsDetects creation of batchable Chef resources (e.g.,package,user,service) inside Ruby loops (e.g.,.each), encouraging use of array resource declarations instead. This reduces unnecessary resource duplication and improves Chef client execution speed. -
LoadAllDataBagItemsFlags overly broad or emptysearchanddata_bag_searchqueries (e.g.,"*:*"or"") that can result in loading all data bag items into memory, causing high memory usage and increased IO. Promotes more selective querying to improve performance.
🛠 Refactoring & Code Reuse
-
Introduced a shared helper module:
lib/rubocop/cop/chef/helpers/resource_matcher.rb, providing reusable AST detection methods:search_call?batchable_resource?includes_guard?node_is_chef_resource?
-
Applied this helper in:
- Newly created cops:
RepeatedResourcesInLoops,LoadAllDataBagItems - Existing cop:
ExecuteAptUpdate
- Newly created cops:
This makes the cops cleaner, DRY, and more maintainable.
📚 Documentation
-
Added a performance cops documentation file under
docs-chef-cops/performance.mdwith:- Explanation of the performance department's goal
- Details and examples for the newly introduced cops
-
Updated
README.mdand related documentation to reference the new cops and helper usage.
✅ Testing
- Added RSpec tests for the two new performance cops.
- Added unit tests for the
ResourceMatcherhelper methods. - Confirmed all existing and new tests pass successfully.
Related Issue
No specific GitHub issue linked. This PR adds new functionality and internal improvements targeting Chef client performance optimization in community cookbooks.
Types of Changes
- [x] ✨ New feature (non-breaking change which adds functionality)
- [x] 🔧 Chore (non-breaking change that refactors or improves internal code/documentation)
Checklist
- [x] I have read the CONTRIBUTING document
- [x] I have run all pre-merge tests locally using
bundle exec rspec - [x] I have updated documentation as required (
performance.md,helpers.md) - [x] I have added/updated tests to cover my changes
- [x] I have signed all commits for [the Developer Certificate of Origin]
- [x] I have verified no unintended
Gemfile.lockchanges exist - [x] I have confirmed that all RuboCop cops are registered properly, and the
.rubocop.ymlis valid
Quality Gate passed
Issues
0 New issues
0 Accepted issues
Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code
@LohithR22 Cool concept to get performance cops added. I'll try to get a look at this in detail this week.
@LohithR22 Hi, can you please correct the linting errors and we can merge it
Quality Gate passed
Issues
1 New issue
0 Accepted issues
Measures
0 Security Hotspots
0.0% Coverage on New Code
1.2% Duplication on New Code
I'm going to see if I (or someone on PR review) can break out autocorrects in front of this so that this PR ends up being less messy to review.
It looks like a full lint pass was run on this based on some other lint configuration that is not in our repo causing changes to hundreds of files, causing this to be entirely un-reviewable. Did you use an LLM - they sometimes do that? Or could be a weird config in your IDE?
Either way - bundle exec cookstyle -a is the auto-correcting linter command for this repo, and that's the only one that should be used.
Can you please remake this PR (or update it) to have your new cop code, and not touch any of the other files.
Thanks!