Cache the results of the last sb_find_leaf operation.
authorRobert Haas <rhaas@postgresql.org>
Tue, 29 Apr 2014 19:30:36 +0000 (19:30 +0000)
committerRobert Haas <rhaas@postgresql.org>
Tue, 29 Apr 2014 19:30:36 +0000 (19:30 +0000)
If all the memory we're accessing is in the same 4GB segment of the address
space, this eliminates calls to sb_find_leaf altogether, a significant
savings.  But even if we're ranging across more than one segment, this
should win in cases that have some degree of access locality.

src/backend/utils/mmgr/sb_region.c

index 47ad11c266d5f5e85ea53091aa5566b60d65b242..1c5156337d152cfd47dbd994e7cb4b9a31ff0ec5 100644 (file)
@@ -219,12 +219,24 @@ sb_lookup_region(void *ptr)
 #if SIZEOF_SIZE_T > 4
        {
                Size    highbits = p >> 32;
+               static Size last_highbits = 0;
+               static sb_lookup_leaf *last_leaf = NULL;
 
-               leaf = sb_find_leaf(highbits, false);
+               /* Quick test to see if we're in same range as before. */
+               if (last_highbits == highbits && last_leaf != NULL)
+                       leaf = last_leaf;
+               else
+               {
+                       leaf = sb_find_leaf(highbits, false);
 
-               /* No lookup table for this 4GB range?  OK, no matching region. */
-               if (leaf == NULL)
-                       return NULL;
+                       /* No lookup table for this 4GB range?  OK, no matching region. */
+                       if (leaf == NULL)
+                               return NULL;
+
+                       /* Remember results of this lookup for next time. */
+                       last_highbits = highbits;
+                       last_leaf = leaf;
+               }
        }
 #else
        leaf = &lookup_root_leaf;