From 3f417f52309d888d6bb8cf6d94fdd34d31d360e7 Mon Sep 17 00:00:00 2001 From: Chris Sloop Date: Wed, 11 Mar 2026 15:55:18 +0100 Subject: [PATCH] perf: skip redundant downcase in Currency.find! on cache hits --- lib/money/currency.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/money/currency.rb b/lib/money/currency.rb index a27759c9..53da2228 100644 --- a/lib/money/currency.rb +++ b/lib/money/currency.rb @@ -11,9 +11,13 @@ class UnknownCurrency < ArgumentError; end class << self def new(currency_iso) - raise UnknownCurrency, "Currency can't be blank" if currency_iso.nil? || currency_iso.to_s.empty? - iso = currency_iso.to_s.downcase - @@loaded_currencies[iso] || @@mutex.synchronize { @@loaded_currencies[iso] = super(iso) } + iso = currency_iso.to_s + return @@loaded_currencies[iso] if @@loaded_currencies.key?(iso) + raise UnknownCurrency, "Currency can't be blank" if iso.empty? + downcased = iso.downcase + @@mutex.synchronize do + @@loaded_currencies[iso] ||= @@loaded_currencies[downcased] ||= super(downcased) + end end alias_method :find!, :new