From 0b9c08b47bb5e95c21b067044ace4e824d19a9c2 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Thu, 19 May 2022 19:36:31 +0530 Subject: [PATCH] [utils] Improve performance using `functools.cache` Closes #3786 --- yt_dlp/compat/functools.py | 12 ++++++++++++ yt_dlp/update.py | 3 ++- yt_dlp/utils.py | 7 +++++-- 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 yt_dlp/compat/functools.py diff --git a/yt_dlp/compat/functools.py b/yt_dlp/compat/functools.py new file mode 100644 index 000000000..36c983642 --- /dev/null +++ b/yt_dlp/compat/functools.py @@ -0,0 +1,12 @@ +# flake8: noqa: F405 +from functools import * # noqa: F403 + +from .compat_utils import passthrough_module + +passthrough_module(__name__, 'functools') +del passthrough_module + +try: + cache # >= 3.9 +except NameError: + cache = lru_cache(maxsize=None) diff --git a/yt_dlp/update.py b/yt_dlp/update.py index 8dcf260f5..d627ae269 100644 --- a/yt_dlp/update.py +++ b/yt_dlp/update.py @@ -7,11 +7,12 @@ import sys import traceback from zipimport import zipimporter -from .compat import compat_realpath +from .compat import compat_realpath, functools from .utils import Popen, encode_compat_str, write_string from .version import __version__ +@functools.cache def detect_variant(): if hasattr(sys, 'frozen'): prefix = 'mac' if sys.platform == 'darwin' else 'win' diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index 41157f5de..0274e330d 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -11,7 +11,6 @@ import datetime import email.header import email.utils import errno -import functools import gzip import hashlib import hmac @@ -39,8 +38,8 @@ import urllib.parse import xml.etree.ElementTree import zlib +from .compat import asyncio, functools # Modules from .compat import ( - asyncio, compat_chr, compat_cookiejar, compat_etree_fromstring, @@ -248,6 +247,7 @@ JSON_LD_RE = r'(?is)]+type=(["\']?)application/ld\+json\1[^>]*>(?P