Optimize performance by eliminating redundant operations and arranging code to avoid unnecessary computations, especially in frequently executed paths. Follow these principles:
Optimize performance by eliminating redundant operations and arranging code to avoid unnecessary computations, especially in frequently executed paths. Follow these principles:
# Instead of this:
primary_container.dependencies.each do |dep|
Homebrew::Install.perform_preinstall_checks_once
# other operations...
end
# Do this:
Homebrew::Install.perform_preinstall_checks_once
primary_container.dependencies.each do |dep|
# other operations...
end
# Instead of repeatedly reading files in a loop:
formulae_and_casks_to_check.each do |formula_or_cask|
autobump_file = formula_or_cask.tap.path/".github/autobump.txt"
next false unless File.exist?(autobump_file)
if File.read(autobump_file).include?(formula_or_cask.name)
# ...
end
end
# Read files once and cache the results:
autobump_files = {}
formulae_and_casks_to_check.each do |formula_or_cask|
tap = formula_or_cask.tap
next if tap.nil?
autobump_files[tap] ||= begin
autobump_path = tap.path/".github/autobump.txt"
autobump_path.exist? ? File.read(autobump_path).lines.map(&:strip) : []
end
# Use cached content
end
# Cache expensive operation results
deploy_new_x86_64_runner = @all_supported || deploy_new_x86_64_runner?
Formula.all.any? do |formula|
# First check the simple condition
next false if formula.class.pour_bottle_only_if != :clt_installed
# Only then perform expensive dependency analysis
non_test_dependencies = Dependency.expand(formula, cache_key: "determine-test-runners") do |_, dependency|
Dependency.prune if dependency.test?
end
# ...
end
These optimizations are particularly important in performance-critical paths that execute frequently or process large amounts of data.
Enter the URL of a public GitHub repository