Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/tools/apply_entity_naming_rename_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,16 @@ def apply_rows(rows: list[RenameRow], *, apply: bool) -> int:
print(f"Skipped rows: {len(skipped_rows)}")

file_to_rows: dict[Path, list[RenameRow]] = {}
unique_old_names: set[str] = set()
for row in executable_rows:
file_to_rows.setdefault(row.file_path, []).append(row)
unique_old_names.add(row.old_name)

# Pre-compile patterns to avoid re-compilation in the nested loop
# and bypass Python's internal regex cache limits.
patterns = {
name: re.compile(rf"\b{re.escape(name)}\b") for name in unique_old_names
}

total_matches = 0
modified_files = 0
Expand All @@ -171,7 +179,7 @@ def apply_rows(rows: list[RenameRow], *, apply: bool) -> int:
file_matches = 0

for row in file_to_rows[file_path]:
pattern = re.compile(rf"\b{re.escape(row.old_name)}\b")
pattern = patterns[row.old_name]
updated_text, count = pattern.subn(row.new_name, updated_text)
file_matches += count
if count:
Expand Down
Loading