]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
perf: use functools.cache where available
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 28 Sep 2022 01:01:52 +0000 (02:01 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 28 Sep 2022 01:03:21 +0000 (02:03 +0100)
Faster than lru_cache; use it when we don't suppose we will ever evict
values.

psycopg/psycopg/_compat.py
psycopg/psycopg/_encodings.py
psycopg/psycopg/pq/misc.py
psycopg/psycopg/types/array.py

index 16b1685c892882f433e31b070ffd7dfd69a1e14e..66d1c8392cc1bcb1b3e1aae76c39dc8d872fb60b 100644 (file)
@@ -29,10 +29,14 @@ else:
 
 if sys.version_info >= (3, 9):
     from zoneinfo import ZoneInfo
+    from functools import cache
     from collections import Counter, deque as Deque
 else:
-    from backports.zoneinfo import ZoneInfo
     from typing import Counter, Deque
+    from functools import lru_cache
+    from backports.zoneinfo import ZoneInfo
+
+    cache = lru_cache(maxsize=None)
 
 if sys.version_info >= (3, 10):
     from typing import TypeAlias, TypeGuard
@@ -52,5 +56,6 @@ __all__ = [
     "TypeAlias",
     "TypeGuard",
     "ZoneInfo",
+    "cache",
     "create_task",
 ]
index b1b21267c5c25a4c4bd6706bbfd60f1036c2c0db..ba09fd4edd87ca74e5c2cbce8e64937681e3f464 100644 (file)
@@ -7,10 +7,10 @@ Mappings between PostgreSQL and Python encodings.
 import re
 import string
 import codecs
-from functools import lru_cache
 from typing import Any, Dict, Optional, TYPE_CHECKING
 
 from .errors import NotSupportedError
+from ._compat import cache
 
 if TYPE_CHECKING:
     from .pq.abc import PGconn
@@ -116,7 +116,7 @@ def conninfo_encoding(conninfo: str) -> str:
     return "utf-8"
 
 
-@lru_cache()
+@cache
 def py2pgenc(name: str) -> bytes:
     """Convert a Python encoding name to PostgreSQL encoding name.
 
@@ -125,7 +125,7 @@ def py2pgenc(name: str) -> bytes:
     return pg_codecs[codecs.lookup(name).name]
 
 
-@lru_cache()
+@cache
 def pg2pyenc(name: bytes) -> str:
     """Convert a Python encoding name to PostgreSQL encoding name.
 
index 92be04965407bad8bf4907b0a20fa69ae149911d..57784419b323e243f0c75ba716dc7af38340aa2c 100644 (file)
@@ -9,10 +9,10 @@ import sys
 import logging
 import ctypes.util
 from typing import cast, NamedTuple, Optional, Union
-from functools import lru_cache
 
 from .abc import PGconn, PGresult
 from ._enums import ConnStatus, TransactionStatus, PipelineStatus
+from .._compat import cache
 from .._encodings import pgconn_encoding
 
 logger = logging.getLogger("psycopg.pq")
@@ -46,7 +46,7 @@ class PGresAttDesc(NamedTuple):
     atttypmod: int
 
 
-@lru_cache()
+@cache
 def find_libpq_full_path() -> Optional[str]:
     if sys.platform == "win32":
         libname = ctypes.util.find_library("libpq.dll")
index 569e61f66d0e07bbf94478f31d2ae84951fdd97b..5ecbac1fa58678d3347a80b5c75e494ad6668888 100644 (file)
@@ -8,13 +8,13 @@ import re
 import struct
 from typing import Any, cast, Callable, Iterator, List
 from typing import Optional, Pattern, Set, Tuple, Type
-from functools import lru_cache
 
 from .. import pq
 from .. import errors as e
 from .. import postgres
 from ..abc import AdaptContext, Buffer, Dumper, DumperKey, NoneType
 from ..adapt import RecursiveDumper, RecursiveLoader, PyFormat
+from .._compat import cache
 from .._struct import pack_len, unpack_len
 from ..postgres import TEXT_OID, INVALID_OID
 from .._typeinfo import TypeInfo
@@ -190,7 +190,7 @@ class ListDumper(BaseListDumper):
             return self._tx.get_dumper(item, PyFormat.TEXT).dump(item)
 
 
-@lru_cache()
+@cache
 def _get_needs_quotes_regexp(delimiter: bytes) -> Pattern[bytes]:
     """Return a regexp to recognise when a value needs quotes
 
@@ -349,7 +349,7 @@ class ArrayLoader(BaseArrayLoader):
     _re_unescape = re.compile(rb"\\(.)")
 
 
-@lru_cache()
+@cache
 def _get_array_parse_regexp(delimiter: bytes) -> Pattern[bytes]:
     """
     Return a regexp to tokenize an array representation into item and brackets