From bc3b066ba8071d7d898d6620042d7c1b31cc8c69 Mon Sep 17 00:00:00 2001 From: Jason Hiltz-Laforge Date: Mon, 21 Jul 2014 19:11:37 +0000 Subject: [PATCH] Remove block in favour of for loop to reduce temporary object allocation during variable context resolution --- lib/liquid/context.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/liquid/context.rb b/lib/liquid/context.rb index d3f6af9..4e74f56 100644 --- a/lib/liquid/context.rb +++ b/lib/liquid/context.rb @@ -194,7 +194,12 @@ module Liquid # Fetches an object starting at the local scope and then moving up the hierachy def find_variable(key) - scope = @scopes.find { |s| s.has_key?(key) } + + # This was changed from find() to find_index() because this is a very hot + # path and find_index() is optimized in MRI to reduce object allocation + index = @scopes.find_index { |s| s.has_key?(key) } + scope = @scopes[index] if index + variable = nil if scope.nil?