Skip to content

Add performance cops and resource matching helper#1046

Open
LohithR22 wants to merge 1 commit intochef:mainfrom
LohithR22:lohith/performance-cop
Open

Add performance cops and resource matching helper#1046
LohithR22 wants to merge 1 commit intochef:mainfrom
LohithR22:lohith/performance-cop

Conversation

@LohithR22
Copy link
Copy Markdown

@LohithR22 LohithR22 commented Jan 15, 2026

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

  • RepeatedResourcesInLoops
    Detects 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.

  • LoadAllDataBagItems
    Flags overly broad or empty search and data_bag_search queries (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

This makes the cops cleaner, DRY, and more maintainable.

📚 Documentation

  • Added a performance cops documentation file under docs-chef-cops/performance.md with:

    • Explanation of the performance department's goal
    • Details and examples for the newly introduced cops

✅ Testing

  • Added RSpec tests for the two new performance cops.
  • Added unit tests for the ResourceMatcher helper 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

  • ✨ New feature (non-breaking change which adds functionality)
  • 🔧 Chore (non-breaking change that refactors or improves internal code/documentation)

Checklist

  • I have read the CONTRIBUTING document
  • I have run all pre-merge tests locally using bundle exec rspec
  • I have updated documentation as required (performance.md, helpers.md)
  • I have added/updated tests to cover my changes
  • I have signed all commits for [the Developer Certificate of Origin]
  • I have verified no unintended Gemfile.lock changes exist
  • I have confirmed that all RuboCop cops are registered properly, and the .rubocop.yml is valid

Signed-off-by: LohithR22 <lohithrgowda22@gmail.com>
@LohithR22 LohithR22 requested review from a team and jaymzh as code owners January 15, 2026 02:08
@sonarqubecloud
Copy link
Copy Markdown

module Cop
module Chef
module Performance
class RepeatedResourcesInLoops < Base
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only the package resource actually supports arrays the way you're implying.

And there is already a cop for that, UseMultipackageInstalls.

Copy link
Copy Markdown
Collaborator

@jaymzh jaymzh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We picked one cop to review, we will come back and review others.

However, we encourage you to split up PRs into a single Cop each.

@jaymzh
Copy link
Copy Markdown
Collaborator

jaymzh commented Apr 21, 2026

@LohithR22 are you still interested in working on this?

@jaymzh jaymzh added the Status: Waiting on Contributor A pull request that has unresolved requested actions from the author. label Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Status: Waiting on Contributor A pull request that has unresolved requested actions from the author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants