From 7abbf1bbf38c1e280a0a2252c44fb1910398ba1b Mon Sep 17 00:00:00 2001 From: mugulmd Date: Mon, 16 Jan 2023 03:43:26 -0800 Subject: [PATCH] [ui] ThumbnailCache: reduce number of system calls --- meshroom/ui/components/thumbnail.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/meshroom/ui/components/thumbnail.py b/meshroom/ui/components/thumbnail.py index a2ca2bae..d5b1fcd1 100644 --- a/meshroom/ui/components/thumbnail.py +++ b/meshroom/ui/components/thumbnail.py @@ -5,6 +5,7 @@ from PySide2.QtGui import QImageReader, QImageWriter import os from pathlib import Path +import stat import hashlib import time import logging @@ -173,22 +174,28 @@ class ThumbnailCache(QObject): # Scan thumbnail directory and gather all thumbnails to remove toRemove = [] remaining = [] - for entry in os.scandir(ThumbnailCache.thumbnailDir): - if not entry.is_file(): + for f_name in os.listdir(ThumbnailCache.thumbnailDir): + pathname = os.path.join(ThumbnailCache.thumbnailDir, f_name) + + # System call to get current item info + f_stat = os.stat(pathname, follow_symlinks=False) + + # Check if this is a regular file + if not stat.S_ISREG(f_stat.st_mode): continue # Compute storage duration since last usage of thumbnail - lastUsage = os.path.getmtime(entry.path) + lastUsage = f_stat.st_mtime storageTime = now - lastUsage - logging.debug(f'[ThumbnailCache] Thumbnail {entry.name} has been stored for {storageTime}s') + logging.debug(f'[ThumbnailCache] Thumbnail {f_name} has been stored for {storageTime}s') if storageTime > ThumbnailCache.storageTimeLimit * 3600 * 24: # Mark as removable if storage time exceeds limit - logging.debug(f'[ThumbnailCache] {entry.name} exceeded storage time limit') - toRemove.append(entry.path) + logging.debug(f'[ThumbnailCache] {f_name} exceeded storage time limit') + toRemove.append(pathname) else: # Store path and last usage time for potentially sorting and removing later - remaining.append((entry.path, lastUsage)) + remaining.append((pathname, lastUsage)) # Remove all thumbnails marked as removable for path in toRemove: