Skip to content

Commit

Permalink
perf: only iter on found escape sequences (#304)
Browse files Browse the repository at this point in the history
Finding used sequences is much faster than iterating over each one of them for each character.

The iteration is still used, but at least it will only be used once per any sequence that is actually used in the string.

Most strings won't include any sequences and will just be fast.

@moduon MT-1075
  • Loading branch information
yajo committed Jul 27, 2023
1 parent 8edb46c commit 9e39a63
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion tomlkit/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ def flush(inc=1):

return i + inc

found_sequences = {seq for seq in escape_sequences if seq in s}

i = 0
while i < len(s):
for seq in escape_sequences:
for seq in found_sequences:
seq_len = len(seq)
if s[i:].startswith(seq):
start = flush(seq_len)
Expand Down

0 comments on commit 9e39a63

Please sign in to comment.