From 17b691ee329d85a1747adfdd1cb569b6b77a3173 Mon Sep 17 00:00:00 2001 From: Tobi Lutke Date: Thu, 12 Mar 2026 17:31:41 -0400 Subject: [PATCH] =?UTF-8?q?Replace=20@blocks.each=20with=20while=20loop=20?= =?UTF-8?q?in=20If=20render=20=E2=80=94=20avoids=20block=20proc=20allocati?= =?UTF-8?q?on=20per=20render\n\nResult:=20{"status":"keep","combined=5F?= =?UTF-8?q?=C2=B5s":3496,"parse=5F=C2=B5s":2356,"render=5F=C2=B5s":1140,"a?= =?UTF-8?q?llocations":24530}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autoresearch.jsonl | 1 + lib/liquid/tags/if.rb | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/autoresearch.jsonl b/autoresearch.jsonl index ccd88b52..82ba6466 100644 --- a/autoresearch.jsonl +++ b/autoresearch.jsonl @@ -24,3 +24,4 @@ {"run":23,"commit":"ca327b0","metric":3445,"metrics":{"parse_µs":2284,"render_µs":1161,"allocations":24647},"status":"keep","description":"Condition#evaluate: skip loop block for simple conditions (no child_relation) — saves 235 allocs","timestamp":1773350691752,"segment":0} {"run":24,"commit":"99454a9","metric":3489,"metrics":{"parse_µs":2353,"render_µs":1136,"allocations":24647},"status":"keep","description":"Replace simple_lookup? byte scan with match? regex — 8x faster per call, cleaner code","timestamp":1773350837721,"segment":0} {"run":25,"commit":"99454a9","metric":3797,"metrics":{"parse_µs":2636,"render_µs":1161,"allocations":29627},"status":"discard","description":"Regex name extraction in try_fast_parse: MatchData creates 5K extra allocs, much worse","timestamp":1773351048938,"segment":0} +{"run":26,"commit":"db348e0","metric":3459,"metrics":{"parse_µs":2318,"render_µs":1141,"allocations":24647},"status":"keep","description":"Inline to_liquid_value in If render — avoids one method dispatch per condition evaluation","timestamp":1773351080001,"segment":0} diff --git a/lib/liquid/tags/if.rb b/lib/liquid/tags/if.rb index 26e3293d..9ad58b5f 100644 --- a/lib/liquid/tags/if.rb +++ b/lib/liquid/tags/if.rb @@ -51,14 +51,17 @@ module Liquid end def render_to_output_buffer(context, output) - @blocks.each do |block| + idx = 0 + blocks = @blocks + while idx < blocks.length + block = blocks[idx] result = block.evaluate(context) - # Inline to_liquid_value fast path — respond_to? check is rarely true result = result.to_liquid_value if result.respond_to?(:to_liquid_value) if result return block.attachment.render_to_output_buffer(context, output) end + idx += 1 end output