From 9886e18d9cfb787b4c3f1fa27b9c3ebf6fd4131a Mon Sep 17 00:00:00 2001 From: c-bata Date: Sat, 3 Dec 2022 21:53:25 +0900 Subject: [PATCH] Use functools.lru_cache to cache os.path.exists call --- optuna_dashboard/_app.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/optuna_dashboard/_app.py b/optuna_dashboard/_app.py index caf2be02..1d16b3a0 100644 --- a/optuna_dashboard/_app.py +++ b/optuna_dashboard/_app.py @@ -53,9 +53,11 @@ BottleView = TypeVar("BottleView", bound=Callable[..., BottleViewReturn]) logger = logging.getLogger(__name__) +# Static files BASE_DIR = os.path.dirname(os.path.abspath(__file__)) STATIC_DIR = os.path.join(BASE_DIR, "public") IMG_DIR = os.path.join(BASE_DIR, "img") +cached_path_exists = functools.lru_cache(maxsize=10)(os.path.exists) # In-memory trials cache trials_cache_lock = threading.Lock() @@ -366,7 +368,7 @@ def create_app(storage: BaseStorage, debug: bool = False) -> Bottle: def send_static(filename: str) -> BottleViewReturn: if not debug and "gzip" in request.headers["Accept-Encoding"]: gz_filename = filename.strip("/\\") + ".gz" - if os.path.exists(os.path.join(STATIC_DIR, gz_filename)): + if cached_path_exists(os.path.join(STATIC_DIR, gz_filename)): filename = gz_filename return static_file(filename, root=STATIC_DIR)