Changeset: c4c5b736bff9 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=c4c5b736bff9
Modified Files:
        NT/monetdb_config.h.in
        configure.ag
        gdk/gdk_align.c
        gdk/gdk_atoms.c
        gdk/gdk_bat.c
        gdk/gdk_batop.c
        gdk/gdk_bbp.c
        gdk/gdk_hash.c
        gdk/gdk_heap.c
        gdk/gdk_imprints.c
        gdk/gdk_logger.c
        gdk/gdk_orderidx.c
        gdk/gdk_private.h
        gdk/gdk_project.c
        gdk/gdk_storage.c
        sql/backends/monet5/sql_result.c
        sql/server/sql_semantic.c
Branch: Aug2018
Log Message:

Don't use snprintf for simple string copy/concatenation: it's slow.
Use stpcpy instead.  For systems that lack this function, we provide
an implementation.


diffs (truncated from 675 to 300 lines):

diff --git a/NT/monetdb_config.h.in b/NT/monetdb_config.h.in
--- a/NT/monetdb_config.h.in
+++ b/NT/monetdb_config.h.in
@@ -877,6 +877,16 @@ c99_snprintf(char *outBuf, size_t size, 
 }
 #endif
 
+static inline char *
+stpcpy(char *restrict dst, const char *restrict src)
+{
+       size_t i;
+       for (i = 0; src[i]; i++)
+               dst[i] = src[i];
+       dst[i] = 0;
+       return dst + i;
+}
+
 /* type used by connect */
 #define socklen_t int
 
