Skip to content

Commit

Permalink
Remove lru_cache from get_active_msgids (#5672)
Browse files Browse the repository at this point in the history
Ref #5670
  • Loading branch information
DanielNoord authored Jan 13, 2022
1 parent aaa3770 commit e75e37a
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions pylint/message/message_id_store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
import functools
from typing import Dict, List, NoReturn, Optional, Tuple

from pylint.exceptions import InvalidMessageError, UnknownMessageError
Expand All @@ -14,6 +13,7 @@ def __init__(self) -> None:
self.__msgid_to_symbol: Dict[str, str] = {}
self.__symbol_to_msgid: Dict[str, str] = {}
self.__old_names: Dict[str, List[str]] = {}
self.__active_msgids: Dict[str, List[str]] = {}

def __len__(self) -> int:
return len(self.__msgid_to_symbol)
Expand Down Expand Up @@ -104,14 +104,17 @@ def _raise_duplicate_msgid(symbol: str, msgid: str, other_msgid: str) -> NoRetur
)
raise InvalidMessageError(error_message)

@functools.lru_cache()
def get_active_msgids(self, msgid_or_symbol: str) -> List[str]:
"""Return msgids but the input can be a symbol.
The cache has no limit as its size will likely stay minimal. For each message we store
about 1000 characters, so even if we would have 1000 messages the cache would only
take up ~= 1 Mb.
self.__active_msgids is used to implement a primitive cache for this function.
"""
try:
return self.__active_msgids[msgid_or_symbol]
except KeyError:
pass

# If we don't have a cached value yet we compute it
msgid: Optional[str]
if msgid_or_symbol[1:].isdigit():
# Only msgid can have a digit as second letter
Expand All @@ -123,4 +126,8 @@ def get_active_msgids(self, msgid_or_symbol: str) -> List[str]:
if not msgid or not symbol:
error_msg = f"No such message id or symbol '{msgid_or_symbol}'."
raise UnknownMessageError(error_msg)
return self.__old_names.get(msgid, [msgid])
ids = self.__old_names.get(msgid, [msgid])

# Add to cache
self.__active_msgids[msgid_or_symbol] = ids
return ids

0 comments on commit e75e37a

Please sign in to comment.