Changeset: 210eb5577f63 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/210eb5577f63
Modified Files:
        gdk/ChangeLog
        gdk/gdk.h
        gdk/gdk_bat.c
        gdk/gdk_bbp.c
        gdk/gdk_logger.c
        gdk/gdk_private.h
        gdk/gdk_rtree.c
        gdk/gdk_select.c
        gdk/gdk_storage.c
        gdk/gdk_tm.c
        monetdb5/mal/mal_profiler.c
        monetdb5/mal/mal_resource.c
        monetdb5/modules/mal/bbp.c
        sql/backends/monet5/sql.c
        sql/backends/monet5/sql_cat.c
        sql/backends/monet5/sql_orderidx.c
Branch: default
Log Message:

Move BAT descriptor into BBPrec structure.
This means, no more separate malloc for each BAT that gets allocated,
and also, BBP_desc(batid) always results in a valid pointer.  If the BAT
is unallocated, its batCacheid value is 0.


diffs (truncated from 709 to 300 lines):

diff --git a/gdk/ChangeLog b/gdk/ChangeLog
--- a/gdk/ChangeLog
+++ b/gdk/ChangeLog
@@ -2,6 +2,8 @@
 # This file is updated with Maddlog
 
 * Tue Mar 26 2024 Sjoerd Mullender <sjo...@acm.org>