diff --git a/configure.ag b/configure.ag
--- a/configure.ag
+++ b/configure.ag
@@ -2586,6 +2586,7 @@ AC_CHECK_FUNCS([\
        setsid \
        shutdown \
        sigaction \
+       stpcpy \
        strcasestr \
        strncasecmp \
        strptime \
@@ -2852,6 +2853,18 @@ typedef enum {
 @%:@define static_assert(expr, mesg)   ((void) 0)
 @%:@endif
 
+@%:@ifndef HAVE_STPCPY
+static inline char *
+stpcpy(char *restrict dst, const char *restrict src)
+{
+       size_t i;
+       for (i = 0; src[i]; i++)
+               dst[i] = src[i];
+       dst[i] = 0;
+       return dst + i;
+}
+@%:@endif
+
 @%:@if defined(HAVE___INT128)
 typedef __int128 hge;
 typedef unsigned __int128 uhge;
diff --git a/gdk/gdk_align.c b/gdk/gdk_align.c
--- a/gdk/gdk_align.c
+++ b/gdk/gdk_align.c
@@ -177,7 +177,7 @@ BATmaterialize(BAT *b)
        IMPSdestroy(b);
        OIDXdestroy(b);
 
-       snprintf(b->theap.filename, sizeof(b->theap.filename), "%s.tail", 
BBP_physical(b->batCacheid));
+       stpconcat(b->theap.filename, BBP_physical(b->batCacheid), ".tail", 
NULL);
        if (HEAPalloc(&b->theap, cnt, sizeof(oid)) != GDK_SUCCEED) {
                b->theap = tail;
                return GDK_FAIL;
@@ -275,7 +275,7 @@ VIEWreset(BAT *b)
                assert(tp || tvp || !b->ttype);
 
                tail.farmid = BBPselectfarm(b->batRole, b->ttype, offheap);
-               snprintf(tail.filename, sizeof(tail.filename), "%s.tail", nme);
+               stpconcat(tail.filename, nme, ".tail", NULL);
                if (b->ttype && HEAPalloc(&tail, cnt, Tsize(b)) != GDK_SUCCEED)
                        goto bailout;
                if (b->tvheap) {
@@ -283,7 +283,7 @@ VIEWreset(BAT *b)
                        if (th == NULL)
                                goto bailout;
                        th->farmid = BBPselectfarm(b->batRole, b->ttype, 
varheap);
-                       snprintf(th->filename, sizeof(th->filename), 
"%s.theap", nme);
+                       stpconcat(th->filename, nme, ".tail", NULL);
                        if (ATOMheap(b->ttype, th, cnt) != GDK_SUCCEED)
                                goto bailout;
                }
diff --git a/gdk/gdk_atoms.c b/gdk/gdk_atoms.c
--- a/gdk/gdk_atoms.c
+++ b/gdk/gdk_atoms.c
@@ -401,7 +401,8 @@ TYPE##ToStr(char **dst, size_t *len, con
 {                                                      \
        atommem(TYPE##Strlen);                          \
        if (is_##TYPE##_nil(*src)) {                    \
-               return snprintf(*dst, *len, "nil");     \
+               strcpy(*dst, "nil");                    \
+               return 3;                               \
        }                                               \
        return snprintf(*dst, *len, FMT, FMTCAST *src); \
 }
@@ -479,11 +480,16 @@ bitToStr(char **dst, size_t *len, const 
 {
        atommem(6);
 
-       if (is_bit_nil(*src))
-               return snprintf(*dst, *len, "nil");
-       if (*src)
-               return snprintf(*dst, *len, "true");
-       return snprintf(*dst, *len, "false");
+       if (is_bit_nil(*src)) {
+               strcpy(*dst, "nil");
+               return 3;
+       }
+       if (*src) {
+               strcpy(*dst, "true");
+               return 4;
+       }
+       strcpy(*dst, "false");
+       return 5;
 }
 
 ssize_t
@@ -533,11 +539,12 @@ batToStr(char **dst, size_t *len, const 
 
        if (is_bat_nil(b) || (s = BBPname(b)) == NULL || *s == 0) {
                atommem(4);
-               return snprintf(*dst, *len, "nil");
+               strcpy(*dst, "nil");
+               return 3;
        }
        i = strlen(s) + 3;
        atommem(i);
-       return snprintf(*dst, *len, "<%s>", s);
+       return stpconcat(*dst, "<", s, ">", NULL) - *dst;
 }
 
 
@@ -975,7 +982,8 @@ dblToStr(char **dst, size_t *len, const 
 
        atommem(dblStrlen);
        if (is_dbl_nil(*src)) {
-               return snprintf(*dst, *len, "nil");
+               strcpy(*dst, "nil");
+               return 3;
        }
        for (i = 4; i < 18; i++) {
                snprintf(*dst, *len, "%.*g", i, *src);
@@ -1042,7 +1050,8 @@ fltToStr(char **dst, size_t *len, const 
 
        atommem(fltStrlen);
        if (is_flt_nil(*src)) {
-               return snprintf(*dst, *len, "nil");
+               strcpy(*dst, "nil");
+               return 3;
        }
        for (i = 4; i < 10; i++) {
                snprintf(*dst, *len, "%.*g", i, *src);
@@ -1748,7 +1757,8 @@ strToStr(char **restrict dst, size_t *re
 {
        if (GDK_STRNIL(src)) {
                atommem(4);
-               return snprintf(*dst, *len, "nil");
+               strcpy(*dst, "nil");
+               return 3;
        } else {
                ssize_t l = 0;
                size_t sz = escapedStrlen(src, NULL, NULL, '"');
@@ -1850,7 +1860,8 @@ OIDtoStr(char **dst, size_t *len, const 
        atommem(oidStrlen);
 
        if (is_oid_nil(*src)) {
-               return snprintf(*dst, *len, "nil");
+               strcpy(*dst, "nil");
+               return 3;
        }
        return snprintf(*dst, *len, OIDFMT "@0", *src);
 }
diff --git a/gdk/gdk_bat.c b/gdk/gdk_bat.c
--- a/gdk/gdk_bat.c
+++ b/gdk/gdk_bat.c
@@ -116,14 +116,12 @@ BATcreatedesc(oid hseq, int tt, int heap
        assert(bn->batCacheid > 0);
 
        const char *nme = BBP_physical(bn->batCacheid);
-       snprintf(bn->theap.filename, sizeof(bn->theap.filename),
-                "%s.tail", nme);
+       stpconcat(bn->theap.filename, nme, ".tail", NULL);
        bn->theap.farmid = BBPselectfarm(role, bn->ttype, offheap);
        if (heapnames && ATOMneedheap(tt)) {
                if ((bn->tvheap = (Heap *) GDKzalloc(sizeof(Heap))) == NULL)
                        goto bailout;
-               snprintf(bn->tvheap->filename, sizeof(bn->tvheap->filename),
-                        "%s.theap", nme);
+               stpconcat(bn->tvheap->filename, nme, ".theap", NULL);
                bn->tvheap->parentid = bn->batCacheid;
                bn->tvheap->farmid = BBPselectfarm(role, bn->ttype, varheap);
        }
@@ -738,10 +736,10 @@ COLcopy(BAT *b, int tt, bool writable, i
 
                        bthp.farmid = BBPselectfarm(role, b->ttype, offheap);
                        thp.farmid = BBPselectfarm(role, b->ttype, varheap);
-                       snprintf(bthp.filename, sizeof(bthp.filename),
-                                "%s.tail", BBP_physical(bn->batCacheid));
-                       snprintf(thp.filename, sizeof(thp.filename), "%s.theap",
-                                BBP_physical(bn->batCacheid));
+                       stpconcat(bthp.filename, BBP_physical(bn->batCacheid),
+                                 ".tail", NULL);
+                       stpconcat(thp.filename, BBP_physical(bn->batCacheid),
+                                 ".theap", NULL);
                        if ((b->ttype && HEAPcopy(&bthp, &b->theap) != 
GDK_SUCCEED) ||
                            (bn->tvheap && HEAPcopy(&thp, b->tvheap) != 
GDK_SUCCEED)) {
                                HEAPfree(&thp, true);
diff --git a/gdk/gdk_batop.c b/gdk/gdk_batop.c
--- a/gdk/gdk_batop.c
+++ b/gdk/gdk_batop.c
@@ -30,8 +30,8 @@ unshare_string_heap(BAT *b)
                        return GDK_FAIL;
                h->parentid = b->batCacheid;
                h->farmid = BBPselectfarm(b->batRole, TYPE_str, varheap);
-               snprintf(h->filename, sizeof(h->filename),
-                        "%s.theap", BBP_physical(b->batCacheid));
+               stpconcat(h->filename, BBP_physical(b->batCacheid),
+                         ".theap", NULL);
                if (HEAPcopy(h, b->tvheap) != GDK_SUCCEED) {
                        HEAPfree(h, true);
                        GDKfree(h);
@@ -479,8 +479,8 @@ append_varsized_bat(BAT *b, BAT *n, BAT 
                        return GDK_FAIL;
                h->parentid = b->batCacheid;
                h->farmid = BBPselectfarm(b->batRole, b->ttype, varheap);
-               snprintf(h->filename, sizeof(h->filename),
-                        "%s.theap", BBP_physical(b->batCacheid));
+               stpconcat(h->filename, BBP_physical(b->batCacheid),
+                         ".theap", NULL);
                if (HEAPcopy(h, b->tvheap) != GDK_SUCCEED) {
                        HEAPfree(h, true);
                        GDKfree(h);
diff --git a/gdk/gdk_bbp.c b/gdk/gdk_bbp.c
--- a/gdk/gdk_bbp.c
+++ b/gdk/gdk_bbp.c
@@ -482,7 +482,7 @@ fixstroffheap(BAT *b, int *restrict offs
                h2 = *b->tvheap;
                if (GDKmove(h2.farmid, srcdir, bnme, "theap", BAKDIR, bnme, 
"theap") != GDK_SUCCEED)
                        GDKfatal("fixstroffheap: cannot make backup of 
%s.theap\n", nme);
-               snprintf(h2.filename, sizeof(h2.filename), "%s.theap", nme);
+               stpconcat(h2.filename, nme, ".theap", NULL);
                h2.base = NULL;
                if (HEAPalloc(&h2, h2.size, 1) != GDK_SUCCEED)
                        GDKfatal("fixstroffheap: allocating new string heap "
@@ -492,7 +492,7 @@ fixstroffheap(BAT *b, int *restrict offs
                h2.free = b->tvheap->free;
                /* load old offset heap and copy contents to new heap */
                h1 = *b->tvheap;
-               snprintf(h1.filename, sizeof(h1.filename), "%s.theap", 
filename);
+               stpconcat(h1.filename, filename, ".theap", NULL);
                h1.base = NULL;
                h1.dirty = 0;
                if (HEAPload(&h1, filename, "theap", false) != GDK_SUCCEED)
@@ -519,7 +519,7 @@ fixstroffheap(BAT *b, int *restrict offs
                GDKfatal("fixstroffheap: cannot make backup of %s.tail\n", nme);
        /* load old offset heap */
        h1 = b->theap;
-       snprintf(h1.filename, sizeof(h1.filename), "%s.tail", filename);
+       stpconcat(h1.filename, filename, ".tail", NULL);
        h1.base = NULL;
        h1.dirty = 0;
        if (HEAPload(&h1, filename, "tail", false) != GDK_SUCCEED)
@@ -528,7 +528,7 @@ fixstroffheap(BAT *b, int *restrict offs
 
        /* create new offset heap */
        h3 = b->theap;
-       snprintf(h3.filename, sizeof(h3.filename), "%s.tail", nme);
+       stpconcat(h3.filename, nme, ".tail", NULL);
        if (HEAPalloc(&h3, b->batCapacity, width) != GDK_SUCCEED)
                GDKfatal("fixstroffheap: allocating new tail heap "
                         "for BAT %d failed\n", b->batCacheid);
@@ -702,7 +702,7 @@ fixfltheap(BAT *b)
                GDKfatal("fixfltheap: cannot make backup of %s.tail\n", nme);
        /* load old heap */
        h1 = b->theap;
-       snprintf(h1.filename, sizeof(h1.filename), "%s.tail", filename);
+       stpconcat(h1.filename, filename, ".tail", NULL);
        h1.base = NULL;
        h1.dirty = 0;
        if (HEAPload(&h1, filename, "tail", false) != GDK_SUCCEED)
@@ -711,7 +711,7 @@ fixfltheap(BAT *b)
 
        /* create new heap */
        h2 = b->theap;
-       snprintf(h2.filename, sizeof(h2.filename), "%s.tail", nme);
+       stpconcat(h2.filename, nme, ".tail", NULL);
        if (HEAPalloc(&h2, b->batCapacity, b->twidth) != GDK_SUCCEED)
                GDKfatal("fixfltheap: allocating new tail heap "
                         "for BAT %d failed\n", b->batCacheid);
@@ -967,8 +967,7 @@ heapinit(BAT *b, const char *buf, int *h
        b->theap.free = (size_t) free;
        b->theap.size = (size_t) size;
        b->theap.base = NULL;
-       snprintf(b->theap.filename, sizeof(b->theap.filename),
-                "%s.tail", filename);
+       stpconcat(b->theap.filename, filename, ".tail", NULL);
        b->theap.storage = (storage_t) storage;
        b->theap.copied = 0;
        b->theap.newstorage = (storage_t) storage;
@@ -998,8 +997,7 @@ vheapinit(BAT *b, const char *buf, int h
                b->tvheap->free = (size_t) free;
                b->tvheap->size = (size_t) size;
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to