avoid allocating seen={} hash in Utils.to_s/inspect when not needed

This commit is contained in:
Tobi Lutke
2026-03-11 09:02:42 -04:00
parent c4186a16fc
commit 3799d4c488
+6 -6
View File
@@ -96,7 +96,7 @@ module Liquid
obj obj
end end
def self.to_s(obj, seen = {}) def self.to_s(obj, seen = nil)
case obj case obj
when BigDecimal when BigDecimal
obj.to_s("F") obj.to_s("F")
@@ -105,30 +105,30 @@ module Liquid
# custom implementation. Otherwise we use Liquid's default # custom implementation. Otherwise we use Liquid's default
# implementation. # implementation.
if obj.class.instance_method(:to_s) == HASH_TO_S_METHOD if obj.class.instance_method(:to_s) == HASH_TO_S_METHOD
hash_inspect(obj, seen) hash_inspect(obj, seen || {})
else else
obj.to_s obj.to_s
end end
when Array when Array
array_inspect(obj, seen) array_inspect(obj, seen || {})
else else
obj.to_s obj.to_s
end end
end end
def self.inspect(obj, seen = {}) def self.inspect(obj, seen = nil)
case obj case obj
when Hash when Hash
# If the custom hash implementation overrides `#inspect`, use their # If the custom hash implementation overrides `#inspect`, use their
# custom implementation. Otherwise we use Liquid's default # custom implementation. Otherwise we use Liquid's default
# implementation. # implementation.
if obj.class.instance_method(:inspect) == HASH_INSPECT_METHOD if obj.class.instance_method(:inspect) == HASH_INSPECT_METHOD
hash_inspect(obj, seen) hash_inspect(obj, seen || {})
else else
obj.inspect obj.inspect
end end
when Array when Array
array_inspect(obj, seen) array_inspect(obj, seen || {})
else else
obj.inspect obj.inspect
end end