Fast-path Hash lookups in VariableLookup#evaluate — skip respond_to? checks for Hash objects\n\nResult: {"status":"keep","combined_µs":4110,"parse_µs":2922,"render_µs":1188,"allocations":25535}

This commit is contained in:
Tobi Lutke
2026-03-11 10:35:29 -04:00
parent b058f79cbf
commit 4df608a12f
+4 -3
View File
@@ -177,9 +177,10 @@ module Liquid
# If object is a hash- or array-like object we look for the # If object is a hash- or array-like object we look for the
# presence of the key and if its available we return it # presence of the key and if its available we return it
if object.respond_to?(:[]) && if object.instance_of?(Hash) ? object.key?(key) :
((object.respond_to?(:key?) && object.key?(key)) || (object.respond_to?(:[]) &&
(object.respond_to?(:fetch) && key.is_a?(Integer))) ((object.respond_to?(:key?) && object.key?(key)) ||
(object.respond_to?(:fetch) && key.is_a?(Integer))))
# if its a proc we will replace the entry with the proc # if its a proc we will replace the entry with the proc
object = context.lookup_and_evaluate(object, key) object = context.lookup_and_evaluate(object, key)