Skip to content

Commit

Permalink
adding caching to context variable lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
ggmichaelgo committed Nov 25, 2024
1 parent 58965aa commit caaf95b
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/liquid/context.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "lru_redux"

module Liquid
# Context keeps the variable stack and resolves variables, as well as keywords
#
Expand Down Expand Up @@ -39,6 +41,7 @@ def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_erro
@filters = []
@global_filter = nil
@disabled_tags = {}
@expression_cache = LruRedux::ThreadSafeCache.new(1000)

# Instead of constructing new StringScanner objects for each Expression parse,
# we recycle the same one.
Expand Down Expand Up @@ -180,7 +183,7 @@ def []=(key, value)
# Example:
# products == empty #=> products.empty?
def [](expression)
evaluate(Expression.parse(expression, @string_scanner))
evaluate(Expression.parse(expression, @string_scanner, @expression_cache))
end

def key?(key)
Expand Down

0 comments on commit caaf95b

Please sign in to comment.