adding caching to context variable lookup

This commit is contained in:
Michael Go
2025-01-07 14:32:46 -04:00
parent 313d01706a
commit 2c7d686690
+4 -1
View File
@@ -1,5 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
require "lru_redux"
module Liquid module Liquid
# Context keeps the variable stack and resolves variables, as well as keywords # Context keeps the variable stack and resolves variables, as well as keywords
# #
@@ -39,6 +41,7 @@ module Liquid
@filters = [] @filters = []
@global_filter = nil @global_filter = nil
@disabled_tags = {} @disabled_tags = {}
@expression_cache = LruRedux::ThreadSafeCache.new(1000)
# Instead of constructing new StringScanner objects for each Expression parse, # Instead of constructing new StringScanner objects for each Expression parse,
# we recycle the same one. # we recycle the same one.
@@ -180,7 +183,7 @@ module Liquid
# Example: # Example:
# products == empty #=> products.empty? # products == empty #=> products.empty?
def [](expression) def [](expression)
evaluate(Expression.parse(expression, @string_scanner)) evaluate(Expression.parse(expression, @string_scanner, @expression_cache))
end end
def key?(key) def key?(key)