Changeset: 5519f87af1df for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/5519f87af1df
Modified Files:
        gdk/gdk.h
        gdk/gdk_bat.c
        gdk/gdk_batop.c
Branch: Jul2021
Log Message:

Use more bat iterators to access data.


diffs (truncated from 1012 to 300 lines):

diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -800,25 +800,6 @@ typedef struct BAT {
 #define tprops         T.props
 
 
-typedef struct BATiter {
-       BAT *b;
-       Heap *h;
-       void *base;
-       Heap *vh;
-       BUN count;
-       uint16_t width;
-       uint8_t shift;
-       int8_t type;
-       oid tseq;
-       union {
-               oid tvid;
-               bool tmsk;
-       };
-#ifndef NDEBUG
-       bool locked;
-#endif
-} BATiter;
-
 /* some access functions for the bitmask type */
 static inline void
 mskSet(BAT *b, BUN p)
@@ -890,6 +871,26 @@ gdk_export size_t HEAPmemsize(Heap *h);
 gdk_export void HEAPdecref(Heap *h, bool remove);
 gdk_export void HEAPincref(Heap *h);
 
+/* BAT iterator, also protects use of BAT heaps with reference counts */
+typedef struct BATiter {
+       BAT *b;
+       Heap *h;
+       void *base;
+       Heap *vh;
+       BUN count;
+       uint16_t width;
+       uint8_t shift;
+       int8_t type;
+       oid tseq;
+       union {
+               oid tvid;
+               bool tmsk;
+       };
+#ifndef NDEBUG
+       bool locked;
+#endif
+} BATiter;
+
 static inline BATiter
 bat_iterator(BAT *b)
 {
@@ -1114,6 +1115,8 @@ typedef var_t stridx_t;
 static inline oid
 BUNtoid(BAT *b, BUN p)
 {
+       oid o;
+
        assert(ATOMtype(b->ttype) == TYPE_oid);
        /* BATcount is the number of valid entries, so with
         * exceptions, the last value can well be larger than
@@ -1122,13 +1125,18 @@ BUNtoid(BAT *b, BUN p)
        assert(b->ttype == TYPE_void || b->tvheap == NULL);
        if (is_oid_nil(b->tseqbase)) {
                if (b->ttype == TYPE_void)
-                       return b->tseqbase;
-               return ((const oid *) b->theap->base)[p + b->tbaseoff];
+                       return b->tseqbase; /* i.e. oid_nil */
+               MT_lock_set(&b->theaplock);
+               o = ((const oid *) b->theap->base)[p + b->tbaseoff];
+               MT_lock_unset(&b->theaplock);
+               return o;
        }
-       oid o = b->tseqbase + p;
+       o = b->tseqbase + p;
        if (b->ttype == TYPE_oid || b->tvheap == NULL) {
                return o;
        }
+       /* b->tvheap != NULL, so we know there will be no parallel
+        * modifications (so no locking) */
        assert(!mask_cand(b));
        /* exceptions only allowed on transient BATs */
        assert(b->batRole == TRANSIENT);
@@ -2000,10 +2008,16 @@ Tpos(BATiter *bi, BUN p)
        return (void*)&bi->tvid;
 }
 
+static inline bool
+Tmskval(BATiter *bi, BUN p)
+{
+       return ((uint32_t *) bi->h->base)[p / 32] & (1U << (p % 32));
+}
+
 static inline void *
 Tmsk(BATiter *bi, BUN p)
 {
-       bi->tmsk = ((uint32_t *) bi->h->base)[p / 32] & (1U << (p % 32));
+       bi->tmsk = Tmskval(bi, p);
        return &bi->tmsk;
 }
 
diff --git a/gdk/gdk_bat.c b/gdk/gdk_bat.c
--- a/gdk/gdk_bat.c
+++ b/gdk/gdk_bat.c
@@ -793,12 +793,11 @@ BAT *
 COLcopy(BAT *b, int tt, bool writable, role_t role)
 {
        BUN bunstocopy = BUN_NONE;
-       BUN cnt;
        BAT *bn = NULL;
+       BATiter bi;
 
        BATcheck(b, NULL);
        assert(tt != TYPE_bat);
-       cnt = b->batCount;
 
        /* maybe a bit ugly to change the requested bat type?? */
        if (b->ttype == TYPE_void && !writable)
@@ -809,6 +808,8 @@ COLcopy(BAT *b, int tt, bool writable, r
                return NULL;
        }
 
+       bi = bat_iterator(b);
+
        /* first try case (1); create a view, possibly with different
         * atom-types */
        if (!writable &&
@@ -818,39 +819,41 @@ COLcopy(BAT *b, int tt, bool writable, r
            (!VIEWtparent(b) ||
             BBP_cache(VIEWtparent(b))->batRestricted == BAT_READ)) {
                bn = VIEWcreate(b->hseqbase, b);
-               if (bn == NULL)
+               if (bn == NULL) {
+                       bat_iterator_end(&bi);
                        return NULL;
+               }
                if (tt != bn->ttype) {
                        bn->ttype = tt;
                        bn->tvarsized = ATOMvarsized(tt);
-                       bn->tseqbase = ATOMtype(tt) == TYPE_oid ? b->tseqbase : 
oid_nil;
+                       bn->tseqbase = ATOMtype(tt) == TYPE_oid ? bi.tseq : 
oid_nil;
                }
        } else {
                /* check whether we need case (4); BUN-by-BUN copy (by
                 * setting bunstocopy != BUN_NONE) */
-               if (ATOMsize(tt) != ATOMsize(b->ttype)) {
+               if (ATOMsize(tt) != ATOMsize(bi.type)) {
                        /* oops, void materialization */
-                       bunstocopy = cnt;
+                       bunstocopy = bi.count;
                } else if (BATatoms[tt].atomFix) {
                        /* oops, we need to fix/unfix atoms */
-                       bunstocopy = cnt;
-               } else if (isVIEW(b)) {
+                       bunstocopy = bi.count;
+               } else if (bi.h && bi.h->parentid != b->batCacheid) {
                        /* extra checks needed for views */
-                       bat tp = VIEWtparent(b);
-
-                       if (tp != 0 && BATcapacity(BBP_cache(tp)) > cnt + cnt)
+                       if (BATcapacity(BBP_cache(bi.h->parentid)) > bi.count + 
bi.count)
                                /* reduced slice view: do not copy too
                                 * much garbage */
-                               bunstocopy = cnt;
+                               bunstocopy = bi.count;
                }
 
                bn = COLnew(b->hseqbase, tt, MAX(1, bunstocopy == BUN_NONE ? 0 
: bunstocopy), role);
-               if (bn == NULL)
+               if (bn == NULL) {
+                       bat_iterator_end(&bi);
                        return NULL;
+               }
 
                if (bn->tvarsized && bn->ttype && bunstocopy == BUN_NONE) {
-                       bn->tshift = b->tshift;
-                       bn->twidth = b->twidth;
+                       bn->tshift = bi.shift;
+                       bn->twidth = bi.width;
                        if (HEAPextend(bn->theap, BATcapacity(bn) << 
bn->tshift, true) != GDK_SUCCEED)
                                goto bunins_failed;
                }
@@ -865,11 +868,11 @@ COLcopy(BAT *b, int tt, bool writable, r
                        Heap bthp, thp;
 
                        bthp = (Heap) {
-                               .farmid = BBPselectfarm(role, b->ttype, 
offheap),
+                               .farmid = BBPselectfarm(role, bi.type, offheap),
                                .parentid = bn->batCacheid,
                        };
                        thp = (Heap) {
-                               .farmid = BBPselectfarm(role, b->ttype, 
varheap),
+                               .farmid = BBPselectfarm(role, bi.type, varheap),
                                .parentid = bn->batCacheid,
                        };
                        settailname(&bthp, BBP_physical(bn->batCacheid),
@@ -877,11 +880,12 @@ COLcopy(BAT *b, int tt, bool writable, r
                        strconcat_len(thp.filename, sizeof(thp.filename),
                                      BBP_physical(bn->batCacheid),
                                      ".theap", NULL);
-                       if ((b->ttype && HEAPcopy(&bthp, b->theap, b->tbaseoff 
<< b->tshift) != GDK_SUCCEED) ||
-                           (bn->tvheap && HEAPcopy(&thp, b->tvheap, 0) != 
GDK_SUCCEED)) {
+                       if ((bi.type && HEAPcopy(&bthp, bi.h, (size_t) ((char 
*) bi.base - bi.h->base)) != GDK_SUCCEED) ||
+                           (bn->tvheap && HEAPcopy(&thp, bi.vh, 0) != 
GDK_SUCCEED)) {
                                HEAPfree(&thp, true);
                                HEAPfree(&bthp, true);
                                BBPreclaim(bn);
+                               bat_iterator_end(&bi);
                                return NULL;
                        }
                        /* succeeded; replace dummy small heaps by the
@@ -900,7 +904,6 @@ COLcopy(BAT *b, int tt, bool writable, r
                } else if (BATatoms[tt].atomFix || tt != TYPE_void || 
ATOMextern(tt)) {
                        /* case (4): one-by-one BUN insert (really slow) */
                        BUN p, q, r = 0;
-                       BATiter bi = bat_iterator(b);
 
                        BATloop(b, p, q) {
                                const void *t = BUNtail(bi, p);
@@ -911,12 +914,11 @@ COLcopy(BAT *b, int tt, bool writable, r
                                }
                                r++;
                        }
-                       bat_iterator_end(&bi);
                        bn->theap->dirty |= bunstocopy > 0;
-               } else if (tt != TYPE_void && b->ttype == TYPE_void) {
+               } else if (tt != TYPE_void && bi.type == TYPE_void) {
                        /* case (4): optimized for unary void
                         * materialization */
-                       oid cur = b->tseqbase, *dst = (oid *) Tloc(bn, 0);
+                       oid cur = bi.tseq, *dst = (oid *) Tloc(bn, 0);
                        oid inc = !is_oid_nil(cur);
 
                        bn->theap->free = bunstocopy * sizeof(oid);
@@ -925,27 +927,27 @@ COLcopy(BAT *b, int tt, bool writable, r
                                *dst++ = cur;
                                cur += inc;
                        }
-               } else if (ATOMstorage(b->ttype) == TYPE_msk) {
+               } else if (ATOMstorage(bi.type) == TYPE_msk) {
                        /* convert number of bits to number of bytes,
                         * and round the latter up to a multiple of
                         * 4 (copy in units of 4 bytes) */
                        bn->theap->free = (bunstocopy + 7) / 8;
                        bn->theap->free = (bn->theap->free + 3) & ~(size_t)3;
                        bn->theap->dirty |= bunstocopy > 0;
-                       memcpy(Tloc(bn, 0), Tloc(b, 0), bn->theap->free);
+                       memcpy(Tloc(bn, 0), bi.base, bn->theap->free);
                } else {
                        /* case (4): optimized for simple array copy */
                        bn->theap->free = bunstocopy * Tsize(bn);
                        bn->theap->dirty |= bunstocopy > 0;
-                       memcpy(Tloc(bn, 0), Tloc(b, 0), bn->theap->free);
+                       memcpy(Tloc(bn, 0), bi.base, bn->theap->free);
                }
                /* copy all properties (size+other) from the source bat */
-               BATsetcount(bn, cnt);
+               BATsetcount(bn, bi.count);
        }
        /* set properties (note that types may have changed in the copy) */
-       if (ATOMtype(tt) == ATOMtype(b->ttype)) {
+       if (ATOMtype(tt) == ATOMtype(bi.type)) {
                if (ATOMtype(tt) == TYPE_oid) {
-                       BATtseqbase(bn, b->tseqbase);
+                       BATtseqbase(bn, bi.tseq);
                } else {
                        BATtseqbase(bn, oid_nil);
                }
@@ -1003,57 +1005,14 @@ COLcopy(BAT *b, int tt, bool writable, r
                bn->batRestricted = BAT_READ;
        TRC_DEBUG(ALGO, ALGOBATFMT " -> " ALGOBATFMT "\n",
                  ALGOBATPAR(b), ALGOBATPAR(bn));
+       bat_iterator_end(&bi);
        return bn;
       bunins_failed:
+       bat_iterator_end(&bi);
        BBPreclaim(bn);
        return NULL;
 }
 
-#ifdef HAVE_HGE
-#define un_move_sz16(src, dst, sz)                     \
-               if (sz == 16) {                         \
-                       * (hge *) dst = * (hge *) src;  \
-               } else
-#else
-#define un_move_sz16(src, dst, sz)                             \
-               if (sz == 16) {                                 \
-                       * (uuid *) dst = * (uuid *) src;        \
-               } else
-#endif
-
-#define un_move(src, dst, sz)                          \
-       do {                                            \
-               un_move_sz16(src,dst,sz)                \
-               if (sz == 8) {                          \
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to