mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Cache small integer to_s (0-999): avoids 267 Integer#to_s allocations per render cycle\n\nResult: {"status":"keep","combined_µs":4158,"parse_µs":2920,"render_µs":1238,"allocations":26128}
This commit is contained in:
@@ -96,8 +96,14 @@ module Liquid
|
|||||||
obj
|
obj
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Cached string representations for common small integers (0-999)
|
||||||
|
# Avoids repeated Integer#to_s allocations during rendering
|
||||||
|
SMALL_INT_STRINGS = Array.new(1000) { |i| i.to_s.freeze }.freeze
|
||||||
|
|
||||||
def self.to_s(obj, seen = nil)
|
def self.to_s(obj, seen = nil)
|
||||||
case obj
|
case obj
|
||||||
|
when Integer
|
||||||
|
return (obj >= 0 && obj < 1000) ? SMALL_INT_STRINGS[obj] : obj.to_s
|
||||||
when BigDecimal
|
when BigDecimal
|
||||||
obj.to_s("F")
|
obj.to_s("F")
|
||||||
when Hash
|
when Hash
|
||||||
|
|||||||
Reference in New Issue
Block a user