Changeset: a32e3210a89f for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/a32e3210a89f
Modified Files:
        clients/Tests/exports.stable.out
        gdk/gdk.h
        gdk/gdk_bat.c
        gdk/gdk_batop.c
        monetdb5/modules/kernel/aggr.c
Branch: resource_management
Log Message:

Merge with default branch.


diffs (truncated from 348 to 300 lines):

diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out
--- a/clients/Tests/exports.stable.out
+++ b/clients/Tests/exports.stable.out
@@ -244,6 +244,7 @@ gdk_return BUNreplace(BAT *b, oid left, 
 gdk_return BUNreplacemulti(BAT *b, const oid *positions, const void *values, 
BUN count, bool force) __attribute__((__warn_unused_result__));
 gdk_return BUNreplacemultiincr(BAT *b, oid position, const void *values, BUN 
count, bool force) __attribute__((__warn_unused_result__));
 BAT *COLcopy(BAT *b, int tt, bool writable, role_t role);
+BAT *COLcopy2(BAT *b, int tt, bool writable, bool mayshare, role_t role);
 BAT *COLnew(oid hseq, int tltype, BUN capacity, role_t role) 
__attribute__((__warn_unused_result__));
 BAT *COLnew2(oid hseq, int tt, BUN cap, role_t role, uint16_t width) 
__attribute__((__warn_unused_result__));
 size_t GDK_mem_maxsize;
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -849,6 +849,7 @@ gdk_export restrict_t BATgetaccess(BAT *
 
 gdk_export gdk_return BATclear(BAT *b, bool force);
 gdk_export BAT *COLcopy(BAT *b, int tt, bool writable, role_t role);
+gdk_export BAT *COLcopy2(BAT *b, int tt, bool writable, bool mayshare, role_t 
role);
 
 gdk_export gdk_return BATgroup(BAT **groups, BAT **extents, BAT **histo, BAT 
*b, BAT *s, BAT *g, BAT *e, BAT *h)
        __attribute__((__access__(write_only, 1)))
diff --git a/gdk/gdk_align.c b/gdk/gdk_align.c
--- a/gdk/gdk_align.c
+++ b/gdk/gdk_align.c
@@ -213,8 +213,7 @@ VIEWcreate(oid seq, BAT *b, BUN l, BUN h
 
 /*
  * The BATmaterialize routine produces in-place materialized version
- * of a void bat (which should not be a VIEW) (later we should add the
- * code for VIEWs).
+ * of a void bat.
  */
 
 gdk_return
@@ -226,7 +225,6 @@ BATmaterialize(BAT *b, BUN cap)
        oid t, *x;
 
        BATcheck(b, GDK_FAIL);
-       assert(!isVIEW(b));
        if (cap == BUN_NONE || cap < BATcapacity(b))
                cap = BATcapacity(b);
        MT_lock_set(&b->theaplock);
diff --git a/gdk/gdk_bat.c b/gdk/gdk_bat.c
--- a/gdk/gdk_bat.c
+++ b/gdk/gdk_bat.c
@@ -634,7 +634,7 @@ wrongtype(int t1, int t2)
  * do inline array[T] inserts.
  */
 BAT *
-COLcopy(BAT *b, int tt, bool writable, role_t role)
+COLcopy2(BAT *b, int tt, bool writable, bool mayshare, role_t role)
 {
        bool slowcopy = false;
        BAT *bn = NULL;
@@ -643,6 +643,11 @@ COLcopy(BAT *b, int tt, bool writable, r
 
        BATcheck(b, NULL);
 
+       /* can't share vheap when persistent */
+       assert(!mayshare || role == TRANSIENT);
+       if (mayshare && role != TRANSIENT)
+               mayshare = false;
+
        /* maybe a bit ugly to change the requested bat type?? */
        if (b->ttype == TYPE_void && !writable)
                tt = TYPE_void;
@@ -706,14 +711,17 @@ COLcopy(BAT *b, int tt, bool writable, r
                }
                bat_iterator_end(&bi);
                return bn;
-       } else {
-               /* check whether we need case (4); BUN-by-BUN copy (by
-                * setting slowcopy to true) */
-               if (ATOMsize(tt) != ATOMsize(bi.type)) {
-                       /* oops, void materialization */
-                       slowcopy = true;
-               } else if (bi.h && bi.h->parentid != b->batCacheid &&
-                          BATcapacity(BBP_desc(bi.h->parentid)) > bi.count + 
bi.count) {
+       }
+
+       /* check whether we need case (4); BUN-by-BUN copy (by
+        * setting slowcopy to true) */
+       if (ATOMsize(tt) != ATOMsize(bi.type)) {
+               /* oops, void materialization */
+               slowcopy = true;
+               mayshare = false;
+       } else if (!mayshare) {
+               if (bi.h && bi.h->parentid != b->batCacheid &&
+                   BATcapacity(BBP_desc(bi.h->parentid)) > bi.count + 
bi.count) {
                        /* reduced slice view: do not copy too much
                         * garbage */
                        slowcopy = true;
@@ -726,33 +734,36 @@ COLcopy(BAT *b, int tt, bool writable, r
                         * of the parent's offset heap */
                        slowcopy = true;
                }
+       }
 
-               bn = COLnew2(b->hseqbase, tt, bi.count, role, bi.width);
-               if (bn == NULL) {
+       bn = COLnew2(b->hseqbase, tt, bi.count, role, bi.width);
+       if (bn == NULL) {
+               goto bunins_failed;
+       }
+       if (bn->tvheap != NULL && bn->tvheap->base == NULL && !mayshare) {
+               /* this combination can happen since the last
+                * argument of COLnew2 not being zero triggers a
+                * skip in the allocation of the tvheap */
+               if (ATOMheap(bn->ttype, bn->tvheap, bn->batCapacity) != 
GDK_SUCCEED) {
                        goto bunins_failed;
                }
-               if (bn->tvheap != NULL && bn->tvheap->base == NULL) {
-                       /* this combination can happen since the last
-                        * argument of COLnew2 not being zero triggers a
-                        * skip in the allocation of the tvheap */
-                       if (ATOMheap(bn->ttype, bn->tvheap, bn->batCapacity) != 
GDK_SUCCEED) {
-                               goto bunins_failed;
-                       }
-               }
+       }
 
-               if (tt == TYPE_void) {
-                       /* case (2): a void,void result => nothing to
-                        * copy! */
-                       bn->theap->free = 0;
-               } else if (!slowcopy) {
-                       /* case (3): just copy the heaps */
-                       if (bn->tvheap && HEAPextend(bn->tvheap, bi.vhfree, 
true) != GDK_SUCCEED) {
-                               goto bunins_failed;
-                       }
-                       memcpy(bn->theap->base, bi.base, bi.hfree);
-                       bn->theap->free = bi.hfree;
-                       bn->theap->dirty = true;
-                       if (bn->tvheap) {
+       if (tt == TYPE_void) {
+               /* case (2): a void,void result => nothing to
+                * copy! */
+               bn->theap->free = 0;
+       } else if (!slowcopy) {
+               /* case (3): just copy the heaps */
+               if (bn->tvheap) {
+                       if (mayshare) {
+                               HEAPincref(bi.vh);
+                               HEAPdecref(bn->tvheap, true);
+                               BBPretain(bi.vh->parentid);
+                               bn->tvheap = bi.vh;
+                       } else {
+                               if (HEAPextend(bn->tvheap, bi.vhfree, true) != 
GDK_SUCCEED)
+                                       goto bunins_failed;
                                memcpy(bn->tvheap->base, bi.vh->base, 
bi.vhfree);
                                bn->tvheap->free = bi.vhfree;
                                bn->tvheap->dirty = true;
@@ -760,55 +771,59 @@ COLcopy(BAT *b, int tt, bool writable, r
                                if (ATOMstorage(b->ttype) == TYPE_str && 
bi.vhfree >= GDK_STRHASHSIZE)
                                        memcpy(bn->tvheap->base, strhash, 
GDK_STRHASHSIZE);
                        }
+               }
+               memcpy(bn->theap->base, bi.base, bi.hfree);
+               bn->theap->free = bi.hfree;
+               bn->theap->dirty = true;
 
-                       /* make sure we use the correct capacity */
-                       if (ATOMstorage(bn->ttype) == TYPE_msk)
-                               bn->batCapacity = (BUN) (bn->theap->size * 8);
-                       else if (bn->ttype)
-                               bn->batCapacity = (BUN) (bn->theap->size >> 
bn->tshift);
-                       else
-                               bn->batCapacity = 0;
-               } else if (tt != TYPE_void || ATOMextern(tt)) {
-                       /* case (4): one-by-one BUN insert (really slow) */
-                       QryCtx *qry_ctx = MT_thread_get_qry_ctx();
+               /* make sure we use the correct capacity */
+               if (ATOMstorage(bn->ttype) == TYPE_msk)
+                       bn->batCapacity = (BUN) (bn->theap->size * 8);
+               else if (bn->ttype)
+                       bn->batCapacity = (BUN) (bn->theap->size >> bn->tshift);
+               else
+                       bn->batCapacity = 0;
+       } else if (tt != TYPE_void || ATOMextern(tt)) {
+               /* case (4): one-by-one BUN insert (really slow) */
+               QryCtx *qry_ctx = MT_thread_get_qry_ctx();
 
-                       TIMEOUT_LOOP_IDX_DECL(p, bi.count, qry_ctx) {
-                               const void *t = BUNtail(bi, p);
+               TIMEOUT_LOOP_IDX_DECL(p, bi.count, qry_ctx) {
+                       const void *t = BUNtail(bi, p);
 
-                               if (bunfastapp_nocheck(bn, t) != GDK_SUCCEED) {
-                                       goto bunins_failed;
-                               }
+                       if (bunfastapp_nocheck(bn, t) != GDK_SUCCEED) {
+                               goto bunins_failed;
                        }
-                       TIMEOUT_CHECK(qry_ctx, 
GOTO_LABEL_TIMEOUT_HANDLER(bunins_failed, qry_ctx));
-                       bn->theap->dirty |= bi.count > 0;
-               } else if (tt != TYPE_void && bi.type == TYPE_void) {
-                       /* case (4): optimized for unary void
-                        * materialization */
-                       oid cur = bi.tseq, *dst = (oid *) Tloc(bn, 0);
-                       const oid inc = !is_oid_nil(cur);
+               }
+               TIMEOUT_CHECK(qry_ctx, 
GOTO_LABEL_TIMEOUT_HANDLER(bunins_failed, qry_ctx));
+               bn->theap->dirty |= bi.count > 0;
+       } else if (tt != TYPE_void && bi.type == TYPE_void) {
+               /* case (4): optimized for unary void
+                * materialization */
+               oid cur = bi.tseq, *dst = (oid *) Tloc(bn, 0);
+               const oid inc = !is_oid_nil(cur);
 
-                       for (BUN p = 0; p < bi.count; p++) {
-                               dst[p] = cur;
-                               cur += inc;
-                       }
-                       bn->theap->free = bi.count * sizeof(oid);
-                       bn->theap->dirty |= bi.count > 0;
-               } 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 = ((bi.count + 31) / 32) * 4;
-                       memcpy(Tloc(bn, 0), bi.base, bn->theap->free);
-                       bn->theap->dirty |= bi.count > 0;
-               } else {
-                       /* case (4): optimized for simple array copy */
-                       bn->theap->free = bi.count << bn->tshift;
-                       memcpy(Tloc(bn, 0), bi.base, bn->theap->free);
-                       bn->theap->dirty |= bi.count > 0;
+               for (BUN p = 0; p < bi.count; p++) {
+                       dst[p] = cur;
+                       cur += inc;
                }
-               /* copy all properties (size+other) from the source bat */
-               BATsetcount(bn, bi.count);
+               bn->theap->free = bi.count * sizeof(oid);
+               bn->theap->dirty |= bi.count > 0;
+       } 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 = ((bi.count + 31) / 32) * 4;
+               memcpy(Tloc(bn, 0), bi.base, bn->theap->free);
+               bn->theap->dirty |= bi.count > 0;
+       } else {
+               /* case (4): optimized for simple array copy */
+               bn->theap->free = bi.count << bn->tshift;
+               memcpy(Tloc(bn, 0), bi.base, bn->theap->free);
+               bn->theap->dirty |= bi.count > 0;
        }
+       /* copy all properties (size+other) from the source bat */
+       BATsetcount(bn, bi.count);
+
        /* set properties (note that types may have changed in the copy) */
        if (ATOMtype(tt) == ATOMtype(bi.type)) {
                if (ATOMtype(tt) == TYPE_oid) {
@@ -880,12 +895,18 @@ COLcopy(BAT *b, int tt, bool writable, r
        TRC_DEBUG(ALGO, ALGOBATFMT " -> " ALGOBATFMT "\n",
                  ALGOBATPAR(b), ALGOBATPAR(bn));
        return bn;
-      bunins_failed:
+  bunins_failed:
        bat_iterator_end(&bi);
        BBPreclaim(bn);
        return NULL;
 }
 
+BAT *
+COLcopy(BAT *b, int tt, bool writable, role_t role)
+{
+       return COLcopy2(b, tt, writable, false, role);
+}
+
 /* Append an array of values of length count to the bat.  For
  * fixed-sized values, `values' is an array of values, for
  * variable-sized values, `values' is an array of pointers to values.
diff --git a/gdk/gdk_batop.c b/gdk/gdk_batop.c
--- a/gdk/gdk_batop.c
+++ b/gdk/gdk_batop.c
@@ -2703,7 +2703,7 @@ BATsort(BAT **sorted, BAT **order, BAT *
                if (bn == NULL)
                        goto error;
                if (bn->ttype == TYPE_void || isVIEW(bn)) {
-                       BAT *b2 = COLcopy(bn, ATOMtype(bn->ttype), true, 
TRANSIENT);
+                       BAT *b2 = COLcopy2(bn, ATOMtype(bn->ttype), true, true, 
TRANSIENT);
                        BBPunfix(bn->batCacheid);
                        bn = b2;
                }
@@ -2715,7 +2715,7 @@ BATsort(BAT **sorted, BAT **order, BAT *
                        pb = NULL;
                }
        } else {
-               bn = COLcopy(b, b->ttype, true, TRANSIENT);
+               bn = COLcopy2(b, b->ttype, true, true, TRANSIENT);
        }
        if (bn == NULL)
                goto error;
diff --git a/monetdb5/modules/kernel/aggr.c b/monetdb5/modules/kernel/aggr.c
--- a/monetdb5/modules/kernel/aggr.c
+++ b/monetdb5/modules/kernel/aggr.c
@@ -22,12 +22,13 @@ static str
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to