Skip to content

Commit 3a98448

Browse files
authored
avoid re-naking the whole merged string in _merge_one_string_group (#5173)
1 parent e34bb1b commit 3a98448

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
- Improve performance on deeply nested parenthesised expressions by no longer
7171
re-scanning the whole atom for every nesting level in `max_delimiter_priority_in_atom`
7272
(#5171)
73+
- Improve performance when merging long runs of implicitly concatenated strings by no
74+
longer re-escaping the whole accumulated string on every merge step (#5173)
7375

7476
### Output
7577

src/black/trans.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,6 @@ def make_naked(string: str, string_prefix: str) -> str:
688688
# NS: naked string
689689
# SS: next string
690690
# NSS: naked next string
691-
S = ""
692691
NS = ""
693692
num_of_strings = 0
694693
next_str_idx = string_idx
@@ -709,14 +708,20 @@ def make_naked(string: str, string_prefix: str) -> str:
709708
has_prefix = bool(next_prefix)
710709
prefix_tracker.append(has_prefix)
711710

712-
S = prefix + QUOTE + NS + NSS + BREAK_MARK + QUOTE
713-
NS = make_naked(S, prefix)
711+
# Each NSS is already naked (prefix and quotes stripped, inner quotes
712+
# escaped, f-string expression quotes toggled), and the parts are
713+
# separated by BREAK_MARK which contains no quote or backslash, so the
714+
# naked group is just their concatenation. Re-running make_naked over the
715+
# whole accumulated string on every iteration rescans all previously
716+
# merged substrings, which is quadratic in the size of the group.
717+
NS = NS + NSS + BREAK_MARK
714718

715719
next_str_idx += 1
716720

717721
# Take a note on the index of the non-STRING leaf.
718722
non_string_idx = next_str_idx
719723

724+
S = prefix + QUOTE + NS + QUOTE
720725
S_leaf = Leaf(token.STRING, S)
721726
if self.normalize_strings:
722727
S_leaf.value = normalize_string_quotes(S_leaf.value)

0 commit comments

Comments
 (0)