diff mbox series

[3/6] cache: Only write files if we have data

Message ID 20230121212305.2171310-3-richard.purdie@linuxfoundation.org
State Accepted, archived
Commit abeff1f80bb1c690b92d535d472dff9df7a56067
Headers show
Series [1/6] server/process: Fix lockfile contents check bug | expand

Commit Message

Richard Purdie Jan. 21, 2023, 9:23 p.m. UTC
By writing the cache files only if there is data to write, we can save a
bit of time.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 lib/bb/cache.py | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

Comments

Christopher Larson Jan. 21, 2023, 9:42 p.m. UTC | #1
Hmm have_data = any(self.cachedata_extras) perhaps?

Sent from my iPhone

> On Jan 21, 2023, at 2:23 PM, Richard Purdie <richard.purdie@linuxfoundation.org> wrote:
> 
> By writing the cache files only if there is data to write, we can save a
> bit of time.
> 
> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
> ---
> lib/bb/cache.py | 18 +++++++++++++++---
> 1 file changed, 15 insertions(+), 3 deletions(-)
> 
> diff --git a/lib/bb/cache.py b/lib/bb/cache.py
> index ee924b2d2b..c19fe26f1b 100644
> --- a/lib/bb/cache.py
> +++ b/lib/bb/cache.py
> @@ -873,6 +873,14 @@ class MultiProcessCache(object):
>         if not self.cachefile:
>             return
> 
> +        have_data = False
> +        for c in self.cachedata_extras:
> +            if c:
> +                have_data = True
> +                break
> +        if not have_data:
> +            return
> +
>         glf = bb.utils.lockfile(self.cachefile + ".lock", shared=True)
> 
>         i = os.getpid()
> @@ -907,6 +915,8 @@ class MultiProcessCache(object):
> 
>         data = self.cachedata
> 
> +        have_data = False
> +
>         for f in [y for y in os.listdir(os.path.dirname(self.cachefile)) if y.startswith(os.path.basename(self.cachefile) + '-')]:
>             f = os.path.join(os.path.dirname(self.cachefile), f)
>             try:
> @@ -921,12 +931,14 @@ class MultiProcessCache(object):
>                 os.unlink(f)
>                 continue
> 
> +            have_data = True
>             self.merge_data(extradata, data)
>             os.unlink(f)
> 
> -        with open(self.cachefile, "wb") as f:
> -            p = pickle.Pickler(f, -1)
> -            p.dump([data, self.__class__.CACHE_VERSION])
> +        if have_data:
> +            with open(self.cachefile, "wb") as f:
> +                p = pickle.Pickler(f, -1)
> +                p.dump([data, self.__class__.CACHE_VERSION])
> 
>         bb.utils.unlockfile(glf)
> 
> -- 
> 2.37.2
> 
> 
> -=-=-=-=-=-=-=-=-=-=-=-
> Links: You receive all messages sent to this group.
> View/Reply Online (#14331): https://lists.openembedded.org/g/bitbake-devel/message/14331
> Mute This Topic: https://lists.openembedded.org/mt/96443149/3617123
> Group Owner: bitbake-devel+owner@lists.openembedded.org
> Unsubscribe: https://lists.openembedded.org/g/bitbake-devel/unsub [kergoth@gmail.com]
> -=-=-=-=-=-=-=-=-=-=-=-
>
Richard Purdie Jan. 21, 2023, 10:15 p.m. UTC | #2
On Sat, 2023-01-21 at 14:42 -0700, Christopher Larson wrote:
> Hmm have_data = any(self.cachedata_extras) perhaps?

That looks a lot neater! :)

Cheers,

Richard
diff mbox series

Patch

diff --git a/lib/bb/cache.py b/lib/bb/cache.py
index ee924b2d2b..c19fe26f1b 100644
--- a/lib/bb/cache.py
+++ b/lib/bb/cache.py
@@ -873,6 +873,14 @@  class MultiProcessCache(object):
         if not self.cachefile:
             return
 
+        have_data = False
+        for c in self.cachedata_extras:
+            if c:
+                have_data = True
+                break
+        if not have_data:
+            return
+
         glf = bb.utils.lockfile(self.cachefile + ".lock", shared=True)
 
         i = os.getpid()
@@ -907,6 +915,8 @@  class MultiProcessCache(object):
 
         data = self.cachedata
 
+        have_data = False
+
         for f in [y for y in os.listdir(os.path.dirname(self.cachefile)) if y.startswith(os.path.basename(self.cachefile) + '-')]:
             f = os.path.join(os.path.dirname(self.cachefile), f)
             try:
@@ -921,12 +931,14 @@  class MultiProcessCache(object):
                 os.unlink(f)
                 continue
 
+            have_data = True
             self.merge_data(extradata, data)
             os.unlink(f)
 
-        with open(self.cachefile, "wb") as f:
-            p = pickle.Pickler(f, -1)
-            p.dump([data, self.__class__.CACHE_VERSION])
+        if have_data:
+            with open(self.cachefile, "wb") as f:
+                p = pickle.Pickler(f, -1)
+                p.dump([data, self.__class__.CACHE_VERSION])
 
         bb.utils.unlockfile(glf)