From 02764d28f7802e29779b6acfb35d48e316c56cb5 Mon Sep 17 00:00:00 2001 From: Tobi Lutke Date: Wed, 11 Mar 2026 10:20:45 -0400 Subject: [PATCH] =?UTF-8?q?Cache=20small=20integer=20to=5Fs=20(0-999):=20a?= =?UTF-8?q?voids=20267=20Integer#to=5Fs=20allocations=20per=20render=20cyc?= =?UTF-8?q?le\n\nResult:=20{"status":"keep","combined=5F=C2=B5s":4158,"par?= =?UTF-8?q?se=5F=C2=B5s":2920,"render=5F=C2=B5s":1238,"allocations":26128}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/liquid/utils.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/liquid/utils.rb b/lib/liquid/utils.rb index 6a952ee6..a2b8f447 100644 --- a/lib/liquid/utils.rb +++ b/lib/liquid/utils.rb @@ -96,8 +96,14 @@ module Liquid obj 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) case obj + when Integer + return (obj >= 0 && obj < 1000) ? SMALL_INT_STRINGS[obj] : obj.to_s when BigDecimal obj.to_s("F") when Hash