Changeset: b0c81da2c752 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/b0c81da2c752
Modified Files:
        gdk/gdk_utils.c
        monetdb5/modules/atoms/json.c
Branch: resource_management
Log Message:

optimize around tls allocator in json module


diffs (truncated from 605 to 300 lines):

diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -2730,23 +2730,26 @@ sa_get_eb(allocator *sa)
 allocator_state *
 sa_open(allocator *sa)
 {
-       allocator_state *res = sa_alloc(sa,
-                       sizeof(allocator_state));
-       if (!res) {
-               if (sa->eb.enabled)
-                       eb_error(&sa->eb, "out of memory", 1000);
-               return NULL;
-       }
+       if (sa) {
+               allocator_state *res = sa_alloc(sa,
+                               sizeof(allocator_state));
+               if (!res) {
+                       if (sa->eb.enabled)
+                               eb_error(&sa->eb, "out of memory", 1000);
+                       return NULL;
+               }
 
-       COND_LOCK_ALLOCATOR(sa);
-       sa->tmp_used += 1;
-       res->nr = sa->nr;
-       res->used = sa->used;
-       res->usedmem = sa->usedmem;
-       res->objects = sa->objects;
-       res->inuse = sa->inuse;
-       COND_UNLOCK_ALLOCATOR(sa);
-       return res;
+               COND_LOCK_ALLOCATOR(sa);
+               sa->tmp_used += 1;
+               res->nr = sa->nr;
+               res->used = sa->used;
+               res->usedmem = sa->usedmem;
+               res->objects = sa->objects;
+               res->inuse = sa->inuse;
+               COND_UNLOCK_ALLOCATOR(sa);
+               return res;
+       }
+       return NULL;
 }
 
 void
