From b5ae3ab195bfb96d4c58268e761ecb12578941dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Mon, 26 Feb 2024 08:42:19 +0100 Subject: [PATCH] Only compile regex pattern once Improve the performance for creating the regexp pattern by caching them. With this change they will be created on demand once and afterwards they are used from the cache. --- pontos/updateheader/updateheader.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pontos/updateheader/updateheader.py b/pontos/updateheader/updateheader.py index fcac6a360..eb4967644 100644 --- a/pontos/updateheader/updateheader.py +++ b/pontos/updateheader/updateheader.py @@ -11,6 +11,7 @@ import re import sys from dataclasses import dataclass +from functools import cache from pathlib import Path from typing import Optional, Sequence, Union @@ -282,11 +283,13 @@ def _get_exclude_list( return exclude_list +@cache def _compile_outdated_regex() -> list[re.Pattern]: """prepare regex patterns to remove old copyright lines""" return [re.compile(rf"^(([#*]|//) ?)?{line}") for line in OLD_LINES] +@cache def _compile_copyright_regex() -> re.Pattern: """prepare the copyright regex""" c_str = r"(SPDX-FileCopyrightText:|[Cc]opyright)"