+- Made some changes to how BAT descriptors are allocated.  They are now
+  allocated in bulk, meaning fewer malloc/free calls during processing.
 - Removed macro BBP_cache and its associated code.  Checking whether a
   BAT is cached (loaded in memory) can be done by checking the BBPLOADED
   bit in the BBP_status value.  Getting a pointer to the BAT descriptor
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -958,7 +958,7 @@ gdk_export void HEAPincref(Heap *h);
 typedef struct {
        char *logical;          /* logical name (may point at bak) */
        char bak[16];           /* logical name backup (tmp_%o) */
-       BAT *desc;              /* the BAT descriptor */
+       BAT descr;              /* the BAT descriptor */
        char *options;          /* A string list of options */
 #if SIZEOF_VOID_P == 4
        char physical[20];      /* dir + basename for storage */
@@ -997,7 +997,7 @@ gdk_export BBPrec *BBP[N_BBPINIT];
 #define BBP_next(i)    BBP_record(i).next
 #define BBP_physical(i)        BBP_record(i).physical
 #define BBP_options(i) BBP_record(i).options
-#define BBP_desc(i)    BBP_record(i).desc
+#define BBP_desc(i)    (&BBP_record(i).descr)
 #define BBP_refs(i)    BBP_record(i).refs
 #define BBP_lrefs(i)   BBP_record(i).lrefs
 #define BBP_status(i)  ((unsigned) ATOMIC_GET(&BBP_record(i).status))
diff --git a/gdk/gdk_bat.c b/gdk/gdk_bat.c
--- a/gdk/gdk_bat.c
+++ b/gdk/gdk_bat.c
@@ -58,22 +58,49 @@
 BAT *
 BATcreatedesc(oid hseq, int tt, bool heapnames, role_t role, uint16_t width)
 {
+       bat bid;
        BAT *bn;
+       Heap *h = NULL, *vh = NULL;
 
        /*
         * Alloc space for the BAT and its dependent records.
         */
        assert(tt >= 0);
 
-       bn = GDKmalloc(sizeof(BAT));
+       if (heapnames) {
+               if ((h = GDKmalloc(sizeof(Heap))) == NULL) {
+                       return NULL;
+               }
+               *h = (Heap) {
+                       .farmid = BBPselectfarm(role, tt, offheap),
+                       .dirty = true,
+               };
 
-       if (bn == NULL)
+               if (ATOMneedheap(tt)) {
+                       if ((vh = GDKmalloc(sizeof(Heap))) == NULL) {
+                               GDKfree(h);
+                               return NULL;
+                       }
+                       *vh = (Heap) {
+                               .farmid = BBPselectfarm(role, tt, varheap),
+                               .dirty = true,
+                       };
+               }
+       }
+
+       bid = BBPallocbat(tt);
+       if (bid == 0) {
+               GDKfree(h);
+               GDKfree(vh);
                return NULL;
+       }
+       bn = BBP_desc(bid);
 
        /*
         * Fill in basic column info
         */
        *bn = (BAT) {
+               .batCacheid = bid,
                .hseqbase = hseq,
 
                .ttype = tt,
@@ -90,39 +117,11 @@ BATcreatedesc(oid hseq, int tt, bool hea
                .batRole = role,
                .batTransient = true,
                .batRestricted = BAT_WRITE,
+               .theap = h,
+               .tvheap = vh,
+               .creator_tid = MT_getpid(),
        };
 
-       if (heapnames) {
-               if ((bn->theap = GDKmalloc(sizeof(Heap))) == NULL) {
-                       GDKfree(bn);
-                       return NULL;
-               }
-               *bn->theap = (Heap) {
-                       .farmid = BBPselectfarm(role, bn->ttype, offheap),
-                       .dirty = true,
-               };
-
-               if (ATOMneedheap(tt)) {
-                       if ((bn->tvheap = GDKmalloc(sizeof(Heap))) == NULL) {
-                               GDKfree(bn->theap);
-                               GDKfree(bn);
-                               return NULL;
-                       }
-                       *bn->tvheap = (Heap) {
-                               .farmid = BBPselectfarm(role, bn->ttype, 
varheap),
-                               .dirty = true,
-                       };
-               }
-       }
-       /*
-        * add to BBP
-        */
-       if (BBPinsert(bn) == 0) {
-               GDKfree(bn->tvheap);
-               GDKfree(bn->theap);
-               GDKfree(bn);
-               return NULL;
-       }
        if (bn->theap) {
                bn->theap->parentid = bn->batCacheid;
                ATOMIC_INIT(&bn->theap->refs, 1);
@@ -729,7 +728,9 @@ BATdestroy(BAT *b)
                HEAPdecref(b->oldtail, false);
                b->oldtail = NULL;
        }
-       GDKfree(b);
+       *b = (BAT) {
+               .batCacheid = 0,
+       };
 }
 
 /*
diff --git a/gdk/gdk_bbp.c b/gdk/gdk_bbp.c
--- a/gdk/gdk_bbp.c
+++ b/gdk/gdk_bbp.c
@@ -797,7 +797,8 @@ BBPreadEntries(FILE *fp, unsigned bbpver
                        }
                        ATOMIC_SET(&BBPsize, b.batCacheid + 1);
                }
-               if (BBP_desc(b.batCacheid) != NULL) {
+               BAT *bn = BBP_desc(b.batCacheid);
+               if (bn->batCacheid != 0) {
                        GDKfree(options);
                        TRC_CRITICAL(GDK, "duplicate entry in BBP.dir (ID = "
                                     "%d) on line %d.", b.batCacheid, lineno);
@@ -817,11 +818,8 @@ BBPreadEntries(FILE *fp, unsigned bbpver
                }
 #endif
 
-               BAT *bn;
                Heap *hn;
-               if ((bn = GDKmalloc(sizeof(BAT))) == NULL ||
-                   (hn = GDKmalloc(sizeof(Heap))) == NULL) {
-                       GDKfree(bn);
+               if ((hn = GDKmalloc(sizeof(Heap))) == NULL) {
                        GDKfree(options);
                        TRC_CRITICAL(GDK, "cannot allocate memory for BAT.");
                        goto bailout;
@@ -834,7 +832,6 @@ BBPreadEntries(FILE *fp, unsigned bbpver
                        assert(b.tvheap == &vh);
                        if ((vhn = GDKmalloc(sizeof(Heap))) == NULL) {
                                GDKfree(hn);
-                               GDKfree(bn);
                                GDKfree(options);
                                TRC_CRITICAL(GDK, "cannot allocate memory for 
BAT.");
                                goto bailout;
@@ -888,7 +885,6 @@ BBPreadEntries(FILE *fp, unsigned bbpver
                BBP_options(b.batCacheid) = options;
                BBP_refs(b.batCacheid) = 0;
                BBP_lrefs(b.batCacheid) = 1;    /* any BAT we encounter here is 
persistent, so has a logical reference */
-               BBP_desc(b.batCacheid) = bn;
                BBP_pid(b.batCacheid) = 0;
                BBP_status_set(b.batCacheid, BBPEXISTING);
                if (BBPnamecheck(BBP_logical(b.batCacheid)) == 0)
@@ -915,11 +911,8 @@ BBPcheckbats(unsigned bbpversion)
                BAT *b;
                char *path;
 
-               if ((b = BBP_desc(bid)) == NULL) {
-                       /* not a valid BAT */
-                       continue;
-               }
-               if (b->ttype == TYPE_void) {
+               b = BBP_desc(bid);
+               if (b->batCacheid == 0 || b->ttype == TYPE_void) {
                        /* no files needed */
                        continue;
                }
@@ -1442,8 +1435,8 @@ fixhashash(bat *hashbats, bat nhashbats)
 {
        for (bat i = 0; i < nhashbats; i++) {
                bat bid = hashbats[i];
-               BAT *b;
-               if ((b = BBP_desc(bid)) == NULL) {
+               BAT *b = BBP_desc(bid);
+               if (b->batCacheid == 0) {
                        /* not a valid BAT (shouldn't happen) */
                        continue;
                }
@@ -1460,7 +1453,7 @@ movestrbats(void)
 {
        for (bat bid = 1, nbat = (bat) ATOMIC_GET(&BBPsize); bid < nbat; bid++) 
{
                BAT *b = BBP_desc(bid);
-               if (b == NULL) {
+               if (b->batCacheid == 0) {
                        /* not a valid BAT */
                        continue;
                }
@@ -1693,7 +1686,6 @@ gdk_return
 BBPjson_upgrade(json_storage_conversion fixJSONStorage)
 {
        bat bid;
-       BAT *b;
        int JSON_type = ATOMindex("json");
        bat nbat = (bat) ATOMIC_GET(&BBPsize);
        bat *upd = GDKmalloc(sizeof(bat) * (size_t) nbat);
@@ -1708,7 +1700,8 @@ BBPjson_upgrade(json_storage_conversion 
        BBPlock();
 
        for (bid = 1; bid < nbat; bid++) {
-               if ((b = BBP_desc(bid)) == NULL) {
+               BAT *b = BBP_desc(bid);
+               if (b->batCacheid == 0) {
                        /* not a valid BAT */
                        continue;
                }
@@ -1761,7 +1754,7 @@ BBPtrim(bool aggressive, bat nbat)
                if ((BBP_status(bid) & (flag | BBPLOADED)) == BBPLOADED &&
                    BBP_refs(bid) == 0 &&
                    BBP_lrefs(bid) != 0 &&
-                   (b = BBP_desc(bid)) != NULL) {
+                   (b = BBP_desc(bid))->batCacheid != 0) {
                        MT_lock_set(&b->theaplock);
                        if (!BATshared(b) &&
                            !isVIEW(b) &&
@@ -1994,7 +1987,7 @@ BBPinit(bool allow_hge_upgrade)
        /* remove trailing free bats from potential free list (they will
         * get added when needed) */
        for (bat i = (bat) ATOMIC_GET(&BBPsize) - 1; i > 0; i--) {
-               if (BBP_desc(i) != NULL)
+               if (BBP_desc(i)->batCacheid != 0)
                        break;
                bbpsize--;
        }
@@ -2003,7 +1996,7 @@ BBPinit(bool allow_hge_upgrade)
        /* add free bats to free list in such a way that low numbered
         * ones are at the head of the list */
        for (bat i = (bat) ATOMIC_GET(&BBPsize) - 1; i > 0; i--) {
-               if (BBP_desc(i) == NULL) {
+               if (BBP_desc(i)->batCacheid == 0) {
                        BBP_next(i) = BBP_free;
                        BBP_free = i;
                }
@@ -2206,7 +2199,7 @@ BBPexit(void)
                        if (BBPvalid(i)) {
                                BAT *b = BBP_desc(i);
 
-                               if (b) {
+                               if (b->batCacheid != 0) {
                                        if (BATshared(b)) {
                                                skipped = true;
                                                continue;
@@ -2565,7 +2558,7 @@ BBPdump(void)
                       BBP_lrefs(i),
                       status,
                       status & BBPLOADED ? "" : " not cached");
-               if (b == NULL) {
+               if (b->batCacheid == 0) {
                        printf(", no descriptor\n");
                        continue;
                }
@@ -2701,7 +2694,7 @@ maybeextend(void)
 
 /* return new BAT id (> 0); return 0 on failure */
 bat
-BBPinsert(BAT *bn)
+BBPallocbat(int tt)
 {
        MT_Id pid = MT_getpid();
        bool lock = locked_by == 0 || locked_by != pid;
@@ -2760,12 +2753,8 @@ BBPinsert(BAT *bn)
 
        /* fill in basic BBP fields for the new bat */
 
-       bn->batCacheid = i;
-       bn->creator_tid = pid;
-
        MT_lock_set(&GDKswapLock(i));
        BBP_status_set(i, BBPDELETING|BBPHOT);
-       BBP_desc(i) = bn;
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to