From b5fc06cedbcac90e54b3857907f972fe6dfce5e8 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Thu, 12 Jun 2025 23:41:46 -0400 Subject: [PATCH] Improve caching `MacOSVersion` --- Library/Homebrew/macos_version.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/macos_version.rb b/Library/Homebrew/macos_version.rb index f71a2e6e73..b88ea22572 100644 --- a/Library/Homebrew/macos_version.rb +++ b/Library/Homebrew/macos_version.rb @@ -2,9 +2,12 @@ # frozen_string_literal: true require "version" +require "cachable" # A macOS version. class MacOSVersion < Version + extend Cachable + # Raised when a macOS version is unsupported. class Error < RuntimeError sig { returns(T.nilable(T.any(String, Symbol))) } @@ -47,8 +50,10 @@ class MacOSVersion < Version sig { params(version: Symbol).returns(T.attached_class) } def self.from_symbol(version) - str = SYMBOLS.fetch(version) { raise MacOSVersion::Error, version } - new(str) + cache.fetch(version) do |v| + str = SYMBOLS.fetch(v) { raise MacOSVersion::Error, v } + cache[v] = new(str) + end end sig { params(version: T.nilable(String)).void } @@ -62,7 +67,7 @@ class MacOSVersion < Version sig { override.params(other: T.untyped).returns(T.nilable(Integer)) } def <=>(other) - return @comparison_cache[other] if @comparison_cache.key?(other) + return @comparison_cache[other.to_s] if @comparison_cache.key?(other.to_s) result = case other when Symbol @@ -76,7 +81,7 @@ class MacOSVersion < Version super end - @comparison_cache[other] = result unless frozen? + @comparison_cache[other.to_s] = result unless frozen? result end