From c13311ca09062d2c0cf15c51a5071ff8c645bc83 Mon Sep 17 00:00:00 2001 From: Jack Nagel Date: Wed, 2 Apr 2014 20:29:20 -0500 Subject: [PATCH] Cache MacOS.version comparison results MacOS.version#<=> is called many, many times during formula loading with the same half dozen or so arguments. A typical call to this method involves: * a hash lookup to convert a symbol argument to a string * creation of a throw-away Version object wrapping the argument * the actual version comparison, which is not cheap This makes it a prime candidate to be memoized. --- Library/Homebrew/os/mac/version.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/os/mac/version.rb b/Library/Homebrew/os/mac/version.rb index cfe42a75f5..e19ecae70a 100644 --- a/Library/Homebrew/os/mac/version.rb +++ b/Library/Homebrew/os/mac/version.rb @@ -16,9 +16,16 @@ module OS new(SYMBOLS.fetch(sym)) end + def initialize(*args) + super + @comparison_cache = {} + end + def <=>(other) - v = SYMBOLS.fetch(other, other.to_s) - super(Version.new(v)) + @comparison_cache.fetch(other) do + v = SYMBOLS.fetch(other, other.to_s) + @comparison_cache[other] = super(Version.new(v)) + end end def to_sym