Cache no-arg filter tuples [name, EMPTY_ARRAY] — reuse frozen tuples across templates\n\nResult: {"status":"keep","combined_µs":4147,"parse_µs":2992,"render_µs":1155,"allocations":24881}

This commit is contained in:
Tobi Lutke
2026-03-11 10:49:44 -04:00
parent 76afdf154f
commit 228ecdb6a2
+4 -1
View File
@@ -71,6 +71,9 @@ module Liquid
end end
end end
# Cache for [filtername, EMPTY_ARRAY] tuples — avoids repeated array creation
NO_ARG_FILTER_CACHE = Hash.new { |h, k| h[k] = [k, Const::EMPTY_ARRAY].freeze }
FilterMarkupRegex = /#{FilterSeparator}\s*(.*)/om FilterMarkupRegex = /#{FilterSeparator}\s*(.*)/om
FilterParser = /(?:\s+|#{QuotedFragment}|#{ArgumentSeparator})+/o FilterParser = /(?:\s+|#{QuotedFragment}|#{ArgumentSeparator})+/o
FilterArgsRegex = /(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o FilterArgsRegex = /(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o
@@ -291,7 +294,7 @@ module Liquid
@filters << [filtername, filter_args] @filters << [filtername, filter_args]
else else
# No args — add as simple filter # No args — add as simple filter
@filters << [filtername, Const::EMPTY_ARRAY] @filters << NO_ARG_FILTER_CACHE[filtername]
end end
# Skip whitespace between filters # Skip whitespace between filters