From 3bcf0092e9beadbe5c3940030e0ffe6e4c40e6da Mon Sep 17 00:00:00 2001 From: James Lu Date: Fri, 7 Jul 2017 14:33:00 -0700 Subject: [PATCH] NetworkCoreWithUtils: wrap irc.to_lower in a lru_cache --- classes.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/classes.py b/classes.py index df0b48a..72abec2 100644 --- a/classes.py +++ b/classes.py @@ -16,6 +16,7 @@ from copy import deepcopy import inspect import ipaddress import queue +import functools try: import ircmatch @@ -473,24 +474,29 @@ class PyLinkNetworkCore(utils.DeprecatedAttributesObject, utils.CamelCaseToSnake return False class PyLinkNetworkCoreWithUtils(PyLinkNetworkCore): + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # Lock for updateTS to make sure only one thread can change the channel TS at one time. self._ts_lock = threading.Lock() - def to_lower(self, text): - """Returns a lowercase representation of text based on the IRC object's - casemapping (rfc1459 or ascii).""" - if self.casemapping == 'rfc1459': + @staticmethod + @functools.lru_cache(maxsize=2048) + def _to_lower_core(text, casemapping='rfc1459'): + if casemapping == 'rfc1459': text = text.replace('{', '[') text = text.replace('}', ']') text = text.replace('|', '\\') text = text.replace('~', '^') # Encode the text as bytes first, and then lowercase it so that only ASCII characters are - # changed. Unicode in channel names, etc. is case sensitive because IRC is just that old of - # a protocol!!! + # changed. Unicode in channel names, etc. *is* case sensitive! return text.encode().lower().decode() + def to_lower(self, text): + """Returns a lowercase representation of text based on the IRC object's + casemapping (rfc1459 or ascii).""" + return self._to_lower_core(text, casemapping=self.casemapping) + def parse_modes(self, target, args): """Parses a modestring list into a list of (mode, argument) tuples. ['+mitl-o', '3', 'person'] => [('+m', None), ('+i', None), ('+t', None), ('+l', '3'), ('-o', 'person')]