@@ -2767,25 +2770,26 @@ sa_close(allocator *sa)
 void
 sa_close_to(allocator *sa, allocator_state *state)
 {
-       COND_LOCK_ALLOCATOR(sa);
-       assert(state);
-       assert(sa_tmp_active(sa));
-       if (state && !sa_has_dependencies(sa)) {
-               assert((state->nr > 0) && (state->nr <= sa->nr));
-               assert(state->used <= SA_BLOCK_SIZE);
-               if (state->nr != sa->nr || state->used != sa->used) {
-                       _sa_free_blks(sa, state->nr);
-                       sa->nr = state->nr;
-                       sa->used = state->used;
-                       sa->usedmem = state->usedmem;
-                       sa->objects = state->objects;
-                       sa->inuse = state->inuse;
+       if (sa) {
+               COND_LOCK_ALLOCATOR(sa);
+               assert(sa_tmp_active(sa));
+               if (state && !sa_has_dependencies(sa)) {
+                       assert((state->nr > 0) && (state->nr <= sa->nr));
+                       assert(state->used <= SA_BLOCK_SIZE);
+                       if (state->nr != sa->nr || state->used != sa->used) {
+                               _sa_free_blks(sa, state->nr);
+                               sa->nr = state->nr;
+                               sa->used = state->used;
+                               sa->usedmem = state->usedmem;
+                               sa->objects = state->objects;
+                               sa->inuse = state->inuse;
+                       }
                }
+               if (sa->tmp_used > 0) {
+                       sa->tmp_used -= 1;
+               }
+               COND_UNLOCK_ALLOCATOR(sa);
        }
-       if (sa->tmp_used > 0) {
-               sa->tmp_used -= 1;
-       }
-       COND_UNLOCK_ALLOCATOR(sa);
 }
 
 bool
diff --git a/monetdb5/modules/atoms/json.c b/monetdb5/modules/atoms/json.c
--- a/monetdb5/modules/atoms/json.c
+++ b/monetdb5/modules/atoms/json.c
@@ -338,12 +338,13 @@ JSONtoString(allocator *ma, str *s, size
 }
 
 static BAT *
-JSONdumpInternal(Client ctx, const JSON *jt, int depth)
+JSONdumpInternal(allocator *ma, Client ctx, const JSON *jt, int depth)
 {
+       (void) ctx;
+       assert(ma);
        int i, idx;
        JSONterm *je;
        size_t buflen = 1024;
-       allocator *ma = ctx->curprg->def->ma;
        char *buffer = ma_alloc(ma, buflen);
        BAT *bn = COLnew(0, TYPE_str, 0, TRANSIENT);
 
@@ -466,17 +467,17 @@ static str
 JSONdump(Client ctx, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
 {
        (void) mb;
-       allocator *ta = MT_thread_getallocator();
-       allocator_state *ta_state = ma_open(ta);
+       allocator *ma = MT_thread_getallocator();
+       allocator_state *ma_state = ma_open(ma);
 
        bat *ret = getArgReference_bat(stk, pci, 0);
        const json *val = (json *) getArgReference(stk, pci, 1);
-       JSON *jt = JSONparse(ta, *val);
+       JSON *jt = JSONparse(ma, *val);
 
        CHECK_JSON(jt);
-       BAT *bn = JSONdumpInternal(ctx, jt, 0);
+       BAT *bn = JSONdumpInternal(ma, ctx, jt, 0);
        JSONfree(jt);
-       ma_close_to(ta, ta_state);
+       ma_close_to(ma, ma_state);
        if (bn == NULL)
                throw(MAL, "json.dump", SQLSTATE(HY013) MAL_MALLOC_FAIL);
        *ret = bn->batCacheid;
@@ -520,7 +521,9 @@ JSONstr2json_intern(allocator *ma, json 
        size_t ln = strlen(*j)+1;
        size_t out_size = 0;
        allocator *ta = MT_thread_getallocator();
-       allocator_state *ta_state = ma_open(ta);
+       allocator_state *ta_state = NULL;
+       if (ta != ma)
+               ta_state = ma_open(ta);
 
        JSON *jt = NULL;
 
@@ -558,7 +561,8 @@ JSONstr2json_intern(allocator *ma, json 
        *ret = buf;
  bailout:
        JSONfree(jt);
-       ma_close_to(ta, ta_state);
+       if (ta_state)
+               ma_close_to(ta, ta_state);
        return msg;
 }
 
@@ -671,10 +675,13 @@ JSONprelude(void)
                throw(MAL, "json.prelude", "cannot allocate filename for json 
upgrade signal file");
        int r = stat(jsonupgrade, &st);
        if (r == 0) {
+               allocator *ma = MT_thread_getallocator();
+               allocator_state *ma_state = ma_open(ma);
                /* The file exists so we need to run the upgrade code */
                if (BBPjson_upgrade(upgradeJSONStorage) != GDK_SUCCEED) {
                        throw(MAL, "json.prelude", "JSON storage upgrade 
failed");
                }
+               ma_close_to(ma, ma_state);
                /* Change the read function of the json atom so that any values 
in the WAL
                 * will also be upgraded.
                 */
@@ -837,9 +844,8 @@ JSONcompile(Client ctx, const char *expr
 }
 
 static str
-JSONgetValue(Client ctx, const JSON *jt, int idx)
+JSONgetValue(allocator *ma, const JSON *jt, int idx)
 {
-       allocator *ma = ctx->curprg->def->ma;
        str s;
 
        if (jt->elm[idx].valuelen == 0)
@@ -852,9 +858,8 @@ JSONgetValue(Client ctx, const JSON *jt,
 
 /* eats res and r */
 static str
-JSONglue(Client ctx, str res, str r, char sep)
+JSONglue(allocator *ma, str res, str r, char sep)
 {
-       allocator *ma = ctx->curprg->def->ma;
        size_t len, l;
        str n;
 
@@ -880,7 +885,7 @@ JSONglue(Client ctx, str res, str r, cha
 
 /* return NULL on no match, return (str) -1 on (malloc) failure, (str) -2 on 
stack overflow */
 static str
-JSONmatch(Client ctx, JSON *jt, int ji, const pattern *terms, int ti, bool 
accumulate)
+JSONmatch(allocator *ma, JSON *jt, int ji, const pattern *terms, int ti, bool 
accumulate)
 {
        str r = NULL, res = NULL;
        int i;
@@ -895,7 +900,7 @@ JSONmatch(Client ctx, JSON *jt, int ji, 
                if (ti + 1 == MAXTERMS)
                        return NULL;
                if (terms[ti + 1].token == END_STEP) {
-                       res = JSONgetValue(ctx, jt, 0);
+                       res = JSONgetValue(ma, jt, 0);
                        if (res == NULL)
                                res = (str) -1;
                        return res;
@@ -907,7 +912,7 @@ JSONmatch(Client ctx, JSON *jt, int ji, 
        case JSON_ARRAY:
                if (terms[ti].name != 0 && terms[ti].token != ANY_STEP) {
                        if (terms[ti].token == END_STEP) {
-                               res = JSONgetValue(ctx, jt, ji);
+                               res = JSONgetValue(ma, jt, ji);
                                if (res == NULL)
                                        res = (str) -1;
                        }
@@ -919,26 +924,26 @@ JSONmatch(Client ctx, JSON *jt, int ji, 
                                || (cnt >= terms[ti].first && cnt <= 
terms[ti].last)) {
                                if (terms[ti].token == ANY_STEP) {
                                        if (jt->elm[i].child)
-                                               r = JSONmatch(ctx, jt, 
jt->elm[i].child, terms, ti, true);
+                                               r = JSONmatch(ma, jt, 
jt->elm[i].child, terms, ti, true);
                                        else
                                                r = 0;
                                } else if (ti + 1 == MAXTERMS) {
                                        return NULL;
                                } else if (terms[ti + 1].token == END_STEP) {
                                        if (jt->elm[i].kind == JSON_VALUE)
-                                               r = JSONgetValue(ctx, jt, 
jt->elm[i].child);
+                                               r = JSONgetValue(ma, jt, 
jt->elm[i].child);
                                        else
-                                               r = JSONgetValue(ctx, jt, i);
+                                               r = JSONgetValue(ma, jt, i);
                                        if (r == NULL)
                                                r = (str) -1;
                                } else {
-                                       r = JSONmatch(ctx, jt, 
jt->elm[i].child, terms, ti + 1, terms[ti].index == INT_MAX);
+                                       r = JSONmatch(ma, jt, jt->elm[i].child, 
terms, ti + 1, terms[ti].index == INT_MAX);
                                }
                                if (r == (str) -1 || r == (str) -2) {
                                        //GDKfree(res);
                                        return r;
                                }
-                               res = JSONglue(ctx, res, r, ',');
+                               res = JSONglue(ma, res, r, ',');
                        }
                }
                break;
@@ -955,18 +960,18 @@ JSONmatch(Client ctx, JSON *jt, int ji, 
                                        if (ti + 1 == MAXTERMS)
                                                return NULL;
                                        if (terms[ti + 1].token == END_STEP) {
-                                               r = JSONgetValue(ctx, jt, 
jt->elm[i].child);
+                                               r = JSONgetValue(ma, jt, 
jt->elm[i].child);
                                                if (r == NULL)
                                                        r = (str) -1;
                                        } else {
-                                               r = JSONmatch(ctx, jt, 
jt->elm[i].child, terms, ti + 1, terms[ti].index == INT_MAX);
+                                               r = JSONmatch(ma, jt, 
jt->elm[i].child, terms, ti + 1, terms[ti].index == INT_MAX);
                                        }
                                        if (r == (str) -1 || r == (str) -2) {
                                                //GDKfree(res);
                                                return r;
                                        }
                                        if (accumulate) {
-                                               res = JSONglue(ctx, res, r, 
',');
+                                               res = JSONglue(ma, res, r, ',');
                                        } else {  // Keep the last matching 
value
                                                //if (res)
                                                //      GDKfree(res);
@@ -975,12 +980,12 @@ JSONmatch(Client ctx, JSON *jt, int ji, 
                                }
                                cnt++;
                        } else if (terms[ti].token == ANY_STEP && 
jt->elm[i].child) {
-                               r = JSONmatch(ctx, jt, jt->elm[i].child, terms, 
ti, true);
+                               r = JSONmatch(ma, jt, jt->elm[i].child, terms, 
ti, true);
                                if (r == (str) -1 || r == (str) -2) {
                                        //GDKfree(res);
                                        return r;
                                }
-                               res = JSONglue(ctx, res, r, ',');
+                               res = JSONglue(ma, res, r, ',');
                                cnt++;
                        }
                }
@@ -1020,7 +1025,7 @@ JSONfilterInternal(Client ctx, json *ret
                goto bailout;
 
        bool accumulate = terms[tidx].token == ANY_STEP || (terms[tidx].name && 
terms[tidx].name[0] == '*');
-       s = JSONmatch(ctx, jt, 0, terms, tidx, accumulate);
+       s = JSONmatch(ma, jt, 0, terms, tidx, accumulate);
        if (s == (char *) -1) {
                msg = createException(MAL, "JSONfilterInternal",
                                                          SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
@@ -1040,7 +1045,7 @@ JSONfilterInternal(Client ctx, json *ret
                        && terms[tidx + 1].token) {
                        tidx += 1;
                        accumulate = terms[tidx].token == ANY_STEP || 
(terms[tidx].name && terms[tidx].name[0] == '*');
-                       s = JSONmatch(ctx, jt, 0, terms, tidx, accumulate);
+                       s = JSONmatch(ma, jt, 0, terms, tidx, accumulate);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to