Skip to contentSkip to conversation

cache: Reuse os.path.exists() results during dependency validation

PATCH In review [email protected] 3 participants 5 messages Sep 23 – Sep 30

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cache: Reuse os.path.exists() results during dependency validation

From: Etienne Cordonnier <[email protected]>

cacheValidUpdate() checks each recipe's file_checksums entries with os.path.exists(). Many recipes reference the same optional class or checksum candidate paths, so the same path is checked repeatedly within one validation pass. In a warm parse of 5,112 recipes (poky, meta-yocto, meta-oe, meta-python), the real cache data shows 585,348 such calls against only 48,757 unique paths, with the hottest path checked 16,894
times: 536,591 calls are redundant. At 3.5us per os.path.exists() call on a nonexistent path on this machine, that is about 1.9s of avoidable syscall time per validation pass. Cache the result per Cache instance, which is discarded and recreated at the start of each validation pass, so a filesystem change between passes is still observed.

AI-Generated: Uses Claude Code (Claude Opus 5.5)

---
 lib/bb/cache.py | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
lib/bb/cache.py +9 -1
index 2361c5684..01020f844 100644
@@ -401,6 +401,8 @@ class Cache(object):
         self.clean = set()
         self.checked = set()
         self.depends_cache = {}
+        # os.path.exists() results for this validation pass (see file_exists())
+        self.exists_cache = {}
         self.data_fn = None
         self.cacheclean = True
         self.data_hash = data_hash
@@ -619,7 +621,8 @@ class Cache(object):
                     if not f:
                         continue
                     f, exist = f.rsplit(":", 1)
-                    if (exist == "True" and not os.path.exists(f)) or (exist == "False" and os.path.exists(f)):
+                    file_exists = self.file_exists(f)
+                    if (exist == "True" and not file_exists) or (exist == "False" and file_exists):
                         self.logger.debug2("%s's file checksum list file %s changed",
                                              fn, f)
                         self.remove(fn)
@@ -657,6 +660,11 @@ class Cache(object):
         self.clean.add(fn)
         return True
 
