Merge branch '21639-keep-cache-dict' refs #21639
[arvados.git] / sdk / python / arvados / diskcache.py
index 1e885c15e2146ea9a0c2cf67a1fb333617729138..528a7d28b58146af1a33eac0ada4b746a9eaa12d 100644 (file)
@@ -32,7 +32,14 @@ class DiskCacheSlot(object):
 
     def get(self):
         self.ready.wait()
-        if isinstance(self.content, mmap.mmap):
+        # 'content' can None, an empty byte string, or a nonempty mmap
+        # region.  If it is an mmap region, we want to advise the
+        # kernel we're going to use it.  This nudges the kernel to
+        # re-read most or all of the block if necessary (instead of
+        # just a few pages at a time), reducing the number of page
+        # faults and improving performance by 4x compared to not
+        # calling madvise.
+        if self.content:
             self.content.madvise(mmap.MADV_WILLNEED)
         return self.content
 
@@ -84,7 +91,6 @@ class DiskCacheSlot(object):
                     os.remove(tmpfile)
                 except:
                     pass
-        return False
 
     def size(self):
         if self.content is None:
@@ -100,7 +106,7 @@ class DiskCacheSlot(object):
             return len(self.content)
 
     def evict(self):
-        if self.content is None or len(self.content) == 0:
+        if not self.content:
             return
 
         # The mmap region might be in use when we decided to evict
@@ -238,7 +244,7 @@ class DiskCacheSlot(object):
 
         # Map in all the files we found, up to maxslots, if we exceed
         # maxslots, start throwing things out.
-        cachelist = collections.OrderedDict()
+        cachelist: collections.OrderedDict = collections.OrderedDict()
         for b in blocks:
             got = DiskCacheSlot.get_from_disk(b[0], cachedir)
             if got is None: