diff mbox series

[1/6] lib/oe/sstatesig: cache the user/group lookups in OEOuthashBasic

Message ID 20261019145837.2487873-1-ross.burton@arm.com
State New
Headers show
Series [1/6] lib/oe/sstatesig: cache the user/group lookups in OEOuthashBasic | expand

Commit Message

Ross Burton Oct. 19, 2026, 2:58 p.m. UTC
The hash includes the user/group names for every file in the sstate
object, so we can save a little time by wrapping pwd.getpwuid() and
grp.getgrgid() in a lru_cache.

In a microbenchmark on my machine, repeated calls of getpwuid() take
0.56ms but the caching wrapper takes 0.09ms.

Signed-off-by: Ross Burton <ross.burton@arm.com>
---
 meta/lib/oe/sstatesig.py | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/meta/lib/oe/sstatesig.py b/meta/lib/oe/sstatesig.py
index 9928f3af495..145bc59617d 100644
--- a/meta/lib/oe/sstatesig.py
+++ b/meta/lib/oe/sstatesig.py
@@ -550,6 +550,11 @@  def OEOuthashBasic(path, sigfile, task, d):
     import grp
     import re
     import fnmatch
+    import functools
+
+    # Caches to speed lookups
+    lookup_uid = functools.lru_cache(lambda uid: pwd.getpwuid(uid).pw_name)
+    lookup_gid = functools.lru_cache(lambda gid: grp.getgrgid(gid).gr_name)
 
     def update_hash(s):
         s = s.encode('utf-8')
@@ -652,8 +657,8 @@  def OEOuthashBasic(path, sigfile, task, d):
                         add_perm(stat.S_IXOTH, 'x')
 
                     try:
-                        update_hash(" %10s" % pwd.getpwuid(s.st_uid).pw_name)
-                        update_hash(" %10s" % grp.getgrgid(s.st_gid).gr_name)
+                        update_hash(" %10s" % lookup_uid(s.st_uid))
+                        update_hash(" %10s" % lookup_gid(s.st_gid))
                     except KeyError as e:
                         msg = ("KeyError: %s\nPath %s is owned by uid %d, gid %d, which doesn't match "
                             "any user/group on target. This may be due to host contamination." %