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.
This commit is contained in:
Jack Nagel 2014-04-02 20:29:20 -05:00
parent bb5e0812fb
commit c13311ca09

View File

@ -16,9 +16,16 @@ module OS
new(SYMBOLS.fetch(sym)) new(SYMBOLS.fetch(sym))
end end
def initialize(*args)
super
@comparison_cache = {}
end
def <=>(other) def <=>(other)
v = SYMBOLS.fetch(other, other.to_s) @comparison_cache.fetch(other) do
super(Version.new(v)) v = SYMBOLS.fetch(other, other.to_s)
@comparison_cache[other] = super(Version.new(v))
end
end end
def to_sym def to_sym