From 2c30ecb2432890daecf8168eb02972403fa7b016 Mon Sep 17 00:00:00 2001 From: Loren Eteval Date: Sun, 3 Sep 2023 10:11:31 +0800 Subject: [PATCH] Add lru_cache(None) to speed up function call Signed-off-by: Loren Eteval --- Furious/Core/TorRelay.py | 13 +++++++------ Furious/Utility/Utility.py | 3 +++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Furious/Core/TorRelay.py b/Furious/Core/TorRelay.py index d9f27d4..bbcbde1 100644 --- a/Furious/Core/TorRelay.py +++ b/Furious/Core/TorRelay.py @@ -13,6 +13,7 @@ import re import signal import logging import threading +import functools import subprocess logger = logging.getLogger(__name__) @@ -82,6 +83,7 @@ class TorRelayStarter(AsyncSubprocessMessage): logger.info(f'{TorRelay.name()} bootstrapped {percentage}%') +@functools.lru_cache(None) def getTorRelayVersion(): try: result = subprocess.run( @@ -97,13 +99,12 @@ def getTorRelayVersion(): except Exception: # Any non-exit exceptions - return '0.0.0' - - -CACHED_VERSION = getTorRelayVersion() + return TorRelay.VERSION_NOT_FOUND class TorRelay(Core): + VERSION_NOT_FOUND = '0.0.0' + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -111,7 +112,7 @@ class TorRelay(Core): @staticmethod def checkIfExists(): - return CACHED_VERSION != '0.0.0' + return TorRelay.version() != TorRelay.VERSION_NOT_FOUND @staticmethod def name(): @@ -119,7 +120,7 @@ class TorRelay(Core): @staticmethod def version(): - return CACHED_VERSION + return getTorRelayVersion() @property def bootstrapPercentage(self): diff --git a/Furious/Utility/Utility.py b/Furious/Utility/Utility.py index 4838a5b..dac628c 100644 --- a/Furious/Utility/Utility.py +++ b/Furious/Utility/Utility.py @@ -14,6 +14,7 @@ import os import copy import ujson import queue +import ctypes import logging import pybase64 import threading @@ -296,6 +297,7 @@ bootstrapIcon = functools.partial(icon, 'bootstrap') bootstrapIconWhite = functools.partial(icon, 'bootstrap/white') +@functools.lru_cache(None) def protocolRepr(protocol): if protocol.lower() == 'vmess': return Protocol.VMess @@ -313,6 +315,7 @@ def getAbsolutePath(path): return path if os.path.isabs(path) else str(ROOT_DIR / path) +@functools.lru_cache(None) def getUbuntuRelease(): try: result = subprocess.run(