Skip to content

Commit

Permalink
refactor: Advance tokenizer array offset instead of using Array#shift (
Browse files Browse the repository at this point in the history
…#1653)

Array#shift would move all the remaining elements of the array, which
is slower for larger arrays.
  • Loading branch information
dylanahsmith committed Nov 9, 2022
1 parent 1cd5ec5 commit 1cdd1f0
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/liquid/tokenizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ def initialize(source, line_numbers = false, line_number: nil, for_liquid_tag: f
@source = source.to_s.to_str
@line_number = line_number || (line_numbers ? 1 : nil)
@for_liquid_tag = for_liquid_tag
@offset = 0
@tokens = tokenize
end

def shift
(token = @tokens.shift) || return
token = @tokens[@offset]
return nil unless token

@offset += 1

if @line_number
@line_number += @for_liquid_tag ? 1 : token.count("\n")
Expand All @@ -31,7 +35,9 @@ def tokenize
tokens = @source.split(TemplateParser)

# removes the rogue empty element at the beginning of the array
tokens.shift if tokens[0]&.empty?
if tokens[0]&.empty?
@offset += 1
end

tokens
end
Expand Down

0 comments on commit 1cdd1f0

Please sign in to comment.