Skip to content

Commit

Permalink
Optimization: use hashes in results.rb to get faster lookup times
Browse files Browse the repository at this point in the history
  • Loading branch information
Drowze committed Jan 23, 2024
1 parent d0b77ab commit f594c30
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lib/coverband/utils/results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ def initialize(report)
def file_with_type(source_file, results_type)
return unless get_results(results_type)

get_results(results_type).source_files.find { |file| file.filename == source_file.filename }
@files_with_type ||= {}
@files_with_type[results_type] ||= get_results(results_type).source_files.map do |source_file|
[source_file.filename, source_file]
end.to_h
@files_with_type[results_type][source_file.filename]
end

def runtime_relevant_coverage(source_file)
Expand Down Expand Up @@ -48,7 +52,11 @@ def runtime_relavent_lines(source_file)
def file_from_path_with_type(full_path, results_type = :merged)
return unless get_results(results_type)

get_results(results_type).source_files.find { |file| file.filename == full_path }
@files_from_path_with_type ||= {}
@files_from_path_with_type[results_type] ||= get_results(results_type).source_files.map do |source_file|
[source_file.filename, source_file]
end.to_h
@files_from_path_with_type[results_type][full_path]
end

def method_missing(method, *args)
Expand All @@ -70,11 +78,11 @@ def respond_to_missing?(method)
private

def get_eager_file(source_file)
eager_loading_coverage.source_files.find { |file| file.filename == source_file.filename }
file_with_type(source_file, Coverband::EAGER_TYPE)
end

def get_runtime_file(source_file)
runtime_coverage.source_files.find { |file| file.filename == source_file.filename }
file_with_type(source_file, Coverband::RUNTIME_TYPE)
end

def eager_loading_coverage
Expand All @@ -93,11 +101,7 @@ def runtime_coverage
def get_results(type)
return nil unless Coverband::ALL_TYPES.include?(type)

if @results.key?(type)
@results[type]
else
@results[type] = Coverband::Utils::Result.new(report[type])
end
@results[type] ||= Coverband::Utils::Result.new(report[type])
end
end
end
Expand Down

0 comments on commit f594c30

Please sign in to comment.