Skip to content

Commit

Permalink
Only compile regex pattern once
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bjoernricks committed Feb 26, 2024
1 parent fcc3b16 commit b5ae3ab
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pontos/updateheader/updateheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)"
Expand Down

0 comments on commit b5ae3ab

Please sign in to comment.