+    def file_exists(self, fn):
+        if fn not in self.exists_cache:
+            self.exists_cache[fn] = os.path.exists(fn)
+        return self.exists_cache[fn]
+
     def remove(self, fn):
         """
         Remove a fn from the cache
Signature · 1 line
2.43.0
#1 lore raw
Quoted 44 lines · On Wed, 2026-09-23 at 19:06 +0200, [email protected] wrote:

From: Etienne Cordonnier <[email protected]>

cacheValidUpdate() checks each recipe's file_checksums entries with os.path.exists(). Many recipes reference the same optional class or checksum candidate paths, so the same path is checked repeatedly within one validation pass. In a warm parse of 5,112 recipes (poky, meta-yocto, meta-oe, meta-python), the real cache data shows 585,348 such calls against only 48,757 unique paths, with the hottest path checked 16,894
times: 536,591 calls are redundant. At 3.5us per os.path.exists() call on a nonexistent path on this machine, that is about 1.9s of avoidable syscall time per validation pass. Cache the result per Cache instance, which is discarded and recreated at the start of each validation pass, so a filesystem change between passes is still observed.

AI-Generated: Uses Claude Code (Claude Opus 5.5)

---
 lib/bb/cache.py | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
lib/bb/cache.py +4 -1
index 2361c5684..01020f844 100644
@@ -401,6 +401,8 @@ class Cache(object):
         self.clean = set()
         self.checked = set()
         self.depends_cache = {}
+        # os.path.exists() results for this validation pass (see file_exists())
+        self.exists_cache = {}
         self.data_fn = None
         self.cacheclean = True
         self.data_hash = data_hash
@@ -619,7 +621,8 @@ class Cache(object):
                     if not f:
                         continue
                     f, exist = f.rsplit(":", 1)
-                    if (exist == "True" and not os.path.exists(f)) or (exist == "False" and os.path.exists(f)):
+                    file_exists = self.file_exists(f)
+                    if (exist == "True" and not file_exists) or (exist == "False" and file_exists):
                         self.logger.debug2("%s's file checksum list file %s changed",
                                              fn, f)
                         self.remove(fn)

lib/bb/cache.py class Cache(object): · line 666

Quoted 7 lines
lib/bb/cache.py +4 -0
@@ -657,6 +660,11 @@ class Cache(object):
         self.clean.add(fn)
         return True
 
+    def file_exists(self, fn):
+        if fn not in self.exists_cache:
+            self.exists_cache[fn] = os.path.exists(fn)
+        return self.exists_cache[fn]

In the example given in the commit message this will result in a dictionary with 48,757 entries. With more layers included this could be larger still.

Would it make sense to use an LRU cache with a maximum size here, as you did in [1]?

[1]: https://lore.kernel.org/bitbake-devel/[email protected]/

Best regards,

Signature · 1 line
Paul Barker
#2 lore raw

Hi Paul,
in the commit with the LRU cache, it was a long-lived cache which could grow over time / over multiple builds (assuming a persistent server using BB_SERVER_TIMEOUT).
In this case, it is a cache instance with a bounded size which can't grow over time and is reset after each bitbake command.

I've measured the dict with 48K entries to use about 13MiB (using sys.getsizeof()), so I'm assuming it's acceptable even for bigger BSPs.

I think in this particular case I think it makes sense to keep it unbounded. I am using such a huge BSP with many layers myself and the slowness of bitbake in this use-case is my motivation for those patches. Do you have a use-case in mind where the size of the cache would be an issue?

Étienne

Quoted 73 lines · Paul Barker
Quoted 25 lines · On Wed, 2026-09-23 at 19:06 +0200, [email protected] wrote:

From: Etienne Cordonnier <[email protected]>

cacheValidUpdate() checks each recipe's file_checksums entries with os.path.exists(). Many recipes reference the same optional class or checksum candidate paths, so the same path is checked repeatedly within one validation pass. In a warm parse of 5,112 recipes (poky, meta-yocto, meta-oe, meta-python), the real cache data shows 585,348 such calls against only 48,757 unique paths, with the hottest path checked 16,894
times: 536,591 calls are redundant. At 3.5us per os.path.exists() call on a nonexistent path on this machine, that is about 1.9s of avoidable syscall time per validation pass. Cache the result per Cache instance, which is discarded and recreated at the start of each validation pass, so a filesystem change between passes is still observed.

AI-Generated: Uses Claude Code (Claude Opus 5.5)

---
 lib/bb/cache.py | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
lib/bb/cache.py +0 -0
index 2361c5684..01020f844 100644

lib/bb/cache.py class Cache(object): · line 404

Quoted 4 lines
lib/bb/cache.py +1 -0
@@ -401,6 +401,8 @@ class Cache(object):
         self.clean = set()
         self.checked = set()
         self.depends_cache = {}
+        # os.path.exists() results for this validation pass (see

file_exists())

lib/bb/cache.py class Cache(object): · line 622

Quoted 9 lines
lib/bb/cache.py +1 -1
+        self.exists_cache = {}
         self.data_fn = None
         self.cacheclean = True
         self.data_hash = data_hash
@@ -619,7 +621,8 @@ class Cache(object):
                     if not f:
                         continue
                     f, exist = f.rsplit(":", 1)
-                    if (exist == "True" and not os.path.exists(f)) or

(exist == "False" and os.path.exists(f)):

lib/bb/cache.py class Cache(object): · line 625

Quoted 2 lines
lib/bb/cache.py +2 -0
+                    file_exists = self.file_exists(f)
+                    if (exist == "True" and not file_exists) or (exist

== "False" and file_exists):

lib/bb/cache.py class Cache(object): · line 626

Quoted 1 line
lib/bb/cache.py +0 -0
                         self.logger.debug2("%s's file checksum list

file %s changed",

lib/bb/cache.py class Cache(object): · line 666

Quoted 10 lines
lib/bb/cache.py +4 -0
                                              fn, f)
                         self.remove(fn)
@@ -657,6 +660,11 @@ class Cache(object):
         self.clean.add(fn)
         return True
 
+    def file_exists(self, fn):
+        if fn not in self.exists_cache:
+            self.exists_cache[fn] = os.path.exists(fn)
+        return self.exists_cache[fn]

In the example given in the commit message this will result in a dictionary with 48,757 entries. With more layers included this could be larger still.

Would it make sense to use an LRU cache with a maximum size here, as you did in [1]?

[1]:
https://lore.kernel.org/bitbake-devel/[email protected]/

Best regards,

--
Paul Barker

#3 lore raw
In reply to [email protected]
Quoted 14 lines · Etienne Cordonnier via lists.openembedded.org

From: Etienne Cordonnier <[email protected]>

cacheValidUpdate() checks each recipe's file_checksums entries with os.path.exists(). Many recipes reference the same optional class or checksum candidate paths, so the same path is checked repeatedly within one validation pass. In a warm parse of 5,112 recipes (poky, meta-yocto, meta-oe, meta-python), the real cache data shows 585,348 such calls against only 48,757 unique paths, with the hottest path checked 16,894
times: 536,591 calls are redundant. At 3.5us per os.path.exists() call on a nonexistent path on this machine, that is about 1.9s of avoidable syscall time per validation pass. Cache the result per Cache instance, which is discarded and recreated at the start of each validation pass, so a filesystem change between passes is still observed.

Quoted 5 lines

AI-Generated: Uses Claude Code (Claude Opus 5.5)

---
 lib/bb/cache.py | 10 +++++++++-

 1 file changed, 9 insertions(+), 1 deletion(-)

I'm wondering if the existing mtime cache could be combined with this so we only hit both once. That might improve the times a bit further since any hit would then need the mtime data later anyway. I'd worry about lifecycle of the data less too since we already clear the mtime cache at the right times (I hope!).

Cheers,

Richard

#4 lore raw

Hi Richard,
I have sent "cache: Cache missing files in the mtime cache" with your idea. It indeed improves the times a bit further.

Étienne

Quoted 32 lines · Richard Purdie
Quoted 14 lines · Etienne Cordonnier via lists.openembedded.org

From: Etienne Cordonnier <[email protected]>

cacheValidUpdate() checks each recipe's file_checksums entries with os.path.exists(). Many recipes reference the same optional class or checksum candidate paths, so the same path is checked repeatedly within one validation pass. In a warm parse of 5,112 recipes (poky, meta-yocto, meta-oe, meta-python), the real cache data shows 585,348 such calls against only 48,757 unique paths, with the hottest path checked 16,894
times: 536,591 calls are redundant. At 3.5us per os.path.exists() call on a nonexistent path on this machine, that is about 1.9s of avoidable syscall time per validation pass. Cache the result per Cache instance, which is discarded and recreated at the start of each validation pass, so a filesystem change between passes is still observed.

Quoted 5 lines

AI-Generated: Uses Claude Code (Claude Opus 5.5)

---
 lib/bb/cache.py | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

I'm wondering if the existing mtime cache could be combined with this so we only hit both once. That might improve the times a bit further since any hit would then need the mtime data later anyway. I'd worry about lifecycle of the data less too since we already clear the mtime cache at the right times (I hope!).

Cheers,

Richard