Changeset: cb334b0ff7d0 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/cb334b0ff7d0
Modified Files:
        gdk/gdk_batop.c
        gdk/gdk_private.h
        gdk/gdk_string.c
        monetdb5/modules/mal/tablet.c
Branch: Jul2021
Log Message:

A bunch of micro optimizations.


diffs (222 lines):

diff --git a/gdk/gdk_batop.c b/gdk/gdk_batop.c
--- a/gdk/gdk_batop.c
+++ b/gdk/gdk_batop.c
@@ -2093,6 +2093,40 @@ BATordered(BAT *b)
                case TYPE_dbl:
                        BAT_ORDERED_FP(dbl);
                        break;
+               case TYPE_str:
+                       for (BUN q = BUNlast(b), p = 1; p < q; p++) {
+                               int c;
+                               const char *p1 = BUNtail(bi, p - 1);
+                               const char *p2 = BUNtail(bi, p);
+                               if (p1 == p2)
+                                       c = 0;
+                               else if (p1[0] == '\200') {
+                                       if (p2[0] == '\200')
+                                               c = 0;
+                                       else
+                                               c = -1;
+                               } else if (p2[0] == '\200')
+                                       c = 1;
+                               else
+                                       c = strcmp(p1, p2);
+                               if (c > 0) {
+                                       b->tnosorted = p;
+                                       TRC_DEBUG(ALGO, "Fixed nosorted(" 
BUNFMT ") for " ALGOBATFMT " (" LLFMT " usec)\n", p, ALGOBATPAR(b), GDKusec() - 
t0);
+                                       goto doreturn;
+                               } else if (c < 0) {
+                                       assert(!b->trevsorted);
+                                       if (b->tnorevsorted == 0) {
+                                               b->tnorevsorted = p;
+                                               TRC_DEBUG(ALGO, "Fixed 
norevsorted(" BUNFMT ") for " ALGOBATFMT "\n", p, ALGOBATPAR(b));
+                                       }
+                               } else if (b->tnokey[1] == 0) {
+                                       assert(!b->tkey);
+                                       b->tnokey[0] = p - 1;
+                                       b->tnokey[1] = p;
+                                       TRC_DEBUG(ALGO, "Fixed nokey(" BUNFMT 
"," BUNFMT") for " ALGOBATFMT "\n", p - 1, p, ALGOBATPAR(b));
+                               }
+                       }
+                       break;
                default: {
                        int (*cmpf)(const void *, const void *) = 
ATOMcompare(b->ttype);
                        for (BUN q = BUNlast(b), p = 1; p < q; p++) {
diff --git a/gdk/gdk_private.h b/gdk/gdk_private.h
--- a/gdk/gdk_private.h
+++ b/gdk/gdk_private.h
@@ -365,7 +365,7 @@ ilog2(BUN x)
        b && b->torderidx ? "O" : "",                                   \
        b ? b->timprints ? "I" : b->theap && b->theap->parentid && 
BBP_cache(b->theap->parentid) && BBP_cache(b->theap->parentid)->timprints ? 
"(I)" : "" : ""
 
-#define BBP_BATMASK    (128 * SIZEOF_SIZE_T - 1)
+#define BBP_BATMASK    ((1 << (SIZEOF_SIZE_T + 5)) - 1)
 #define BBP_THREADMASK 63
 
 struct PROPrec {
diff --git a/gdk/gdk_string.c b/gdk/gdk_string.c
--- a/gdk/gdk_string.c
+++ b/gdk/gdk_string.c
@@ -115,7 +115,7 @@ strCleanHash(Heap *h, bool rebuild)
                        strhash = strHash(s);
                off = strhash & GDK_STRHASHMASK;
                newhash[off] = (stridx_t) (pos - extralen - sizeof(stridx_t));
-               pos += strLen(s);
+               pos += strlen(s) + 1;
        }
        /* only set dirty flag if the hash table actually changed */
        if (memcmp(newhash, h->base, sizeof(newhash)) != 0) {
@@ -136,7 +136,7 @@ strCleanHash(Heap *h, bool rebuild)
                        pos += pad + extralen;
                        s = h->base + pos;
                        assert(strLocate(h, s) != 0);
-                       pos += strLen(s);
+                       pos += strlen(s) + 1;
                }
        }
 #endif
@@ -170,7 +170,7 @@ strLocate(Heap *h, const char *v)
        /* search the linked list */
        for (ref = ((stridx_t *) h->base) + off; *ref; ref = next) {
                next = (stridx_t *) (h->base + *ref);
-               if (strCmp(v, (str) (next + 1) + extralen) == 0)
+               if (strcmp(v, (str) (next + 1) + extralen) == 0)
                        return (var_t) ((sizeof(stridx_t) + *ref + extralen));  
/* found */
        }
        return 0;
@@ -256,7 +256,7 @@ strPut(BAT *b, var_t *dst, const void *V
        const char *v = V;
        Heap *h = b->tvheap;
        size_t pad;
-       size_t pos, len = strLen(v);
+       size_t pos, len = strlen(v) + 1;
        const size_t extralen = h->hashash ? EXTRALEN : 0;
        stridx_t *bucket;
        BUN off, strhash;
@@ -293,7 +293,7 @@ strPut(BAT *b, var_t *dst, const void *V
 
                        do {
                                pos = *ref + sizeof(stridx_t) + extralen;
-                               if (strCmp(v, h->base + pos) == 0) {
+                               if (strcmp(v, h->base + pos) == 0) {
                                        /* found */
                                        return *dst = (var_t) pos;
                                }
@@ -304,7 +304,7 @@ strPut(BAT *b, var_t *dst, const void *V
                         * linked list, so only look at single
                         * entry */
                        pos = *bucket + extralen;
-                       if (strCmp(v, h->base + pos) == 0) {
+                       if (strcmp(v, h->base + pos) == 0) {
                                /* already in heap: reuse */
                                return *dst = (var_t) pos;
                        }
diff --git a/monetdb5/modules/mal/tablet.c b/monetdb5/modules/mal/tablet.c
--- a/monetdb5/modules/mal/tablet.c
+++ b/monetdb5/modules/mal/tablet.c
@@ -1223,6 +1223,16 @@ mkdfa(const unsigned char *sep, size_t s
        return dfa;
 }
 
+#ifdef __GNUC__
+/* __builtin_expect returns its first argument; it is expected to be
+ * equal to the second argument */
+#define unlikely(expr) __builtin_expect((expr) != 0, 0)
+#define likely(expr)   __builtin_expect((expr) != 0, 1)
+#else
+#define unlikely(expr) (expr)
+#define likely(expr)   (expr)
+#endif
+
 static void
 SQLproducer(void *p)
 {
@@ -1267,7 +1277,7 @@ SQLproducer(void *p)
                // we may be reading from standard input and may be out of input
                // warn the consumers
                if (ateof[cur] && partial) {
-                       if (partial) {
+                       if (unlikely(partial)) {
                                tablet_error(task, rowno, int_nil, "incomplete 
record at end of file", s);
                                task->b->pos += partial;
                        }
@@ -1275,7 +1285,7 @@ SQLproducer(void *p)
                }
 
                if (task->errbuf && task->errbuf[0]) {
-                       if (GDKerrbuf && GDKerrbuf[0]) {
+                       if (unlikely(GDKerrbuf && GDKerrbuf[0])) {
                                tablet_error(task, rowno, int_nil, GDKerrbuf, 
"SQLload_file");
 /*                             TRC_DEBUG(MAL_SERVER, "Bailout on SQLload\n");*/
                                ateof[cur] = true;
@@ -1291,7 +1301,7 @@ SQLproducer(void *p)
                s = task->input[cur];
                base = end;
                /* avoid too long records */
-               if (end - s + task->b->len - task->b->pos >= 
task->rowlimit[cur]) {
+               if (unlikely(end - s + task->b->len - task->b->pos >= 
task->rowlimit[cur])) {
                        /* the input buffer should be extended, but 'base' is 
not shared
                           between the threads, which we can not now update.
                           Mimick an ateof instead; */
@@ -1342,26 +1352,28 @@ SQLproducer(void *p)
                                } else {
                                        /* check for correctly encoded UTF-8 */
                                        if (nutf > 0) {
-                                               if ((*e & 0xC0) != 0x80)
+                                               if (unlikely((*e & 0xC0) != 
0x80))
                                                        goto badutf8;
-                                               if (m != 0 && (*e & m) == 0)
+                                               if (unlikely(m != 0 && (*e & m) 
== 0))
                                                        goto badutf8;
                                                m = 0;
                                                nutf--;
-                                       } else if ((*e & 0xE0) == 0xC0) {
-                                               nutf = 1;
-                                               if ((e[0] & 0x1E) == 0)
+                                       } else if ((*e & 0x80) != 0) {
+                                               if ((*e & 0xE0) == 0xC0) {
+                                                       nutf = 1;
+                                                       if (unlikely((e[0] & 
0x1E) == 0))
+                                                               goto badutf8;
+                                               } else if ((*e & 0xF0) == 0xE0) 
{
+                                                       nutf = 2;
+                                                       if ((e[0] & 0x0F) == 0)
+                                                               m = 0x20;
+                                               } else if (likely((*e & 0xF8) 
== 0xF0)) {
+                                                       nutf = 3;
+                                                       if ((e[0] & 0x07) == 0)
+                                                               m = 0x30;
+                                               } else {
                                                        goto badutf8;
-                                       } else if ((*e & 0xF0) == 0xE0) {
-                                               nutf = 2;
-                                               if ((e[0] & 0x0F) == 0)
-                                                       m = 0x20;
-                                       } else if ((*e & 0xF8) == 0xF0) {
-                                               nutf = 3;
-                                               if ((e[0] & 0x07) == 0)
-                                                       m = 0x30;
-                                       } else if ((*e & 0x80) != 0) {
-                                               goto badutf8;
+                                               }
                                        }
                                        /* check for quoting and the row 
separator */
                                        if (bs) {
@@ -1385,7 +1397,7 @@ SQLproducer(void *p)
                        if (*e == 0) {
                                partial = e - s;
                                /* found an incomplete record, saved for next 
round */
-                               if (s+partial < end) {
+                               if (unlikely(s+partial < end)) {
                                        /* found a EOS in the input */
                                        tablet_error(task, rowno, int_nil, 
"record too long (EOS found)", "");
                                        ateof[cur] = true;
@@ -1477,7 +1489,7 @@ SQLproducer(void *p)
                /* move the non-parsed correct row data to the head of the next 
buffer */
                end = s = task->input[cur];
        }
-       if (cnt < task->maxrow && task->maxrow != BUN_NONE) {
+       if (unlikely(cnt < task->maxrow && task->maxrow != BUN_NONE)) {
                char msg[256];
                snprintf(msg, sizeof(msg), "incomplete record at end of 
file:%s\n", s);
                task->as->error = GDKstrdup(msg);
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to