Changeset: 6fd61f5a2a07 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=6fd61f5a2a07
Modified Files:
        monetdb5/modules/atoms/str.c
        monetdb5/modules/atoms/str.h
        monetdb5/modules/kernel/batstr.c
Branch: alloc-less-str
Log Message:

Use one less buffer


diffs (287 lines):

diff --git a/monetdb5/modules/atoms/str.c b/monetdb5/modules/atoms/str.c
--- a/monetdb5/modules/atoms/str.c
+++ b/monetdb5/modules/atoms/str.c
@@ -3928,13 +3928,13 @@ STRRtrim(str *res, const str *arg1)
 
 /* return a list of codepoints in s */
 static str
-trimchars(int **chars, size_t *nchars, size_t *n, const char *s, size_t len_s, 
const char *malfunc)
+trimchars(str *buf, size_t *buflen, size_t *n, const char *s, size_t len_s, 
const char *malfunc)
 {
        size_t len = 0, nlen = len_s * sizeof(int);
        int c, *cbuf;
 
-       CHECK_INT_BUFFER_LENGTH(chars, nchars, nlen, malfunc);
-       cbuf = *chars;
+       CHECK_STR_BUFFER_LENGTH(buf, buflen, nlen, malfunc);
+       cbuf = *((int**)buf);
 
        while (*s) {
                UTF8_GETCHAR(c, s);
@@ -3948,7 +3948,7 @@ illegal:
 }
 
 str
-str_strip2(str *buf, size_t *buflen, int **chars, size_t *nchars, const char 
*s, const char *s2)
+str_strip2(str *buf, size_t *buflen, const char *s, const char *s2)
 {
        str msg = MAL_SUCCEED;
        size_t len, n, n2, n3;
@@ -3962,13 +3962,13 @@ str_strip2(str *buf, size_t *buflen, int
                strcpy(*buf, s);
                return MAL_SUCCEED;
        } else {
-               if ((msg = trimchars(chars, nchars, &n3, s2, n2, "str.strip2")) 
!= MAL_SUCCEED)
+               if ((msg = trimchars(buf, buflen, &n3, s2, n2, "str.strip2")) 
!= MAL_SUCCEED)
                        return msg;
                len = strlen(s);
-               n = lstrip(s, len, *chars, n3);
+               n = lstrip(s, len, *((int**)buf), n3);
                s += n;
                len -= n;
-               n = rstrip(s, len, *chars, n3);
+               n = rstrip(s, len, *((int**)buf), n3);
 
                n++;
                CHECK_STR_BUFFER_LENGTH(buf, buflen, n, "str.strip2");
@@ -3982,28 +3982,23 @@ str_strip2(str *buf, size_t *buflen, int
 static str
 STRStrip2(str *res, const str *arg1, const str *arg2)
 {
-       size_t buflen = INITIAL_STR_BUFFER_LENGTH, nchars = 
INITIAL_INT_BUFFER_LENGTH;
+       size_t buflen = INITIAL_STR_BUFFER_LENGTH;
        str buf = GDKmalloc(buflen), msg;
-       int *chars = GDKmalloc(nchars);
 
        *res = NULL;
-       if (!buf || !chars) {
-               GDKfree(buf);
-               GDKfree(chars);
+       if (!buf)
                throw(MAL, "str.strip2", SQLSTATE(HY013) MAL_MALLOC_FAIL);
-       }
-       msg = str_strip2(&buf, &buflen, &chars, &nchars, *arg1, *arg2);
+       msg = str_strip2(&buf, &buflen, *arg1, *arg2);
        if (!msg && !(*res = GDKstrdup(buf))) {
                msg = createException(MAL, "str.strip2", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
        }
 
-       GDKfree(chars);
        GDKfree(buf);
        return msg;
 }
 
 str
-str_ltrim2(str *buf, size_t *buflen, int **chars, size_t *nchars, const char 
*s, const char *s2)
+str_ltrim2(str *buf, size_t *buflen, const char *s, const char *s2)
 {
        str msg = MAL_SUCCEED;
        size_t len, n, n2, n3, nallocate;
@@ -4017,10 +4012,10 @@ str_ltrim2(str *buf, size_t *buflen, int
                strcpy(*buf, s);
                return MAL_SUCCEED;
        } else {
-               if ((msg = trimchars(chars, nchars, &n3, s2, n2, "str.ltrim2")) 
!= MAL_SUCCEED)
+               if ((msg = trimchars(buf, buflen, &n3, s2, n2, "str.ltrim2")) 
!= MAL_SUCCEED)
                        return msg;
                len = strlen(s);
-               n = lstrip(s, len, *chars, n3);
+               n = lstrip(s, len, *((int**)buf), n3);
                nallocate = len - n + 1;
 
                CHECK_STR_BUFFER_LENGTH(buf, buflen, nallocate, "str.ltrim2");
@@ -4034,28 +4029,23 @@ str_ltrim2(str *buf, size_t *buflen, int
 static str
 STRLtrim2(str *res, const str *arg1, const str *arg2)
 {
-       size_t buflen = INITIAL_STR_BUFFER_LENGTH, nchars = 
INITIAL_INT_BUFFER_LENGTH;
+       size_t buflen = INITIAL_STR_BUFFER_LENGTH;
        str buf = GDKmalloc(buflen), msg;
-       int *chars = GDKmalloc(nchars);
 
        *res = NULL;
-       if (!buf || !chars) {
-               GDKfree(buf);
-               GDKfree(chars);
+       if (!buf)
                throw(MAL, "str.ltrim2", SQLSTATE(HY013) MAL_MALLOC_FAIL);
-       }
-       msg = str_ltrim2(&buf, &buflen, &chars, &nchars, *arg1, *arg2);
+       msg = str_ltrim2(&buf, &buflen, *arg1, *arg2);
        if (!msg && !(*res = GDKstrdup(buf))) {
                msg = createException(MAL, "str.ltrim2", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
        }
 
-       GDKfree(chars);
        GDKfree(buf);
        return msg;
 }
 
 str
-str_rtrim2(str *buf, size_t *buflen, int **chars, size_t *nchars, const char 
*s, const char *s2)
+str_rtrim2(str *buf, size_t *buflen, const char *s, const char *s2)
 {
        str msg = MAL_SUCCEED;
        size_t len, n, n2, n3;
@@ -4069,10 +4059,10 @@ str_rtrim2(str *buf, size_t *buflen, int
                strcpy(*buf, s);
                return MAL_SUCCEED;
        } else {
-               if ((msg = trimchars(chars, nchars, &n3, s2, n2, "str.rtrim2")) 
!= MAL_SUCCEED)
+               if ((msg = trimchars(buf, buflen, &n3, s2, n2, "str.ltrim2")) 
!= MAL_SUCCEED)
                        return msg;
                len = strlen(s);
-               n = rstrip(s, len, *chars, n3);
+               n = rstrip(s, len, *((int**)buf), n3);
                n++;
 
                CHECK_STR_BUFFER_LENGTH(buf, buflen, n, "str.rtrim2");
@@ -4086,22 +4076,17 @@ str_rtrim2(str *buf, size_t *buflen, int
 static str
 STRRtrim2(str *res, const str *arg1, const str *arg2)
 {
-       size_t buflen = INITIAL_STR_BUFFER_LENGTH, nchars = 
INITIAL_INT_BUFFER_LENGTH;
+       size_t buflen = INITIAL_STR_BUFFER_LENGTH;
        str buf = GDKmalloc(buflen), msg;
-       int *chars = GDKmalloc(nchars);
 
        *res = NULL;
-       if (!buf || !chars) {
-               GDKfree(buf);
-               GDKfree(chars);
+       if (!buf)
                throw(MAL, "str.rtrim2", SQLSTATE(HY013) MAL_MALLOC_FAIL);
-       }
-       msg = str_rtrim2(&buf, &buflen, &chars, &nchars, *arg1, *arg2);
+       msg = str_rtrim2(&buf, &buflen, *arg1, *arg2);
        if (!msg && !(*res = GDKstrdup(buf))) {
                msg = createException(MAL, "str.rtrim2", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
        }
 
-       GDKfree(chars);
        GDKfree(buf);
        return msg;
 }
diff --git a/monetdb5/modules/atoms/str.h b/monetdb5/modules/atoms/str.h
--- a/monetdb5/modules/atoms/str.h
+++ b/monetdb5/modules/atoms/str.h
@@ -16,7 +16,6 @@
 /* The batstr module functions use a single buffer to avoid malloc/free 
overhead.
    Note the buffer should be always large enough to hold null strings, so less 
testing will be required */
 #define INITIAL_STR_BUFFER_LENGTH  MAX(strlen(str_nil) + 1, 1024)
-#define INITIAL_INT_BUFFER_LENGTH  1024 * sizeof(int)
 
 /* The batstr module functions use a single buffer to avoid malloc/free 
overhead.
    Note the buffer should be always large enough to hold null strings, so less 
testing will be required */
@@ -33,19 +32,6 @@
                } \
        } while (0)
 
-#define CHECK_INT_BUFFER_LENGTH(BUFFER, BUFFER_LEN, NEXT_LEN, OP) \
-       do {  \
-               if (NEXT_LEN > *BUFFER_LEN) { \
-                       size_t newlen = NEXT_LEN + (1024 * sizeof(int)); \
-                       int *newbuf = GDKmalloc(newlen); \
-                       if (!newbuf) \
-                               throw(MAL, OP, SQLSTATE(HY013) 
MAL_MALLOC_FAIL); \
-                       GDKfree(*BUFFER); \
-                       *BUFFER = newbuf; \
-                       *BUFFER_LEN = newlen; \
-               } \
-       } while (0)
-
 extern int str_utf8_length(const char *s);
 extern int str_nbytes(const char *s);
 
@@ -68,9 +54,9 @@ extern str str_upper(str *buf, size_t *b
 extern str str_strip(str *buf, size_t *buflen, const char *s);
 extern str str_ltrim(str *buf, size_t *buflen, const char *s);
 extern str str_rtrim(str *buf, size_t *buflen, const char *s);
-extern str str_strip2(str *buf, size_t *buflen, int **chars, size_t *nchars, 
const char *s, const char *s2);
-extern str str_ltrim2(str *buf, size_t *buflen, int **chars, size_t *nchars, 
const char *s, const char *s2);
-extern str str_rtrim2(str *buf, size_t *buflen, int **chars, size_t *nchars, 
const char *s, const char *s2);
+extern str str_strip2(str *buf, size_t *buflen, const char *s, const char *s2);
+extern str str_ltrim2(str *buf, size_t *buflen, const char *s, const char *s2);
+extern str str_rtrim2(str *buf, size_t *buflen, const char *s, const char *s2);
 extern str str_lpad(str *buf, size_t *buflen, const char *s, int len);
 extern str str_rpad(str *buf, size_t *buflen, const char *s, int len);
 extern str str_lpad2(str *buf, size_t *buflen, const char *s, int len, const 
char *s2);
diff --git a/monetdb5/modules/kernel/batstr.c b/monetdb5/modules/kernel/batstr.c
--- a/monetdb5/modules/kernel/batstr.c
+++ b/monetdb5/modules/kernel/batstr.c
@@ -296,17 +296,16 @@ bailout:
  * Output type: str (a BAT of strings)
  */
 static str
-do_batstr_conststr_str(bat *res, const bat *l, const str *s2, const char 
*name, str (*func)(str*, size_t*, int**, size_t*, const char*, const char*))
+do_batstr_conststr_str(bat *res, const bat *l, const str *s2, const char 
*name, str (*func)(str*, size_t*, const char*, const char*))
 {
        BATiter bi;
        BAT *bn = NULL, *b = NULL;
        BUN p, q;
-       size_t buflen = INITIAL_STR_BUFFER_LENGTH, nchars = 
INITIAL_INT_BUFFER_LENGTH;
+       size_t buflen = INITIAL_STR_BUFFER_LENGTH;
        str x, y = *s2, buf = GDKmalloc(buflen), msg = MAL_SUCCEED;
-       int *chars = GDKmalloc(nchars);
        bool nils = false;
 
-       if (!buf || !chars) {
+       if (!buf) {
                msg = createException(MAL, name, SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                goto bailout;
        }
@@ -324,7 +323,7 @@ do_batstr_conststr_str(bat *res, const b
        for (p = 0; p < q ; p++) {
                x = (str) BUNtail(bi, p);
 
-               if ((msg = func(&buf, &buflen, &chars, &nchars, x, y)) != 
MAL_SUCCEED)
+               if ((msg = func(&buf, &buflen, x, y)) != MAL_SUCCEED)
                        goto bailout;
                if (tfastins_nocheckVAR(bn, p, buf, Tsize(bn)) != GDK_SUCCEED) {
                        msg = createException(MAL, name, SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
@@ -335,7 +334,6 @@ do_batstr_conststr_str(bat *res, const b
 
 bailout:
        GDKfree(buf);
-       GDKfree(chars);
        if (b)
                BBPunfix(b->batCacheid);
        if (bn && !msg) {
@@ -355,17 +353,16 @@ bailout:
  * Output type: str (a BAT of strings)
  */
 static str
-do_batstr_batstr_str(bat *res, const bat *l, const bat *l2, const char *name, 
str (*func)(str*, size_t*, int**, size_t*, const char*, const char*))
+do_batstr_batstr_str(bat *res, const bat *l, const bat *l2, const char *name, 
str (*func)(str*, size_t*, const char*, const char*))
 {
        BATiter lefti, righti;
        BAT *bn = NULL, *left = NULL, *right = NULL;
        BUN p, q;
-       size_t buflen = INITIAL_STR_BUFFER_LENGTH, nchars = 
INITIAL_INT_BUFFER_LENGTH;
+       size_t buflen = INITIAL_STR_BUFFER_LENGTH;
        str x, y, buf = GDKmalloc(buflen), msg = MAL_SUCCEED;
-       int *chars = GDKmalloc(nchars);
        bool nils = false;
 
-       if (!buf || !chars) {
+       if (!buf) {
                msg = createException(MAL, name, SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                goto bailout;
        }
@@ -389,7 +386,7 @@ do_batstr_batstr_str(bat *res, const bat
                x = (str) BUNtail(lefti, p);
                y = (str) BUNtail(righti, p);
 
-               if ((msg = func(&buf, &buflen, &chars, &nchars, x, y)) != 
MAL_SUCCEED)
+               if ((msg = func(&buf, &buflen, x, y)) != MAL_SUCCEED)
                        goto bailout;
                if (tfastins_nocheckVAR(bn, p, buf, Tsize(bn)) != GDK_SUCCEED) {
                        msg = createException(MAL, name, SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
@@ -400,7 +397,6 @@ do_batstr_batstr_str(bat *res, const bat
 
 bailout:
        GDKfree(buf);
-       GDKfree(chars);
        if (left)
                BBPunfix(left->batCacheid);
        if (right)
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to