diff --git a/Lib/tarfile.py b/Lib/tarfile.py index d5d8a469779f50b..8fe8fecda6d0185 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1658,6 +1658,9 @@ class TarFile(object): extraction_filter = None # The default filter for extraction. + uname_cache = {} # Cached mappings of uid -> uname, gid -> gname + gname_cache = {} + def __init__(self, name=None, mode="r", fileobj=None, format=None, tarinfo=None, dereference=None, ignore_zeros=None, encoding=None, errors="surrogateescape", pax_headers=None, debug=None, @@ -2105,16 +2108,25 @@ def gettarinfo(self, name=None, arcname=None, fileobj=None): tarinfo.mtime = statres.st_mtime tarinfo.type = type tarinfo.linkname = linkname + + # Calls to pwd.getpwuid() and grp.getgrgid() tend to be expensive. To + # speed things up, cache the resolved usernames and group names. if pwd: - try: - tarinfo.uname = pwd.getpwuid(tarinfo.uid)[0] - except KeyError: - pass + if not tarinfo.uid in self.uname_cache: + try: + self.uname_cache[tarinfo.uid] = pwd.getpwuid(tarinfo.uid)[0] + except KeyError: + pass + + tarinfo.uname = self.uname_cache[tarinfo.uid] if grp: - try: - tarinfo.gname = grp.getgrgid(tarinfo.gid)[0] - except KeyError: - pass + if not tarinfo.gid in self.gname_cache: + try: + self.gname_cache[tarinfo.gid] = grp.getgrgid(tarinfo.gid)[0] + except KeyError: + pass + + tarinfo.gname = self.gname_cache[tarinfo.gid] if type in (CHRTYPE, BLKTYPE): if hasattr(os, "major") and hasattr(os, "minor"): diff --git a/Misc/NEWS.d/next/Library/2024-07-02-15-56-42.gh-issue-121267.yFBWkh.rst b/Misc/NEWS.d/next/Library/2024-07-02-15-56-42.gh-issue-121267.yFBWkh.rst new file mode 100644 index 000000000000000..ca18bf37471bad3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-07-02-15-56-42.gh-issue-121267.yFBWkh.rst @@ -0,0 +1,2 @@ +Improve the performance of tarfile when writing files, by caching user names +and group names.