From 9e39a63c17e0d1435d18c0aca92ec0feec815c2a Mon Sep 17 00:00:00 2001 From: Jairo Llopis <973709+yajo@users.noreply.github.com> Date: Thu, 27 Jul 2023 02:43:09 +0100 Subject: [PATCH] perf: only iter on found escape sequences (#304) 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 --- tomlkit/_utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tomlkit/_utils.py b/tomlkit/_utils.py index a6ed9c64..f87fd7b5 100644 --- a/tomlkit/_utils.py +++ b/tomlkit/_utils.py @@ -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)