Skip to content

Commit

Permalink
Refactor element method to improve performance
Browse files Browse the repository at this point in the history
Also add `ruby-prof` gem.
  • Loading branch information
silug committed Apr 10, 2024
1 parent 1a843a2 commit 587d13a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ group :development do
gem 'irb'
gem 'pry'
gem 'pry-byebug'
gem 'ruby-prof'
end
6 changes: 3 additions & 3 deletions lib/compliance_engine/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ComplianceEngine::Check < ComplianceEngine::Component
#
# @return [Hash] the settings of the check
def settings
element('settings')
element['settings']
end

# Returns the Puppet class parameters of the check
Expand All @@ -26,14 +26,14 @@ def hiera
#
# @return [String] the type of the check
def type
element('type')
element['type']
end

# Returns the remediation data of the check
#
# @return [Hash] the remediation data of the check
def remediation
element('remediation')
element['remediation']
end

# Invalidate all cached data
Expand Down
44 changes: 18 additions & 26 deletions lib/compliance_engine/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(name, data: nil)
@component ||= { key: name, fragments: {} }
end

attr_accessor :component, :facts, :enforcement_tolerance, :environment_data, :cache
attr_accessor :component, :facts, :enforcement_tolerance, :environment_data

# Invalidate all cached data
#
Expand All @@ -29,7 +29,7 @@ def invalidate_cache(data = nil)
@enforcement_tolerance = data&.enforcement_tolerance
@environment_data = data&.environment_data
@fragments = nil
@cache = nil
@element = nil
end

# Adds a value to the fragments array of the component.
Expand Down Expand Up @@ -65,43 +65,43 @@ def key
#
# @return [String] the title of the component
def title
element('title')
element['title']
end

# Returns the description of the component
#
# @return [String] the description of the component
def description
element('description')
element['description']
end

# Returns the oval ids of the component
#
# @return [Array] the oval ids of the component
def oval_ids
element('oval-ids')
element['oval-ids']
end

# Returns the controls of the component
#
# @return [Hash] the controls of the component
def controls
element('controls')
element['controls']
end

# Returns the identifiers of the component
#
# @return [Hash] the identifiers of the component
def identifiers
element('identifiers')
element['identifiers']
end

# Returns the ces of the component
#
# @return [Array, Hash] the ces of the component
# @note This returns an Array for checks and a Hash for other components
def ces
element('ces')
element['ces']
end

private
Expand Down Expand Up @@ -212,29 +212,21 @@ def fragments
@fragments
end

# Returns an element of the component
# Returns a merged view of the component fragments
#
# @param [String] key The key of the element
# @return [Object] the element of the component
def element(key)
return cache[key] if cache&.key?(key)

cache ||= {}
def element
return @element unless @element.nil?

fragments.each_value do |fragment|
next unless fragment.key?(key)

if fragment[key].is_a?(Array)
cache[key] ||= []
cache[key] += fragment[key]
elsif fragment[key].is_a?(Hash)
cache[key] ||= {}
cache[key] = cache[key].deep_merge!(fragment[key])
else
cache[key] = fragment[key]
end
@element = if @element.nil?
fragment
else
@element.deep_merge!(fragment)
end
end

cache[key]
@element = {} if @element.nil?
@element
end
end

0 comments on commit 587d13a

Please sign in to comment.