From 428bded17a71d74a632a433515f84392f489b58a Mon Sep 17 00:00:00 2001 From: "Pryor, Adam" Date: Fri, 17 Oct 2025 15:46:19 -0500 Subject: [PATCH] Add cache for user/group checking (#780) * Add cache for user/group checking * Fix self Signed-off-by: Maisam Arif [ROCm/amdsmi commit: b2b0815b371a3f8001ee427d021e2d2c0acc58c6] --- projects/amdsmi/amdsmi_cli/amdsmi_helpers.py | 29 +++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/projects/amdsmi/amdsmi_cli/amdsmi_helpers.py b/projects/amdsmi/amdsmi_cli/amdsmi_helpers.py index ac0e5cb2f4..a1874b78a0 100755 --- a/projects/amdsmi/amdsmi_cli/amdsmi_helpers.py +++ b/projects/amdsmi/amdsmi_cli/amdsmi_helpers.py @@ -36,6 +36,7 @@ import pwd from enum import Enum from pathlib import Path from typing import List, Set, Union +from functools import lru_cache # Import amdsmi library from amdsmi_init import * @@ -1136,20 +1137,22 @@ class AMDSMIHelpers(): for i in self.progressbar(range(timeInSeconds), title, 40, add_newline=add_newline): time.sleep(1) - def _user_name(self, uid: int) -> str: - try: - return pwd.getpwuid(uid).pw_name - except Exception: - # In containers, the UID may not resolve to a name - return str(uid) - - def _group_name(self, gid: int) -> str: - try: + @lru_cache(maxsize=128) + def _cached_group_name(self, gid: int) -> str: + try: return grp.getgrgid(gid).gr_name - except Exception: - # In containers, the GID may not resolve to a name + except Exception: + # In containers, the UID may not resolve to a name return str(gid) + @lru_cache(maxsize=128) + def _cached_user_name(self, uid: int) -> str: + try: + return pwd.getpwuid(uid).pw_name + except Exception: + # In containers, the GID may not resolve to a name + return str(uid) + # Attempt to grab file info def _stat_info(self, path: str) -> dict: try: @@ -1157,8 +1160,8 @@ class AMDSMIHelpers(): return { "uid": st.st_uid, "gid": st.st_gid, - "user": self._user_name(st.st_uid), - "group": self._group_name(st.st_gid), + "user": self._cached_user_name(st.st_uid), + "group": self._cached_group_name(st.st_gid), } except Exception as e: return {"error": str(e)}